-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
translate.go
270 lines (222 loc) · 5.89 KB
/
translate.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
package models
import (
"database/sql"
"fmt"
"reflect"
"regexp"
"time"
"github.com/gofrs/uuid"
)
type ErrEditPrerequisiteFailed struct {
field string
expected interface{}
actual interface{}
}
func (e *ErrEditPrerequisiteFailed) Error() string {
expected := "_blank_"
if e.expected != "" {
expected = fmt.Sprintf("“**%v**”", e.expected)
}
actual := "_blank_"
if e.actual != "" {
actual = fmt.Sprintf("“**%v**”", e.actual)
}
return fmt.Sprintf("Expected %s to be %s, but was %s.", e.field, expected, actual)
}
// fromEdit translates edit object fields into entity fields
type fromEdit struct {
}
func (c *fromEdit) string(out *string, in *string) {
if in != nil {
*out = *in
}
}
func (c *fromEdit) nullString(out *sql.NullString, in *string, old *string) {
if in != nil {
out.String = *in
out.Valid = true
} else if old != nil {
*out = sql.NullString{}
}
}
func (c *fromEdit) nullInt64(out *sql.NullInt64, in *int64, old *int64) {
if in != nil {
out.Int64 = *in
out.Valid = true
} else if old != nil {
*out = sql.NullInt64{}
}
}
func (c *fromEdit) sqlDate(out *SQLDate, in *string, old *string) {
if in != nil {
out.String = *in
out.Valid = true
} else if old != nil {
*out = SQLDate{}
}
}
func (c *fromEdit) nullUUID(out *uuid.NullUUID, in *uuid.UUID, old *uuid.UUID) {
if in != nil {
out.UUID = *in
out.Valid = true
} else if old != nil {
*out = uuid.NullUUID{}
}
}
// editDiff translates edit details input fields into edit data
type editDiff struct {
}
type stringEnum interface {
IsValid() bool
String() string
}
func (d *editDiff) string(old *string, new *string) (oldOut *string, newOut *string) {
if old != nil && (new == nil || *new != *old) {
oldVal := *old
oldOut = &oldVal
}
if new != nil && (old == nil || *new != *old) {
newVal := *new
newOut = &newVal
}
return
}
func (d *editDiff) nullString(old sql.NullString, new *string) (oldOut *string, newOut *string) {
if old.Valid && (new == nil || *new != old.String) {
oldVal := old.String
oldOut = &oldVal
}
if new != nil && *new != "" && (!old.Valid || *new != old.String) {
newVal := *new
newOut = &newVal
}
return
}
func (d *editDiff) nullInt64(old sql.NullInt64, new *int) (oldOut *int64, newOut *int64) {
if old.Valid && (new == nil || int64(*new) != old.Int64) {
oldVal := old.Int64
oldOut = &oldVal
}
if new != nil && (!old.Valid || int64(*new) != old.Int64) {
newVal := int64(*new)
newOut = &newVal
}
return
}
func (d *editDiff) nullUUID(old uuid.NullUUID, new *uuid.UUID) (oldOut *uuid.UUID, newOut *uuid.UUID) {
if old.Valid && (new == nil || *new != old.UUID) {
oldOut = &old.UUID
}
if new != nil && (!old.Valid || *new != old.UUID) {
newOut = new
}
return
}
func (d *editDiff) nullStringEnum(old sql.NullString, new stringEnum) (oldOut *string, newOut *string) {
newNil := reflect.ValueOf(new).IsNil()
if old.Valid && (newNil || !new.IsValid() || new.String() != old.String) {
oldVal := old.String
oldOut = &oldVal
}
if !newNil && new.IsValid() && (!old.Valid || new.String() != old.String) {
newVal := new.String()
newOut = &newVal
}
return
}
func (d *editDiff) fuzzyDate(oldDate SQLDate, oldAcc sql.NullString, new *string) (outOldDate, outOldAcc, outNewDate, outNewAcc *string) {
if new == nil && oldDate.Valid {
outOldDate = &oldDate.String
if oldAcc.Valid {
outOldAcc = &oldAcc.String
}
} else if new != nil {
newDate, newAccuracy, _ := ParseFuzzyString(new)
if !oldDate.Valid || newDate.String != oldDate.String || newAccuracy.String != oldAcc.String {
outNewDate = &newDate.String
newAccuracy := newAccuracy.String
outNewAcc = &newAccuracy
if oldDate.Valid {
outOldDate = &oldDate.String
}
if oldAcc.Valid {
outOldAcc = &oldAcc.String
}
}
}
return
}
//nolint:unused
func (d *editDiff) sqlDate(old SQLDate, new *string) (oldOut *string, newOut *string) {
if old.Valid && (new == nil || *new != old.String) {
oldVal := old.String
oldOut = &oldVal
}
if new != nil && (!old.Valid || *new != old.String) {
newVal := *new
newOut = &newVal
}
return
}
type editValidator struct {
err error
}
func (v *editValidator) error(field string, expected interface{}, actual interface{}) error {
return &ErrEditPrerequisiteFailed{field, expected, actual}
}
func (v *editValidator) string(field string, old *string, current string) {
if v.err != nil {
return
}
if old != nil && *old != current {
v.err = v.error(field, *old, current)
}
}
func (v *editValidator) int64(field string, old *int64, current int64) {
if v.err != nil {
return
}
if old != nil && *old != current {
v.err = v.error(field, *old, current)
}
}
func (v *editValidator) uuid(field string, old *uuid.UUID, current uuid.NullUUID) {
if v.err != nil {
return
}
if old != nil && (!current.Valid || (*old != current.UUID)) {
currentUUID := ""
if current.Valid {
currentUUID = current.UUID.String()
}
v.err = v.error(field, old.String(), currentUUID)
}
}
var ErrInvalidDate = fmt.Errorf("invalid fuzzy date")
var dateValidator = regexp.MustCompile(`^\d{4}(-\d{2}){0,2}$`)
func ParseFuzzyString(date *string) (SQLDate, sql.NullString, error) {
if date == nil {
return SQLDate{Valid: false}, sql.NullString{Valid: false}, nil
}
if !dateValidator.MatchString(*date) {
return SQLDate{Valid: false}, sql.NullString{Valid: false}, ErrInvalidDate
}
accuracy := DateAccuracyEnumDay
fuzzyDate := *date
if len(fuzzyDate) == 4 {
accuracy = DateAccuracyEnumYear
fuzzyDate += "-01-01"
} else if len(fuzzyDate) == 7 {
accuracy = DateAccuracyEnumMonth
fuzzyDate += "-01"
}
_, err := time.Parse("2006-01-02", fuzzyDate)
if err != nil {
return SQLDate{Valid: false}, sql.NullString{Valid: false}, ErrInvalidDate
}
return SQLDate{String: fuzzyDate, Valid: true}, sql.NullString{String: accuracy.String(), Valid: true}, nil
}
func ValidateFuzzyString(date *string) error {
_, _, err := ParseFuzzyString(date)
return err
}