better pbany parsing
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gogo/protobuf/proto"
|
||||
"google.golang.org/protobuf/types/known/anypb"
|
||||
|
||||
"git.tornberg.me/go-cart-actor/pkg/actor"
|
||||
messages "git.tornberg.me/go-cart-actor/pkg/messages"
|
||||
@@ -458,17 +459,34 @@ func TestRegistryDefensiveErrors(t *testing.T) {
|
||||
t.Fatalf("expected no results when message slice nil")
|
||||
}
|
||||
}
|
||||
|
||||
type SubscriptionDetailsRequest struct {
|
||||
Id *string `json:"id,omitempty"`
|
||||
OfferingCode string `json:"offeringCode,omitempty"`
|
||||
SigningType string `json:"signingType,omitempty"`
|
||||
Data json.RawMessage `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
func (sd *SubscriptionDetailsRequest) ToMessage() *messages.UpsertSubscriptionDetails {
|
||||
return &messages.UpsertSubscriptionDetails{
|
||||
Id: sd.Id,
|
||||
OfferingCode: sd.OfferingCode,
|
||||
SigningType: sd.SigningType,
|
||||
Data: &anypb.Any{Value: sd.Data},
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubscriptionDetailsJSONValidation(t *testing.T) {
|
||||
reg := newRegistry()
|
||||
g := newTestGrain()
|
||||
|
||||
// Valid JSON on create
|
||||
jsonStr := `{"offeringCode": "OFFJSON", "signingType": "TYPEJSON", "data": {"value": "eyJvayI6dHJ1ZX0="}}`
|
||||
var validCreate messages.UpsertSubscriptionDetails
|
||||
jsonStr := `{"offeringCode": "OFFJSON", "signingType": "TYPEJSON", "data": {"value":"eyJvayI6dHJ1ZX0=","a":1}}`
|
||||
var validCreate SubscriptionDetailsRequest
|
||||
if err := json.Unmarshal([]byte(jsonStr), &validCreate); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
applyOK(t, reg, g, &validCreate)
|
||||
applyOK(t, reg, g, validCreate.ToMessage())
|
||||
if len(g.SubscriptionDetails) != 1 {
|
||||
t.Fatalf("expected one subscription detail after valid create, got %d", len(g.SubscriptionDetails))
|
||||
}
|
||||
@@ -476,7 +494,7 @@ func TestSubscriptionDetailsJSONValidation(t *testing.T) {
|
||||
for k := range g.SubscriptionDetails {
|
||||
id = k
|
||||
}
|
||||
if string(g.SubscriptionDetails[id].Meta) != `{"ok":true}` {
|
||||
if string(g.SubscriptionDetails[id].Meta) != `{"value":"eyJvayI6dHJ1ZX0=","a":1}` {
|
||||
t.Fatalf("expected meta stored as valid json, got %s", string(g.SubscriptionDetails[id].Meta))
|
||||
}
|
||||
|
||||
@@ -514,7 +532,7 @@ func TestSubscriptionDetailsJSONValidation(t *testing.T) {
|
||||
}
|
||||
|
||||
// Empty Data should not overwrite existing meta
|
||||
jsonStr5 := fmt.Sprintf(`{"id": "%s", "data": {"value": ""}}`, id)
|
||||
jsonStr5 := fmt.Sprintf(`{"id": "%s"}`, id)
|
||||
var emptyUpdate messages.UpsertSubscriptionDetails
|
||||
if err := json.Unmarshal([]byte(jsonStr5), &emptyUpdate); err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -5,41 +5,23 @@ import (
|
||||
"fmt"
|
||||
|
||||
messages "git.tornberg.me/go-cart-actor/pkg/messages"
|
||||
"google.golang.org/protobuf/types/known/anypb"
|
||||
)
|
||||
|
||||
func UpsertSubscriptionDetails(g *CartGrain, m *messages.UpsertSubscriptionDetails) error {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Helper to get data bytes from Data field
|
||||
getDataBytes := func(data *anypb.Any) ([]byte, error) {
|
||||
if data == nil {
|
||||
return nil, nil
|
||||
}
|
||||
if len(data.Value) > 0 {
|
||||
return data.Value, nil
|
||||
}
|
||||
if data.TypeUrl != "" {
|
||||
return []byte(data.TypeUrl), nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
metaBytes := m.Data.GetValue()
|
||||
|
||||
// Create new subscription details when Id is nil.
|
||||
if m.Id == nil {
|
||||
// Validate JSON if provided.
|
||||
var meta json.RawMessage
|
||||
dataBytes, err := getDataBytes(m.Data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if dataBytes != nil {
|
||||
if !json.Valid(dataBytes) {
|
||||
if metaBytes != nil {
|
||||
if !json.Valid(metaBytes) {
|
||||
return fmt.Errorf("subscription details invalid json")
|
||||
}
|
||||
meta = json.RawMessage(dataBytes)
|
||||
meta = json.RawMessage(metaBytes)
|
||||
}
|
||||
|
||||
id := MustNewCartId().String()
|
||||
@@ -68,15 +50,11 @@ func UpsertSubscriptionDetails(g *CartGrain, m *messages.UpsertSubscriptionDetai
|
||||
existing.SigningType = m.SigningType
|
||||
changed = true
|
||||
}
|
||||
dataBytes, err := getDataBytes(m.Data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if dataBytes != nil {
|
||||
if !json.Valid(dataBytes) {
|
||||
if metaBytes != nil {
|
||||
if !json.Valid(metaBytes) {
|
||||
return fmt.Errorf("subscription details invalid json")
|
||||
}
|
||||
existing.Meta = dataBytes
|
||||
existing.Meta = json.RawMessage(metaBytes)
|
||||
changed = true
|
||||
}
|
||||
if changed {
|
||||
|
||||
Reference in New Issue
Block a user