-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalue.go
More file actions
424 lines (362 loc) · 12.3 KB
/
value.go
File metadata and controls
424 lines (362 loc) · 12.3 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package debefix
import (
"bytes"
"context"
"fmt"
"html/template"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
)
// SetValueRefIDData is a special ValueMultiple which sets the RefID for the current row.
// The field containing it is not added to the row.
type SetValueRefIDData struct {
RefID RefID
}
// SetValueRefID is a special ValueMultiple which sets the RefID for the current row.
// The field containing it is not added to the row.
func SetValueRefID(refID RefID) SetValueRefIDData {
return SetValueRefIDData{
RefID: refID,
}
}
var _ ValueMultiple = SetValueRefIDData{}
func (v SetValueRefIDData) Resolve(ctx context.Context, resolvedData *ResolvedData, tableID TableID, fieldName string, values ValuesMutable) error {
return NewResolveError("SetValueRefID is not meant to be resolved")
}
// ValueInternalIDData represents a field value from a table row by internal id.
// It implements Value, QueryRow, and can return a UpdateQuery.
type ValueInternalIDData struct {
TableID TableID
InternalID uuid.UUID
FieldName string
}
// ValueInternalID represents a field value from a table row by internal id.
// It implements Value, QueryRow, and can return a UpdateQuery.
func ValueInternalID(tableID TableID, internalID uuid.UUID, fieldName string) ValueInternalIDData {
return ValueInternalIDData{
TableID: tableID,
InternalID: internalID,
FieldName: fieldName,
}
}
var _ Value = (*ValueInternalIDData)(nil)
var _ ValueDependencies = (*ValueInternalIDData)(nil)
var _ QueryRow = (*ValueInternalIDData)(nil)
func (v ValueInternalIDData) ResolveValue(ctx context.Context, resolvedData *ResolvedData, values Values) (any, bool, error) {
rv, err := resolvedData.FindInternalIDValue(v)
if err != nil {
return nil, false, err
}
return rv, true, nil
}
func (v ValueInternalIDData) TableDependencies() []TableID {
return []TableID{v.TableID}
}
func (v ValueInternalIDData) QueryRow(data *Data) (QueryRowResult, error) {
row, err := data.FindInternalIDRow(v.TableID, v.InternalID)
if err != nil {
return QueryRowResult{}, err
}
return QueryRowResult{TableID: v.TableID, Row: row}, nil
}
func (v ValueInternalIDData) UpdateQuery(keyFields []string) UpdateQuery {
return UpdateQueryRow(v, keyFields)
}
// ValueRefIDData represents a field value from a table row by its RefID.
// It implements Value, QueryRow, and can return a UpdateQuery.
type ValueRefIDData struct {
TableID TableID
RefID RefID
FieldName string
}
// ValueRefID represents a field value from a table row by its RefID.
// It implements Value, QueryRow, and can return a UpdateQuery.
func ValueRefID(tableID TableID, refID RefID, fieldName string) ValueRefIDData {
return ValueRefIDData{
TableID: tableID,
RefID: refID,
FieldName: fieldName,
}
}
var _ Value = (*ValueRefIDData)(nil)
var _ ValueDependencies = (*ValueRefIDData)(nil)
var _ QueryRow = (*ValueRefIDData)(nil)
func (v ValueRefIDData) ResolveValue(ctx context.Context, resolvedData *ResolvedData, values Values) (any, bool, error) {
value, err := resolvedData.FindRefIDRowValue(v)
if err != nil {
return nil, false, err
}
return value, true, nil
}
func (v ValueRefIDData) TableDependencies() []TableID {
return []TableID{v.TableID}
}
func (v ValueRefIDData) QueryRow(data *Data) (QueryRowResult, error) {
row, err := data.FindRefIDRow(v.TableID, v.RefID)
if err != nil {
return QueryRowResult{}, err
}
return QueryRowResult{TableID: v.TableID, Row: row}, nil
}
func (v ValueRefIDData) UpdateQuery(keyFields []string) UpdateQuery {
return UpdateQueryRow(v, keyFields)
}
// ValueBaseTimeAddData is a Value that calculates a date based on the Data base time.
type ValueBaseTimeAddData struct {
AddDays int
AddHours int
AddMinutes int
AddSeconds int
}
// ValueBaseTimeAdd is a Value that calculates a date based on the Data base time.
func ValueBaseTimeAdd(options ...ValueBaseTimeAddDataOption) ValueBaseTimeAddData {
ret := ValueBaseTimeAddData{}
for _, option := range options {
option(&ret)
}
return ret
}
var _ Value = (*ValueBaseTimeAddData)(nil)
type ValueBaseTimeAddDataOption func(*ValueBaseTimeAddData)
func WithAddDate(days, hours, minutes, seconds int) ValueBaseTimeAddDataOption {
return func(v *ValueBaseTimeAddData) {
v.AddDays = days
v.AddHours = hours
v.AddMinutes = minutes
v.AddSeconds = seconds
}
}
func WithAddTime(hours, minutes, seconds int) ValueBaseTimeAddDataOption {
return func(v *ValueBaseTimeAddData) {
v.AddHours = hours
v.AddMinutes = minutes
v.AddSeconds = seconds
}
}
func WithAddDays(days int) ValueBaseTimeAddDataOption {
return func(v *ValueBaseTimeAddData) {
v.AddDays = days
}
}
func WithAddHours(hours int) ValueBaseTimeAddDataOption {
return func(v *ValueBaseTimeAddData) {
v.AddHours = hours
}
}
func WithAddMinutes(minutes int) ValueBaseTimeAddDataOption {
return func(v *ValueBaseTimeAddData) {
v.AddMinutes = minutes
}
}
func WithAddSeconds(seconds int) ValueBaseTimeAddDataOption {
return func(v *ValueBaseTimeAddData) {
v.AddSeconds = seconds
}
}
func (v ValueBaseTimeAddData) ResolveValue(ctx context.Context, resolvedData *ResolvedData, values Values) (any, bool, error) {
t := resolvedData.BaseTime.Add((time.Hour * time.Duration(v.AddHours)) + (time.Minute * time.Duration(v.AddMinutes)) + (time.Second * time.Duration(v.AddSeconds)))
if v.AddDays > 0 {
t = t.AddDate(0, 0, v.AddDays)
}
return t, true, nil
}
// ValueFormatData is a Value that formats a string based on other field's values.
type ValueFormatData struct {
Format string
Args []any
}
// ValueFormat is a Value that formats a string based on other field's values.
func ValueFormat(format string, args ...any) ValueFormatData {
return ValueFormatData{
Format: format,
Args: args,
}
}
var _ Value = (*ValueFormatData)(nil)
var _ ValueDependencies = (*ValueFormatData)(nil)
func (v ValueFormatData) ResolveValue(ctx context.Context, resolvedData *ResolvedData, values Values) (any, bool, error) {
fmtArgs, argOk, err := resolvedData.ResolveArgs(ctx, values, v.Args...)
if err != nil {
return nil, false, err
}
if !argOk {
return nil, false, ResolveLater
}
return fmt.Sprintf(v.Format, fmtArgs...), true, nil
}
func (v ValueFormatData) TableDependencies() []TableID {
var deps []TableID
for _, arg := range v.Args {
if vd, ok := arg.(ValueDependencies); ok {
deps = append(deps, vd.TableDependencies()...)
}
}
return deps
}
// ValueTemplateData is a Value that formats a string based on other field's values using "text/template".
type ValueTemplateData struct {
Template string
Args map[string]any
}
// ValueTemplate is a Value that formats a string based on other field's values using "text/template".
func ValueTemplate(template string, args map[string]any) ValueTemplateData {
return ValueTemplateData{
Template: template,
Args: args,
}
}
var _ Value = (*ValueTemplateData)(nil)
var _ ValueDependencies = (*ValueTemplateData)(nil)
func (v ValueTemplateData) ResolveValue(ctx context.Context, resolvedData *ResolvedData, values Values) (any, bool, error) {
fmtArgs, argOk, err := resolvedData.ResolveMapArgs(ctx, values, v.Args)
if err != nil {
return nil, false, err
}
if !argOk {
return nil, false, ResolveLater
}
tmpl, err := template.New("tmpl").Parse(v.Template)
if err != nil {
return nil, false, NewResolveErrorf("failed to parse template: %w", err)
}
var buf bytes.Buffer
err = tmpl.Option("missingkey=error").Execute(&buf, fmtArgs)
if err != nil {
return nil, false, NewResolveErrorf("failed to execute template: %w", err)
}
return buf.String(), true, nil
}
func (v ValueTemplateData) TableDependencies() []TableID {
var deps []TableID
for _, arg := range v.Args {
if vd, ok := arg.(ValueDependencies); ok {
deps = append(deps, vd.TableDependencies()...)
}
}
return deps
}
// ValueFieldValueData is a Value which returns the value of a field of the current row.
type ValueFieldValueData struct {
FieldName string
}
func ValueFieldValue(fieldName string) ValueFieldValueData {
return ValueFieldValueData{
FieldName: fieldName,
}
}
var _ Value = (*ValueFieldValueData)(nil)
func (d ValueFieldValueData) ResolveValue(ctx context.Context, resolvedData *ResolvedData, values Values) (any, bool, error) {
value, ok := values.Get(d.FieldName)
if !ok {
return nil, false, ResolveLater
}
return value, true, nil
}
// ValueRefFieldValueData is a Value which returns the value of a field of a row of the passed table where one of the
// other row's field value is equal to one of the current row's field value.
type ValueRefFieldValueData struct {
SourceFieldName string // the field name of the current row to compare for equality.
TableID TableID // the table ID of the other table.
CompareFieldName string // the field name of the other table row (from TableID) to compare for equality.
ReturnFieldName string // the field name of the other table row to have its value returned.
}
// ValueRefFieldValue is a Value which returns the value of a field of a row of the passed table where one of the
// other row's field value is equal to one of the current row's field value.
func ValueRefFieldValue(sourceFieldName string, tableID TableID, compareFieldName string,
returnFieldName string) ValueRefFieldValueData {
return ValueRefFieldValueData{
SourceFieldName: sourceFieldName,
TableID: tableID,
CompareFieldName: compareFieldName,
ReturnFieldName: returnFieldName,
}
}
var _ Value = (*ValueRefFieldValueData)(nil)
var _ ValueDependencies = (*ValueRefFieldValueData)(nil)
func (d ValueRefFieldValueData) ResolveValue(ctx context.Context, resolvedData *ResolvedData, values Values) (any, bool, error) {
sourceValue, ok := values.Get(d.SourceFieldName)
if !ok {
return nil, false, nil
}
resolveRow, err := resolvedData.FindTableRow(d.TableID, func(innerRow *Row) (bool, error) {
ivalue, ok := innerRow.Values.Get(d.CompareFieldName)
if ok {
if cmp.Equal(sourceValue, ivalue) {
return true, nil
}
}
return false, nil
})
if err != nil {
return nil, false, err
}
value, ok := resolveRow.Values.Get(d.ReturnFieldName)
if !ok {
return nil, false, nil
}
return value, true, nil
}
func (d ValueRefFieldValueData) TableDependencies() []TableID {
return []TableID{d.TableID}
}
// ValueDefaultData returns the DefaultValue if the other Value don't return a valid value (returns "false" for the 2nd
// result value).
type ValueDefaultData struct {
Value Value
DefaultValue any
}
// ValueDefault returns the DefaultValue if the other Value don't return a valid value (returns "false" for the 2nd
// result value).
func ValueDefault(value Value, defaultValue any) ValueDefaultData {
return ValueDefaultData{
Value: value,
DefaultValue: defaultValue,
}
}
var _ Value = (*ValueDefaultData)(nil)
var _ ValueDependencies = (*ValueDefaultData)(nil)
func (d ValueDefaultData) ResolveValue(ctx context.Context, resolvedData *ResolvedData, values Values) (any, bool, error) {
value, ok, err := d.Value.ResolveValue(ctx, resolvedData, values)
if err != nil {
return nil, false, err
}
if !ok {
return d.DefaultValue, true, nil
}
return value, true, nil
}
func (d ValueDefaultData) TableDependencies() []TableID {
if td, ok := d.Value.(ValueDependencies); ok {
return td.TableDependencies()
}
return nil
}
type ValueFormatCallback func(ctx context.Context, resolvedData *ResolvedData, values Values, value any) (any, bool, error)
// ValueFormatFuncData resolves Value, and allows it to be changed on a callback formatting function.
type ValueFormatFuncData struct {
Value Value
FormatFunc ValueFormatCallback
}
// ValueFormatFunc resolves Value, and allows it to be changed on a callback formatting function.
func ValueFormatFunc(value Value, formatFunc ValueFormatCallback) ValueFormatFuncData {
return ValueFormatFuncData{
Value: value,
FormatFunc: formatFunc,
}
}
var _ Value = (*ValueFormatFuncData)(nil)
var _ ValueDependencies = (*ValueFormatFuncData)(nil)
func (d ValueFormatFuncData) ResolveValue(ctx context.Context, resolvedData *ResolvedData, values Values) (any, bool, error) {
value, ok, err := d.Value.ResolveValue(ctx, resolvedData, values)
if err != nil || !ok {
return nil, ok, err
}
return d.FormatFunc(ctx, resolvedData, values, value)
}
func (d ValueFormatFuncData) TableDependencies() []TableID {
if td, ok := d.Value.(ValueDependencies); ok {
return td.TableDependencies()
}
return nil
}