Skip to content

Commit

Permalink
feat: skip unexported field on readStructMetadata
Browse files Browse the repository at this point in the history
add example test case
  • Loading branch information
saddit committed Oct 28, 2022
1 parent 3bb055c commit 0258d9d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
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
19 changes: 19 additions & 0 deletions example_test.go
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/url"
"os"
"testing"

"github.com/ilyakaznacheev/cleanenv"
)
Expand Down Expand Up @@ -271,3 +272,21 @@ func ExampleUsage() {
// THREE string
// third parameter
}

func ExampleUnexportedField(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 := cleanenv.ReadConfig("example/simple_config/config.yml", &conf); err != nil {
t.Fatal(err)
}
t.Logf("%+v", conf)
}

0 comments on commit 0258d9d

Please sign in to comment.