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

feat: add envSuffix tag to use environment variable with prefix #71

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions configor_test.go
Expand Up @@ -33,6 +33,7 @@ type testConfig struct {
Name string
Email string `required:"true"`
}
MetaInfo string `envSuffix:"META_INFO"`

Anonymous `anonymous:"true"`

Expand Down Expand Up @@ -65,6 +66,7 @@ func generateDefaultConfig() testConfig {
Email: "wosmvp@gmail.com",
},
},
MetaInfo: "defaultMetaInfo",
Anonymous: Anonymous{
Description: "This is an anonymous embedded struct whose environment variables should NOT include 'ANONYMOUS'",
},
Expand Down Expand Up @@ -562,6 +564,28 @@ func TestReadFromEnvironmentWithSpecifiedEnvName(t *testing.T) {
}
}

func TestReadFromEnvironmentWithSpecifiedEnvSuffixName(t *testing.T) {
config := generateDefaultConfig()

if bytes, err := json.Marshal(config); err == nil {
if file, err := ioutil.TempFile("/tmp", "configor"); err == nil {
defer file.Close()
defer os.Remove(file.Name())
file.Write(bytes)
var result testConfig
os.Setenv("CONFIGOR_META_INFO", "env_meta_info")
defer os.Setenv("CONFIGOR_META_INFO", "")
Load(&result, file.Name())

var defaultConfig = generateDefaultConfig()
defaultConfig.MetaInfo = "env_meta_info"
if !reflect.DeepEqual(result, defaultConfig) {
t.Errorf("result should equal to original configuration")
}
}
}
}

func TestAnonymousStruct(t *testing.T) {
config := generateDefaultConfig()

Expand Down
28 changes: 19 additions & 9 deletions utils.go
Expand Up @@ -270,22 +270,32 @@ func (configor *Configor) processTags(config interface{}, prefixes ...string) er
configType := configValue.Type()
for i := 0; i < configType.NumField(); i++ {
var (
envNames []string
fieldStruct = configType.Field(i)
field = configValue.Field(i)
envName = fieldStruct.Tag.Get("env") // read configuration from shell env
envNames []string
fieldStruct = configType.Field(i)
field = configValue.Field(i)
envName = fieldStruct.Tag.Get("env") // read configuration from shell env
envSuffixName = fieldStruct.Tag.Get("envSuffix") // read configuration from shell env
)

if !field.CanAddr() || !field.CanInterface() {
continue
}

if envName == "" {
envNames = append(envNames, strings.Join(append(prefixes, fieldStruct.Name), "_")) // Configor_DB_Name
envNames = append(envNames, strings.ToUpper(strings.Join(append(prefixes, fieldStruct.Name), "_"))) // CONFIGOR_DB_NAME
} else {
envNames = []string{envName}
if envName != "" {
envNames = append(envNames, envName)
}
if envSuffixName != "" {
envNames = append(
envNames,
strings.Join(append(prefixes, envSuffixName), "_"), // Configor_DB_Name
strings.ToUpper(strings.Join(append(prefixes, envSuffixName), "_")), // CONFIGOR_DB_NAME
)
}
envNames = append(
envNames,
strings.Join(append(prefixes, fieldStruct.Name), "_"),
strings.ToUpper(strings.Join(append(prefixes, fieldStruct.Name), "_")),
)

if configor.Config.Verbose {
fmt.Printf("Trying to load struct `%v`'s field `%v` from env %v\n", configType.Name(), fieldStruct.Name, strings.Join(envNames, ", "))
Expand Down