Skip to content

feat: add custom json marshaler for sdk.Statement #1936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions sdk/assertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@
// Remove the binding key
delete(jsonObject, "binding")

// Deep patch: fix "value" inside "statement" if it is a string
if statement, ok := jsonObject["statement"].(map[string]interface{}); ok {

Check failure on line 112 in sdk/assertion.go

View workflow job for this annotation

GitHub Actions / go (sdk)

shadow: declaration of "ok" shadows declaration at line 111 (govet)
if valueStr, ok := statement["value"].(string); ok {
var valueObj interface{}
if err := json.Unmarshal([]byte(valueStr), &valueObj); err == nil {
statement["value"] = valueObj // replace the string with parsed object
}
}
}

// Marshal the map back to JSON
assertionJSON, err = json.Marshal(jsonObject)
if err != nil {
Expand All @@ -123,6 +133,32 @@
return ocrypto.SHA256AsHex(transformedJSON), nil
}

func (s *Statement) MarshalJSON() ([]byte, error) {
// Define a custom struct for serialization
type Alias Statement
aux := &struct {
Value interface{} `json:"value,omitempty"`
*Alias
}{
Alias: (*Alias)(s),
}

if s.Format == "json-structured" {
raw := json.RawMessage(s.Value)
var tmp interface{}
// Attempt to decode Value to validate it's actual json
if err := json.Unmarshal(raw, &tmp); err == nil {
aux.Value = raw
} else {
aux.Value = s.Value
}
} else {
aux.Value = s.Value
}

return json.Marshal(aux)
}

func (s *Statement) UnmarshalJSON(data []byte) error {
// Define a custom struct for deserialization
type Alias Statement
Expand Down
Loading