multiconfig icon indicating copy to clipboard operation
multiconfig copied to clipboard

Strange behavior when working with pointer field

Open zhaoyao opened this issue 10 years ago • 1 comments

Test code

package main

import "github.com/koding/multiconfig"
import "os"
import "time"

type duration struct {
    time.Duration
}

func (d *duration) UnmarshalText(text []byte) error {
    var err error
    d.Duration, err = time.ParseDuration(string(text))
    return err
}

type A struct {
    //B *duration `default:"10s"`

    // works as expected
    B *duration `toml:"b"`
}

func main() {
    a := &A{}
    multiconfig.MustLoadWithPath(os.Args[1], a)
    println(a.B.String())
}

When specify argument value through env or cli args for pointer type, will cause multiconfig compain "not support type: ptr"

zhaoyao avatar Sep 08 '15 09:09 zhaoyao

Hi @zhaoyao. Thanks for the excellent bug. I found the issue and it's due the TagLoader. However if you would trigger the EnvLoader with something like A_B=12s go run demo.go b.toml you would again get the same error.

The underlying problem is, the TOML loader uses the TOML package to unmarshal your file on top of the struct. That package has no problem with it and deals good with pointer to float64 (time.Duration type).

But we use a custom field setter (fieldSet function), and that particular function doesn't support pointer to types yet. It can be fix indeed and we are going to look at it. Thanks again for the bug report, much appreciated!

fatih avatar Sep 08 '15 15:09 fatih