github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/queries/helpers.go (about)

     1  package queries
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  )
     7  
     8  // NonZeroDefaultSet returns the fields included in the
     9  // defaults slice that are non zero values
    10  func NonZeroDefaultSet(defaults []string, obj interface{}) []string {
    11  	c := make([]string, 0, len(defaults))
    12  
    13  	val := reflect.Indirect(reflect.ValueOf(obj))
    14  	typ := val.Type()
    15  	nf := typ.NumField()
    16  
    17  	for _, def := range defaults {
    18  		found := false
    19  		for i := 0; i < nf; i++ {
    20  			field := typ.Field(i)
    21  			name, _ := getBoilTag(field)
    22  
    23  			if name != def {
    24  				continue
    25  			}
    26  
    27  			found = true
    28  			fieldVal := val.Field(i)
    29  
    30  			zero := reflect.Zero(fieldVal.Type())
    31  			if !reflect.DeepEqual(zero.Interface(), fieldVal.Interface()) {
    32  				c = append(c, def)
    33  			}
    34  			break
    35  		}
    36  
    37  		if !found {
    38  			panic(fmt.Sprintf("could not find field name %s in type %T", def, obj))
    39  		}
    40  	}
    41  
    42  	return c
    43  }