1
1
// Copyright 2015-2017 Piprate Limited
2
+ // Copyright 2025 Siemens AG
2
3
//
3
4
// Licensed under the Apache License, Version 2.0 (the "License");
4
5
// 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
48
49
}
49
50
50
51
// 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 )
54
54
if err != nil {
55
55
return nil , err
56
56
}
@@ -66,7 +66,10 @@ func (api *JsonLdApi) Compact(activeCtx *Context, activeProperty string, element
66
66
return nil , err
67
67
}
68
68
69
- propType := activeCtx .GetTermDefinition (activeProperty )["@type" ]
69
+ propType := ""
70
+ if td := activeCtx .GetTermDefinition (activeProperty ); td != nil {
71
+ propType = td .typ
72
+ }
70
73
if _ , isMap := compactedValue .(map [string ]interface {}); ! isMap || propType == "@json" {
71
74
return compactedValue , nil
72
75
}
@@ -94,9 +97,8 @@ func (api *JsonLdApi) Compact(activeCtx *Context, activeProperty string, element
94
97
}
95
98
96
99
// 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 )
100
102
if err != nil {
101
103
return nil , err
102
104
}
@@ -122,9 +124,8 @@ func (api *JsonLdApi) Compact(activeCtx *Context, activeProperty string, element
122
124
// process in lexicographical order, see https://github.com/json-ld/json-ld.org/issues/616
123
125
sort .Strings (types )
124
126
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 )
128
129
if err != nil {
129
130
return nil , err
130
131
}
@@ -273,15 +274,15 @@ func (api *JsonLdApi) Compact(activeCtx *Context, activeProperty string, element
273
274
}
274
275
275
276
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 {
279
280
return nil , err
280
281
}
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 {})
283
284
}
284
- nestResult = result [nestProperty .( string ) ].(map [string ]interface {})
285
+ nestResult = result [nestProperty ].(map [string ]interface {})
285
286
}
286
287
287
288
AddValue (nestResult , itemActiveProperty , make ([]interface {}, 0 ), true , false , true , false )
@@ -302,15 +303,16 @@ func (api *JsonLdApi) Compact(activeCtx *Context, activeProperty string, element
302
303
303
304
// if itemActiveProperty is a @nest property, add values to nestResult, otherwise result
304
305
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 {
308
310
return nil , err
309
311
}
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 {})
312
314
}
313
- nestResult = result [nestProperty .( string ) ].(map [string ]interface {})
315
+ nestResult = result [nestProperty ].(map [string ]interface {})
314
316
}
315
317
316
318
// get @list value if appropriate
@@ -471,12 +473,13 @@ func (api *JsonLdApi) Compact(activeCtx *Context, activeProperty string, element
471
473
mapKey = v .(string )
472
474
}
473
475
} 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
477
480
}
478
481
479
- containerKey , err := activeCtx .CompactIri (indexKey .( string ) , nil , true , false )
482
+ containerKey , err := activeCtx .CompactIri (indexKey , nil , true , false )
480
483
if err != nil {
481
484
return nil , err
482
485
}
@@ -490,7 +493,7 @@ func (api *JsonLdApi) Compact(activeCtx *Context, activeProperty string, element
490
493
var propsArray []interface {}
491
494
compactedItemMap , isMap := compactedItem .(map [string ]interface {})
492
495
if isMap {
493
- props , found := compactedItemMap [indexKey .( string ) ]
496
+ props , found := compactedItemMap [indexKey ]
494
497
if found {
495
498
propsArray = Arrayify (props )
496
499
} else {
@@ -510,11 +513,11 @@ func (api *JsonLdApi) Compact(activeCtx *Context, activeProperty string, element
510
513
} else {
511
514
switch len (others ) {
512
515
case 0 :
513
- delete (compactedItemMap , indexKey .( string ) )
516
+ delete (compactedItemMap , indexKey )
514
517
case 1 :
515
- compactedItemMap [indexKey .( string ) ] = others [0 ]
518
+ compactedItemMap [indexKey ] = others [0 ]
516
519
default :
517
- compactedItemMap [indexKey .( string ) ] = others
520
+ compactedItemMap [indexKey ] = others
518
521
}
519
522
}
520
523
}
0 commit comments