sqlboiler/queries/helpers.go

32 lines
708 B
Go
Raw Permalink Normal View History

package queries
import (
"fmt"
"reflect"
2017-07-31 05:34:54 +02:00
"github.com/volatiletech/sqlboiler/strmangle"
)
// 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))
for _, d := range defaults {
fieldName := strmangle.TitleCase(d)
field := val.FieldByName(fieldName)
if !field.IsValid() {
panic(fmt.Sprintf("Could not find field name %s in type %T", fieldName, obj))
}
zero := reflect.Zero(field.Type())
if !reflect.DeepEqual(zero.Interface(), field.Interface()) {
c = append(c, d)
}
}
return c
}