-
Notifications
You must be signed in to change notification settings - Fork 2k
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
viper.Unmarshal() ignoring UnmarshalYAML interface #338
Comments
Just had the same problem and my interface looked the same as yours. @TimJones did you find a way to do this? |
@augier Sorry this went so long without a reply. Unfortunately I never found a way around this, I simply had to force one format over the other. If you had any better luck I'd be interested in hearing about it! |
Took me a while to find the reason... If you call Ideally func (m *MyType) UnmarshalMap(value interface{}) error {
// your code ...
} Imo this is not a bug in viper itself, that's kind of "by design". (Filed an issue on |
Great work @tubocurarin !! This was quite the gotcha for me!! |
This is related of how Viper processes the Config File. Read more at: spf13/viper#338
Turns out Viper does not support YAML unmarshalling directly but goes through a map structure as an intermediate: spf13/viper#338
[This issue][1] does a good job explaining the why behind this admittedly-hacky code. What I would have preferred to do is add `UnmarshalJSON`, `UnmarshalTOML`, (and so on) methods to both `FileConfig` and `Config`, but switching to viper means we don't invoke those custom unmarshalers _at all_, because viper is going from $file_bytes=>`map[string]interface{}`=(via mapstructure)>$your_type. So, we define a temporary struct (so far, so good) that will play nicely with mapstructure's unmarshaling. We then take those string maps and hand them off to the individual Config structs to run through the kv-`parseOne` loop as before. Since we're using viper to load from files now (and viper does not invoke our custom unmarshal functions ...), we no longer need them, so I deleted all of that code as well. [1]: spf13/viper#338 (comment) Signed-off-by: Andrew Mason <amason@slack-corp.com>
I am trying to implement a custom unmarshaler for a YAML config to allow a key to support multiple types (in my personal case, a string and sequence). I am trying to configure that if the YAML value is a string type, I can manipulate it into a sequence (e.g. splitting on spaces, quotes, etc).
With the above code, I expect an output of:
but I get the unchanged original data instead:
I'm not 100% sure I have the interface right, but the API doc for
gopkg.in/yaml.v2
doesn't go into detail.The text was updated successfully, but these errors were encountered: