Skip to content
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

Revert removal of JSON-LD array compacting #102

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions did/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ func (d *Document) AddCapabilityDelegation(v *VerificationMethod) {
d.CapabilityDelegation.Add(v)
}

func (d Document) MarshalJSON() ([]byte, error) {
type alias Document
tmp := alias(d)
if data, err := json.Marshal(tmp); err != nil {
return nil, err
} else {
return marshal.NormalizeDocument(data, marshal.Unplural(contextKey), marshal.Unplural(controllerKey))
}
}

func (d *Document) UnmarshalJSON(b []byte) error {
document, err := ParseDocument(string(b))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion did/test/did1-expected.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"@context": ["https://www.w3.org/ns/did/v1"],
"@context": "https://www.w3.org/ns/did/v1",
"id": "did:nuts:04cf1e20-378a-4e38-ab1b-401a5018c9ff",
"controller": [
"did:nuts:04cf1e20-378a-4e38-ab1b-401a5018c9ff",
Expand Down
15 changes: 5 additions & 10 deletions internal/marshal/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,12 @@ func Plural(key string) Normalizer {
}
}

// PluralValueOrMap returns a Normalizer that behaves like Plural but leaves maps as simply a map. In other words,
// it only turns singular values into an array, except maps.
func PluralValueOrMap(key string) Normalizer {
// Unplural returns a Normalizer that converts arrays with a single value into a singular value. It is the opposite
// of the Plural normalizer.
func Unplural(key string) Normalizer {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks a bit weird for reversal, but I removed the unused PluralValueOrMap func

return func(m map[string]interface{}) {
value := m[key]
if value == nil {
return
} else if _, isMap := value.(map[string]interface{}); isMap {
return
} else if _, isSlice := value.([]interface{}); !isSlice {
m[key] = []interface{}{m[key]}
if arr, _ := m[key].([]interface{}); len(arr) == 1 {
m[key] = arr[0]
}
}
}
6 changes: 5 additions & 1 deletion vc/vc.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,11 @@ func (vc VerifiableCredential) MarshalJSON() ([]byte, error) {
// Must be a JSON-LD credential
type alias VerifiableCredential
tmp := alias(vc)
return json.Marshal(tmp)
if data, err := json.Marshal(tmp); err != nil {
return nil, err
} else {
return marshal.NormalizeDocument(data, pluralContext, marshal.Unplural(typeKey), marshal.Unplural(credentialSubjectKey), marshal.Unplural(credentialStatusKey), marshal.Unplural(proofKey))
}
}

func (vc *VerifiableCredential) UnmarshalJSON(b []byte) error {
Expand Down
6 changes: 5 additions & 1 deletion vc/vp.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ func (vp VerifiablePresentation) MarshalJSON() ([]byte, error) {
}
type alias VerifiablePresentation
tmp := alias(vp)
return json.Marshal(tmp)
if data, err := json.Marshal(tmp); err != nil {
return nil, err
} else {
return marshal.NormalizeDocument(data, pluralContext, marshal.Unplural(typeKey), marshal.Unplural(verifiableCredentialKey), marshal.Unplural(proofKey))
}
}

func (vp *VerifiablePresentation) UnmarshalJSON(b []byte) error {
Expand Down
32 changes: 30 additions & 2 deletions vc/vp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,36 @@ func TestVerifiablePresentation_MarshalJSON(t *testing.T) {
if !assert.NoError(t, err) {
return
}
assert.Contains(t, string(bytes), "\"proof\":[{")
assert.Contains(t, string(bytes), "\"verifiableCredential\":[{")
assert.Contains(t, string(bytes), "\"proof\":{")
assert.Contains(t, string(bytes), "\"verifiableCredential\":{")
})
t.Run("ok - multiple credential and proof", func(t *testing.T) {
input := VerifiablePresentation{
VerifiableCredential: []VerifiableCredential{
{
Type: []ssi.URI{VerifiableCredentialTypeV1URI()},
},
{
Type: []ssi.URI{VerifiableCredentialTypeV1URI()},
},
},
Proof: []interface{}{
JSONWebSignature2020Proof{
Jws: "",
},
JSONWebSignature2020Proof{
Jws: "",
},
},
}

bytes, err := json.Marshal(input)

if !assert.NoError(t, err) {
return
}
assert.Contains(t, string(bytes), "\"proof\":[")
assert.Contains(t, string(bytes), "\"verifiableCredential\":[")
})
})

Expand Down