Skip to content

Not able to parse config with nested struct #36

@douglascamata

Description

@douglascamata

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions