Skip to content

Commit 8dc5d54

Browse files
committed
Allow @container: @set on @type
This change addresses w3c/json-ld-syntax#34
1 parent f856835 commit 8dc5d54

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

ld/api_compact.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
package ld
1616

17-
import "sort"
17+
import (
18+
"sort"
19+
)
1820

1921
// Compact operation compacts the given input using the context
2022
// according to the steps in the Compaction Algorithm:
@@ -98,6 +100,9 @@ func (api *JsonLdApi) Compact(activeCtx *Context, activeProperty string, element
98100
expandedValue := elem[expandedProperty]
99101

100102
if expandedProperty == "@id" || expandedProperty == "@type" {
103+
104+
alias := activeCtx.CompactIri(expandedProperty, nil, true, false)
105+
101106
var compactedValue interface{}
102107

103108
compactedValues := make([]interface{}, 0)
@@ -107,16 +112,16 @@ func (api *JsonLdApi) Compact(activeCtx *Context, activeProperty string, element
107112
compactedValues = append(compactedValues, cv)
108113
}
109114

110-
if len(compactedValues) == 1 {
115+
cont := activeCtx.GetContainer(alias)
116+
isTypeContainer := expandedProperty == "@type" && (len(cont) > 0 && cont[0] == "@set")
117+
if len(compactedValues) == 1 && (!activeCtx.processingMode(1.1) || !isTypeContainer) {
111118
compactedValue = compactedValues[0]
112119
} else {
113120
compactedValue = compactedValues
114121
}
115-
116-
alias := activeCtx.CompactIri(expandedProperty, nil, true, false)
122+
117123
compValArray, isArray := compactedValue.([]interface{})
118-
AddValue(result, alias, compactedValue, isArray && len(compValArray) == 0, true)
119-
124+
AddValue(result, alias, compactedValue, isArray && (len(compValArray) == 0 || isTypeContainer), true)
120125
continue
121126
}
122127

ld/context.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,6 @@ func (c *Context) createTermDefinition(context map[string]interface{}, term stri
340340

341341
defined[term] = false
342342

343-
if IsKeyword(term) {
344-
return NewJsonLdError(KeywordRedefinition, term)
345-
}
346-
347-
delete(c.termDefinitions, term)
348343
value := context[term]
349344
mapValue, isMap := value.(map[string]interface{})
350345
idValue, hasID := mapValue["@id"]
@@ -365,6 +360,18 @@ func (c *Context) createTermDefinition(context map[string]interface{}, term stri
365360
return NewJsonLdError(InvalidTermDefinition, value)
366361
}
367362

363+
if IsKeyword(term) {
364+
vmap, isMap := value.(map[string]interface{})
365+
isTypeContainerAsSet := term == "@type" && isMap && vmap["@container"] == "@set"
366+
if c.processingMode(1.1) && isTypeContainerAsSet {
367+
// this is the only case were redefining a keyword is allowed
368+
} else {
369+
return NewJsonLdError(KeywordRedefinition, term)
370+
}
371+
}
372+
373+
delete(c.termDefinitions, term)
374+
368375
// casting the value so it doesn't have to be done below everytime
369376
val := mapValue
370377

@@ -468,7 +475,7 @@ func (c *Context) createTermDefinition(context map[string]interface{}, term stri
468475
// 15)
469476
} else if vocabValue, containsVocab := c.values["@vocab"]; containsVocab {
470477
definition["@id"] = vocabValue.(string) + term
471-
} else {
478+
} else if term != "@type" {
472479
return NewJsonLdError(InvalidIRIMapping, "relative term definition without vocab mapping")
473480
}
474481
}
@@ -596,6 +603,10 @@ func (c *Context) createTermDefinition(context map[string]interface{}, term stri
596603
}
597604

598605
definition["@container"] = container
606+
607+
if term == "@type" {
608+
definition["@id"] = "@type"
609+
}
599610
}
600611

601612
// scoped contexts

0 commit comments

Comments
 (0)