From 07f03ebd0c5f4427583f7a3024c80f28f193dccb Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sat, 8 Apr 2023 14:47:57 +0800 Subject: [PATCH] fix: should not conflict on lower members (#3095) --- core/conf/config.go | 4 ++++ core/conf/config_test.go | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/core/conf/config.go b/core/conf/config.go index 7d2e76e6..c39c73bf 100644 --- a/core/conf/config.go +++ b/core/conf/config.go @@ -242,6 +242,10 @@ func buildStructFieldsInfo(tp reflect.Type) (*fieldInfo, error) { for i := 0; i < tp.NumField(); i++ { field := tp.Field(i) + if !field.IsExported() { + continue + } + name := getTagName(field) lowerCaseName := toLowerCase(name) ft := mapping.Deref(field.Type) diff --git a/core/conf/config_test.go b/core/conf/config_test.go index a0d15140..9667f93f 100644 --- a/core/conf/config_test.go +++ b/core/conf/config_test.go @@ -1040,6 +1040,24 @@ func TestLoadNamedFieldOverwritten(t *testing.T) { }) } +func TestLoadLowerMemberShouldNotConflict(t *testing.T) { + type ( + Redis struct { + db uint + } + + Config struct { + db uint + Redis + } + ) + + var c Config + assert.NoError(t, LoadFromJsonBytes([]byte(`{}`), &c)) + assert.Zero(t, c.db) + assert.Zero(t, c.Redis.db) +} + func TestFillDefaultUnmarshal(t *testing.T) { t.Run("nil", func(t *testing.T) { type St struct{}