Skip to content

Commit

Permalink
Don't compact arrays (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
reinkrul authored Dec 1, 2023
1 parent c2e3cb8 commit 3abeed3
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 89 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,11 @@ Use `ParseVerifiableCredential(raw string)` and `ParseVerifiablePresentation(raw
go get github.com/nuts-foundation/go-did
```
## Testing
```shell
go test ./... -tags=jwx_es256k
```

## State of the library
We keep the API stable, breaking changes will only be introduced in new major versions.
36 changes: 1 addition & 35 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,31 +265,7 @@ 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))
if err != nil {
return err
}
type alias Service
var result alias
if err := json.Unmarshal(normalizedData, &result); err != nil {
return err
}
*s = (Service)(result)
return nil
}

// Unmarshal unmarshalls the service endpoint into a domain-specific type.
// UnmarshalServiceEndpoint unmarshalls the service endpoint into a domain-specific type.
func (s Service) UnmarshalServiceEndpoint(target interface{}) error {
var valueToMarshal interface{}
if asSlice, ok := s.ServiceEndpoint.([]interface{}); ok && len(asSlice) == 1 {
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 @@ -278,11 +278,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(credentialStatusKey), marshal.Unplural(proofKey))
}
return json.Marshal(tmp)
}

func (vc *VerifiableCredential) UnmarshalJSON(b []byte) error {
Expand Down
5 changes: 3 additions & 2 deletions vc/vc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ func TestVerifiableCredential_JSONMarshalling(t *testing.T) {

t.Run("marshal empty VC", func(t *testing.T) {
input := VerifiableCredential{}
marshalled, err := json.Marshal(input)
actual, err := json.Marshal(input)
require.NoError(t, err)
assert.Equal(t, "{\"@context\":null,\"credentialSubject\":null,\"issuer\":\"\",\"proof\":null,\"type\":null}", string(marshalled))
const expected = "{\"@context\":null,\"credentialSubject\":null,\"issuer\":\"\",\"proof\":null,\"type\":null}"
assert.JSONEq(t, expected, string(actual))
})
})
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 3abeed3

Please sign in to comment.