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

Not able to parse config with nested struct #36

Open
douglascamata opened this issue Aug 2, 2019 · 2 comments
Open

Not able to parse config with nested struct #36

douglascamata opened this issue Aug 2, 2019 · 2 comments

Comments

@douglascamata
Copy link

douglascamata commented Aug 2, 2019

I'm trying to parse this YAML file:

targets:
  - description: "etcd example"
    urls: ["https://172.17.0.10:2379", "https://172.17.0.11:2379", "https://172.17.0.5:2379"]
    tls_config:
      ca_file_path: "/etc/etcd/etcd-client-ca.crt"
      cert_file_path: "/etc/etcd/etcd-client.crt"
      key_file_path: "/etc/etcd/etcd-client.key"
      insecure_skip_verify: true

Using this Code:

type Config struct {
	TargetConfig  []TargetConfig     `id:"targets"`
}

type TargetConfig struct {
	Description string
	URLs        []string  `id:"urls"`
	TLSConfig   TLSConfig `id:"tls_config"`
}

type TLSConfig struct {
	CaFilePath         string `id:"ca_file_path"`
	CertFilePath       string `id:"cert_file_path"`
	KeyFilePath        string `id:"key_file_path"`
	InsecureSkipVerify bool   `id:"insecure_skip_verify" default:"false"`
}

func loadConfig() (*Config, error) {
	var cfg Config
	err := gonfig.Load(&cfg, gonfig.Conf{
		ConfigFileVariable:  "configfile",
		FileDefaultFilename: "config.yaml",
		FileDecoder:         gonfig.DecoderYAML,
		FlagDisable:         false,
	})
	if err != nil {
		return &cfg, err
	}

	return &cfg, err
}

But no matter what I do, it doesn't work. I always get this error: error loading config vars from config file: failed to set option 'targets': failed to convert to struct: failed to set value in nested struct slice option 'tls_config': incompatible type: map[string]interface {} not convertible to endpoints.TLSConfig

@douglascamata
Copy link
Author

@stevenroose could you help with this?

@stevenroose
Copy link
Owner

Hmm, it's been over a year since I looked at this project and at Go :)

Could you perhaps produce a simple extra unit tests that doesn't work? You can PR it, so we can use it to debug/fix the problem.

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

2 participants