Debian Codegraph @

context.go

raw symbols SCIP
1 package jsonld
2
3 import (
4 "encoding/json"
5 "strings"
6 )
7
8 // From the JSON-LD spec 3.3
9 // https://www.w3.org/TR/json-ld/#dfn-keyword
10 const (
11 // @context
12 // Used to define the short-hand names that are used throughout a JSON-LD document.
13 // These short-hand names are called terms and help developers to express specific identifiers in a compact manner.
14 // The @context keyword is described in detail in section 5.1 The Context.
15 ContextKw Term = "@context"
16 // @id
17 //Used to uniquely identify things that are being described in the document with IRIs or blank node identifiers.
18 // This keyword is described in section 5.3 Node Identifiers.
19 IdKw Term = "@id"
20 // @value
21 // Used to specify the data that is associated with a particular property in the graph.
22 // This keyword is described in section 6.9 String Internationalization and section 6.4 Typed Values.
23 ValueKw Term = "@value"
24 // @language
25 // Used to specify the language for a particular string value or the default language of a JSON-LD document.
26 // This keyword is described in section 6.9 String Internationalization.
27 LanguageKw Term = "@language"
28 //@type
29 //Used to set the data type of a node or typed value. This keyword is described in section 6.4 Typed Values.
30 TypeKw Term = "@type"
31 // @container
32 // Used to set the default container type for a term. This keyword is described in section 6.11 Sets and Lists.
33 ContainerKw Term = "@container"
34 //@list
35 //Used to express an ordered set of data. This keyword is described in section 6.11 Sets and Lists.
36 ListKw Term = "@list"
37 // @set
38 // Used to express an unordered set of data and to ensure that values are always represented as arrays.
39 // This keyword is described in section 6.11 Sets and Lists.
40 SetKw Term = "@set"
41 // @reverse
42 // Used to express reverse properties. This keyword is described in section 6.12 Reverse Properties.
43 ReverseKw Term = "@reverse"
44 // @index
45 // Used to specify that a container is used to index information and that processing should continue deeper
46 // into a JSON data structure. This keyword is described in section 6.16 Data Indexing.
47 IndexKw Term = "@index"
48 // @base
49 // Used to set the base IRI against which relative IRIs are resolved. T
50 // his keyword is described in section 6.1 Base IRI.
51 BaseKw Term = "@base"
52 // @vocab
53 // Used to expand properties and values in @type with a common prefix IRI.
54 // This keyword is described in section 6.2 Default Vocabulary.
55 VocabKw Term = "@vocab"
56 // @graph
57 // Used to express a graph. This keyword is described in section 6.13 Named Graphs.
58 GraphKw Term = "@graph"
59 )
60
61 // ContentType is the content type of JsonLD documents
62 const ContentType = `application/ld+json; profile="https://www.w3.org/ns/activitystreams"`
63
64 type (
65 // Ref basic type
66 LangRef string
67 // Term represents the JSON-LD term for @context maps
68 Term string
69 // IRI is a International Resource Identificator
70 IRI string
71 // Terms is an array of Term values
72 Terms []Term
73 )
74
75 // Nillable
76 type Nillable interface {
77 IsNil() bool
78 }
79
80 type IRILike interface {
81 IsCompact() bool
82 IsAbsolute() bool
83 IsRelative() bool
84 }
85
86 func (i IRI) IsCompact() bool {
87 return !i.IsAbsolute() && strings.Contains(string(i), ":")
88 }
89 func (i IRI) IsAbsolute() bool {
90 return strings.Contains(string(i), "https://")
91 }
92 func (i IRI) IsRelative() bool {
93 return !i.IsAbsolute()
94 }
95
96 var keywords = Terms{
97 BaseKw,
98 ContextKw,
99 ContainerKw,
100 GraphKw,
101 IdKw,
102 IndexKw,
103 LanguageKw,
104 ListKw,
105 ReverseKw,
106 SetKw,
107 TypeKw,
108 ValueKw,
109 VocabKw,
110 }
111
112 const NilTerm Term = "-"
113 const NilLangRef LangRef = "-"
114
115 type ContextObject struct {
116 ID interface{} `jsonld:"@id,omitempty,collapsible"`
117 Type interface{} `jsonld:"@type,omitempty,collapsible"`
118 }
119
120 // Context is of of the basic JSON-LD elements.
121 // It represents an array of ContextElements
122 type Context []ContextElement
123
124 // ContextElement is used to map terms to IRIs or JSON objects.
125 // Terms are case sensitive and any valid string that is not a reserved JSON-LD
126 // keyword can be used as a term.
127 type ContextElement struct {
128 Term Term
129 IRI IRI
130 }
131
132 func GetContext() Context {
133 return Context{}
134 }
135
136 //type Context Collapsible
137
138 // Collapsible is an interface used by the JSON-LD marshaller to collapse a struct to one single value
139 type Collapsible interface {
140 Collapse() interface{}
141 }
142
143 // Collapse returns the plain text collapsed value of the current Context object
144 func (c Context) Collapse() interface{} {
145 if len(c) == 1 && len(c[0].IRI) > 0 {
146 return c[0].IRI
147 }
148 for _, el := range c {
149 if el.Term == NilTerm {
150
151 }
152 }
153
154 return c
155 }
156
157 // Collapse returns the plain text collapsed value of the current IRI string
158 func (i IRI) Collapse() interface{} {
159 return i
160 }
161
162 // MarshalText basic stringify function
163 func (i IRI) MarshalText() ([]byte, error) {
164 return []byte(i), nil
165 }
166
167 // MarshalJSON returns the JSON document represented by the current Context
168 // This should return :
169 // If only one element in the context and the element has no Term -> json marshaled string
170 // If multiple elements in the context without Term -> json marshaled array of strings
171 // If multiple elements where at least one doesn't have a Term and one has a Term -> json marshaled array
172 // If multiple elements where all have Terms -> json marshaled object
173 func (c Context) MarshalJSON() ([]byte, error) {
174 mapIRI := make(map[Term]IRI, 0)
175 arr := make([]interface{}, 0)
176 i := 0
177 if len(c) == 1 && len(c[0].IRI) > 0 {
178 return json.Marshal(c[0].IRI)
179 }
180 for _, el := range c {
181 t := el.Term
182 iri := el.IRI
183 if t.IsNil() {
184 arr = append(arr, iri)
185 i += 1
186 } else {
187 if len(iri) > 0 {
188 mapIRI[t] = iri
189 }
190 }
191 }
192 if len(mapIRI) > 0 {
193 if len(arr) == 0 {
194 return json.Marshal(mapIRI)
195 }
196 arr = append(arr, mapIRI)
197 }
198 return json.Marshal(arr)
199 }
200
201 // UnmarshalJSON tries to load the Context from the incoming json value
202 func (c *Context) UnmarshalJSON(data []byte) error {
203 return nil
204 }
205
206 // IsNil returns if current LangRef is equal to empty string or to its nil value
207 func (l LangRef) IsNil() bool {
208 return len(l) == 0 || l == NilLangRef
209 }
210
211 // IsNil returns if current IRI is equal to empty string
212 func (i IRI) IsNil() bool {
213 return len(i) == 0
214 }
215
216 // IsNil returns if current Term is equal to empty string or to its nil value
217 func (i Term) IsNil() bool {
218 return len(i) == 0 || i == NilTerm
219 }