From 60101b0980d095cb92e765a018f4198ca2d1e82c Mon Sep 17 00:00:00 2001 From: ch3nnn Date: Tue, 14 May 2024 00:09:19 +0800 Subject: [PATCH] fix: json tag omitempty convert error --- maputil/map.go | 7 ++++--- maputil/map_test.go | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/maputil/map.go b/maputil/map.go index e724dba1..b8cd70af 100644 --- a/maputil/map.go +++ b/maputil/map.go @@ -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" ) @@ -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 } diff --git a/maputil/map_test.go b/maputil/map_test.go index 7d01611c..9737e36f 100644 --- a/maputil/map_test.go +++ b/maputil/map_test.go @@ -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 {