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

The time.Time type is converted to empty map #334

Open
van-scott opened this issue Jun 16, 2023 · 6 comments
Open

The time.Time type is converted to empty map #334

van-scott opened this issue Jun 16, 2023 · 6 comments

Comments

@van-scott
Copy link

type Person struct {
Name string json:"name"
Age int json:"age"
Time time.Time json:"time"
Address Address json:"address"
}

Why a time.Time type has to be converted to map, not recursive transfer is not on the line, configuration is not, read a lot of posts, what hook, hook function is not good. I recommend using the github.com/fatih/structs library, I really buy it

@acabarbaye
Copy link

could time.Time be transformed into a RFC3339 string rather than an empty map in the same way as what is done for a time.Duration which is converting into its string representation?

@GiGurra
Copy link

GiGurra commented Nov 10, 2023

Same problem

@danhawkins
Copy link

converting time to time also doesn't work for me, I have this failing test to demonstrate

func TestDecoder_timeStructs(t *testing.T) {
	type Input struct {
		TheTime time.Time
	}
	timeNow := time.Now()
	input := &Input{
		TheTime: timeNow,
	}

	type Output struct {
		TheTime time.Time
	}

	actual := &Output{}
	config := &DecoderConfig{
		Result:               &actual,
		IgnoreUntaggedFields: true,
	}

	decoder, err := NewDecoder(config)
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	err = decoder.Decode(input)
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	expected := Output{
		TheTime: timeNow,
	}

	if !reflect.DeepEqual(expected, actual) {
		t.Fatalf("Decode() expected: %#v\ngot: %#v", expected, actual)
	}
}
--- FAIL: TestDecoder_timeStructs (0.00s)
    /Users/danny/Code/go/src/github.com/mitchellh/mapstructure/mapstructure_test.go:2768: Decode() expected: mapstructure.Output{TheTime:time.Date(2023, time.December, 8, 20, 0, 27, 838026000, time.Local)}
        got: &mapstructure.Output{TheTime:time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC)}

@dangquyitt
Copy link

converting time to time also doesn't work for me, I have this failing test to demonstrate

func TestDecoder_timeStructs(t *testing.T) {
	type Input struct {
		TheTime time.Time
	}
	timeNow := time.Now()
	input := &Input{
		TheTime: timeNow,
	}

	type Output struct {
		TheTime time.Time
	}

	actual := &Output{}
	config := &DecoderConfig{
		Result:               &actual,
		IgnoreUntaggedFields: true,
	}

	decoder, err := NewDecoder(config)
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	err = decoder.Decode(input)
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	expected := Output{
		TheTime: timeNow,
	}

	if !reflect.DeepEqual(expected, actual) {
		t.Fatalf("Decode() expected: %#v\ngot: %#v", expected, actual)
	}
}
--- FAIL: TestDecoder_timeStructs (0.00s)
    /Users/danny/Code/go/src/github.com/mitchellh/mapstructure/mapstructure_test.go:2768: Decode() expected: mapstructure.Output{TheTime:time.Date(2023, time.December, 8, 20, 0, 27, 838026000, time.Local)}
        got: &mapstructure.Output{TheTime:time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC)}

It also doesn't work for me; I hope it will soon support this case.

@garrettladley
Copy link

I am also running into this with a function for a project I'm working on. Maybe this will be handled in go-viper/mapstructure? Will open an issue there

@sheregeda
Copy link

I'm experiencing the same issue.

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

No branches or pull requests

7 participants