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

time.Time encoded as map[string]interface{}{} #191

Closed
smyrman opened this issue May 15, 2020 · 12 comments
Closed

time.Time encoded as map[string]interface{}{} #191

smyrman opened this issue May 15, 2020 · 12 comments

Comments

@smyrman
Copy link

smyrman commented May 15, 2020

Noticed that if encoding a struct into a map, time.Time entries are passed into empty maps, which doesn't seam very useful.

I could add a hook to encode it into a string instance, but because hooks run as pre-processors, it's not possible to preserve it as a time.Time.

Example code:

package main

import (
	"fmt"
	"time"

	"github.com/mitchellh/mapstructure"
)

func PayloadEncoder(target *map[string]interface{}) (*mapstructure.Decoder, error) {
	return mapstructure.NewDecoder(&mapstructure.DecoderConfig{
		//DecodeHook: mapstructure.ComposeDecodeHookFunc(
		// <- no hook can be added here to solve this.
		//),
		Result: target,
	})
}

type MyStruct struct {
	CreatedAt time.Time
}

func main() {
	p1 := MyStruct{
		CreatedAt: time.Now(),
	}
	m1 := make(map[string]interface{})
	d1, err := PayloadEncoder(&m1)
	fmt.Println("\nerr:", err, "\ndec:", d1, "\nmap:", m1)
	err2 := d1.Decode(p1)
	fmt.Printf("\nerr: %v\noutput: %#v\n", err2, m1)
}

Output:


err: <nil> 
dec: &{0xc0001be000} 
map: map[]

err: <nil>
output: map[string]interface {}{"CreatedAt":map[string]interface {}{}}
@Dragomir-Ivanov
Copy link

Hi @smyrman ,
Have you considered using "github.com/fatih/structs"?

https://play.golang.org/p/rFTAG5bcUI4

@mitchellh
Copy link
Owner

I believe #183 should address this.

@yjwong
Copy link

yjwong commented Dec 23, 2020

Hello @mitchellh, firstly, thanks for this library. I've been trying to achieve something similar, converting from a struct into a map[string]interface{} while preserving time.Time fields, but looking at the changes in #183, I wasn't entirely sure how to do it.

I tried setting a new reflect.Value to the to argument but it seems like we can't change the type of a reflect.Value.

Are there any examples/hints on how this can be achieved?

@smyrman
Copy link
Author

smyrman commented Jan 29, 2021

Agree with @yjwong; I don't quite see how #183 allows solving this problem.

The problem is that after DecodeHookExec is run, then mapstructure would still run through the "default" transitions, in which case it will find that it is converting a time.Time to an interface{} value, and convert it to an empty map[string]interface{}.

@mimol91
Copy link

mimol91 commented Feb 11, 2021

Having this same issue

1 similar comment
@MessageDream
Copy link

Having this same issue

@gatspy
Copy link

gatspy commented Sep 27, 2021

mark this issue

@johnny-oriente
Copy link

Same issue

@Dragomir-Ivanov
Copy link

@johnny-oriente I recommend using struct2 for conversion from struct -> map[string]interface{}.
https://github.com/worldline-go/struct2

@Dragomir-Ivanov
Copy link

@smyrman, Hi there! This new https://github.com/worldline-go/struct2 package may be of interest to you too.

@smyrman
Copy link
Author

smyrman commented Jul 27, 2022

We have work-around this issue long ago by writing our own implementation of the struct / slice -> map[string]interface{} / []interface{} conversion path, and using mapstructure for the to struct path only.

The struct2 omitnested struct tag might work as a way to fix the problem for others though (if you can live with their interface naming; not a topic for this issue).

@Dragomir-Ivanov
Copy link

@smyrman They are new kid on the block, so they might be willing to clean up. Their interface is not that big though, so renaming might be easy.

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

8 participants