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 bug: skip no exported field on readStructMetadata #107

Merged
merged 2 commits into from Nov 22, 2022
Merged
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
5 changes: 4 additions & 1 deletion cleanenv.go
Expand Up @@ -333,7 +333,10 @@ func readStructMetadata(cfgRoot interface{}) ([]structMeta, error) {

// process nested structure (except of supported ones)
if fld := s.Field(idx); fld.Kind() == reflect.Struct {

//skip unexported
if !fld.CanInterface() {
continue
}
// add structure to parsing stack
if _, found := validStructs[fld.Type()]; !found {
prefix, _ := fType.Tag.Lookup(TagEnvPrefix)
Expand Down
23 changes: 23 additions & 0 deletions cleanenv_test.go
Expand Up @@ -1187,3 +1187,26 @@ func TestTimeLocation(t *testing.T) {
t.Errorf("wrong location pointers: got %p, want %p", S.Location, want)
}
}

func TestSkipUnexportedField(t *testing.T) {
conf := struct {
Database struct {
Host string `yaml:"host" env:"DB_HOST" env-description:"Database host"`
Port string `yaml:"port" env:"DB_PORT" env-description:"Database port"`
} `yaml:"database"`
server struct {
Host string `yaml:"host" env:"SRV_HOST,HOST" env-description:"Server host" env-default:"localhost"`
Port string `yaml:"port" env:"SRV_PORT,PORT" env-description:"Server port" env-default:"8080"`
} `yaml:"server"`
}{}

if err := ReadConfig("example/simple_config/config.yml", &conf); err != nil {
t.Fatal(err)
}
if conf.server.Host != "" || conf.server.Port != "" {
t.Fatal("unexpect value on unexported fields")
}
if conf.Database.Host == "" || conf.Database.Port == "" {
t.Fatal("expect value on exported fields")
}
}