chore: clear errors on conf conflict keys (#2972)

Co-authored-by: kevin.wan <kevin.wan@yijinin.com>
master
Kevin Wan 2 years ago committed by GitHub
parent 9911c11e9c
commit 7d5d7d9085
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -127,7 +127,7 @@ func MustLoad(path string, v any, opts ...Option) {
func addOrMergeFields(info *fieldInfo, key string, child *fieldInfo) error { func addOrMergeFields(info *fieldInfo, key string, child *fieldInfo) error {
if prev, ok := info.children[key]; ok { if prev, ok := info.children[key]; ok {
if child.mapField != nil { if child.mapField != nil {
return newDupKeyError(key) return newConflictKeyError(key)
} }
if err := mergeFields(prev, key, child.children); err != nil { 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 { if _, ok := info.children[lowerCaseName]; ok {
return newDupKeyError(lowerCaseName) return newConflictKeyError(lowerCaseName)
} }
info.children[lowerCaseName] = &fieldInfo{ info.children[lowerCaseName] = &fieldInfo{
@ -169,7 +169,7 @@ func buildAnonymousFieldInfo(info *fieldInfo, lowerCaseName string, ft reflect.T
} }
default: default:
if _, ok := info.children[lowerCaseName]; ok { if _, ok := info.children[lowerCaseName]; ok {
return newDupKeyError(lowerCaseName) return newConflictKeyError(lowerCaseName)
} }
info.children[lowerCaseName] = &fieldInfo{ 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 { func mergeFields(prev *fieldInfo, key string, children map[string]*fieldInfo) error {
if len(prev.children) == 0 || len(children) == 0 { if len(prev.children) == 0 || len(children) == 0 {
return newDupKeyError(key) return newConflictKeyError(key)
} }
// merge fields // merge fields
for k, v := range children { for k, v := range children {
if _, ok := prev.children[k]; ok { if _, ok := prev.children[k]; ok {
return newDupKeyError(k) return newConflictKeyError(k)
} }
prev.children[k] = v prev.children[k] = v
@ -314,14 +314,14 @@ func toLowerCaseKeyMap(m map[string]any, info *fieldInfo) map[string]any {
return res return res
} }
type dupKeyError struct { type conflictKeyError struct {
key string key string
} }
func newDupKeyError(key string) dupKeyError { func newConflictKeyError(key string) conflictKeyError {
return dupKeyError{key: key} return conflictKeyError{key: key}
} }
func (e dupKeyError) Error() string { func (e conflictKeyError) Error() string {
return fmt.Sprintf("duplicated key %s", e.key) return fmt.Sprintf("conflict key %s, pay attention to anonymous fields", e.key)
} }

@ -9,7 +9,7 @@ import (
"github.com/zeromicro/go-zero/core/hash" "github.com/zeromicro/go-zero/core/hash"
) )
var dupErr dupKeyError var dupErr conflictKeyError
func TestLoadConfig_notExists(t *testing.T) { func TestLoadConfig_notExists(t *testing.T) {
assert.NotNil(t, Load("not_a_file", nil)) assert.NotNil(t, Load("not_a_file", nil))
@ -672,7 +672,7 @@ func Test_FieldOverwrite(t *testing.T) {
input := []byte(`{"Name": "hello"}`) input := []byte(`{"Name": "hello"}`)
err := LoadFromJsonBytes(input, val) err := LoadFromJsonBytes(input, val)
assert.ErrorAs(t, err, &dupErr) assert.ErrorAs(t, err, &dupErr)
assert.Equal(t, newDupKeyError("name").Error(), err.Error()) assert.Equal(t, newConflictKeyError("name").Error(), err.Error())
} }
validate(&St1{}) validate(&St1{})
@ -715,7 +715,7 @@ func Test_FieldOverwrite(t *testing.T) {
input := []byte(`{"Name": "hello"}`) input := []byte(`{"Name": "hello"}`)
err := LoadFromJsonBytes(input, val) err := LoadFromJsonBytes(input, val)
assert.ErrorAs(t, err, &dupErr) assert.ErrorAs(t, err, &dupErr)
assert.Equal(t, newDupKeyError("name").Error(), err.Error()) assert.Equal(t, newConflictKeyError("name").Error(), err.Error())
} }
validate(&St0{}) validate(&St0{})
@ -1023,17 +1023,17 @@ func TestLoadNamedFieldOverwritten(t *testing.T) {
} }
func createTempFile(ext, text string) (string, error) { 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 { if err != nil {
return "", err 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 return "", err
} }
filename := tmpfile.Name() filename := tmpFile.Name()
if err = tmpfile.Close(); err != nil { if err = tmpFile.Close(); err != nil {
return "", err return "", err
} }

Loading…
Cancel
Save