-
Notifications
You must be signed in to change notification settings - Fork 2
/
examineyaml.go
337 lines (292 loc) · 6.84 KB
/
examineyaml.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package main
import (
"fmt"
"strings"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
type yamlExaminatorImpl struct {
filter string
publishable bool
}
func newYamlExaminatorImpl(filter string, publishable bool) *yamlExaminatorImpl {
return &yamlExaminatorImpl{
filter: filter,
publishable: publishable,
}
}
func (examinator *yamlExaminatorImpl) ExamineYaml(yml []byte) (beProcessed bool, err error) {
fm := make(map[interface{}]interface{})
if err := yaml.Unmarshal(yml, fm); err != nil {
return false, errors.Wrap(err, "failed to unmarshal front matter")
}
if examinator.publishable {
if ok, err := checkPublishable(fm); err != nil {
return false, errors.Wrap(err, "failed to check publish or draft field in front matter")
} else if !ok {
return false, nil
}
}
if examinator.filter != "" {
if ok, err := checkFilter(fm, examinator.filter); err != nil {
return false, errors.Wrap(err, "failed to check filter field in front matter")
} else if !ok {
return false, nil
}
}
return true, nil
}
func checkPublishable(fm map[interface{}]interface{}) (ok bool, err error) {
if vdraft, ok := fm["draft"]; ok {
if isdraft, ok := vdraft.(bool); ok && !isdraft {
return true, nil
}
} else if vpublishable, ok := fm["publish"]; ok {
if ispublishable, ok := vpublishable.(bool); ok && ispublishable {
return true, nil
}
}
return false, nil
}
func checkFilter(fm map[interface{}]interface{}, filter string) (value bool, err error) {
token, err := tokenizeFilter(filter)
if err != nil {
return false, err
}
nd, _, err := parseTokens(token)
if err != nil {
return false, newMainErr(MAIN_ERR_KIND_INVALID_FILTER_FORMAT)
}
return evaluateNode(nd, fm)
}
func evaluateNode(nd *nodeImpl, fm map[interface{}]interface{}) (value bool, err error) {
if nd.kind == NODE_IDENT {
name := nd.name
if v, ok := fm[name]; ok {
if value, ok := v.(bool); ok {
return value, nil
}
} else {
return false, nil
}
return false, newMainErr(MAIN_ERR_KIND_INVALID_FILTER_FORMAT)
} else if nd.kind == NODE_TRUE {
return true, nil
} else if nd.kind == NODE_NOT {
if v, err := evaluateNode(nd.left, fm); err != nil {
return false, err
} else {
return !v, nil
}
}
left, err := evaluateNode(nd.left, fm)
if err != nil {
return false, err
}
right, err := evaluateNode(nd.right, fm)
if err != nil {
return false, err
}
if nd.kind == NODE_AND {
return left && right, nil
} else {
return left || right, nil
}
}
type tokenKind = uint
const (
TOKEN_IDENT = iota + 1
TOKEN_AND
TOKEN_OR
TOKEN_NOT
TOKEN_RESERVED
TOKEN_EOS
)
type tokenImpl struct {
kind tokenKind
name string
next *tokenImpl
}
type nodeKind uint
const (
NODE_IDENT nodeKind = iota + 1
NODE_AND
NODE_OR
NODE_NOT
NODE_TRUE
)
type nodeImpl struct {
kind nodeKind
name string
left *nodeImpl
right *nodeImpl
}
func parseTokens(cur *tokenImpl) (nd *nodeImpl, next *tokenImpl, err error) {
left, cur, err := andNode(cur)
if err != nil {
return nil, nil, err
}
if cur != nil && cur.kind == TOKEN_OR {
right, next, err := parseTokens(cur.next)
if err != nil {
return nil, nil, err
}
return newParentNode(NODE_OR, left, right), next, nil
}
return left, cur, nil
}
func andNode(cur *tokenImpl) (nd *nodeImpl, next *tokenImpl, err error) {
left, cur, err := unaryNode(cur)
if err != nil {
return nil, nil, err
}
if cur != nil && cur.kind == TOKEN_AND {
right, next, err := andNode(cur.next)
if err != nil {
return nil, nil, err
}
return newParentNode(NODE_AND, left, right), next, nil
}
return left, cur, err
}
func unaryNode(cur *tokenImpl) (nd *nodeImpl, next *tokenImpl, err error) {
if cur == nil {
return nil, nil, newMainErr(MAIN_ERR_KIND_INVALID_FILTER_FORMAT)
}
if cur.kind == TOKEN_NOT {
left, next, err := unaryNode(cur.next)
if err != nil {
return nil, nil, err
}
nd = newParentNode(NODE_NOT, left, nil)
return nd, next, nil
}
return primaryNode(cur)
}
func primaryNode(cur *tokenImpl) (nd *nodeImpl, next *tokenImpl, err error) {
if cur == nil {
return nil, nil, newMainErr(MAIN_ERR_KIND_INVALID_FILTER_FORMAT)
}
if cur.kind == TOKEN_IDENT {
next = cur.next
return newIdentNode(cur.name), next, nil
}
if cur.kind == TOKEN_RESERVED && cur.name == "(" {
nd, cur, err = parseTokens(cur.next)
if err != nil {
return nil, nil, err
}
if cur.kind == TOKEN_RESERVED && cur.name == ")" {
next = cur.next
return nd, next, nil
}
}
if cur.kind == TOKEN_EOS {
return newParentNode(NODE_TRUE, nil, nil), nil, nil
}
return nil, nil, newMainErr(MAIN_ERR_KIND_INVALID_FILTER_FORMAT)
}
func newParentNode(kind nodeKind, left *nodeImpl, right *nodeImpl) *nodeImpl {
return &nodeImpl{
kind: kind,
left: left,
right: right,
}
}
func newIdentNode(name string) *nodeImpl {
return &nodeImpl{
kind: NODE_IDENT,
name: name,
}
}
func newTokenImpl(kind tokenKind, name string, pre *tokenImpl) *tokenImpl {
child := &tokenImpl{
kind: kind,
name: name,
}
pre.next = child
return child
}
func tokenizeFilter(input string) (token *tokenImpl, err error) {
head := new(tokenImpl)
runes := []rune(input)
cur := head
p := 0
for i := 0; i < len(runes); i++ {
if p >= len(runes) {
break
}
if p < len(runes)-1 {
if string(runes[p:p+2]) == "&&" {
cur = newTokenImpl(TOKEN_AND, "", cur)
p += 2
continue
}
if string(runes[p:p+2]) == "||" {
cur = newTokenImpl(TOKEN_OR, "", cur)
p += 2
continue
}
}
if string(runes[p:p+1]) == "!" {
cur = newTokenImpl(TOKEN_NOT, "", cur)
p++
continue
}
if runes[p] == '(' || runes[p] == ')' {
cur = newTokenImpl(TOKEN_RESERVED, string(runes[p:p+1]), cur)
p++
continue
}
if length := consumeIdent(runes[p:]); length > 0 {
cur = newTokenImpl(TOKEN_IDENT, string(runes[p:p+length]), cur)
p += length
continue
}
return nil, newTokinizeErr(string(runes), p)
}
cur = newTokenImpl(TOKEN_EOS, "", cur)
return head.next, nil
}
func consumeIdent(runes []rune) (length int) {
for i := 0; i < len(runes); i++ {
char := runes[i]
if 'a' <= char && char <= 'z' {
continue
}
if 'A' <= char && char <= 'Z' {
continue
}
if '0' <= char && char <= '9' {
continue
}
if char == '-' || char == '_' {
continue
}
return i
}
return len(runes)
}
type FilterErrorKind uint
const (
FILTER_ERROR_TOKENIZE FilterErrorKind = iota + 1
FILTER_ERROR_PARSE
FILTER_ERROR_EVALUATE
)
type FilterErr struct {
kind FilterErrorKind
message string
}
func newFilterErrf(kind FilterErrorKind, format string, a ...interface{}) *FilterErr {
err := new(FilterErr)
err.kind = kind
err.message = fmt.Sprintf(format, a...)
return err
}
func (err *FilterErr) Error() string {
return err.message
}
func newTokinizeErr(filter string, pos int) *FilterErr {
errHere := strings.Repeat(" ", pos) + "↑"
return newFilterErrf(FILTER_ERROR_TOKENIZE, "-filter=%s\n %s", filter, errHere)
}