Skip to content

Configuration.parse throwing error when any field of type time.Duration set in config file #117

@reaandrew

Description

@reaandrew

Here is a failing test which shows the problem:

func TestSerialisation(t *testing.T) {
    Convey("Marhsal Duration", t, func() {
        input := "duration: 5m"
        c := &Configuration{}
        c.parse([]byte(input))

        So(c.Duration, ShouldEqual, time.Duration(5*time.Minute))
    })
}

The error which was swallowed was:

WARN[0000] Unable to parse config file
error unmarshaling JSON: json: cannot unmarshal string into Go value of type time.Duration

The reason why this might have gone under the radar was due to the use of for _,test := range cases which were being used in the project with Ginkgo. It is only recently been noticed that this does not work as expected and so seems to have hidden this error.

It is not the change of testing frameworks as the above test can be executed as a native go test and the result would be much the same. This could be related to the YAML package change but it still looks like this has been here due to the type time.Duration.

The Fix seems to be implementing a UnmarshalJSON on the type Configuration which is possible now we use the new library.

You can still use binary in your YAML files though - just store them without the !!binary tag and decode the base64 in your code (e.g. in the custom JSON methods MarshalJSON and UnmarshalJSON).

https://github.com/ghodss/yaml

A really good post which shows a way of avoiding duplication when dealing with "not a few fields" is here http://choly.ca/post/go-json-marshalling/

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions