From fb7664a764b6bb34074a1a7848f78fa53bf0dccb Mon Sep 17 00:00:00 2001 From: "kevin.wan" Date: Mon, 6 Mar 2023 20:27:20 +0800 Subject: [PATCH] fix: config map with json tag --- core/conf/config.go | 12 +++++- core/conf/config_test.go | 82 ++++++++++++++++++++++++++++++---------- 2 files changed, 73 insertions(+), 21 deletions(-) diff --git a/core/conf/config.go b/core/conf/config.go index 5917ec11..aaa69b8d 100644 --- a/core/conf/config.go +++ b/core/conf/config.go @@ -76,7 +76,7 @@ func LoadFromJsonBytes(content []byte, v any) error { } var m map[string]any - if err := jsonx.Unmarshal(content, &m); err != nil { + if err = jsonx.Unmarshal(content, &m); err != nil { return err } @@ -239,7 +239,7 @@ func buildStructFieldsInfo(tp reflect.Type) (*fieldInfo, error) { for i := 0; i < tp.NumField(); i++ { field := tp.Field(i) - name := field.Name + name := getTagName(field) lowerCaseName := toLowerCase(name) ft := mapping.Deref(field.Type) // flatten anonymous fields @@ -255,6 +255,14 @@ func buildStructFieldsInfo(tp reflect.Type) (*fieldInfo, error) { return info, nil } +func getTagName(field reflect.StructField) string { + if tag, ok := field.Tag.Lookup(jsonTagKey); ok { + return tag + } + + return field.Name +} + func mergeFields(prev *fieldInfo, key string, children map[string]*fieldInfo) error { if len(prev.children) == 0 || len(children) == 0 { return newConflictKeyError(key) diff --git a/core/conf/config_test.go b/core/conf/config_test.go index 64e9fd34..5ce32051 100644 --- a/core/conf/config_test.go +++ b/core/conf/config_test.go @@ -1022,24 +1022,6 @@ func TestLoadNamedFieldOverwritten(t *testing.T) { }) } -func createTempFile(ext, text string) (string, error) { - tmpFile, err := os.CreateTemp(os.TempDir(), hash.Md5Hex([]byte(text))+"*"+ext) - if err != nil { - return "", err - } - - if err := os.WriteFile(tmpFile.Name(), []byte(text), os.ModeTemporary); err != nil { - return "", err - } - - filename := tmpFile.Name() - if err = tmpFile.Close(); err != nil { - return "", err - } - - return filename, nil -} - func TestFillDefaultUnmarshal(t *testing.T) { t.Run("nil", func(t *testing.T) { type St struct{} @@ -1079,7 +1061,7 @@ func TestFillDefaultUnmarshal(t *testing.T) { assert.Equal(t, st.C, "c") }) - t.Run("has vaue", func(t *testing.T) { + t.Run("has value", func(t *testing.T) { type St struct { A string `json:",default=a"` B string @@ -1091,3 +1073,65 @@ func TestFillDefaultUnmarshal(t *testing.T) { assert.Error(t, err) }) } + +func TestConfigWithJsonTag(t *testing.T) { + t.Run("map with value", func(t *testing.T) { + var input = []byte(`[BannedNotificationTemplates] +[BannedNotificationTemplates.pt-BR] +EmailTemplate = "910707,2,3,4" +[BannedNotificationTemplates.ch-MY] +EmailTemplate = "910707,2,3,4"`) + + type BannedNotificationTemplates struct { + EmailTemplate string + } + + type Config struct { + BannedNotificationTemplatesMap map[string]BannedNotificationTemplates `json:"BannedNotificationTemplates"` // 各个语言的封禁模板设置, map.key=语言 + } + + var c Config + if assert.NoError(t, LoadFromTomlBytes(input, &c)) { + assert.Len(t, c.BannedNotificationTemplatesMap, 2) + } + }) + + t.Run("map with ptr value", func(t *testing.T) { + var input = []byte(`[BannedNotificationTemplates] +[BannedNotificationTemplates.pt-BR] +EmailTemplate = "910707,2,3,4" +[BannedNotificationTemplates.ch-MY] +EmailTemplate = "910707,2,3,4"`) + + type BannedNotificationTemplates struct { + EmailTemplate string + } + + type Config struct { + BannedNotificationTemplatesMap map[string]*BannedNotificationTemplates `json:"BannedNotificationTemplates"` // 各个语言的封禁模板设置, map.key=语言 + } + + var c Config + if assert.NoError(t, LoadFromTomlBytes(input, &c)) { + assert.Len(t, c.BannedNotificationTemplatesMap, 2) + } + }) +} + +func createTempFile(ext, text string) (string, error) { + tmpFile, err := os.CreateTemp(os.TempDir(), hash.Md5Hex([]byte(text))+"*"+ext) + if err != nil { + return "", err + } + + if err := os.WriteFile(tmpFile.Name(), []byte(text), os.ModeTemporary); err != nil { + return "", err + } + + filename := tmpFile.Name() + if err = tmpFile.Close(); err != nil { + return "", err + } + + return filename, nil +}