Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Encode: DecodeHook not called for struct fields #166

Open

Description

@corani

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.

Activity

  1. camdencheek commented on Mar 29, 2020

    @camdencheek
    Contributor

    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.

  2. camdencheek commented on Nov 30, 2020

    @camdencheek
    Contributor

    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.

    package main
    
    import (
    	"fmt"
    
    	"github.com/mitchellh/mapstructure"
    )
    
    type A struct {
    	Btype *B
    }
    
    type B struct {
    	Bval string
    }
    
    func main() {
    	a := A{
    		Btype: &B{
    			Bval: "test",
    		},
    	}
    
    	var m map[string]interface{}
    	if err := mapstructure.Decode(a, &m); err != nil {
    		panic(err)
    	}
    
    	fmt.Printf("%#v\n", m)
    }

    Output for v1.3.3:

    map[string]interface {}{"Btype":(*main.B)(0xc000098560)}

    Output for v1.4.0:

    map[string]interface {}{"Btype":map[string]interface {}{"Bval":"test"}}
  3. MeteorSis commented on Feb 10, 2021

    @MeteorSis

    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)}

  4. voodoo-dn commented on Feb 26, 2021

    @voodoo-dn

    @MeteorSis The same for me after switching to v1.4.1

  5. 4 remaining items

    Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址