Skip to content

Commit

Permalink
Don't compact arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
reinkrul committed Nov 29, 2023
1 parent 3293353 commit d9d7dc1
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 74 deletions.
22 changes: 1 addition & 21 deletions did/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,6 @@ 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 Expand Up @@ -275,18 +265,8 @@ type Service struct {
ServiceEndpoint interface{} `json:"serviceEndpoint,omitempty"`
}

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

func (s *Service) UnmarshalJSON(data []byte) error {
normalizedData, err := marshal.NormalizeDocument(data, pluralContext, marshal.PluralValueOrMap(serviceEndpointKey))
normalizedData, err := marshal.NormalizeDocument(data, pluralContext)
if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion did/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ const keyAgreementKey = "keyAgreement"
const capabilityInvocationKey = "capabilityInvocation"
const capabilityDelegationKey = "capabilityDelegation"
const verificationMethodKey = "verificationMethod"
const serviceEndpointKey = "serviceEndpoint"

var pluralContext = marshal.Plural(contextKey)
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
10 changes: 0 additions & 10 deletions internal/marshal/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,6 @@ func Plural(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 {
return func(m map[string]interface{}) {
if arr, _ := m[key].([]interface{}); len(arr) == 1 {
m[key] = arr[0]
}
}
}

// 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 {
Expand Down
6 changes: 1 addition & 5 deletions vc/vc.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,7 @@ func (vc VerifiableCredential) MarshalJSON() ([]byte, error) {
// Must be a JSON-LD credential
type alias VerifiableCredential
tmp := alias(vc)
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(proofKey))
}
return json.Marshal(tmp)
}

func (vc *VerifiableCredential) UnmarshalJSON(b []byte) error {
Expand Down
3 changes: 2 additions & 1 deletion vc/vc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ func TestVerifiableCredential_JSONMarshalling(t *testing.T) {
assert.True(t, strings.HasPrefix(string(marshalled), "{"))

t.Run("marshal empty VC", func(t *testing.T) {
const expected = `{"@context":null,"credentialSubject":null,"issuanceDate":"0001-01-01T00:00:00Z","issuer":"","proof":null,"type":null}`
input := VerifiableCredential{}
marshalled, err := json.Marshal(input)
require.NoError(t, err)
assert.Equal(t, "{\"@context\":null,\"credentialSubject\":null,\"issuanceDate\":\"0001-01-01T00:00:00Z\",\"issuer\":\"\",\"proof\":null,\"type\":null}", string(marshalled))
assert.JSONEq(t, expected, string(marshalled))
})
})
t.Run("JWT", func(t *testing.T) {
Expand Down
6 changes: 1 addition & 5 deletions vc/vp.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,7 @@ func (vp VerifiablePresentation) MarshalJSON() ([]byte, error) {
}
type alias VerifiablePresentation
tmp := alias(vp)
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))
}
return json.Marshal(tmp)
}

func (vp *VerifiablePresentation) UnmarshalJSON(b []byte) error {
Expand Down
32 changes: 2 additions & 30 deletions vc/vp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,36 +65,8 @@ func TestVerifiablePresentation_MarshalJSON(t *testing.T) {
if !assert.NoError(t, err) {
return
}
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\":[")
assert.Contains(t, string(bytes), "\"proof\":[{")
assert.Contains(t, string(bytes), "\"verifiableCredential\":[{")
})
})

Expand Down

0 comments on commit d9d7dc1

Please sign in to comment.