forked from go-rel/changeset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathput_change.go
37 lines (31 loc) · 810 Bytes
/
put_change.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
package changeset
import (
"reflect"
"strings"
)
// PutChangeErrorMessage is the default error message for PutChange.
var PutChangeErrorMessage = "{field} is invalid"
// PutChange to changeset.
func PutChange(ch *Changeset, field string, value interface{}, opts ...Option) {
options := Options{
message: PutChangeErrorMessage,
}
options.apply(opts)
if typ, exist := ch.types[field]; exist {
if value != (interface{})(nil) {
valTyp := reflect.TypeOf(value)
if valTyp.Kind() == reflect.Ptr {
valTyp = valTyp.Elem()
}
if valTyp.ConvertibleTo(typ) {
ch.changes[field] = value
return
}
} else {
ch.changes[field] = reflect.Zero(reflect.PtrTo(typ)).Interface()
return
}
}
msg := strings.Replace(options.message, "{field}", field, 1)
AddError(ch, field, msg)
}