-
Notifications
You must be signed in to change notification settings - Fork 4
/
kee.go
executable file
·116 lines (97 loc) · 3.03 KB
/
kee.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
// Package kee simplifies generating, parsing, composing, encoding and decoding resource identifiers
package kee
import (
"regexp"
"bytes"
"text/template"
)
var (
// UUID handler for creating Universally Unique Identifiers
UUID UUIDCtrl
// FPIID handler for creating Fixed Precision Integer Identifiers
FPIID FPIIDCtrl
// APIID handler for creating Arbitrary Precision Integer Identifiers
APIID APIIDCtrl
// TOTP handler for One-time Time Based Passwords
TOTP TOTPCtrl
// JUMBLE handler for word-jumble identifiers
JUMBLE JUMCtrl
)
func init() {
UUID = UUIDCtrl{
&UUIDOptions,
map[string]string{ // Namespaces for Version 3 and 5
"DNS": "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
"URL": "6ba7b811-9dad-11d1-80b4-00c04fd430c8",
"OID": "6ba7b812-9dad-11d1-80b4-00c04fd430c8",
"X500": "6ba7b814-9dad-11d1-80b4-00c04fd430c8",
"NIL": "00000000-0000-0000-0000-000000000000",
},
}
FPIID = FPIIDCtrl{&FPIIDOptions}
APIID = APIIDCtrl{&APIIDOptions}
TOTP = TOTPCtrl{&TOTPOptions}
JUMBLE = JUMCtrl{
phrase: []jumWord{
&jumAdjectives{},
&jumNouns{},
&jumVerbs{},
&jumAdverbs{},
},
}
}
// Handler is a handler for custom IDs. Use NewHandler to instantiate.
type Handler struct {
repat string
tmpl string
}
// GenericID type is for custom identifiers
type GenericID struct {
idStr string
idMap map[string]string
}
// String returns canonical string representation of the ID
func (id GenericID) String() string {
return id.idStr
}
// Map returns a map of ID values specfied by handler's regex
func (id GenericID) Map() map[string]string {
return id.idMap
}
// Parses s using supplied regexp and returns GenericID instance
func (p Handler) Parse(s string) (GenericID, error) {
res := make(map[string]string)
re, err := regexp.Compile(p.repat)
if err != nil { return GenericID{}, err }
names := re.SubexpNames()
result := re.FindStringSubmatch(s)
for k, v := range result {
if k == 0 { continue }
res[string(names[k])] = string(v)
}
inst := GenericID{
idStr: s,
idMap: res,
}
return inst, nil
}
// Composes m using supplied template and returns GenericID instance
func (p Handler) Compose(m map[string]string) (GenericID, error) {
var res string
var buf bytes.Buffer
t := template.New("t")
t, err := t.Parse(p.tmpl)
if err != nil { return GenericID{}, err }
err = t.Execute(&buf, m)
if err != nil { return GenericID{}, err }
res = buf.String()
inst := GenericID{
idStr: res,
idMap: m,
}
return inst, nil
}
// NewHandler returns a custom ID handler with provided pattern and template
func NewHandler(repat string, tmpl string) Handler {
return Handler{repat, tmpl}
}