2016-03-16 08:04:26 +01:00
|
|
|
package boil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2016-05-02 08:34:25 +02:00
|
|
|
|
2016-08-09 09:59:30 +02:00
|
|
|
"github.com/vattle/sqlboiler/strmangle"
|
2016-03-16 08:04:26 +01:00
|
|
|
)
|
|
|
|
|
2016-05-02 08:34:25 +02:00
|
|
|
// NonZeroDefaultSet returns the fields included in the
|
|
|
|
// defaults slice that are non zero values
|
2016-09-03 20:54:23 +02:00
|
|
|
func NonZeroDefaultSet(defaults []string, obj interface{}) []string {
|
2016-05-02 08:34:25 +02:00
|
|
|
c := make([]string, 0, len(defaults))
|
|
|
|
|
2016-05-17 12:00:56 +02:00
|
|
|
val := reflect.Indirect(reflect.ValueOf(obj))
|
2016-05-02 08:34:25 +02:00
|
|
|
|
|
|
|
for _, d := range defaults {
|
2016-09-03 20:54:23 +02:00
|
|
|
fieldName := strmangle.TitleCase(d)
|
2016-05-02 08:34:25 +02:00
|
|
|
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
|
|
|
|
}
|