有没有一种使用hcl/v2解码自定义类型的方法?我在找类似于encoding/json.Unmarshaler的东西。我尝试过实现encoding.TextUnmarshaler,但没有成功。
下面是一个用例。
type Duration struct {
time.Duration
}
func (d *Duration) UnmarshalText(data []byte) error {
d0, err := time.ParseDuration(string(data))
if err != nil {
return err
}
d.Duration = d0
return nil
}注:--我在使用v2
发布于 2020-05-07 07:11:52
https://pkg.go.dev/github.com/hashicorp/hcl/v2@v2.5.0/hclsimple?tab=doc#example-package-NativeSyntax
应该是你所需要的。
摘自文件:
type Config struct {
Foo string `hcl:"foo"`
Baz string `hcl:"baz"`
}
const exampleConfig = `
foo = "bar"
baz = "boop"
`
var config Config
err := hclsimple.Decode(
"example.hcl", []byte(exampleConfig),
nil, &config,
)
if err != nil {
log.Fatalf("Failed to load configuration: %s", err)
}
fmt.Printf("Configuration is %v\n", config)https://stackoverflow.com/questions/60515131
复制相似问题