forked from gocraft/meta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
244 lines (207 loc) · 4.22 KB
/
string.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
package meta
import (
"bytes"
"database/sql/driver"
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"unicode/utf8"
)
//
// String
//
type String struct {
Val string
Nullity
Presence
Path string
}
type StringOptions struct {
Required bool
DiscardBlank bool
Strip bool
Blank bool
Null bool
MinRunesPresent bool
MinRunes int
MaxRunesPresent bool
MaxRunes int
MaxBytesPresent bool
MaxBytes int
In []string
}
func NewString(s string) String {
return String{s, Nullity{false}, Presence{true}, ""}
}
func (s *String) ParseOptions(tag reflect.StructTag) interface{} {
opts := &StringOptions{
Required: false,
DiscardBlank: true,
Strip: true,
Blank: false,
Null: false,
MinRunesPresent: false,
MinRunes: 0,
MaxRunesPresent: false,
MaxRunes: 0,
MaxBytesPresent: false,
MaxBytes: 0,
}
// need this here to implement discard_blank
if tag.Get("meta_required") == "true" {
opts.Required = true
}
if tag.Get("meta_discard_blank") == "false" {
opts.DiscardBlank = false
}
if tag.Get("meta_strip") == "false" {
opts.Strip = false
}
if tag.Get("meta_blank") == "true" {
opts.Blank = true
}
if tag.Get("meta_null") == "true" {
opts.Null = true
}
if minRunesString := tag.Get("meta_min_runes"); minRunesString != "" {
minRunes, err := strconv.ParseInt(minRunesString, 10, 0)
if err != nil {
panic(err.Error())
}
opts.MinRunesPresent = true
opts.MinRunes = int(minRunes)
}
if maxRunesString := tag.Get("meta_max_runes"); maxRunesString != "" {
maxRunes, err := strconv.ParseInt(maxRunesString, 10, 0)
if err != nil {
panic(err.Error())
}
opts.MaxRunesPresent = true
opts.MaxRunes = int(maxRunes)
}
if maxBytesString := tag.Get("meta_max_bytes"); maxBytesString != "" {
maxBytes, err := strconv.ParseInt(maxBytesString, 10, 0)
if err != nil {
panic(err.Error())
}
opts.MaxBytesPresent = true
opts.MaxBytes = int(maxBytes)
}
if in := tag.Get("meta_in"); in != "" {
for _, s := range strings.Split(in, ",") {
opts.In = append(opts.In, strings.TrimSpace(s))
}
}
return opts
}
func (s *String) JSONValue(path string, i interface{}, options interface{}) Errorable {
s.Path = path
if i == nil {
opts := options.(*StringOptions)
if opts.Null {
s.Present = true
s.Null = true
return nil
}
return s.FormValue("", options)
}
switch value := i.(type) {
case string:
return s.FormValue(value, options)
case bool:
return s.FormValue(fmt.Sprint(i), options)
case json.Number:
return s.FormValue(fmt.Sprint(i), options)
}
return ErrString
}
func (s *String) FormValue(value string, options interface{}) Errorable {
if !utf8.ValidString(value) {
return ErrUtf8
}
opts := options.(*StringOptions)
// strip
if opts.Strip {
value = strings.TrimSpace(value)
}
runeCount := utf8.RuneCountInString(value)
if runeCount == 0 {
if opts.Blank {
s.Present = true
return nil
}
if opts.Null {
s.Present = true
s.Null = true
return nil
}
if opts.Required {
return ErrBlank
}
if !opts.DiscardBlank {
s.Present = true
return ErrBlank
}
return nil
}
// min_runes
if opts.MinRunesPresent {
if runeCount < opts.MinRunes {
return ErrMinRunes
}
}
// max_runes
if opts.MaxRunesPresent {
if runeCount > opts.MaxRunes {
return ErrMaxRunes
}
}
if opts.MaxBytesPresent {
if len(value) > opts.MaxBytes {
return ErrMaxBytes
}
}
// in
if len(opts.In) > 0 {
found := false
for _, v := range opts.In {
if v == value {
found = true
}
}
if !found {
return ErrIn
}
}
// success
s.Val = value
s.Present = true
return nil
}
func (s String) Value() (driver.Value, error) {
if s.Present && !s.Null {
return s.Val, nil
}
return nil, nil
}
func (s String) MarshalJSON() ([]byte, error) {
if s.Present && !s.Null {
return MetaJson.Marshal(s.Val)
}
return nullString, nil
}
func (s *String) UnmarshalJSON(bs []byte) error {
if bytes.Equal(nullString, bs) {
s.Nullity = Nullity{true}
return nil
}
err := MetaJson.Unmarshal(bs, &s.Val)
if err != nil {
return err
}
s.Nullity = Nullity{false}
s.Presence = Presence{true}
return nil
}