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