@@ -37,9 +37,10 @@ const (
37
37
type Option struct {
38
38
// setting this value to true will ignore copying zero values of all the fields, including bools, as well as a
39
39
// struct having all it's fields set to their zero values respectively (see IsZero() in reflect/value.go)
40
- IgnoreEmpty bool
41
- DeepCopy bool
42
- Converters []TypeConverter
40
+ IgnoreEmpty bool
41
+ CaseSensitive bool
42
+ DeepCopy bool
43
+ Converters []TypeConverter
43
44
}
44
45
45
46
func (opt Option ) converters () map [converterPair ]TypeConverter {
@@ -300,7 +301,7 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error)
300
301
break
301
302
}
302
303
303
- toField := dest . FieldByName ( destFieldName )
304
+ toField := fieldByName ( dest , destFieldName , opt . CaseSensitive )
304
305
if toField .IsValid () {
305
306
if toField .CanSet () {
306
307
isSet , err := set (toField , fromField , opt .DeepCopy , converters )
@@ -346,7 +347,7 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error)
346
347
}
347
348
348
349
if fromMethod .IsValid () && fromMethod .Type ().NumIn () == 0 && fromMethod .Type ().NumOut () == 1 && ! shouldIgnore (fromMethod , opt .IgnoreEmpty ) {
349
- if toField := dest . FieldByName ( destFieldName ); toField .IsValid () && toField .CanSet () {
350
+ if toField := fieldByName ( dest , destFieldName , opt . CaseSensitive ); toField .IsValid () && toField .CanSet () {
350
351
values := fromMethod .Call ([]reflect.Value {})
351
352
if len (values ) >= 1 {
352
353
set (toField , values [0 ], opt .DeepCopy , converters )
@@ -741,3 +742,11 @@ func driverValuer(v reflect.Value) (i driver.Valuer, ok bool) {
741
742
i , ok = v .Addr ().Interface ().(driver.Valuer )
742
743
return
743
744
}
745
+
746
+ func fieldByName (v reflect.Value , name string , caseSensitive bool ) reflect.Value {
747
+ if caseSensitive {
748
+ return v .FieldByName (name )
749
+ }
750
+
751
+ return v .FieldByNameFunc (func (n string ) bool { return strings .EqualFold (n , name ) })
752
+ }
0 commit comments