-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnamespace.go
More file actions
231 lines (190 loc) · 6.6 KB
/
namespace.go
File metadata and controls
231 lines (190 loc) · 6.6 KB
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
package namespace
import (
"encoding"
"github.com/yehan2002/errors"
)
// See https://www.minecraft.net/en-us/article/minecraft-snapshot-17w43a
// Default this is the namespace used for minecraft items/effects ect.
// Plugins should use their own namespace if they aren't overriding vanilla features
var Default NS = Namespace("minecraft")
const (
// ErrEmpty the namespaced key is empty.
ErrEmpty = errors.Const("namespace: namespaced key is empty")
// ErrNil the namespace or key is nil.
// This is returned by methods on the zero value of [NS] or [NSK]
ErrNil = errors.Const("namespace: nil namespace or key")
// ErrTooLong the namespaced key exceeded the max allowed length.
ErrTooLong = errors.Const("namespace: namespaced key is too long")
// ErrInvalidChar the namespaced key contained an invalid character
ErrInvalidChar = errors.Const("namespace: namespaced key contains illegal characters")
// ErrTrailingSep the nsk contained a trailing `:`
ErrTrailingSep = errors.Const("namespace: namespace contains trailing ':' character")
)
var (
_ encoding.TextMarshaler = (*NS)(nil)
_ encoding.TextUnmarshaler = (*NS)(nil)
_ encoding.TextMarshaler = (*NSK)(nil)
_ encoding.TextUnmarshaler = (*NSK)(nil)
)
var namespaces = syncMap[string, *ns]{
v: map[string]*ns{},
New: func(n string) *ns {
ns := &ns{name: n, keys: &syncMap[string, *nsk]{v: map[string]*nsk{}}}
ns.keys.New = func(k string) *nsk {
return &nsk{ns: NS{ns}, key: k, full: ns.name + ":" + k}
}
return ns
},
}
// NS is a namespace.
// A namespace can only contain digits, lowercase letters, underscores and hyphens.
//
// To compare namespaces use `==` operator directly (not on the pointer value) or use the [NS.Equal] method.
type NS struct{ ns *ns }
type ns struct {
name string
keys *syncMap[string, *nsk]
}
// Equal returns if this namespace and the given namespace are equal.
func (n NS) Equal(n2 NS) bool { return n.ns == n2.ns }
// IsNil returns if this nsk is nil.
// if this returns true, calling [NS.Key] will panic.
func (n NS) IsNil() bool { return n.ns == nil }
// Key creates a new key inside this namespace.
// All invalid characters in the will be replaced with an underscore.
//
// This panics if the length of the key is larger than [MaxLength] or is zero.
// If [NS.IsNil] returns true, this will panic.
func (n NS) Key(k string) NSK {
if n.ns == nil {
panic(ErrNil)
}
if nsk, ok := n.ns.keys.Get(k); ok {
return NSK{nsk}
}
_, k, _ = parseNSK(k, false, true, false)
return NSK{n.ns.keys.GetOrCreate(k)}
}
// ParseKey creates a new key inside this namespace.
// Unlike [NS.Key], this will return an error if any invalid characters are encountered.
//
// This returns an error if the length of the key is larger than [MaxLength] or is zero
// If [NS.IsNil] returns true, this will return [ErrNil].
func (n NS) ParseKey(k string) (nsk NSK, err error) {
if n.ns == nil {
return NSK{}, ErrNil
}
if nsk, ok := n.ns.keys.Get(k); ok {
return NSK{nsk}, nil
}
_, k, err = parseNSK(k, true, true, false)
if err != nil {
return NSK{}, err
}
return NSK{n.ns.keys.GetOrCreate(k)}, nil
}
// MarshalText implements encoding.TextMarshaler
func (n *NS) MarshalText() (text []byte, err error) { return []byte(n.String()), nil }
// UnmarshalText implements encoding.TextUnmarshaler
func (n *NS) UnmarshalText(text []byte) (err error) {
*n, err = ParseNamespace(string(text), true)
return
}
func (n NS) String() string {
if n.ns == nil {
return ""
}
return n.ns.name
}
// NSK is a namespaced key.
// A namespace can only contain digits, lowercase letters, underscores, hyphens, forward slash and dots.
//
// To compare namespaces use `==` operator directly (not on the pointer value) or use the [NSK.Equal] method.
type NSK struct{ nsk *nsk }
type nsk struct {
ns NS
key, full string
}
// Equal returns if this namespaced key is equal to the given namespaced key.
func (n NSK) Equal(n2 NSK) bool { return n.nsk == n2.nsk }
// IsNil returns if this nsk is nil.
// if this returns true, calling [NSK.Namespace] will panic.
func (n NSK) IsNil() bool { return n.nsk == nil }
// Namespace gets the namespace for this key.
// If [NSK.IsNil] returns true, this will panic.
func (n NSK) Namespace() NS {
if n.nsk == nil {
panic(ErrNil)
}
return n.nsk.ns
}
// Key gets the key part of the namespaced key (the part after the ':').
// If the namespaced key is nil, this returns an empty string.
func (n NSK) Key() string {
if n.nsk == nil {
return ""
}
return n.nsk.key
}
// MarshalText implements encoding.TextMarshaler
func (n *NSK) MarshalText() (text []byte, err error) { return []byte(n.String()), nil }
// UnmarshalText implements encoding.TextUnmarshaler
func (n *NSK) UnmarshalText(text []byte) (err error) {
*n, err = ParseKey(string(text), true)
return
}
func (n NSK) String() string {
if n.nsk == nil {
return ""
}
return n.nsk.full
}
// Namespace creates a new namespace from the given string.
//
// This panics if the length of the namespace is larger than [MaxLength] or is zero.
// All invalid characters in the namespace are replaced with underscores.
func Namespace(namespace string) NS {
ns, err := ParseNamespace(namespace, false)
if err != nil {
panic(err)
}
return ns
}
// ParseNamespace creates a new namespace from the given string.
//
// This returns an error if the length of the namespace is larger than [MaxLength] or is zero.
// If strict mode is enabled, this returns an error when it encounters an invalid character.
// Otherwise, this replaces all invalid characters with underscores.
func ParseNamespace(v string, strict bool) (NS, error) {
if ns, ok := namespaces.Get(v); ok {
return NS{ns}, nil
}
ns, _, err := parseNSK(v, strict, true, true)
if err != nil {
return NS{}, err
}
return NS{namespaces.GetOrCreate(ns)}, nil
}
// Key creates a new namespaced key from the given string.
//
// This panics if the length of the key is larger than [MaxLength] or is zero.
// All invalid characters in the namespaced key are replaced with underscores.
func Key(namespacedKey string) NSK {
nsk, err := ParseKey(namespacedKey, false)
if err != nil {
panic(err)
}
return nsk
}
// ParseKey creates a new namespaced key from the given string.
//
// This returns an error if the length of the key is larger than [MaxLength] or is zero.
// If strict mode is enabled, this returns an error when it encounters an invalid character.
// Otherwise, this replaces all invalid characters with underscores.
func ParseKey(namespacedKey string, strict bool) (NSK, error) {
ns, k, err := parseNSK(namespacedKey, strict, false, false)
if err != nil {
return NSK{}, err
}
return NSK{namespaces.GetOrCreate(ns).keys.GetOrCreate(k)}, nil
}