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

unmarshal string into time.Duration #201

Closed
l3uddz opened this issue Feb 23, 2021 · 3 comments
Closed

unmarshal string into time.Duration #201

l3uddz opened this issue Feb 23, 2021 · 3 comments

Comments

@l3uddz
Copy link

l3uddz commented Feb 23, 2021

Would it be possible to get support for unmarshalling strings into a time.Duration variable (such as 10h / 10h30m etc..)

Thanks

@franchb
Copy link

franchb commented Feb 24, 2021

The same issue:

package main

import (
	"fmt"
	"time"

	"github.com/goccy/go-yaml"
)

type Config struct {
	Batching Batching `yaml:"batching"`
}

type Batching struct {
	Timeout time.Duration `yaml:"timeout"`
	Limit   int           `yaml:"limit"`
}

func main() {
	sample := `
batching:
  timeout: 1s
  limit: 200
`
	var b Config
	if err := yaml.Unmarshal([]byte(sample), &b); err != nil {
		panic(err)
	}
	fmt.Printf("%+v", b)
}

Results in:

panic: cannot unmarshal string into Go struct field Config.Batching of type time.Duration

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

quentinmit added a commit to quentinmit/go-yaml that referenced this issue Aug 11, 2021
This uses `time.ParseDuration` and `time.Duration.String`, and is
compatible with gopkg.in/yaml's encoding.
goccy added a commit that referenced this issue Aug 25, 2021
Encode and decode time.Duration fields (#201)
@franchb
Copy link

franchb commented Feb 12, 2022

The latest version parses this case correctly, see https://play.golang.org/p/aH8Pmd4QnMe

@goccy should be this issue closed?

@goccy
Copy link
Owner

goccy commented Feb 14, 2022

@franchb Yes ! Thank you for notification !

@goccy goccy closed this as completed Feb 14, 2022
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

3 participants