Skip to content
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

Decoding Struct to Map with Embedded pointer to Struct #267

Closed
ghost opened this issue Dec 30, 2021 · 2 comments
Closed

Decoding Struct to Map with Embedded pointer to Struct #267

ghost opened this issue Dec 30, 2021 · 2 comments

Comments

@ghost
Copy link

ghost commented Dec 30, 2021

I'm trying to decode a struct into a map[string]interface{} using the RecursiveStructToMapHookFunc. If the struct contains an embedded struct, then per the first case in TestStructToMapHookFuncTabled we have

type b struct {
	TestKey string
}

type a struct {
	Sub b
}

testStruct := a{
	Sub: b{
		TestKey: "testval",
	},
}

testMap := map[string]interface{}{
	"Sub": map[string]interface{}{
		"TestKey": "testval",
	},
}

var result map[string]interface{}

d, _ := NewDecoder(&DecoderConfig{
	DecodeHook: RecursiveStructToMapHookFunc,
	Result:     &result,
})

and d.decode(testStruct) produces the equivalent of testMap. However, if we change the Sub field in struct a to be a pointer to a struct of type b, setup testStruct accordingly, and finally decode then the recursive decoding from struct to map does not apply.

type b struct {
	TestKey string
}

type a struct {
	Sub *b
}

testStruct := a{
	Sub: &b{
		TestKey: "testval",
	},
}

var result map[string]interface{}

d, _ := NewDecoder(&DecoderConfig{
	DecodeHook: RecursiveStructToMapHookFunc,
	Result:     &result,
})

Decode produces map[Sub:0xc000700010] or similar. I'm looking for a way to achieve the same result as testMap above, so essentially structs containing pointers to structs are treated as structs with embedded structs.

@vlanse
Copy link
Contributor

vlanse commented Jan 21, 2022

Hope #271 will help

@mitchellh
Copy link
Owner

Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants