-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
153 lines (121 loc) · 2.93 KB
/
parse.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
package path
import (
"errors"
"reflect"
"strconv"
"strings"
"time"
"cloud.google.com/go/civil"
"github.com/gopatchy/jsrest"
)
type timeVal struct {
time time.Time
precision time.Duration
}
var (
ErrUnsupportedType = errors.New("unsupported type")
ErrUnknownTimeFormat = errors.New("unknown time format")
)
func parse(str string, t any) (any, error) {
typ := reflect.TypeOf(t)
if typ.Kind() == reflect.Slice {
typ = typ.Elem()
}
if typ.Kind() == reflect.Pointer {
typ = typ.Elem()
}
// TODO: Consider attempting to convert to string in default case
switch typ.Kind() { //nolint:exhaustive
case reflect.Int:
return parseInt(str)
case reflect.Int64:
return strconv.ParseInt(str, 10, 64)
case reflect.Uint:
return parseUint(str)
case reflect.Uint64:
return strconv.ParseUint(str, 10, 64)
case reflect.Float32:
return parseFloat32(str)
case reflect.Float64:
return strconv.ParseFloat(str, 64)
case reflect.String:
return str, nil
case reflect.Bool:
return strconv.ParseBool(str)
case reflect.Struct:
switch typ {
case reflect.TypeOf(time.Time{}):
return parseTime(str)
case reflect.TypeOf(civil.Date{}):
return civil.ParseDate(str)
}
}
return nil, jsrest.Errorf(jsrest.ErrBadRequest, "%T (%w)", t, ErrUnsupportedType)
}
func parseInt(str string) (int, error) {
val, err := strconv.ParseInt(str, 10, strconv.IntSize)
return int(val), err
}
func parseUint(str string) (uint, error) {
val, err := strconv.ParseUint(str, 10, strconv.IntSize)
return uint(val), err
}
func parseFloat32(str string) (float32, error) {
val, err := strconv.ParseFloat(str, 32)
return float32(val), err
}
type timeFormat struct {
format string
precision time.Duration
}
var timeFormats = []timeFormat{
{
format: "2006-01-02-07:00",
precision: 24 * time.Hour,
// TODO: Support field annotation to change start vs end of day
// TODO: Support timezone context passed down to allow naked date
},
{
format: "2006-01-02T15:04:05Z",
precision: 1 * time.Second,
},
{
format: "2006-01-02T15:04:05-07:00",
precision: 1 * time.Second,
},
}
func parseTime(str string) (*timeVal, error) {
if strings.ToLower(str) == "now" {
return &timeVal{
time: time.Now(),
precision: 1 * time.Nanosecond,
}, nil
}
for _, format := range timeFormats {
tm, err := time.Parse(format.format, str)
if err != nil {
continue
}
return &timeVal{
time: tm,
precision: format.precision,
}, nil
}
i, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return nil, jsrest.Errorf(jsrest.ErrBadRequest, "%s (%w)", str, ErrUnknownTimeFormat)
}
// UNIX Seconds: 2969-05-03
// UNIX Millis: 1971-01-01
// Intended to give us a wide range of useful values in both schemes
if i > 31536000000 {
return &timeVal{
time: time.UnixMilli(i),
precision: 1 * time.Millisecond,
}, nil
}
return &timeVal{
time: time.Unix(i, 0),
precision: 1 * time.Second,
}, nil
}