Skip to content

Commit

Permalink
fix: expand api (make similar to js implementation)
Browse files Browse the repository at this point in the history
  • Loading branch information
skynet2 committed Oct 21, 2024
1 parent 257ca0d commit 4ff4486
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
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

0 comments on commit 4ff4486

Please sign in to comment.