Repository: zeromicro/go-zero
Pull request #2972 (2023-03-06 04:35:49)

chore: clear errors on conf conflict keys

codecov[bot]: # [Codecov](https://codecov.io/gh/zeromicro/go-zero/pull/2972?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#2972](https://codecov.io/gh/zeromicro/go-zero/pull/2972?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (a39aba2) into [master](https://codecov.io/gh/zeromicro/go-zero/commit/9911c11e9cf99cf7676c877957fa75f4ab2b9f05?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (9911c11) will **not change** coverage.
> The diff coverage is `100.00%`.


| [Impacted Files](https://codecov.io/gh/zeromicro/go-zero/pull/2972?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [core/conf/config.go](https://codecov.io/gh/zeromicro/go-zero/pull/2972?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9jb25mL2NvbmZpZy5nbw==) | `98.52% <100.00%> (ø)` | |
| [core/syncx/spinlock.go](https://codecov.io/gh/zeromicro/go-zero/pull/2972?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9zeW5jeC9zcGlubG9jay5nbw==) | `62.50% <0.00%> (-37.50%)` | :arrow_down: |
| [core/logx/rotatelogger.go](https://codecov.io/gh/zeromicro/go-zero/pull/2972?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9sb2d4L3JvdGF0ZWxvZ2dlci5nbw==) | `72.69% <0.00%> (+1.10%)` | :arrow_up: |


Patch:
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)
 }


Test patch:
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
 	}