From 7d5d7d908542b13eb2db0afe4f5e3a28aee2ebc0 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Mon, 6 Mar 2023 12:45:17 +0800 Subject: [PATCH] chore: clear errors on conf conflict keys (#2972) Co-authored-by: kevin.wan --- core/conf/config.go | 20 ++++++++++---------- core/conf/config_test.go | 14 +++++++------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/core/conf/config.go b/core/conf/config.go index 739aaf5d..5917ec11 100644 --- a/core/conf/config.go +++ b/core/conf/config.go @@ -127,7 +127,7 @@ func MustLoad(path string, v any, opts ...Option) { func addOrMergeFields(info *fieldInfo, key string, child *fieldInfo) error { if prev, ok := info.children[key]; ok { if child.mapField != nil { - return newDupKeyError(key) + return newConflictKeyError(key) } if err := mergeFields(prev, key, child.children); err != nil { @@ -160,7 +160,7 @@ func buildAnonymousFieldInfo(info *fieldInfo, lowerCaseName string, ft reflect.T } if _, ok := info.children[lowerCaseName]; ok { - return newDupKeyError(lowerCaseName) + return newConflictKeyError(lowerCaseName) } info.children[lowerCaseName] = &fieldInfo{ @@ -169,7 +169,7 @@ func buildAnonymousFieldInfo(info *fieldInfo, lowerCaseName string, ft reflect.T } default: if _, ok := info.children[lowerCaseName]; ok { - return newDupKeyError(lowerCaseName) + return newConflictKeyError(lowerCaseName) } info.children[lowerCaseName] = &fieldInfo{ @@ -257,13 +257,13 @@ func buildStructFieldsInfo(tp reflect.Type) (*fieldInfo, error) { func mergeFields(prev *fieldInfo, key string, children map[string]*fieldInfo) error { if len(prev.children) == 0 || len(children) == 0 { - return newDupKeyError(key) + return newConflictKeyError(key) } // merge fields for k, v := range children { if _, ok := prev.children[k]; ok { - return newDupKeyError(k) + return newConflictKeyError(k) } prev.children[k] = v @@ -314,14 +314,14 @@ func toLowerCaseKeyMap(m map[string]any, info *fieldInfo) map[string]any { return res } -type dupKeyError struct { +type conflictKeyError struct { key string } -func newDupKeyError(key string) dupKeyError { - return dupKeyError{key: key} +func newConflictKeyError(key string) conflictKeyError { + return conflictKeyError{key: key} } -func (e dupKeyError) Error() string { - return fmt.Sprintf("duplicated key %s", e.key) +func (e conflictKeyError) Error() string { + return fmt.Sprintf("conflict key %s, pay attention to anonymous fields", e.key) } diff --git a/core/conf/config_test.go b/core/conf/config_test.go index 8dba474f..64e9fd34 100644 --- a/core/conf/config_test.go +++ b/core/conf/config_test.go @@ -9,7 +9,7 @@ import ( "github.com/zeromicro/go-zero/core/hash" ) -var dupErr dupKeyError +var dupErr conflictKeyError func TestLoadConfig_notExists(t *testing.T) { assert.NotNil(t, Load("not_a_file", nil)) @@ -672,7 +672,7 @@ func Test_FieldOverwrite(t *testing.T) { input := []byte(`{"Name": "hello"}`) err := LoadFromJsonBytes(input, val) assert.ErrorAs(t, err, &dupErr) - assert.Equal(t, newDupKeyError("name").Error(), err.Error()) + assert.Equal(t, newConflictKeyError("name").Error(), err.Error()) } validate(&St1{}) @@ -715,7 +715,7 @@ func Test_FieldOverwrite(t *testing.T) { input := []byte(`{"Name": "hello"}`) err := LoadFromJsonBytes(input, val) assert.ErrorAs(t, err, &dupErr) - assert.Equal(t, newDupKeyError("name").Error(), err.Error()) + assert.Equal(t, newConflictKeyError("name").Error(), err.Error()) } validate(&St0{}) @@ -1023,17 +1023,17 @@ func TestLoadNamedFieldOverwritten(t *testing.T) { } func createTempFile(ext, text string) (string, error) { - tmpfile, err := os.CreateTemp(os.TempDir(), hash.Md5Hex([]byte(text))+"*"+ext) + 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 { + if err := os.WriteFile(tmpFile.Name(), []byte(text), os.ModeTemporary); err != nil { return "", err } - filename := tmpfile.Name() - if err = tmpfile.Close(); err != nil { + filename := tmpFile.Name() + if err = tmpFile.Close(); err != nil { return "", err }