-
Notifications
You must be signed in to change notification settings - Fork 68
/
automation.go
349 lines (309 loc) · 8.55 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
338
339
340
341
342
343
344
345
346
347
348
349
package monday
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"text/scanner"
"time"
)
var (
wordsRx = regexp.MustCompile("(\\p{L}+)")
debugLayoutDef = false
)
// An InvalidTypeError indicates that data was parsed incorrectly as a result
// of a type mismatch.
type InvalidTypeError struct {
error
}
// An InvalidLengthError is returned when an item's length was longer or
// shorter than expected for a particular token.
type InvalidLengthError struct {
error
}
// NewInvalidTypeError instantiates an InvalidTypeError.
func NewInvalidTypeError() InvalidTypeError {
return InvalidTypeError{error: errors.New("invalid type for token")}
}
// NewInvalidLengthError instantiates an InvalidLengthError.
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 (lls lengthLimitSpan) scanInt(s *scanner.Scanner) (int, error) {
return -1, NewInvalidTypeError()
}
func (lls lengthLimitSpan) scanString(s *scanner.Scanner) (string, error) {
return "", NewInvalidTypeError()
}
func (lls lengthLimitSpan) isString() bool { return false }
func (lls 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 (lss limitedStringSpan) scanString(s *scanner.Scanner) (string, error) {
tok := s.Scan()
if tok != scanner.EOF && tok == -2 {
return s.TokenText(), nil
}
return "", NewInvalidTypeError()
}
func (lss limitedStringSpan) isString() bool { return true }
func (lss limitedStringSpan) String() string {
return fmt.Sprintf("[limitedStringSpan:%v]", lss.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 (rs rangeIntSpan) scanInt(s *scanner.Scanner) (int, error) {
var tok = s.Scan()
var negative bool
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
}
if debugLayoutDef {
fmt.Printf("invalid tok: %v '%s'\n", tok, s.TokenText())
}
return 0, NewInvalidTypeError()
}
func (rs rangeIntSpan) String() string {
return fmt.Sprintf("[rangeIntSpan:%v]", rs.lengthLimitSpan)
}
type delimiterSpan struct {
lengthLimitSpan
character string
}
func initDelimiterSpan(character string, minLength, maxLength int) delimiterSpan {
return delimiterSpan{
lengthLimitSpan: initLengthLimitSpan(minLength, maxLength),
character: character,
}
}
func (ds delimiterSpan) scanString(s *scanner.Scanner) (string, error) {
tok := s.Scan()
if tok != scanner.EOF && tok != -2 && tok != 45 && tok != -3 {
return s.TokenText(), nil
}
if debugLayoutDef {
fmt.Printf("expected tok:=!(-2,-3,45), received:%d ('%s')\n", tok, s.TokenText())
}
return "", NewInvalidTypeError()
}
func (ds delimiterSpan) isString() bool { return false }
func (ds delimiterSpan) isDelimiter() bool { return true }
func (ds delimiterSpan) String() string {
return fmt.Sprintf("[delimiterSpan '%s':%v]", ds.character, ds.lengthLimitSpan)
}
type layoutDef struct {
spans []layoutSpanI
errorPosition int
}
func (ld *layoutDef) validate(value string) bool {
s := &scanner.Scanner{}
s.Init(strings.NewReader(value))
s.Whitespace = 0
for _, span := range ld.spans {
if span.isString() || span.isDelimiter() {
if _, err := span.scanString(s); err != nil {
ld.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)
}
ld.errorPosition = s.Pos().Offset
return false
}
}
ld.errorPosition = s.Pos().Offset
return s.Pos().Offset == len(value)
}
// A LocaleDetector parses time.Time values by using various heuristics and
// techniques to determine which locale should be used to parse the
// time.Time value. As not all possible locales and formats are supported,
// this process can be somewhat lossy and inaccurate.
type LocaleDetector struct {
localeMap map[string]*set
lastLocale Locale
layoutsMap map[string]layoutDef
lastErrorPosition int
}
func (ld *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
// 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)
}
ret := layoutDef{spans: result}
ld.layoutsMap[layout] = ret
return ret
}
func (ld *LocaleDetector) validateValue(layout string, value string) bool {
l, ok := ld.layoutsMap[layout]
if !ok {
l = ld.prepareLayout(layout)
}
result := l.validate(value)
ld.lastErrorPosition = l.errorPosition
return result
}
func (ld *LocaleDetector) errorPosition() int { return ld.lastErrorPosition }
func (ld *LocaleDetector) addWords(words []string, v Locale) {
for _, w := range words {
l := strings.ToLower(w)
if _, ok := ld.localeMap[w]; !ok {
ld.localeMap[w] = newSet(v)
if l != w {
ld.localeMap[l] = newSet(v)
}
} else {
ld.localeMap[w].Add(v)
if l != w {
ld.localeMap[l].Add(v)
}
}
}
}
// NewLocaleDetector instances a LocaleDetector instance.
func NewLocaleDetector() *LocaleDetector {
ld := &LocaleDetector{localeMap: make(map[string]*set), lastLocale: LocaleEnGB, layoutsMap: make(map[string]layoutDef)}
for _, v := range ListLocales() {
days := GetShortDays(v)
ld.addWords(days, v)
days = GetLongDays(v)
ld.addWords(days, v)
months := GetShortMonths(v)
ld.addWords(months, v)
months = GetLongMonths(v)
ld.addWords(months, v)
}
return ld
}
// Parse will attempt to parse a time.Time struct from a layout (format) and a
// value to parse from.
//
// If no locale can be determined, this method will return an error and an
// empty time object.
func (ld *LocaleDetector) Parse(layout, value string) (time.Time, error) {
if ld.validateValue(layout, value) {
ld.lastLocale = ld.detectLocale(value)
return ParseInLocation(layout, value, time.UTC, ld.lastLocale)
}
return time.Time{}, &time.ParseError{
Value: value,
Layout: layout,
Message: fmt.Sprintf("'%s' not matches to '%s' last error position = %d\n", value, layout, ld.lastErrorPosition),
}
}
func (ld *LocaleDetector) detectLocale(value string) Locale {
localesMap := make(map[Locale]int)
for _, v := range wordsRx.FindAllStringSubmatchIndex(value, -1) {
word := strings.ToLower(value[v[0]:v[1]])
if len(word) <= 1 {
continue
}
if localesSet, ok := ld.localeMap[word]; ok {
localesSet.Each(func(loc Locale) bool {
if _, ok := localesMap[loc]; !ok {
localesMap[loc] = 1
} else {
localesMap[loc]++
}
return true
})
}
}
var result Locale = LocaleEnUS
frequency := 0
for key, counter := range localesMap {
if counter > frequency {
frequency = counter
result = key
}
}
return result
}