Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix case-insensitivity #188

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ const (
type Option struct {
// setting this value to true will ignore copying zero values of all the fields, including bools, as well as a
// struct having all it's fields set to their zero values respectively (see IsZero() in reflect/value.go)
IgnoreEmpty bool
CaseSensitive bool
DeepCopy bool
Converters []TypeConverter
IgnoreEmpty bool
CaseInsensitive bool
DeepCopy bool
Converters []TypeConverter
}

func (opt Option) converters() map[converterPair]TypeConverter {
Expand Down Expand Up @@ -329,7 +329,7 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error)
break
}

toField := fieldByName(dest, destFieldName, opt.CaseSensitive)
toField := fieldByName(dest, destFieldName, opt.CaseInsensitive)
if toField.IsValid() {
if toField.CanSet() {
isSet, err := set(toField, fromField, opt.DeepCopy, converters)
Expand Down Expand Up @@ -375,7 +375,7 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error)
}

if fromMethod.IsValid() && fromMethod.Type().NumIn() == 0 && fromMethod.Type().NumOut() == 1 && !shouldIgnore(fromMethod, opt.IgnoreEmpty) {
if toField := fieldByName(dest, destFieldName, opt.CaseSensitive); toField.IsValid() && toField.CanSet() {
if toField := fieldByName(dest, destFieldName, opt.CaseInsensitive); toField.IsValid() && toField.CanSet() {
values := fromMethod.Call([]reflect.Value{})
if len(values) >= 1 {
set(toField, values[0], opt.DeepCopy, converters)
Expand Down Expand Up @@ -771,10 +771,9 @@ func driverValuer(v reflect.Value) (i driver.Valuer, ok bool) {
return
}

func fieldByName(v reflect.Value, name string, caseSensitive bool) reflect.Value {
if caseSensitive {
return v.FieldByName(name)
func fieldByName(v reflect.Value, name string, caseInsensitive bool) reflect.Value {
if caseInsensitive {
return v.FieldByNameFunc(func(n string) bool { return strings.EqualFold(n, name) })
}

return v.FieldByNameFunc(func(n string) bool { return strings.EqualFold(n, name) })
return v.FieldByName(name)
}
2 changes: 1 addition & 1 deletion copier_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func BenchmarkNamaCopy(b *testing.B) {
for x := 0; x < b.N; x++ {
employee := &Employee{
Name: user.Name,
NickName: &user.Nickname,
Nickname: &user.Nickname,
Age: int64(user.Age),
FakeAge: int(*user.FakeAge),
DoubleAge: user.DoubleAge(),
Expand Down
Loading