Skip to content

Commit

Permalink
Merge pull request #1 from skynet2/fix-expand-api
Browse files Browse the repository at this point in the history
fix: expand api
  • Loading branch information
fqutishat authored Oct 30, 2024
2 parents 257ca0d + 04967f7 commit 0b1ef7a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion ld/api_expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions ld/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
64 changes: 63 additions & 1 deletion ld/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <http://purl.org/dc/terms/created> "2024-10-17T18:17:31Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> _:c14n2 .
_:c14n0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://w3id.org/security#DataIntegrityProof> _:c14n2 .
_:c14n0 <https://w3id.org/security#cryptosuite> "eddsa-rdfc-2022"^^<https://w3id.org/security#cryptosuiteString> _:c14n2 .
_:c14n0 <https://w3id.org/security#proofPurpose> <https://w3id.org/security#assertionMethod> _:c14n2 .
_:c14n0 <https://w3id.org/security#proofValue> "z49q4jLydafVc5qGuRg5WAnG7au6uddhCCr2pNK23JJKzgwhWQUCDGym5GDqJM76ZzEWaU8y53xZffFk7XsLYo8ek"^^<https://w3id.org/security#multibase> _:c14n2 .
_:c14n0 <https://w3id.org/security#verificationMethod> <did:key:z6MkpJySvETLnxhQG9DzEdmKJtysBDjuuTeDfUj1uNNCUqcj#z6MkpJySvETLnxhQG9DzEdmKJtysBDjuuTeDfUj1uNNCUqcj> _:c14n2 .
_:c14n1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://www.w3.org/2018/credentials#VerifiablePresentation> .
_:c14n1 <https://www.w3.org/2018/credentials#verifiableCredential> _:c14n3 .
_:c14n4 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://www.w3.org/2018/credentials#VerifiableCredential> _:c14n3 .
_:c14n4 <https://w3id.org/security#proof> _:c14n2 _:c14n3 .
_:c14n4 <https://www.w3.org/2018/credentials#credentialSubject> <did:example:subject> _:c14n3 .
_:c14n4 <https://www.w3.org/2018/credentials#issuer> <did:key:z6MkpJySvETLnxhQG9DzEdmKJtysBDjuuTeDfUj1uNNCUqcj> _:c14n3 .
`)
}

0 comments on commit 0b1ef7a

Please sign in to comment.