-
Notifications
You must be signed in to change notification settings - Fork 679
Support struct wrapped in interface as target for decoding #187
Comments
I could also use this to avoid alot of duplicate code. |
This can work if the underlying type is a pointer: var cc interface{}
cc = &struct{ Foo, Bar string }{} It doesn't work if its not a pointer. If this can be fixed I'd love to fix it, I'm actually not sure if this is fixable. |
As soon as I said that, I have a fix for it... one sec! |
This fixes #187. If the value of an interface is not addressable, we can't decode into it because we can't replace any keys. To work around this, we copy the value, decode into that, and then replace the full value.
Out of curiosity: even if it wasn‘t a pointer it should be possible to deep-clone the assigned struct and assign the clone to the interface? Not sure it is needed as this is a perfect workaround, but I feel this would still be doable? |
That is basically what ended up happening in the linked PR. We didn’t do a “deep” copy though, just a shallow one (which will subsequently copy if necessary). This behavior should be acceptable since the expectation with map structure is the target structure is modified. But what I ended up doing was: if I couldn’t address the value, then I make a copy (which can be addressed), decode into there, then reassign. The pseudo code would be like this:
|
One of the use cases for mapstructure is setting default values that won't be overridden by zero values- won't those get overridden if shallow copy is used although they were present in the original struct? |
mitchellh/mapstructure added mitchellh/mapstructure#187 with 1.3.2 which somehow breaks the decode() function of kusible. Added TODOs where applicable to check later
mitchellh/mapstructure added mitchellh/mapstructure#187 with 1.3.2 which somehow breaks the decode() function of kusible. Added TODOs where applicable to check later
mitchellh/mapstructure added mitchellh/mapstructure#187 with 1.3.2 which somehow breaks the decode() function of kusible. Added TODOs where applicable to check later
Similar to #186, mapstructure is not handling structs assigned to interfaces:
Will give
Would this be something worthwile to address with a PR or are there constraints that would prevent handling this case?
The text was updated successfully, but these errors were encountered: