1

could you elaborate on how to unmarshal config with embedded struct ptr using viper?

I have a config structure like so:

type Config struct {
    ...
    TracingConfig   *HttpTracingConfig `mapstructure:"tracing"`
    ...
}

type HttpTracingConfig struct {
    *HttpReporterConfig `mapstructure:",squash" validate:"required"`
    ...
}

type HttpReporterConfig struct {
    Url *string `mapstructure:"url" validate:"required"`
}

And config file:

{
  ...
  "tracing": {
    "url": "http://zipkin-svc...",
    ...
  },
  ...
}

So I try to load config:

func Load(configPath string) (*Config, error) {
    viper.SetConfigFile(configPath)

    if err := viper.ReadInConfig(); err != nil {
        return nil, err
    }

    var config Config
    if err := viper.Unmarshal(&config); err != nil {
        return nil, err
    }

    return &config, validator.Struct(&config)
}

But got an error: * HttpReporterConfig: unsupported type for squash: ptr

And how do I fix that behaviour?

Thanks!

0 Answers0