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

Can you specify name to be case insensitive #853

Open
sjmark opened this issue Mar 23, 2023 · 2 comments
Open

Can you specify name to be case insensitive #853

sjmark opened this issue Mar 23, 2023 · 2 comments

Comments

@sjmark
Copy link

sjmark commented Mar 23, 2023

190 lines of a reflectx/reflect.go

func (m *Mapper) TraversalsByNameFunc(t reflect.Type, names []string, fn func(int, []int) error) error {
t = Deref(t)
mustBe(t, reflect.Struct)
tm := m.TypeMap(t)
for i, name := range names {
fi, ok := tm.Names[name]
if !ok {
if err := fn(i, nil); err != nil {
return err
}
} else {
if err := fn(i, fi.Index); err != nil {
return err
}
}
}
return nil
}

@farid-alfernass
Copy link

farid-alfernass commented May 4, 2023

#859
sure, To make the TraversalsByNameFunc function in your code case-insensitive, you can modify the fi, ok := tm.Names[name] line to convert the name to lowercase using the strings.ToLower()

you can modify the fi, ok := tm.Names[name] line to convert the name to lowercase using the strings.ToLower() function

tm.Names[strings.ToLower(name)]

This way, the function will find struct fields regardless of the case of their names.

@sjmark
Copy link
Author

sjmark commented May 4, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants