copystructure icon indicating copy to clipboard operation
copystructure copied to clipboard

Enable option to not unwrap from pointer

Open alexkreidler opened this issue 6 years ago • 0 comments

From the last lines of copystructure.go:

// wrapPtr is a helper that takes v and always make it *v. copystructure
// stores things internally as pointers until the last moment before unwrapping

Feature request: add an option (in Config) or somewhere to disable unwrapping at the last step.

This would allow an API like copystructure.Copy(val MyStruct) *MyStruct, which I believe would be useful for many people.

It would then make reflect.ValueOf(m *MyStruct).CanSet() true allowing me to use copystructure in conjunction with mapstructure. (e.g. I copy a struct and then decode a map into it)

My situation

My specific use case is I have a function GetConfig() interface{} which returns an interface{} containing some type of struct.

I want to decode a map into this struct using mapstructure, but I can't because the interface{} only contains a MyStruct{} not a *MyStruct{}, so CanSet is false. Additionally, mapstructure doesn't support decoding into a *interface{}, and even if it did, the underlying struct still wouldn't be able to be modified.

However I think if I can copy the interface{}/MyStruct{} to a *MyStruct I would be able to use mapstructure to do what I want.

I don't know enough about the internals to do this, but it seems like a simple thing if I just knew where.

I would really appreciate this, it would make all the difference!

Thanks for making some awesome libraries!

alexkreidler avatar Dec 29 '19 05:12 alexkreidler