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

fix: Load panics when trying to load a non primitive type field with a string inner type #84

Open
wants to merge 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions configor_test.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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