diff --git a/ld/api_frame.go b/ld/api_frame.go index 55cc3de..a2114b0 100644 --- a/ld/api_frame.go +++ b/ld/api_frame.go @@ -617,21 +617,18 @@ func removeEmbed(state *FramingContext, id string) { func removeDependents(embeds map[string]*EmbedNode, id string) { // get embed keys as a separate array to enable deleting keys in map for idDep, e := range embeds { - var p map[string]interface{} - if e.parent != nil { - var isMap bool - p, isMap = e.parent.(map[string]interface{}) - if !isMap { - continue - } - } else { - p = make(map[string]interface{}) + if e.parent == nil { + continue } - - pid := p["@id"].(string) - if id == pid { - delete(embeds, idDep) - removeDependents(embeds, idDep) + p, isMap := e.parent.(map[string]interface{}) + if !isMap { + continue + } + if pid, hasID := p["@id"]; hasID { + if id == pid.(string) { + delete(embeds, idDep) + removeDependents(embeds, idDep) + } } } } diff --git a/ld/api_frame_test.go b/ld/api_frame_test.go index cebbf01..f1dd300 100644 --- a/ld/api_frame_test.go +++ b/ld/api_frame_test.go @@ -15,10 +15,12 @@ package ld_test import ( + "encoding/json" "testing" . "github.com/piprate/json-gold/ld" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestGetFrameFlag(t *testing.T) { @@ -76,3 +78,121 @@ func TestGetFrameFlag(t *testing.T) { ), ) } + +func TestJsonLdApi_Frame(t *testing.T) { + var input any + err := json.Unmarshal([]byte(shaclValidationResult), &input) + require.NoError(t, err) + var frame any + err = json.Unmarshal([]byte(shaclValidationResultFrame), &frame) + require.NoError(t, err) + framed, err := NewJsonLdProcessor().Frame(input, frame, nil) + require.NoError(t, err) + assert.Equal(t, []any{map[string]any{ + "sh:resultPath": "https://example.com/hasScrewable", + "sh:sourceShape": map[string]any{ + "sh:or": map[string]any{ + "@list": []any{ + map[string]any{ + "sh:class": "https://example.com/Bolt", + "type": "sh:NodeShape", + }, + map[string]any{ + "sh:class": "https://example.com/Screw", + "type": "sh:NodeShape", + }, + }, + }, + "sh:path": "https://example.com/hasScrewable", + "type": "sh:PropertyShape", + }, + "type": "sh:ValidationResult", + }}, framed["@graph"]) +} + +const shaclValidationResult = ` +[ + { + "@id": "_:ccbca2cd103643858f1087647dd5399717617", + "@type": [ + "http://www.w3.org/ns/shacl#ValidationResult" + ], + "http://www.w3.org/ns/shacl#resultPath": [ + { + "@id": "https://example.com/hasScrewable" + } + ], + "http://www.w3.org/ns/shacl#sourceShape": [ + { + "@id": "_:node94090" + } + ] + }, + { + "@id": "_:node94090", + "@type": [ + "http://www.w3.org/ns/shacl#PropertyShape" + ], + "http://www.w3.org/ns/shacl#or": [ + { + "@list": [ + { + "@id": "_:node94092" + }, + { + "@id": "_:node94094" + } + ] + } + ], + "http://www.w3.org/ns/shacl#path": [ + { + "@id": "https://example.com/hasScrewable" + } + ] + }, + { + "@id": "_:node94092", + "@type": [ + "http://www.w3.org/ns/shacl#NodeShape" + ], + "http://www.w3.org/ns/shacl#class": [ + { + "@id": "https://example.com/Bolt" + } + ] + }, + { + "@id": "_:node94094", + "@type": [ + "http://www.w3.org/ns/shacl#NodeShape" + ], + "http://www.w3.org/ns/shacl#class": [ + { + "@id": "https://example.com/Screw" + } + ] + } +]` + +const shaclValidationResultFrame = `{ + "@context": { + "sh": "http://www.w3.org/ns/shacl#", + "sh:resultPath": { + "@id": "sh:resultPath", + "@type": "@id" + }, + "sh:path": { + "@id": "sh:path", + "@type": "@id" + }, + "sh:class": { + "@id": "sh:class", + "@type": "@id" + }, + "type": "@type", + "value": "@value", + "id": "@id" + }, + "@type": "sh:ValidationResult" +}`