-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
134 lines (119 loc) · 3.78 KB
/
parser.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
package retable
import (
"fmt"
"strconv"
"strings"
"time"
)
type Parser interface {
ParseInt(string) (int64, error)
ParseUnt(string) (uint64, error)
ParseFloat(string) (float64, error)
ParseBool(string) (bool, error)
ParseTime(string) (time.Time, error)
ParseDuration(string) (time.Duration, error)
}
var _ Parser = new(StringParser)
type StringParser struct {
TrueStrings []string `json:"trueStrings"`
FalseStrings []string `json:"falseStrings"`
NilStrings []string `json:"nilStrings"`
TimeFormats []string `json:"timeFormats"`
}
func NewStringParser() *StringParser {
c := &StringParser{
TrueStrings: []string{"true", "True", "TRUE", "yes", "Yes", "YES", "1"},
FalseStrings: []string{"false", "False", "FALSE", "no", "No", "NO", "0"},
NilStrings: []string{"", "nil", "<nil>", "null", "NULL"},
TimeFormats: timeFormats,
}
return c
}
func (p *StringParser) ParseInt(str string) (int64, error) {
return strconv.ParseInt(str, 10, 64)
}
func (p *StringParser) ParseUnt(str string) (uint64, error) {
return strconv.ParseUint(str, 10, 64)
}
func (p *StringParser) ParseFloat(str string) (float64, error) {
f, err := strconv.ParseFloat(str, 64)
if err != nil {
numDot := strings.Count(str, ".")
numComma := strings.Count(str, ",")
switch {
case numComma == 1 && numDot == 0:
f, e := strconv.ParseFloat(strings.ReplaceAll(str, ",", "."), 64)
if e != nil {
return 0, err // return original error
}
return f, nil
// TODO: add more cases
}
return 0, err
}
return f, nil
}
func (p *StringParser) ParseBool(str string) (bool, error) {
for _, val := range p.TrueStrings {
if str == val {
return true, nil
}
}
for _, val := range p.FalseStrings {
if str == val {
return false, nil
}
}
return false, fmt.Errorf("cannot parse %q as bool", str)
}
func (p *StringParser) ParseTime(str string) (time.Time, error) {
for _, format := range p.TimeFormats {
t, err := time.Parse(format, str)
if err == nil {
return t, nil
}
}
return time.Time{}, fmt.Errorf("cannot parse %q as time", str)
}
func (p *StringParser) ParseDuration(str string) (time.Duration, error) {
return time.ParseDuration(str)
}
func ParseTime(str string) (t time.Time, format string, err error) {
for _, format := range timeFormats {
t, err = time.Parse(format, str)
if err == nil {
return t, format, nil
}
}
return time.Time{}, "", fmt.Errorf("cannot parse %q as time", str)
}
var timeFormats = []string{
time.RFC3339Nano, // "2006-01-02T15:04:05.999999999Z07:00"
time.RFC3339, // "2006-01-02T15:04:05Z07:00"
formatBrowserLocalTime, // "2006-01-02T15:04"
time.RFC1123Z, // "Mon, 02 Jan 2006 15:04:05 -0700"
time.RFC850, // "Monday, 02-Jan-06 15:04:05 MST"
time.RFC1123, // "Mon, 02 Jan 2006 15:04:05 MST"
time.RubyDate, // "Mon Jan 02 15:04:05 -0700 2006"
time.UnixDate, // "Mon Jan _2 15:04:05 MST 2006"
time.ANSIC, // "Mon Jan _2 15:04:05 2006"
time.RFC822Z, // "02 Jan 06 15:04 -0700"
time.RFC822, // "02 Jan 06 15:04 MST"
time.StampNano, // "Jan _2 15:04:05.000000000"
time.StampMicro, // "Jan _2 15:04:05.000000"
time.StampMilli, // "Jan _2 15:04:05.000"
time.Stamp, // "Jan _2 15:04:05"
formatTimeString, // "2006-01-02 15:04:05.999999999 -0700 MST"
time.DateTime, // "2006-01-02 15:04:05"
formatDateTimeMinute, // "2006-01-02 15:04"
time.DateOnly, // "2006-01-02"
formatDateTimeGerman, // "02.01.2006 15:04:05"
formatDateGerman, // "02.01.2006"
}
const (
formatDateTimeMinute = "2006-01-02 15:04"
formatDateTimeGerman = "02.01.2006 15:04:05"
formatDateGerman = "02.01.2006"
formatTimeString = "2006-01-02 15:04:05.999999999 -0700 MST"
formatBrowserLocalTime = "2006-01-02T15:04"
)