-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
62 lines (55 loc) · 1.56 KB
/
update.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
package dal
import (
"errors"
"fmt"
"strings"
)
// A FieldPath is a non-empty sequence of non-empty fields that reference a value.
//
// A FieldPath value should only be necessary if one of the field names contains
// one of the runes ".˜*/[]". Most methods accept a simpler form of field path
// as a string in which the individual fields are separated by dots.
// For example,
//
// []string{"a", "b"}
//
// is equivalent to the string form
//
// "a.b"
//
// but
//
// []string{"*"}
//
// has no equivalent string form.
type FieldPath []string
// Update defines an update of a single field
type Update struct {
Field string
FieldPath FieldPath
Value any
}
// Validate validates the update
func (v Update) Validate() error {
if strings.TrimSpace(v.Field) == "" && len(v.FieldPath) == 0 {
return errors.New("either FieldVal or FieldPath must be provided")
}
if v.Field != "" && len(v.FieldPath) > 0 {
return fmt.Errorf("both FieldVal and FieldPath are provided: %v, %+v", v.Field, v.FieldPath)
}
return nil
}
type sentinel int
const (
// DeleteField is used as a value in a call to Update or Set with merge to indicate
// that the corresponding child should be deleted.
DeleteField sentinel = iota
// ServerTimestamp is used as a value in a call to Update to indicate that the
// child's value should be set to the time at which the server processed
// the request.
//
// ServerTimestamp must be the value of a field directly; it cannot appear in
// array or struct values, or in any value that is itself inside an array or
// struct.
ServerTimestamp
)