forked from goodsign/monday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
automation.go
337 lines (298 loc) · 7.93 KB
/
automation.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 monday
import (
"errors"
"fmt"
"gopkg.in/fatih/set.v0"
"regexp"
"strconv"
"strings"
"text/scanner"
"time"
)
var (
wordsRx = regexp.MustCompile("(\\p{L}+)")
debugLayoutDef bool = false
)
type InvalidTypeError struct {
error
}
type InvalidLengthError struct {
error
}
func NewInvalidTypeError() InvalidTypeError {
return InvalidTypeError{error: errors.New("invalid type for token")}
}
func NewInvalidLengthError() InvalidLengthError {
return InvalidLengthError{error: errors.New("invalid length for token")}
}
type layoutSpanI interface {
scanInt(s *scanner.Scanner) (int, error)
scanString(s *scanner.Scanner) (string, error)
isString() bool
isDelimiter() bool
}
type lengthLimitSpan struct {
minLength int
maxLength int
}
func (this lengthLimitSpan) scanInt(s *scanner.Scanner) (int, error) {
return -1, NewInvalidTypeError()
}
func (this lengthLimitSpan) scanString(s *scanner.Scanner) (string, error) {
return "", NewInvalidTypeError()
}
func (this lengthLimitSpan) isString() bool { return false }
func (this lengthLimitSpan) isDelimiter() bool { return false }
func initLengthLimitSpan(min, max int) lengthLimitSpan {
return lengthLimitSpan{
minLength: min,
maxLength: max,
}
}
type limitedStringSpan struct {
lengthLimitSpan
}
func initLimitedStringSpan(minLength, maxLength int) limitedStringSpan {
return limitedStringSpan{lengthLimitSpan: initLengthLimitSpan(minLength, maxLength)}
}
func (this limitedStringSpan) scanString(s *scanner.Scanner) (string, error) {
tok := s.Scan()
if tok != scanner.EOF && tok == -2 {
return s.TokenText(), nil
}
return "", NewInvalidTypeError()
}
func (this limitedStringSpan) isString() bool { return true }
func (this limitedStringSpan) String() string {
return fmt.Sprintf("[limitedStringSpan:%v]", this.lengthLimitSpan)
}
type rangeIntSpan struct {
lengthLimitSpan
min int
max int
}
func initRangeIntSpan(minValue, maxValue, minLength, maxLength int) rangeIntSpan {
return rangeIntSpan{
lengthLimitSpan: initLengthLimitSpan(minLength, maxLength),
min: minValue,
max: maxValue,
}
}
func (this rangeIntSpan) scanInt(s *scanner.Scanner) (int, error) {
var tok = s.Scan()
var negative bool = false
if tok == 45 {
negative = true
if debugLayoutDef {
fmt.Printf("scan negative:'%s'\n", s.TokenText())
}
tok = s.Scan()
} else if tok == 43 { // positive
tok = s.Scan()
}
if tok == -3 {
str := s.TokenText()
i, err := strconv.Atoi(str)
if err != nil {
return 0, err
}
if negative {
i = i * -1
}
return i, nil
} else {
if debugLayoutDef {
fmt.Printf("invalid tok: %s '%s'\n", tok, s.TokenText())
}
}
return 0, NewInvalidTypeError()
}
func (this rangeIntSpan) String() string {
return fmt.Sprintf("[rangeIntSpan:%v]", this.lengthLimitSpan)
}
type delimiterSpan struct {
lengthLimitSpan
character string
}
func initDelimiterSpan(character string, minLength, maxLength int) delimiterSpan {
return delimiterSpan{
lengthLimitSpan: initLengthLimitSpan(minLength, maxLength),
character: character,
}
}
func (this delimiterSpan) scanString(s *scanner.Scanner) (string, error) {
tok := s.Scan()
if tok != scanner.EOF && tok != -2 && tok != 45 && tok != -3 {
return s.TokenText(), nil
} else {
if debugLayoutDef {
fmt.Printf("expected tok:=!(-2,-3,45), received:%d ('%s')\n", tok, s.TokenText())
}
}
return "", NewInvalidTypeError()
}
func (this delimiterSpan) isString() bool { return false }
func (this delimiterSpan) isDelimiter() bool { return true }
func (this delimiterSpan) String() string {
return fmt.Sprintf("[delimiterSpan '%s':%v]", this.character, this.lengthLimitSpan)
}
type layoutDef struct {
spans []layoutSpanI
errorPosition int
}
func (this *layoutDef) validate(value string) bool {
s := &scanner.Scanner{}
s.Init(strings.NewReader(value))
s.Whitespace = 0
for _, span := range this.spans {
if span.isString() || span.isDelimiter() {
if _, err := span.scanString(s); err != nil {
this.errorPosition = s.Pos().Offset
if debugLayoutDef {
fmt.Printf("error at pos: %d: %s (span=%+v) - expected string or delimiter\n", s.Pos().Offset, err.Error(), span)
}
return false
}
} else if _, err := span.scanInt(s); err != nil {
if debugLayoutDef {
fmt.Printf("error at pos: %d: %s (span=%+v) - expected integer\n", s.Pos().Offset, err.Error(), span)
}
this.errorPosition = s.Pos().Offset
return false
}
}
this.errorPosition = s.Pos().Offset
return s.Pos().Offset == len(value)
}
type LocaleDetector struct {
localeMap map[string]*set.Set
lastLocale Locale
layoutsMap map[string]layoutDef
lastErrorPosition int
}
func (this *LocaleDetector) prepareLayout(layout string) layoutDef {
s := scanner.Scanner{}
s.Init(strings.NewReader(layout))
s.Whitespace = 0
result := make([]layoutSpanI, 0)
var tok rune
// var pos int = 0
var span layoutSpanI
var sign bool = false
// var neg bool = false
for tok != scanner.EOF {
tok = s.Scan()
switch tok {
case -2: // text
span = initLimitedStringSpan(1, -1)
case -3: // digit
span = initRangeIntSpan(-1, -1, 1, -1)
if sign {
sign = false
}
case 45: // negative sign
sign = true
// neg = s.TokenText() == "-"
continue
case 43: // positive sign
sign = true
continue
case scanner.EOF:
continue
default: // fixed character
span = initDelimiterSpan(s.TokenText(), 1, 1)
}
result = append(result, span)
// length := s.Pos().Offset - pos
// pos = s.Pos().Offset
// fmt.Printf("tok'%s' [%d %d] length=%d\n", s.TokenText(), pos, s.Pos().Offset, length)
}
if debugLayoutDef {
fmt.Printf("layout:'%s'\n", layout)
fmt.Printf("layout:%v\n", result)
}
ld := layoutDef{spans: result}
this.layoutsMap[layout] = ld
return ld
}
func (this *LocaleDetector) validateValue(layout string, value string) bool {
l, ok := this.layoutsMap[layout]
if !ok {
l = this.prepareLayout(layout)
}
result := l.validate(value)
this.lastErrorPosition = l.errorPosition
return result
}
func (this *LocaleDetector) errorPosition() int { return this.lastErrorPosition }
func (this *LocaleDetector) addWords(words []string, v Locale) {
for _, w := range words {
l := strings.ToLower(w)
if _, ok := this.localeMap[w]; !ok {
this.localeMap[w] = set.New(v)
if l != w {
this.localeMap[l] = set.New(v)
}
} else {
this.localeMap[w].Add(v)
if l != w {
this.localeMap[l].Add(v)
}
}
}
}
func NewLocaleDetector() *LocaleDetector {
this := &LocaleDetector{localeMap: make(map[string]*set.Set), lastLocale: LocaleEnGB, layoutsMap: make(map[string]layoutDef)}
for _, v := range ListLocales() {
days := GetShortDays(v)
this.addWords(days, v)
days = GetLongDays(v)
this.addWords(days, v)
months := GetShortMonths(v)
this.addWords(months, v)
months = GetLongMonths(v)
this.addWords(months, v)
}
return this
}
/**
**/
func (this *LocaleDetector) Parse(layout, value string) (time.Time, error) {
if this.validateValue(layout, value) {
this.lastLocale = this.detectLocale(value)
return ParseInLocation(layout, value, time.UTC, this.lastLocale)
} else {
return time.Time{}, &time.ParseError{
Value: value,
Layout: layout,
Message: fmt.Sprintf("'%s' not matches to '%s' last error position = %d\n", value, layout, this.lastErrorPosition),
}
}
}
func (this *LocaleDetector) detectLocale(value string) Locale {
var localesMap map[Locale]int = make(map[Locale]int)
for _, v := range wordsRx.FindAllStringSubmatchIndex(value, -1) {
word := strings.ToLower(value[v[0]:v[1]])
if localesSet, ok := this.localeMap[word]; ok {
localesSet.Each(func(i interface{}) bool {
if loc, ok := i.(Locale); ok {
if _, ok := localesMap[loc]; !ok {
localesMap[loc] = 1
} else {
localesMap[loc]++
}
}
return true
})
}
}
var result Locale = LocaleEnUS
var frequency int = 0
for key, counter := range localesMap {
if counter > frequency {
frequency = counter
result = key
}
}
return result
}