From 4ff4486aba91a0e189d16bc693c6fed60f9d29c3 Mon Sep 17 00:00:00 2001 From: Stas Dm Date: Mon, 21 Oct 2024 18:42:57 +0200 Subject: [PATCH 1/2] fix: expand api (make similar to js implementation) --- Makefile | 1 + ld/api_expand.go | 3 ++- ld/context.go | 12 ++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index e768675..adb17fb 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ vet: go vet github.com/piprate/json-gold/... test: vet + go clean -testcache go test github.com/piprate/json-gold/... test-cov: vet diff --git a/ld/api_expand.go b/ld/api_expand.go index 45276a0..5e35140 100644 --- a/ld/api_expand.go +++ b/ld/api_expand.go @@ -790,10 +790,11 @@ func (api *JsonLdApi) expandObject(activeCtx *Context, activeProperty string, ex } } + isContainer := termCtx.HasContainerMapping2(td, "@graph") isContainerGraph := termCtx.HasContainerMapping(key, "@graph") isContainerID := termCtx.HasContainerMapping(key, "@id") isContainerIndex := termCtx.HasContainerMapping(key, "@index") - if isContainerGraph && !isContainerID && !isContainerIndex { + if (isContainerGraph || isContainer) && !isContainerID && !isContainerIndex { evList := Arrayify(expandedValue) rVal := make([]interface{}, 0) for _, ev := range evList { diff --git a/ld/context.go b/ld/context.go index 0e6837f..fafbe9c 100644 --- a/ld/context.go +++ b/ld/context.go @@ -1720,6 +1720,18 @@ func (c *Context) HasContainerMapping(property string, val string) bool { return false } +func (c *Context) HasContainerMapping2(dict map[string]interface{}, val string) bool { + if container, hasContainer := dict["@container"]; hasContainer { + for _, container := range container.([]interface{}) { + if container == val { + return true + } + } + } + + return false +} + // IsReverseProperty returns true if the given property is a reverse property func (c *Context) IsReverseProperty(property string) bool { td := c.GetTermDefinition(property) From 04967f7f074b91a61963069426f3a178482c2a05 Mon Sep 17 00:00:00 2001 From: Stas Dm Date: Wed, 30 Oct 2024 12:39:23 +0100 Subject: [PATCH 2/2] feat: add vp test --- ld/processor_test.go | 64 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/ld/processor_test.go b/ld/processor_test.go index f6d2836..e1148c0 100644 --- a/ld/processor_test.go +++ b/ld/processor_test.go @@ -27,8 +27,9 @@ import ( "testing" "time" - . "github.com/piprate/json-gold/ld" "github.com/stretchr/testify/assert" + + . "github.com/piprate/json-gold/ld" ) // RewriteHostTransport is an http.RoundTripper that rewrites requests @@ -694,3 +695,64 @@ func (er *EarlReport) write(filename string) { _, _ = f.Write(b) _, _ = f.WriteString("\n") } + +func TestVPNormalization(t *testing.T) { + proc := NewJsonLdProcessor() + options := NewJsonLdOptions("") + options.Algorithm = AlgorithmURDNA2015 + options.ProduceGeneralizedRdf = true + + input := `{ + "@context": [ + "https://www.w3.org/ns/credentials/v2" + ], + "type": [ + "VerifiablePresentation" + ], + "verifiableCredential": [ + { + "@context": [ + "https://www.w3.org/ns/credentials/v2" + ], + "type": [ + "VerifiableCredential" + ], + "issuer": "did:key:z6MkpJySvETLnxhQG9DzEdmKJtysBDjuuTeDfUj1uNNCUqcj", + "credentialSubject": { + "id": "did:example:subject" + }, + "proof": { + "type": "DataIntegrityProof", + "created": "2024-10-17T18:17:31Z", + "verificationMethod": "did:key:z6MkpJySvETLnxhQG9DzEdmKJtysBDjuuTeDfUj1uNNCUqcj#z6MkpJySvETLnxhQG9DzEdmKJtysBDjuuTeDfUj1uNNCUqcj", + "cryptosuite": "eddsa-rdfc-2022", + "proofPurpose": "assertionMethod", + "proofValue": "z49q4jLydafVc5qGuRg5WAnG7au6uddhCCr2pNK23JJKzgwhWQUCDGym5GDqJM76ZzEWaU8y53xZffFk7XsLYo8ek" + } + } + ] +}` + + var dict map[string]interface{} + err := json.Unmarshal([]byte(input), &dict) + assert.NoError(t, err) + + options.Format = "application/n-quads" + output2, err := proc.Normalize(dict, options) + assert.NoError(t, err) + + assert.EqualValues(t, output2.(string), + `_:c14n0 "2024-10-17T18:17:31Z"^^ _:c14n2 . +_:c14n0 _:c14n2 . +_:c14n0 "eddsa-rdfc-2022"^^ _:c14n2 . +_:c14n0 _:c14n2 . +_:c14n0 "z49q4jLydafVc5qGuRg5WAnG7au6uddhCCr2pNK23JJKzgwhWQUCDGym5GDqJM76ZzEWaU8y53xZffFk7XsLYo8ek"^^ _:c14n2 . +_:c14n0 _:c14n2 . +_:c14n1 . +_:c14n1 _:c14n3 . +_:c14n4 _:c14n3 . +_:c14n4 _:c14n2 _:c14n3 . +_:c14n4 _:c14n3 . +_:c14n4 _:c14n3 . +`) +}