-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
121 lines (112 loc) · 3.04 KB
/
utils.go
File metadata and controls
121 lines (112 loc) · 3.04 KB
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
package zed
import (
"errors"
"github.com/vmihailenco/tagparser/v2"
"reflect"
"strconv"
"strings"
)
type pair[TF, TS any] struct {
first TF
second TS
}
func makePair[TF, TS any](first TF, second TS) pair[TF, TS] {
return pair[TF, TS]{
first: first,
second: second,
}
}
func parseStructFieldName(f reflect.StructField) (string, bool) {
name := f.Name
t := f.Tag.Get("zed")
tagName := t[:strings.Index(t, ",")]
if tagName == "-" {
return "", false
}
if tagName != "" {
name = tagName
}
return name, true
}
func isUuidType(t reflect.Type) bool {
return t.PkgPath() == "github.com/google/uuid" && t.Name() == "UUID"
}
func isTimeType(t reflect.Type) bool {
return t.PkgPath() == "time" && t.Name() == "Time"
}
func parseNumFieldOpts[T integer | float](f *NumSchema[T], t *tagparser.Tag) (*NumSchema[T], error) {
if t.HasOption("min") {
if !t.HasOption("min_err") {
return f, errors.New("`min` option is set but `min_err` option is missing")
}
v, e := strconv.ParseInt(t.Options["min"], 10, 64)
if e != nil {
return f, errors.New("invalid value for `min` option")
}
f.Min(v, t.HasOption("exclusive_min"), t.Options["min_err"])
}
if t.HasOption("max") {
if !t.HasOption("max_err") {
return f, errors.New("`max` option is set but `max_err` option is missing")
}
v, e := strconv.ParseInt(t.Options["max"], 10, 64)
if e != nil {
return f, errors.New("invalid value for `max` option")
}
f.Max(v, t.HasOption("exclusive_max"), t.Options["max_err"])
}
return f, nil
}
func parseStringFieldOpts(f *StringSchema, t *tagparser.Tag) (*StringSchema, error) {
if t.HasOption("min_len") {
if !t.HasOption("min_len_err") {
return f, errors.New("`min_len` option is set but `min_len_err` option is missing")
}
v, e := strconv.ParseUint(t.Options["min_len"], 10, 64)
if e != nil {
return f, errors.New("invalid value for `min_len` option")
}
f.MinLen(v, t.Options["min_len_err"])
}
if t.HasOption("max_len") {
if !t.HasOption("max_len_err") {
return f, errors.New("`max_len` option is set but `max_len_err` option is missing")
}
v, e := strconv.ParseUint(t.Options["max_len"], 10, 64)
if e != nil {
return f, errors.New("invalid value for `max_len` option")
}
f.MaxLen(v, t.Options["max_len_err"])
}
if t.HasOption("pattern") {
if !t.HasOption("pattern_err") {
return f, errors.New("`pattern` option is set but `pattern_err` option is missing")
}
f.Pattern(t.Options["pattern"], t.Options["pattern_err"])
}
return f, nil
}
func parseTimeFieldOpts(f *DateTimeSchema, t *tagparser.Tag) (*DateTimeSchema, error) {
if t.HasOption("epoch_unit") {
switch t.Options["epoch_unit"] {
case "nanosecond":
case "ns":
f.EpochUnit(EpochNanosecond)
case "microsecond":
case "us":
f.EpochUnit(EpochMicrosecond)
case "millisecond":
case "ms":
f.EpochUnit(EpochMillisecond)
case "second":
case "s":
f.EpochUnit(EpochSecond)
default:
return f, errors.New("invalid value for `epoch_unit` option")
}
}
if t.HasOption("layout") {
f.Layout(t.Options["layout"])
}
return f, nil
}