Skip to content

Commit a5ebe3c

Browse files
authored
Modify NewPathFromDocument function to support nested fields (#66)
* Modify NewPathFromDocument function to support nested fields
1 parent 8a26302 commit a5ebe3c

File tree

2 files changed

+77
-9
lines changed

2 files changed

+77
-9
lines changed

merklize/merklize.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,14 @@ func pathFromDocument(ldCtx *ld.Context, docObj interface{},
399399
return nil, fmt.Errorf("@id attr is not of type string: %T", id)
400400
}
401401

402+
termContext, termHasCtx := m["@context"]
403+
if termHasCtx {
404+
ldCtx, err = ldCtx.Parse(termContext)
405+
if err != nil {
406+
return nil, err
407+
}
408+
}
409+
402410
moreParts, err := pathFromDocument(ldCtx, docObjMap[term], newPathParts,
403411
true)
404412
if err != nil {

merklize/merklize_test.go

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,52 @@ import (
2323
"github.com/stretchr/testify/require"
2424
)
2525

26+
const nestedFieldDocument = `{
27+
"@context": [
28+
{
29+
"@version": 1.1,
30+
"@protected": true,
31+
"id": "@id",
32+
"type": "@type",
33+
"CustomType": {
34+
"@id": "urn:uuid:79f824ba-fee3-11ed-be56-0242ac120002",
35+
"@context": {
36+
"@version": 1.1,
37+
"@protected": true,
38+
"@propagate": true,
39+
"id": "@id",
40+
"type": "@type",
41+
"xsd": "http://www.w3.org/2001/XMLSchema#",
42+
"customField": {
43+
"@id": "polygon-vocab:customField",
44+
"@type": "xsd:string"
45+
},
46+
"polygon-vocab": "urn:uuid:87caf7a2-fee3-11ed-be56-0242ac120001#",
47+
"objectField": {
48+
"@id": "polygon-vocab:objectField",
49+
"@context": {
50+
"@version": 1.1,
51+
"@protected": true,
52+
"id": "@id",
53+
"type": "@type",
54+
"customNestedField": {
55+
"@id": "polygon-vocab:customNestedField",
56+
"@type": "xsd:integer"
57+
}
58+
}
59+
}
60+
}
61+
}
62+
}
63+
],
64+
"id": "urn:urn:e27a921e-fee5-11ed-be56-0242ac100000",
65+
"type": ["CustomType"],
66+
"customField": "1234",
67+
"objectField": {
68+
"customNestedField": 1
69+
}
70+
}`
71+
2672
const testDocument = `{
2773
"@context": [
2874
"https://www.w3.org/2018/credentials/v1",
@@ -685,17 +731,31 @@ func TestFieldPathFromContext(t *testing.T) {
685731
}
686732

687733
func TestPathFromDocument(t *testing.T) {
688-
in := "credentialSubject.1.birthDate"
689-
result, err := NewPathFromDocument([]byte(testDocument), in)
690-
require.NoError(t, err)
734+
t.Run("path with index array", func(t *testing.T) {
735+
in := "credentialSubject.1.birthDate"
736+
result, err := NewPathFromDocument([]byte(testDocument), in)
737+
require.NoError(t, err)
691738

692-
want, err := NewPath(
693-
"https://www.w3.org/2018/credentials#credentialSubject",
694-
1,
695-
"http://schema.org/birthDate")
696-
require.NoError(t, err)
739+
want, err := NewPath(
740+
"https://www.w3.org/2018/credentials#credentialSubject",
741+
1,
742+
"http://schema.org/birthDate")
743+
require.NoError(t, err)
697744

698-
require.Equal(t, want, result)
745+
require.Equal(t, want, result)
746+
})
747+
748+
t.Run("path to nested field", func(t *testing.T) {
749+
path, err := NewPathFromDocument([]byte(nestedFieldDocument),
750+
"objectField.customNestedField")
751+
require.NoError(t, err)
752+
753+
want, err := NewPath(
754+
"urn:uuid:87caf7a2-fee3-11ed-be56-0242ac120001#objectField",
755+
"urn:uuid:87caf7a2-fee3-11ed-be56-0242ac120001#customNestedField")
756+
require.NoError(t, err)
757+
require.Equal(t, want, path)
758+
})
699759
}
700760

701761
func TestMkValueInt(t *testing.T) {

0 commit comments

Comments
 (0)