Skip to content

Commit

Permalink
fix: When trying to load a config with a field that has a non primiti…
Browse files Browse the repository at this point in the history
…ve type of inner type string from environment, Load panics, this fix adresses this issue
  • Loading branch information
riftsin committed Jan 19, 2023
1 parent f7a0fc7 commit 956ca86
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
21 changes: 21 additions & 0 deletions configor_test.go
Expand Up @@ -696,3 +696,24 @@ func TestLoad_FS(t *testing.T) {
t.Error("expected to have foo: bar in config")
}
}

func TestLoadConfigFromEnvWithFieldInnerTypeStringButRealTypeSomethingElse(t *testing.T) {
type myStringType string
type myTestConfig struct {
Name myStringType
}
var result myTestConfig
key := "MYTEST_NAME"
expected := "bob"
err := os.Setenv(key, expected)
if err != nil {
t.Fatal(err)
}
defer os.Unsetenv(key)
New(&Config{
ENVPrefix: "MYTEST",
}).Load(&result)
if string(result.Name) != expected {
t.Errorf("expected '%s' got '%s'", expected, result.Name)
}
}
6 changes: 5 additions & 1 deletion utils.go
Expand Up @@ -307,7 +307,11 @@ func (configor *Configor) processTags(config interface{}, prefixes ...string) er
field.Set(reflect.ValueOf(true))
}
case reflect.String:
field.Set(reflect.ValueOf(value))
if field.Type() == reflect.TypeOf(value) {
field.Set(reflect.ValueOf(value))
break
}
fallthrough
default:
if err := yaml.Unmarshal([]byte(value), field.Addr().Interface()); err != nil {
return err
Expand Down

0 comments on commit 956ca86

Please sign in to comment.