get raw bytes
Some checks failed
Build and Publish / BuildAndDeployArm64 (push) Failing after 1m38s
Build and Publish / BuildAndDeployAmd64 (push) Failing after 1m41s

This commit is contained in:
matst80
2025-11-18 22:27:49 +01:00
parent 7a7c577374
commit ba0e820956
2 changed files with 59 additions and 34 deletions

View File

@@ -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++