Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #151 #152

Merged
merged 1 commit into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions internal/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,13 @@ func Normalize(value interface{}) (interface{}, error) {
return nil, fmt.Errorf("invalid dtype %s", rType.Name())
}

func createRenameMap(rv reflect.Value) map[string]string {
renameMap := make(map[string]string)
func createRenameMap(rv reflect.Value, renameMap map[string]string) map[string]string {
for i := 0; i < rv.NumField(); i++ {
fieldType := rv.Type().Field(i)
if fieldType.Anonymous {
createRenameMap(rv.Field(i), renameMap)
continue
}

renameTo := fieldType.Name
renameFrom := fieldType.Name
Expand Down Expand Up @@ -205,7 +208,7 @@ func rename(fields map[string]interface{}, v interface{}) map[string]interface{}
return nil
}

renameMap := createRenameMap(rv)
renameMap := createRenameMap(rv, make(map[string]string))
m := make(map[string]interface{})
for key, value := range fields {
renamedFieldName := renameMap[key]
Expand Down
6 changes: 3 additions & 3 deletions internal/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type BaseModel struct {
ID string `clover:""`
ID string `clover:"_id" json:"cloverId"`
}

type TestStruct struct {
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestNormalize(t *testing.T) {

require.Nil(t, m["uint"]) // testing omitempty
require.Equal(t, m["IntPtr"], int64(100))
require.Equal(t, m["ID"], "UID")
require.Equal(t, m["_id"], "UID")

s1 := &TestStruct{}
err = Convert(m, s1)
Expand Down Expand Up @@ -168,4 +168,4 @@ func TestJsonTag(t *testing.T) {
require.NoError(t, err)

require.Equal(t, s, &ns)
}
}
Loading