forked from infobloxopen/atlas-app-toolkit
/
v0.go
323 lines (267 loc) · 6.44 KB
/
v0.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package bloxid
import (
"bytes"
"encoding/base32"
"encoding/hex"
"errors"
"fmt"
"strings"
)
const (
UniqueIDEncodedMinCharSize = 16
VersionUnknown Version = iota
Version0 Version = iota
)
type Version uint8
func (v Version) String() string {
name, ok := version_name[v]
if !ok {
return version_name[VersionUnknown]
}
return name
}
var version_name = map[Version]string{
VersionUnknown: "unknown",
Version0: "blox0",
}
var name_version = map[string]Version{
"unknown": VersionUnknown,
"blox0": Version0,
}
var (
ErrInvalidVersion error = errors.New("invalid bloxid version")
ErrInvalidEntityDomain error = errors.New("entity domain must be non-empty")
ErrInvalidEntityType error = errors.New("entity type must be non-empty")
ErrInvalidUniqueIDLen error = errors.New("unique ID did not meet minimum length requirements")
ErrIDEmpty error = errors.New("empty bloxid")
ErrV0Parts error = errors.New("invalid number of parts found")
)
const (
V0Delimiter = "."
bloxidTypeLen = 4
)
type EncodeDecodeOpts func(o *V0Options)
// NewV0 parse a string into a typed guid, return an error
// if the string fails validation.
func NewV0(bloxid string, fnOpts ...EncodeDecodeOpts) (*V0, error) {
opts := generateV0Options(fnOpts...)
if len(strings.TrimSpace(bloxid)) == 0 {
return generateV0(opts)
}
return parseV0(bloxid, opts.hashidSalt)
}
func generateV0Options(fnOpts ...EncodeDecodeOpts) *V0Options {
var opts *V0Options = new(V0Options)
for _, fn := range fnOpts {
fn(opts)
}
return opts
}
func parseV0(bloxid, salt string) (*V0, error) {
if len(bloxid) == 0 {
return nil, ErrIDEmpty
}
if err := validateV0(bloxid); err != nil {
return nil, err
}
parts := strings.Split(bloxid, V0Delimiter)
v0 := &V0{
version: name_version[parts[0]],
entityDomain: parts[1],
entityType: parts[2],
realm: parts[3],
encoded: parts[4],
}
decodedTall := strings.ToUpper(parts[4])
decoded, err := base32.StdEncoding.DecodeString(decodedTall)
if err != nil {
return nil, fmt.Errorf("unable to decode id: %s", err)
}
switch {
case bytes.HasPrefix(decoded, hashIDPrefixBytes):
var hashed string
hashed = strings.TrimSpace(string(decoded[bloxidTypeLen:]))
v0.hashIDInt64, err = getInt64FromHashID(hashed, salt)
if err != nil {
return nil, err
}
v0.decoded = fmt.Sprintf("%v", v0.hashIDInt64)
v0.scheme = IDSchemeHashID
case bytes.HasPrefix(decoded, extrinsicIDPrefixBytes):
v0.decoded = strings.TrimSpace(string(decoded[bloxidTypeLen:]))
v0.scheme = IDSchemeExtrinsic
default:
if len(v0.encoded) < DefaultUniqueIDEncodedCharSize {
return nil, ErrInvalidUniqueIDLen
}
v0.decoded = hex.EncodeToString(decoded)
v0.scheme = IDSchemeRandom
}
return v0, nil
}
func validateV0(bloxid string) error {
parts := strings.Split(bloxid, V0Delimiter)
if len(parts) != 5 {
return ErrV0Parts
}
if ver := parts[0]; ver != Version0.String() {
return ErrInvalidVersion
}
if entityDomain := parts[1]; len(entityDomain) == 0 {
return ErrInvalidEntityDomain
}
if entityType := parts[2]; len(entityType) == 0 {
return ErrInvalidEntityType
}
if len(parts[4]) < UniqueIDEncodedMinCharSize {
return ErrInvalidUniqueIDLen
}
return nil
}
var _ ID = &V0{}
// V0 represents a typed guid
type V0 struct {
version Version
entityDomain string
entityType string
realm string
decoded string
encoded string
hashIDInt64 int64
scheme string
}
// Serialize the typed guid as a string
func (v *V0) String() string {
s := []string{
v.Version(),
v.entityDomain,
v.entityType,
v.realm,
v.encoded,
}
return strings.Join(s, V0Delimiter)
}
// Version of the string
func (v *V0) Version() string {
if v == nil {
return VersionUnknown.String()
}
return v.version.String()
}
// Domain implements ID.domain
func (v *V0) Domain() string {
if v == nil {
return ""
}
return v.entityDomain
}
// Type implements ID.entityType
func (v *V0) Type() string {
if v == nil {
return ""
}
return v.entityType
}
// Realm implements ID.realm
func (v *V0) Realm() string {
if v == nil {
return ""
}
return v.realm
}
// DecodedID implements ID.decoded
func (v *V0) DecodedID() string {
if v == nil {
return ""
}
return v.decoded
}
// EncodedID implements ID.encoded
func (v *V0) EncodedID() string {
if v == nil {
return ""
}
return v.encoded
}
// HashIDInt64 implements ID.hashIDInt64
func (v *V0) HashIDInt64() int64 {
if v == nil || v.scheme != IDSchemeHashID {
return -1
}
return v.hashIDInt64
}
// Scheme of the id
func (v *V0) Scheme() string {
if v == nil {
return ""
}
return v.scheme
}
// V0Options required options to create a typed guid
type V0Options struct {
entityDomain string
entityType string
realm string
hashidSalt string
scheme string
schemer Schemer
}
type GenerateV0Opts func(o *V0Options)
func WithEntityDomain(domain string) func(o *V0Options) {
return func(o *V0Options) {
o.entityDomain = domain
}
}
func WithEntityType(eType string) func(o *V0Options) {
return func(o *V0Options) {
o.entityType = eType
}
}
func WithRealm(realm string) func(o *V0Options) {
return func(o *V0Options) {
o.realm = realm
}
}
func generateV0(opts *V0Options, fnOpts ...GenerateV0Opts) (*V0, error) {
for _, fn := range fnOpts {
fn(opts)
}
encoded, decoded, err := uniqueID(opts)
if err != nil {
return nil, err
}
v0 := &V0{
version: Version0,
realm: opts.realm,
decoded: decoded,
encoded: encoded,
entityDomain: opts.entityDomain,
entityType: opts.entityType,
scheme: opts.scheme,
}
if !isNilInterface(opts.schemer) {
if t, ok := opts.schemer.(*schemerHashIDInt64); ok {
v0.hashIDInt64 = t.HashIDInt64
}
}
return v0, nil
}
func uniqueID(opts *V0Options) (encoded, decoded string, err error) {
if !isNilInterface(opts.schemer) {
opts.scheme, decoded, encoded, err = opts.schemer.FromEntityID(opts)
return
}
// default to random scheme to generate the entity id
rndm := randDefault()
decoded = hex.EncodeToString(rndm)
encoded = strings.ToLower(base32.StdEncoding.EncodeToString(rndm))
opts.scheme = IDSchemeRandom
return
}
func encodeLowerAlphaNumeric(idPrefix, decoded string) string {
const rfc4648NoPaddingChars = 5
rem := rfc4648NoPaddingChars - ((len(decoded) + len(idPrefix)) % rfc4648NoPaddingChars)
pad := strings.Repeat(" ", rem)
padded := idPrefix + decoded + pad
return strings.ToLower(base32.StdEncoding.EncodeToString([]byte(padded)))
}