mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-17 11:12:28 +08:00
fix: json tag omitempty convert error (#218)
This commit is contained in:
@@ -6,9 +6,11 @@ package maputil
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"golang.org/x/exp/constraints"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/exp/constraints"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/slice"
|
"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++ {
|
for i := 0; i < s.NumField(); i++ {
|
||||||
field := s.Field(i)
|
field := s.Field(i)
|
||||||
tag := field.Tag
|
tag := field.Tag
|
||||||
name := tag.Get("json")
|
name, _, _ := strings.Cut(tag.Get("json"), ",")
|
||||||
|
|
||||||
if name == jsonTag {
|
if name == jsonTag {
|
||||||
return field.Name
|
return field.Name
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -485,7 +485,7 @@ func TestMapToStruct(t *testing.T) {
|
|||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Age int `json:"age"`
|
Age int `json:"age"`
|
||||||
Phone string `json:"phone"`
|
Phone string `json:"phone"`
|
||||||
Addr *Address `json:"address"`
|
Addr *Address `json:"address,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
Address struct {
|
Address struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user