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

yaml:"service.name" couldn't be parsed correctly after 46dea54a68b3e94c239093186902985c7ced97b5 #99

Open
masterginger opened this issue Aug 6, 2018 · 2 comments

Comments

@masterginger
Copy link

base.yaml

service:
  name: abc

main.go

package main

import (
	"go.uber.org/config"
	"fmt"
)

func main() {
	type ServiceConfig struct {
		Name string
	}
	type cfg struct {
		Name string `yaml:"service.name"`
                Service ServiceConfig
	}

	// provider, err := config.NewYAML(config.File("base.yaml"))
        provider, err := config.NewYAMLProviderFromFiles("base.yaml")
	if err != nil {
		panic(err) // handle error
	}

	var c cfg
	if err := provider.Get("").Populate(&c); err != nil {
		panic(err) // handle error
	}

	fmt.Printf("%+v\n", c)
}

Expected result:
{Name:abc {Service:{Name:abc}}

Actual result:
{Name: {Service:{Name:abc}}

Pinned down to 46dea54 after git bisect

@masterginger
Copy link
Author

Any updates?

@twilly
Copy link
Contributor

twilly commented Nov 2, 2018

The cfg structure is asking for the scalar "service.name" and that is clearly not what is in your base.yaml. Get splits key names on periods and the underlying go-yaml/yaml does not. So if you want the underlying yaml parser to populate service mapping name scalar, then you'll need to unfold the cfg structure:

type cfg struct {
    Service struct {
        Name string
    }
}

If you prefer the period splitting behavior, then call Get(...).Populate(...) for every value you'd like to pick up.

I'd also try to avoid using go-yaml/yaml field tags if at all possible. The DSL behavior isn't prescribed in this package's documentation, nor is the leaking of go-yaml/yaml. You'd be in a tight spot if the backing parser were to be changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants