From 0258d9dc08f88a4b9ee4f8ed72647991430bb03e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?ShiJh=E2=AD=90?= Date: Fri, 28 Oct 2022 06:47:24 +0000 Subject: [PATCH 1/2] feat: skip unexported field on readStructMetadata add example test case --- cleanenv.go | 5 ++++- example_test.go | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/cleanenv.go b/cleanenv.go index 5dd5e7b..a64dcaf 100644 --- a/cleanenv.go +++ b/cleanenv.go @@ -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) diff --git a/example_test.go b/example_test.go index 107854b..8247f2a 100644 --- a/example_test.go +++ b/example_test.go @@ -5,6 +5,7 @@ import ( "fmt" "net/url" "os" + "testing" "github.com/ilyakaznacheev/cleanenv" ) @@ -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) +} \ No newline at end of file From cae814fc5aa5ed896a5b623c7e8596b316966059 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?ShiJh=E2=AD=90?= Date: Wed, 9 Nov 2022 02:24:07 +0000 Subject: [PATCH 2/2] fix: refactor test-case --- cleanenv_test.go | 23 +++++++++++++++++++++++ example_test.go | 19 ------------------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/cleanenv_test.go b/cleanenv_test.go index b83cce0..d51490c 100644 --- a/cleanenv_test.go +++ b/cleanenv_test.go @@ -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") + } +} diff --git a/example_test.go b/example_test.go index 8247f2a..107854b 100644 --- a/example_test.go +++ b/example_test.go @@ -5,7 +5,6 @@ import ( "fmt" "net/url" "os" - "testing" "github.com/ilyakaznacheev/cleanenv" ) @@ -272,21 +271,3 @@ 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) -} \ No newline at end of file