-
Notifications
You must be signed in to change notification settings - Fork 0
/
curie.go
296 lines (247 loc) · 6.23 KB
/
curie.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//
// Copyright (C) 2020 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/curie
//
/*
Package curie implements the type for compact URI. It defines a generic syntax
for expressing URIs by abbreviated literal as defined by the W3C.
https://www.w3.org/TR/2010/NOTE-curie-20101216/
*/
package curie
import (
"encoding/json"
"fmt"
"net/url"
"strings"
)
//------------------------------------------------------------------------------
//
// IRI
//
//------------------------------------------------------------------------------
// IRI is compact URI, defined as superset of XML QNames, with the modification
// that the format of the strings after the colon is looser.
//
// safe_curie := '[' curie ']'
// curie := [ [ prefix ] ':' ] reference
// prefix := NCName
// reference := irelative-ref (as defined in IRI, RFC 3987)
type IRI string
// Safe transforms CURIE to safe string
func (iri IRI) Safe() string {
if len(iri) == 0 {
return ""
}
return "[" + string(iri) + "]"
}
// MarshalJSON `IRI ⟼ "[prefix:suffix]"`
func (iri IRI) MarshalJSON() ([]byte, error) {
if len(iri) == 0 {
return json.Marshal("")
}
return json.Marshal(iri.Safe())
}
// UnmarshalJSON `"[prefix:suffix]" ⟼ IRI`
func (iri *IRI) UnmarshalJSON(b []byte) error {
var path string
err := json.Unmarshal(b, &path)
if err != nil {
return err
}
*iri = New(path)
return nil
}
//------------------------------------------------------------------------------
//
// Prefixes
//
//------------------------------------------------------------------------------
// Prefixes is a collection of prefixes defined by the application
type Prefixes interface {
Create(string) IRI
Lookup(string) (string, bool)
}
// Namespaces is constant in-memory collection of prefixes defined by the application
type Namespaces map[string]string
// Create new URI using prefix table
func (ns Namespaces) Create(uri string) IRI {
// Note: All non-ASCII code points in the IRI should next be encoded as UTF-8
// https://en.wikipedia.org/wiki/Internationalized_Resource_Identifier
// https://www.ietf.org/rfc/rfc3987.html#section-5.3.2.3
for key, val := range ns {
if strings.HasPrefix(uri, val) {
ref := Decode(uri[len(val):])
return IRI(key + ":" + string(ref))
}
}
return IRI(uri)
}
// Lookup prefix in the map
func (ns Namespaces) Lookup(prefix string) (string, bool) {
val, exists := ns[prefix]
return val, exists
}
//------------------------------------------------------------------------------
//
// IRI algebra
//
//------------------------------------------------------------------------------
// New transform category of strings to IRI.
// It expects UTF-8 string according to RFC 3987.
func New(iri string, args ...interface{}) IRI {
val := iri
if len(val) > 0 && (val[0] == '[' && val[len(val)-1] == ']') {
val = val[1 : len(val)-1]
}
if len(args) > 0 {
val = fmt.Sprintf(iri, args...)
}
return IRI(val)
}
// IsEmpty is an alias to curie.Rank(iri) == 0
func IsEmpty(iri IRI) bool {
return len(iri) == 0
}
// Built-in CURIE ranks
const (
EMPTY = iota
PREFIX
REFERENCE
)
// Rank of CURIE, number of elements
// Rank is an alias of len(curie.Seq(iri))
func Rank(iri IRI) int {
if len(iri) == 0 {
return EMPTY
}
n := strings.IndexRune(string(iri), ':')
if n == len(iri)-1 {
return PREFIX
}
return REFERENCE
}
// Seq Returns CURIE segments
//
// a: ⟼ [ a ]
// b ⟼ [ , b ]
// a:b ⟼ [a, b]
// a:b/c/d ⟼ [a, b/c/d ]
func Seq(iri IRI) (string, string) {
if len(iri) == 0 {
return "", ""
}
n := strings.IndexRune(string(iri), ':')
if n == -1 {
return "", string(iri)
}
if n == len(iri)-1 {
return string(iri)[:n], ""
}
return string(iri)[:n], string(iri)[n+1:]
}
// Prefix decomposes CURIE and return its prefix CURIE as string value.
func Prefix(iri IRI) string {
if len(iri) == 0 {
return ""
}
n := strings.IndexRune(string(iri), ':')
if n == -1 {
return ""
}
return string(iri)[:n]
}
// Reference decomposes CURIE and return its reference as string value.
func Reference(iri IRI) string {
if len(iri) == 0 {
return ""
}
n := strings.IndexRune(string(iri), ':')
if n == -1 {
return string(iri)
}
return string(iri)[n+1:]
}
// URI converts CURIE to fully qualified URL
//
// wikipedia:CURIE ⟼ http://en.wikipedia.org/wiki/CURIE
func URI(prefixes Prefixes, iri IRI) string {
uri, err := URL(prefixes, iri)
if err != nil {
return string(iri)
}
return uri.String()
}
// URI converts fully qualified URL to CURIE
//
// http://en.wikipedia.org/wiki/CURIE ⟼ wikipedia:CURIE
func FromURI(prefixes Prefixes, uri string) IRI {
return prefixes.Create(uri)
}
// URL converts CURIE to fully qualified url.URL type
//
// wikipedia:CURIE ⟼ http://en.wikipedia.org/wiki/CURIE
func URL(prefixes Prefixes, iri IRI) (*url.URL, error) {
if len(iri) == 0 {
return new(url.URL), nil
}
//
// A host language MAY declare a default prefix value, or
// MAY provide a mechanism for defining a default prefix value.
// In such a host language, when the prefix is omitted from a CURIE,
// the default prefix value MUST be used.
//
uri := string(iri)
if prefix, exists := prefixes.Lookup(Prefix(iri)); exists {
uri = prefix + Reference(iri)
}
return url.Parse(uri)
}
// Eq compares two CURIEs, return true if they are equal.
func Eq(a, b IRI) bool {
return a == b
}
// Lt compares two CURIEs, return true if left element is less than supplied one.
func Lt(a, b IRI) bool {
if Rank(a) != Rank(b) {
return Rank(a) < Rank(b)
}
return a < b
}
// Join composes segments into new descendant CURIE.
//
// a:b × [c, d, e] ⟼ a:b/c/d/e
func Join(iri IRI, segments ...string) IRI {
if len(segments) == 0 {
return iri
}
// custom strings.Join that ignores empty segments
n := (len(segments) - 1)
for i := 0; i < len(segments); i++ {
n += len(segments[i])
}
var b strings.Builder
b.Grow(n)
b.WriteString(segments[0])
for _, s := range segments[1:] {
if s != "" {
b.WriteString("/")
b.WriteString(s)
}
}
path := b.String()
if len(path) == 0 {
return iri
}
//
switch Rank(iri) {
case EMPTY:
return IRI(path)
case PREFIX:
return IRI(string(iri) + path)
default:
return IRI(string(iri) + "/" + path)
}
}