-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathput_assoc.go
34 lines (31 loc) · 899 Bytes
/
put_assoc.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
package changeset
import (
"reflect"
"strings"
)
// PutAssocErrorMessage is the default error message for PutAssoc.
var PutAssocErrorMessage = "{field} is invalid"
// PutAssoc to changeset.
func PutAssoc(ch *Changeset, field string, value interface{}, opts ...Option) {
options := Options{
message: PutAssocErrorMessage,
}
options.apply(opts)
if typ, exist := ch.types[field]; exist {
if typ.Kind() == reflect.Struct {
valTyp := reflect.TypeOf(value)
if valTyp == reflect.TypeOf(ch) {
ch.changes[field] = value
return
}
} else if typ.Kind() == reflect.Slice && typ.Elem().Kind() == reflect.Struct {
valTyp := reflect.TypeOf(value)
if valTyp.Kind() == reflect.Slice && valTyp.Elem().ConvertibleTo(reflect.TypeOf(ch)) {
ch.changes[field] = value
return
}
}
}
msg := strings.Replace(options.message, "{field}", field, 1)
AddError(ch, field, msg)
}