-
Notifications
You must be signed in to change notification settings - Fork 0
/
placeholder.go
230 lines (203 loc) · 4.25 KB
/
placeholder.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
package nvault
import (
"fmt"
"regexp"
"github.com/mitchellh/mapstructure"
)
// PathFragment ...
type PathFragment struct {
Type string
Fragment string
}
func (pf PathFragment) String() string {
return fmt.Sprintf("%s: %s", pf.Fragment, pf.Type)
}
// Path ...
type Path []PathFragment
// Equal ...
func (p Path) Equal(other Path) bool {
for i, left := range p {
if left != other[i] {
return false
}
}
return true
}
// AddRoot ...
func (p Path) AddRoot(root PathFragment) Path {
return append(Path{root}, p...)
}
func (p Path) String() (s string) {
for _, f := range p {
s = fmt.Sprintf("%s/%s", s, f.Fragment)
}
return
}
// Match ...
func (p Path) Match(search Path) (result bool) {
if search[0].Fragment != "$" {
return
}
for i, fragment := range search[1:] {
if len(p) <= i {
break
}
if fragment.Fragment == "*" {
result = true
} else {
switch fragment.Type {
case "string":
result = p[i].Fragment == fragment.Fragment
case "regexp":
result, _ = regexp.MatchString(p[i].Fragment, fragment.Fragment)
case "number":
result = fragment.Fragment == p[i].Fragment
}
}
if !result {
break
}
}
return
}
// Placeholder ...
type Placeholder map[string]interface{}
// Matches ...
func (p Placeholder) Matches(search []Path) (results []Path) {
for _, path := range p.Paths() {
for _, s := range search {
if path.Match(s) {
results = append(results, path)
}
}
}
return
}
// Paths ...
func (p Placeholder) Paths() (paths []Path) {
for f, v := range p {
root := PathFragment{"string", f}
switch vi := v.(type) {
case map[string]interface{}:
v := Placeholder(vi)
for _, path := range v.Paths() {
path := path.AddRoot(root)
paths = append(paths, path)
}
case []map[string]interface{}:
v := Placeholders(vi)
for _, path := range v.Paths() {
path := path.AddRoot(root)
paths = append(paths, path)
}
default:
paths = append(paths, Path{root})
}
}
return paths
}
// Set ...
func (p Placeholder) Set(path Path, value interface{}) error {
if len(path) == 0 {
return nil
}
switch pi := p[path[0].Fragment].(type) {
case map[string]interface{}:
p := Placeholder(pi)
return p.Set(Path(path[1:]), value)
case []map[string]interface{}:
p := Placeholders(pi)
return p.Set(Path(path[1:]), value)
default:
p[path[0].Fragment] = value
}
return nil
}
// Get ...
func (p Placeholder) Get(path Path) (interface{}, error) {
if len(path) == 0 {
return p, nil
}
switch pi := p[path[0].Fragment].(type) {
case map[string]interface{}:
p := Placeholder(pi)
return p.Get(Path(path[1:]))
case []map[string]interface{}:
p := Placeholders(pi)
return p.Get(Path(path[1:]))
default:
return pi, nil
}
}
// Placeholders ...
type Placeholders []map[string]interface{}
// Matches ...
func (ps Placeholders) Matches(search []Path) (results []Path) {
if len(search) == 0 {
return ps.Paths()
}
for _, path := range ps.Paths() {
for _, s := range search {
if path.Match(s) {
results = append(results, path)
}
}
}
return
}
// Paths ...
func (ps Placeholders) Paths() (paths []Path) {
for i, p := range ps {
root := PathFragment{"string", fmt.Sprintf("%d", i)}
p := Placeholder(p)
for _, path := range p.Paths() {
path := path.AddRoot(root)
paths = append(paths, path)
}
}
return paths
}
// Set ...
func (ps Placeholders) Set(path Path, value interface{}) error {
if len(path) == 0 {
return nil
}
for i, p := range ps {
if path[0].Fragment != fmt.Sprintf("%d", i) {
continue
}
p := Placeholder(p)
return p.Set(Path(path[1:]), value)
}
return nil
}
// Get ...
func (ps Placeholders) Get(path Path) (interface{}, error) {
if len(path) == 0 {
return ps, nil
}
for i, p := range ps {
if path[0].Fragment != fmt.Sprintf("%d", i) {
continue
}
p := Placeholder(p)
return p.Get(Path(path[1:]))
}
return nil, nil
}
// Encrypt ...
func (p *Placeholder) Encrypt(s interface{}, config *Config, paths ...Path) error {
err := Encrypt(p, config, paths...)
if err != nil {
return err
}
return mapstructure.Decode(p, s)
}
// Decrypt ...
func (p *Placeholder) Decrypt(s interface{}, config *Config, paths ...Path) error {
err := Decrypt(p, config, paths...)
if err != nil {
return err
}
return mapstructure.Decode(p, s)
}