-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapi.go
202 lines (195 loc) · 5.53 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package gojsonld
import (
"strings"
)
/*
Implements the Expansion algorithm.
args:
input - a valid JSON-LD document (represented as a map, an array, or a literal in Go)
options - the options to be used during processing
returns:
a tuple consisting of the result and an error code. If the execution was successful,
the error code is nil
*/
func Expand(input interface{}, options *Options) ([]interface{}, error) {
// 1)
//TODO implement API with promises
// 2)
//TODO handle remote context
inputString, isString := input.(string)
if isString && strings.Contains(inputString, ":") {
remoteDocument, remoteErr := options.DocumentLoader.
loadDocument(inputString)
if remoteErr != nil {
return nil, LOADING_DOCUMENT_FAILED
}
if options.Base == "" {
options.Base = inputString
}
input = remoteDocument.document
}
// 3)
activeContext := Context{}
activeContext.init(options)
// 4)
if options.ExpandContext != nil {
var expandContext interface{}
mapContext,
hasContext := options.ExpandContext.(map[string]interface{})["@context"]
if hasContext {
expandContext = mapContext
}
emptyArray := make([]string, 0)
tmpContext, parseErr := parse(&activeContext, expandContext, emptyArray)
if !isNil(parseErr) {
return nil, parseErr
}
activeContext = *tmpContext
}
// 5)
//TODO load remote context
// 6)
expanded, expandErr := expand(&activeContext, nil, input)
if expandErr != nil {
return nil, expandErr
}
// Final step of expansion algorithm
expandedMap, isMap := expanded.(map[string]interface{})
graphVal, hasGraph := expandedMap["@graph"]
if isMap && hasGraph && len(expandedMap) == 1 {
expanded = graphVal
} else if isNil(expanded) {
expanded = make([]interface{}, 0)
}
if _, isArray := expanded.([]interface{}); !isArray {
tmpArray := make([]interface{}, 0)
tmpArray = append(tmpArray, expanded)
expanded = tmpArray
}
// 7)
return expanded.([]interface{}), nil
}
/*
Implements the Compaction algorithm.
args:
input - a valid JSON-LD document (represented as a map, an array, or a literal in Go)
options - the options to be used during processing
returns:
a tuple consisting of the result and an error code. If the execution was successful,
the error code is nil
*/
func Compact(input interface{}, context interface{},
options *Options) (map[string]interface{}, error) {
// 1)
// TODO use promises
// 2)
expanded, expandErr := Expand(input, options)
if !isNil(expandErr) {
return nil, expandErr
}
//7)
contextMap, isMap := context.(map[string]interface{})
contextValue, hasContext := contextMap["@context"]
if isMap && hasContext {
context = contextValue
}
activeContext := Context{}
activeContext.init(options)
emptyArray := make([]string, 0)
tmpContext, parseErr := parse(&activeContext, context, emptyArray)
if !isNil(parseErr) {
return nil, parseErr
}
activeContext = *tmpContext
//8)
compacted, compactErr := compact(&activeContext, "", expanded,
options.CompactArrays)
if !isNil(compactErr) {
return nil, compactErr
}
//final step of Compaction algorithm
compactedArray, isArray := compacted.([]interface{})
if isArray {
if len(compactedArray) == 0 {
compacted = make(map[string]interface{}, 0)
} else {
graphArg := "@graph"
iri, compactErr := compactIri(&activeContext, &graphArg, nil,
true, false)
if !isNil(compactErr) {
return nil, compactErr
}
tmpMap := make(map[string]interface{}, 0)
tmpMap[*iri] = compacted
compacted = tmpMap
}
}
if !isNil(compacted) && !isNil(context) {
contextMap, isMap := context.(map[string]interface{})
contextArray, isArray := context.([]interface{})
if (isMap && len(contextMap) > 0) || (isArray && len(contextArray) > 0) {
compacted.(map[string]interface{})["@context"] = context
}
}
//9
return compacted.(map[string]interface{}), nil
}
/*
Implements the Flattening algorithm.
args:
input - a valid JSON-LD document (represented as a map, an array, or a literal in Go)
options - the options to be used during processing
returns:
a tuple consisting of the result and an error code. If the execution was successful,
the error code is nil
*/
func Flatten(input interface{}, context interface{},
options *Options) (interface{}, error) {
// 2 - 6
expanded, expandErr := Expand(input, options)
if !isNil(expandErr) {
return nil, expandErr
}
// 7)
contextMap, isMap := context.(map[string]interface{})
contextValue, hasContext := contextMap["@context"]
if isMap && hasContext {
context = contextValue
}
// 8)
flattened, flattenErr := flatten(expanded, context, options)
if !isNil(flattenErr) {
return nil, flattenErr
}
return flattened, nil
}
/*
Converts a JSON-LD document to a RDF dataset
args:
input - a valid JSON-LD document (represented as a map, an array, or a literal in Go)
options - the options to be used during processing
returns:
a tuple consisting of the resulting RDF dataset and an error code.
If the execution was successful, the error code is nil
*/
func ToRDF(input interface{}, options *Options) (*Dataset, error) {
activeContext := &Context{}
activeContext.init(options)
rdfDataset, err := toRDF(activeContext, input)
if !isNil(err) {
return nil, err
}
return rdfDataset, nil
}
/*
Converts a RDF dataset into a JSON-LD document.
args:
input - an RDF dataset
options - the options to be used during processing
returns:
the resulting JSON-LD dataset. The algorithm always returns an array.
*/
func FromRDF(input *Dataset, options *Options) []interface{} {
jsonObject := fromRDF(input, options.UseNativeTypes, options.UseRdfType)
return jsonObject
}