gonfig icon indicating copy to clipboard operation
gonfig copied to clipboard

Not able to parse config with nested struct

Open douglascamata opened this issue 6 years ago • 2 comments

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 avatar Aug 02 '19 17:08 douglascamata

@stevenroose could you help with this?

douglascamata avatar Aug 13 '19 09:08 douglascamata

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.

stevenroose avatar Aug 14 '19 16:08 stevenroose