Skip to content

Commit

Permalink
fix: json tag omitempty convert error (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
ch3nnn authored May 14, 2024
1 parent 53fa210 commit f0235c4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 4 additions & 3 deletions maputil/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ package maputil

import (
"fmt"
"golang.org/x/exp/constraints"
"reflect"
"sort"
"strings"

"golang.org/x/exp/constraints"

"github.com/duke-git/lancet/v2/slice"
)
Expand Down Expand Up @@ -377,8 +379,7 @@ func getFieldNameByJsonTag(structObj any, jsonTag string) string {
for i := 0; i < s.NumField(); i++ {
field := s.Field(i)
tag := field.Tag
name := tag.Get("json")

name, _, _ := strings.Cut(tag.Get("json"), ",")
if name == jsonTag {
return field.Name
}
Expand Down
2 changes: 1 addition & 1 deletion maputil/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ func TestMapToStruct(t *testing.T) {
Name string `json:"name"`
Age int `json:"age"`
Phone string `json:"phone"`
Addr *Address `json:"address"`
Addr *Address `json:"address,omitempty"`
}

Address struct {
Expand Down

0 comments on commit f0235c4

Please sign in to comment.