Skip to content

Commit 5eec423

Browse files
authored
Merge pull request #83 from michaeladler/feat/refactor-maps
feat: improve performance and type-safety
2 parents 6795ff5 + dd9b662 commit 5eec423

File tree

4 files changed

+354
-286
lines changed

4 files changed

+354
-286
lines changed

ld/api_compact.go

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2015-2017 Piprate Limited
2+
// Copyright 2025 Siemens AG
23
//
34
// Licensed under the Apache License, Version 2.0 (the "License");
45
// you may not use this file except in compliance with the License.
@@ -48,9 +49,8 @@ func (api *JsonLdApi) Compact(activeCtx *Context, activeProperty string, element
4849
}
4950

5051
// use any scoped context on activeProperty
51-
td := activeCtx.GetTermDefinition(activeProperty)
52-
if ctx, hasCtx := td["@context"]; hasCtx {
53-
newCtx, err := activeCtx.parse(ctx, make([]string, 0), false, true, false, true)
52+
if td := activeCtx.GetTermDefinition(activeProperty); td != nil && td.hasContext {
53+
newCtx, err := activeCtx.parse(td.context, make([]string, 0), false, true, false, true)
5454
if err != nil {
5555
return nil, err
5656
}
@@ -66,7 +66,10 @@ func (api *JsonLdApi) Compact(activeCtx *Context, activeProperty string, element
6666
return nil, err
6767
}
6868

69-
propType := activeCtx.GetTermDefinition(activeProperty)["@type"]
69+
propType := ""
70+
if td := activeCtx.GetTermDefinition(activeProperty); td != nil {
71+
propType = td.typ
72+
}
7073
if _, isMap := compactedValue.(map[string]interface{}); !isMap || propType == "@json" {
7174
return compactedValue, nil
7275
}
@@ -94,9 +97,8 @@ func (api *JsonLdApi) Compact(activeCtx *Context, activeProperty string, element
9497
}
9598

9699
// apply property-scoped context after reverting term-scoped context
97-
propertyScopedCtx := inputCtx.GetTermDefinition(activeProperty)["@context"]
98-
if propertyScopedCtx != nil {
99-
newCtx, err := activeCtx.parse(propertyScopedCtx, nil, false, true, false, true)
100+
if td := inputCtx.GetTermDefinition(activeProperty); td != nil && td.context != nil {
101+
newCtx, err := activeCtx.parse(td.context, nil, false, true, false, true)
100102
if err != nil {
101103
return nil, err
102104
}
@@ -122,9 +124,8 @@ func (api *JsonLdApi) Compact(activeCtx *Context, activeProperty string, element
122124
// process in lexicographical order, see https://github.com/json-ld/json-ld.org/issues/616
123125
sort.Strings(types)
124126
for _, tt := range types {
125-
td := inputCtx.GetTermDefinition(tt)
126-
if ctx, hasCtx := td["@context"]; hasCtx {
127-
newCtx, err := activeCtx.parse(ctx, nil, false, false, false, false)
127+
if td := inputCtx.GetTermDefinition(tt); td != nil && td.hasContext {
128+
newCtx, err := activeCtx.parse(td.context, nil, false, false, false, false)
128129
if err != nil {
129130
return nil, err
130131
}
@@ -273,15 +274,15 @@ func (api *JsonLdApi) Compact(activeCtx *Context, activeProperty string, element
273274
}
274275

275276
nestResult := result
276-
nestProperty, hasNest := activeCtx.GetTermDefinition(itemActiveProperty)["@nest"]
277-
if hasNest {
278-
if err := api.checkNestProperty(activeCtx, nestProperty.(string)); err != nil {
277+
if td := activeCtx.GetTermDefinition(itemActiveProperty); td != nil && td.nest != "" {
278+
nestProperty := td.nest
279+
if err := api.checkNestProperty(activeCtx, nestProperty); err != nil {
279280
return nil, err
280281
}
281-
if _, isMap := result[nestProperty.(string)].(map[string]interface{}); !isMap {
282-
result[nestProperty.(string)] = make(map[string]interface{})
282+
if _, isMap := result[nestProperty].(map[string]interface{}); !isMap {
283+
result[nestProperty] = make(map[string]interface{})
283284
}
284-
nestResult = result[nestProperty.(string)].(map[string]interface{})
285+
nestResult = result[nestProperty].(map[string]interface{})
285286
}
286287

287288
AddValue(nestResult, itemActiveProperty, make([]interface{}, 0), true, false, true, false)
@@ -302,15 +303,16 @@ func (api *JsonLdApi) Compact(activeCtx *Context, activeProperty string, element
302303

303304
// if itemActiveProperty is a @nest property, add values to nestResult, otherwise result
304305
nestResult := result
305-
nestProperty, hasNest := activeCtx.GetTermDefinition(itemActiveProperty)["@nest"]
306-
if hasNest {
307-
if err := api.checkNestProperty(activeCtx, nestProperty.(string)); err != nil {
306+
307+
if td := activeCtx.GetTermDefinition(itemActiveProperty); td != nil && td.nest != "" {
308+
nestProperty := td.nest
309+
if err := api.checkNestProperty(activeCtx, nestProperty); err != nil {
308310
return nil, err
309311
}
310-
if _, isMap := result[nestProperty.(string)].(map[string]interface{}); !isMap {
311-
result[nestProperty.(string)] = make(map[string]interface{})
312+
if _, isMap := result[nestProperty].(map[string]interface{}); !isMap {
313+
result[nestProperty] = make(map[string]interface{})
312314
}
313-
nestResult = result[nestProperty.(string)].(map[string]interface{})
315+
nestResult = result[nestProperty].(map[string]interface{})
314316
}
315317

316318
// get @list value if appropriate
@@ -471,12 +473,13 @@ func (api *JsonLdApi) Compact(activeCtx *Context, activeProperty string, element
471473
mapKey = v.(string)
472474
}
473475
} else if isIndexContainer {
474-
indexKey := activeCtx.GetTermDefinition(itemActiveProperty)["@index"]
475-
if indexKey == nil {
476-
indexKey = "@index"
476+
477+
indexKey := "@index"
478+
if td := activeCtx.GetTermDefinition(itemActiveProperty); td != nil && td.index != "" {
479+
indexKey = td.index
477480
}
478481

479-
containerKey, err := activeCtx.CompactIri(indexKey.(string), nil, true, false)
482+
containerKey, err := activeCtx.CompactIri(indexKey, nil, true, false)
480483
if err != nil {
481484
return nil, err
482485
}
@@ -490,7 +493,7 @@ func (api *JsonLdApi) Compact(activeCtx *Context, activeProperty string, element
490493
var propsArray []interface{}
491494
compactedItemMap, isMap := compactedItem.(map[string]interface{})
492495
if isMap {
493-
props, found := compactedItemMap[indexKey.(string)]
496+
props, found := compactedItemMap[indexKey]
494497
if found {
495498
propsArray = Arrayify(props)
496499
} else {
@@ -510,11 +513,11 @@ func (api *JsonLdApi) Compact(activeCtx *Context, activeProperty string, element
510513
} else {
511514
switch len(others) {
512515
case 0:
513-
delete(compactedItemMap, indexKey.(string))
516+
delete(compactedItemMap, indexKey)
514517
case 1:
515-
compactedItemMap[indexKey.(string)] = others[0]
518+
compactedItemMap[indexKey] = others[0]
516519
default:
517-
compactedItemMap[indexKey.(string)] = others
520+
compactedItemMap[indexKey] = others
518521
}
519522
}
520523
}

ld/api_expand.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2015-2017 Piprate Limited
2+
// Copyright 2025 Siemens AG
23
//
34
// Licensed under the Apache License, Version 2.0 (the "License");
45
// you may not use this file except in compliance with the License.
@@ -85,7 +86,10 @@ func (api *JsonLdApi) Expand(activeCtx *Context, activeProperty string, element
8586
}
8687

8788
// Get any property-scoped context for activeProperty
88-
propertyScopedCtx := activeCtx.GetTermDefinition(activeProperty)["@context"]
89+
var propertyScopedCtx interface{}
90+
if td := activeCtx.GetTermDefinition(activeProperty); td != nil {
91+
propertyScopedCtx = td.context
92+
}
8993

9094
// second, determine if any type-scoped context should be reverted; it
9195
// should only be reverted when the following are all true:
@@ -178,9 +182,8 @@ func (api *JsonLdApi) Expand(activeCtx *Context, activeProperty string, element
178182
}
179183

180184
for _, tt := range types {
181-
td := typeScopedContext.GetTermDefinition(tt)
182-
if ctx, hasCtx := td["@context"]; hasCtx {
183-
newCtx, err := activeCtx.parse(ctx, nil, false, false, false, false)
185+
if td := typeScopedContext.GetTermDefinition(tt); td != nil && td.hasContext {
186+
newCtx, err := activeCtx.parse(td.context, nil, false, false, false, false)
184187
if err != nil {
185188
return nil, err
186189
}
@@ -666,10 +669,10 @@ func (api *JsonLdApi) expandObject(activeCtx *Context, activeProperty string, ex
666669
// use potential scoped context for key
667670
termCtx := activeCtx
668671
td := activeCtx.GetTermDefinition(key)
669-
if ctx, hasCtx := td["@context"]; hasCtx {
672+
if td != nil && td.hasContext {
670673
// TODO: fix calling a private method
671674
//termCtx, err = activeCtx.Parse(ctx)
672-
termCtx, err = activeCtx.parse(ctx, make([]string, 0), false, true, false, true)
675+
termCtx, err = activeCtx.parse(td.context, make([]string, 0), false, true, false, true)
673676
if err != nil {
674677
return err
675678
}
@@ -679,7 +682,8 @@ func (api *JsonLdApi) expandObject(activeCtx *Context, activeProperty string, ex
679682
if termCtx.HasContainerMapping(key, "@language") && isMap {
680683
var expandedValueList []interface{}
681684

682-
dir, hasDir := td["@direction"]
685+
dir, hasDir := td.direction, td.hasDirection
686+
683687
for _, language := range GetOrderedKeys(valueMap) {
684688
expandedLanguage, err := termCtx.ExpandIri(language, false, true, nil, nil)
685689
if err != nil {
@@ -706,7 +710,7 @@ func (api *JsonLdApi) expandObject(activeCtx *Context, activeProperty string, ex
706710
if dir != nil {
707711
v["@direction"] = dir
708712
}
709-
} else if defaultDir, found := termCtx.values["@direction"]; found {
713+
} else if defaultDir := termCtx.values.direction; defaultDir != "" {
710714
v["@direction"] = defaultDir
711715
}
712716
expandedValueList = append(expandedValueList, v)
@@ -715,18 +719,18 @@ func (api *JsonLdApi) expandObject(activeCtx *Context, activeProperty string, ex
715719
expandedValue = expandedValueList
716720
} else if termCtx.HasContainerMapping(key, "@index") && isMap { // 7.6)
717721
asGraph := termCtx.HasContainerMapping(key, "@graph")
718-
indexKey := termCtx.GetTermDefinition(key)["@index"]
719-
if indexKey == nil {
720-
indexKey = "@index"
722+
indexKey := "@index"
723+
if tdKey := termCtx.GetTermDefinition(key); tdKey != nil && tdKey.index != "" {
724+
indexKey = tdKey.index
721725
}
722726
var propertyIndex string
723727
if indexKey != "@index" {
724-
propertyIndex, err = activeCtx.ExpandIri(indexKey.(string), false, true, nil, nil)
728+
propertyIndex, err = activeCtx.ExpandIri(indexKey, false, true, nil, nil)
725729
if err != nil {
726730
return err
727731
}
728732
}
729-
expandedValue, err = api.expandIndexMap(termCtx, key, valueMap, indexKey.(string), asGraph, propertyIndex,
733+
expandedValue, err = api.expandIndexMap(termCtx, key, valueMap, indexKey, asGraph, propertyIndex,
730734
opts)
731735
if err != nil {
732736
return err
@@ -747,6 +751,7 @@ func (api *JsonLdApi) expandObject(activeCtx *Context, activeProperty string, ex
747751
}
748752
} else {
749753
isList := expandedProperty == "@list"
754+
tdKey := activeCtx.GetTermDefinition(key)
750755
if isList || expandedProperty == "@set" {
751756
nextActiveProperty := activeProperty
752757
if isList && expandedActiveProperty == "@graph" {
@@ -756,7 +761,7 @@ func (api *JsonLdApi) expandObject(activeCtx *Context, activeProperty string, ex
756761
if err != nil {
757762
return err
758763
}
759-
} else if activeCtx.GetTermDefinition(key)["@type"] == "@json" {
764+
} else if tdKey != nil && tdKey.typ == "@json" {
760765
expandedValue = map[string]interface{}{
761766
"@type": "@json",
762767
"@value": value,
@@ -908,15 +913,12 @@ func (api *JsonLdApi) expandIndexMap(activeCtx *Context, activeProperty string,
908913

909914
indexCtx := activeCtx
910915
// if indexKey is @type, there may be a context defined for it
911-
if indexKey == "@type" {
912-
td := activeCtx.GetTermDefinition(key)
913-
if ctx, hasCtx := td["@context"]; hasCtx {
914-
newCtx, err := activeCtx.Parse(ctx)
915-
if err != nil {
916-
return nil, err
917-
}
918-
indexCtx = newCtx
916+
if td := activeCtx.GetTermDefinition(key); indexKey == "@type" && td != nil && td.hasContext {
917+
newCtx, err := activeCtx.Parse(td.context)
918+
if err != nil {
919+
return nil, err
919920
}
921+
indexCtx = newCtx
920922
}
921923

922924
// 7.6.2.1)

0 commit comments

Comments
 (0)