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

[BUG] config.Scan does not scan duration types for yaml #1857

Closed
belljustin opened this issue Jul 18, 2020 · 4 comments
Closed

[BUG] config.Scan does not scan duration types for yaml #1857

belljustin opened this issue Jul 18, 2020 · 4 comments

Comments

@belljustin
Copy link

config.Scan does not scan duration types for yaml.

  1. What are you trying to do?
    I have a struct that contains a time.Duration. I am trying to use config to Scan a yaml file into this struct.
  2. What did you expect to happen?
    I expect when using a value such as "1000s" it will create will fill the struct with a 1000 * time.Second. If I use config.Get("duration").Duration(0), I get my expected result.
  3. What happens instead?
    When using config.Scan, I get the zero value.

How to reproduce the bug:

Yaml:

duration: 1000s

Go:

package main

import (
	"fmt"
	"time"

	"github.com/micro/go-micro/v2/config"
)

type Config struct {
	Duration time.Duration
}

func main() {
	config.LoadFile("config.yaml")
	fmt.Println(config.Get("duration").Duration(0))

	var conf Config
	config.Scan(&conf)
	fmt.Println(conf)
}

Output:

16m40s
{Duration: 0s}

Environment:
go version go1.14 darwin/amd64

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/jbell/Library/Caches/go-build"
GOENV="/Users/jbell/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/jbell/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/jbell/.go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/jbell/.go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/jbell/go/src/github.com/belljustin/scratch/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/9f/w1d7k0d97mz1y7fc9cgpl8100000gp/T/go-build458731707=/tmp/go-build -gno-record-gcc-switches -fno-common"
@belljustin belljustin changed the title [BUG] [BUG] config.Scan does not scan duration types for yaml Jul 18, 2020
@kaisawind
Copy link

kaisawind commented Jul 26, 2020

#1749 json: cannot unmarshal string into Go struct field Config of type time.Duration.

@ShaileshSurya
Copy link

Is this bug still open? I am a first-time contributor can I take this?

@kesslerdev
Copy link

It's works with

package config

import (
	"time"

	str2duration "github.com/xhit/go-str2duration"
)

type Duration time.Duration

type Config struct {
	Loop  Duration `json:"loop"`
}

// UnmarshalText implements the text unmarshaller method
func (x *Duration) UnmarshalText(text []byte) error {
	name := string(text)
	tmp, err := str2duration.Str2Duration(name)
	if err != nil {
		return err
	}
	*x = Duration(tmp)
	return nil
}

@belljustin
Copy link
Author

This is the same issue as the one linked by @kaisawind . Closing this in favour of that one.

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

4 participants