You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 22, 2024. It is now read-only.
Repository navigation
This repository was archived by the owner on Jul 22, 2024. It is now read-only.
Encode: DecodeHook not called for struct fields #166
I use the DecodeHook to translate the values of certain fields. This works great when "decoding" from map -> struct, but doesn't work the other way around.
When I "encode" from struct -> map, the DecodeHook is called only once, for the whole struct. I would expect it to be called for each field as well.
I've currently hacked it into my decoderHook function, by iterating over the fields and recursively calling itself. But this is rather tedious and has the disadvantage that I can't change the types of fields.
Hi @corani -- I suspect the issue you're having is related to #124. The crux of the issue is that when encoding from struct -> map[string]interface{}, after the first round of decoding, the receiver type is just an interface{}, and since the fields of the struct can be directly assigned to an interface{}, the fields of the struct are copied over as-is.
To my knowledge, there is no non-manual way around this right now, but I ran into this same issue which led me to write #183. If this is still relevant to you, I'd be happy to take feedback on that PR on whether that fix works for your case.
Interestingly, it looks like this behavior has changed between v1.3.3 and v1.4.0, and this appears to no longer be an issue. I don't immediately see which change did it, but the following code works how I'd expect it to now.
Hi @camdencheek I just tested your code on v1.4.1. It doesn't work recursively :(
Output for v1.4.0: map[string]interface {}{"Btype":map[string]interface {}{"Bval":"test"}}
Output for v1.4.1: map[string]interface {}{"Btype":(*main.B)(0xc000012580)}
Activity
camdencheek commentedon Mar 29, 2020
Hi @corani -- I suspect the issue you're having is related to #124. The crux of the issue is that when encoding from struct ->
map[string]interface{}, after the first round of decoding, the receiver type is just aninterface{}, and since the fields of the struct can be directly assigned to aninterface{}, the fields of the struct are copied over as-is.To my knowledge, there is no non-manual way around this right now, but I ran into this same issue which led me to write #183. If this is still relevant to you, I'd be happy to take feedback on that PR on whether that fix works for your case.
camdencheek commentedon Nov 30, 2020
Interestingly, it looks like this behavior has changed between v1.3.3 and v1.4.0, and this appears to no longer be an issue. I don't immediately see which change did it, but the following code works how I'd expect it to now.
Output for v1.3.3:
Output for v1.4.0:
MeteorSis commentedon Feb 10, 2021
Hi @camdencheek I just tested your code on v1.4.1. It doesn't work recursively :(
Output for v1.4.0:
map[string]interface {}{"Btype":map[string]interface {}{"Bval":"test"}}Output for v1.4.1:
map[string]interface {}{"Btype":(*main.B)(0xc000012580)}voodoo-dn commentedon Feb 26, 2021
@MeteorSis The same for me after switching to v1.4.1
4 remaining items