-
Notifications
You must be signed in to change notification settings - Fork 35
LegacyStringToSliceHookFunc #99
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -161,6 +161,24 @@ | |
| } | ||
| } | ||
|
|
||
| func LegacyStringToSliceHookFunc(sep string) DecodeHookFunc { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please add a comment why this is added? I have added this to Viper: https://github.com/spf13/viper/blob/master/viper.go#L1000 I also wonder if it would make sense to call it
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to migrate from the old mapstructure to the go-viper/mapstruct without additional dependencies. I'm wondering if the official approach requires importing the spy13/viper repository to use the handler you mentioned above. If yes, then okay.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But spy13/viper require some additional changes, we don't use Viper structure. We would like to use it as a normal function.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @KrzysztofKornalewski-Reply that's just an example. It's basically the same function that you submitted. I copied it earlier to Viper to keep backward compatibility. I linked it as an example to demonstrate:
Legacy feels outdated, fragile.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sagikazarmark Is there any chance to keep this legacy hook in a repository? |
||
| return func( | ||
| f reflect.Kind, | ||
| t reflect.Kind, | ||
| data interface{}) (interface{}, error) { | ||
| if f != reflect.String || t != reflect.Slice { | ||
| return data, nil | ||
| } | ||
|
|
||
| raw := data.(string) | ||
| if raw == "" { | ||
| return []string{}, nil | ||
| } | ||
|
|
||
| return strings.Split(raw, sep), nil | ||
| } | ||
| } | ||
|
|
||
| // StringToTimeDurationHookFunc returns a DecodeHookFunc that converts | ||
| // strings to time.Duration. | ||
| func StringToTimeDurationHookFunc() DecodeHookFunc { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KrzysztofKornalewski-Reply Is this sufficient or do we also need to handle lists as well? I think the example in #64 is
""to[]int{}.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes this should be sufficient, as you know from this migration PR:
openbao/openbao#290
There are two errors, and based on my tests and debugging, this approach resolves both. When I manually added
LegacyStringToSliceHookFunc, both failed test cases were resolved.The conversion problem for
"1,2,3,"for example, occurs because the new Hook implementation ofStringToSliceHookFunc()only works when the target is a slice of strings. Previously, it worked for any slices.About
""to[]int{}it also occurs because of workflow has changed in StringToSliceHookFunc().They also store this solution locally in spf13/viper.
https://github.com/spf13/viper/blob/260b8e9a3c75f49c374251f10fa1529d9e704b6a/viper.go#L1000
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KrzysztofKornalewski-Reply Ahh, I see now. Makes sense, thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KrzysztofKornalewski-Reply In the meantime, you could also open this as a PR against OpenBao I think, and see if that solves the issues with PR 290 :-)