get raw bytes
This commit is contained in:
@@ -5,6 +5,7 @@ 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 {
|
||||
@@ -12,15 +13,33 @@ func UpsertSubscriptionDetails(g *CartGrain, m *messages.UpsertSubscriptionDetai
|
||||
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
|
||||
}
|
||||
|
||||
// Create new subscription details when Id is nil.
|
||||
if m.Id == nil {
|
||||
// Validate JSON if provided.
|
||||
var meta json.RawMessage
|
||||
if m.Data != nil && len(m.Data.Value) > 0 {
|
||||
if !json.Valid(m.Data.Value) {
|
||||
dataBytes, err := getDataBytes(m.Data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if dataBytes != nil {
|
||||
if !json.Valid(dataBytes) {
|
||||
return fmt.Errorf("subscription details invalid json")
|
||||
}
|
||||
meta = json.RawMessage(m.Data.Value)
|
||||
meta = json.RawMessage(dataBytes)
|
||||
}
|
||||
|
||||
id := MustNewCartId().String()
|
||||
@@ -49,15 +68,16 @@ func UpsertSubscriptionDetails(g *CartGrain, m *messages.UpsertSubscriptionDetai
|
||||
existing.SigningType = m.SigningType
|
||||
changed = true
|
||||
}
|
||||
if m.Data != nil {
|
||||
// Only validate & assign if there is content; empty -> leave as-is.
|
||||
if len(m.Data.Value) > 0 {
|
||||
if !json.Valid(m.Data.Value) {
|
||||
return fmt.Errorf("subscription details invalid json")
|
||||
}
|
||||
existing.Meta = m.Data.Value
|
||||
changed = true
|
||||
dataBytes, err := getDataBytes(m.Data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if dataBytes != nil {
|
||||
if !json.Valid(dataBytes) {
|
||||
return fmt.Errorf("subscription details invalid json")
|
||||
}
|
||||
existing.Meta = dataBytes
|
||||
changed = true
|
||||
}
|
||||
if changed {
|
||||
existing.Version++
|
||||
|
||||
Reference in New Issue
Block a user