forked from volatiletech/sqlboiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
43 lines (34 loc) · 823 Bytes
/
helpers.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
package queries
import (
"fmt"
"reflect"
)
// NonZeroDefaultSet returns the fields included in the
// defaults slice that are non zero values
func NonZeroDefaultSet(defaults []string, obj interface{}) []string {
c := make([]string, 0, len(defaults))
val := reflect.Indirect(reflect.ValueOf(obj))
typ := val.Type()
nf := typ.NumField()
for _, def := range defaults {
found := false
for i := 0; i < nf; i++ {
field := typ.Field(i)
name, _ := getBoilTag(field)
if name != def {
continue
}
found = true
fieldVal := val.Field(i)
zero := reflect.Zero(fieldVal.Type())
if !reflect.DeepEqual(zero.Interface(), fieldVal.Interface()) {
c = append(c, def)
}
break
}
if !found {
panic(fmt.Sprintf("could not find field name %s in type %T", def, obj))
}
}
return c
}