kelseyhightower / envconfig

Golang library for managing configuration data from environment variables
MIT License
5.01k stars 377 forks source link

envconfig does not reach nested structs #175

Closed dakyskye closed 4 years ago

dakyskye commented 4 years ago

package bot:

type Config struct {
    Token    string `required:"true" envconfig:"BOT_TOKEN"`
    GuildID  string `required:"true" split_words:"true"`
    Channels Channels
    Roles    Roles
}

type Channels struct {
    Readme          string `split_words:"true" required:"true"`
    SupportedGuilds string `split_words:"true" required:"true"`
    General         string `split_words:"true" required:"true"`
    Operators       string `split_words:"true" required:"true"`
}

type Roles struct {
    Bot      string `split_words:"true" required:"true"`
    Member   string `split_words:"true" required:"true"`
    Operator string `split_words:"true" required:"true"`
    Muted    string `split_words:"true" required:"true"`
}

package storage:

type Config struct {
    MySQLHost     string `required:"true" envconfig:"MYSQL_HOST"`
    MySQLPort     string `required:"true" envconfig:"MYSQL_PORT"`
    MySQLUser     string `required:"true" envconfig:"MYSQL_USER"`
    MySQLPassword string `required:"true" envconfig:"MYSQL_PASSWORD"`
    MySQLDatabase string `required:"true" envconfig:"MYSQL_DATABASE"`
}

package service:

type botConfig = bot.Config

type storageConfig = storage.Config

type Config struct {
    Version string
    botConfig
    storageConfig
}

package main:

func main() {
    config := new(service.Config)

    config.Version = version

    err := envconfig.Process("PISD", config)
    if err != nil {
        zap.L().Fatal("can not process environment variables", zap.Error(err))
    }

    // ...
}

Process func does not process the Config types of bot and service packages.

dakyskye commented 4 years ago

I have also tried:

type botConfig struct {
    bot.Config
}

type storageConfig struct {
    storage.Config
}

type Config struct {
    Version   string
    botConfig 
    storageConfig
}

the issue is the same - Process func does not process them.

colega commented 4 years ago

envconfig can't see those keys since they're unexported, try renaming botConfig to BotConfig and storageConfig to StorageConfig.

dakyskye commented 4 years ago

Oh that's correct :+1: