Skip to content

Commit 7fbf064

Browse files
committed
修复map 用uint int为key时转义问题
1 parent 55cfc45 commit 7fbf064

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

filter/parser.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package filter
22

33
import (
44
"encoding"
5+
"fmt"
56
"reflect"
67
)
78

@@ -133,6 +134,17 @@ func getSelectTag(scene string, pkgInfo string, i int, typeOf reflect.Type) tagI
133134
return selectTag
134135
}
135136

137+
// map的key为数值 bool 和字符串
138+
func isMapKey(t reflect.Value) string {
139+
switch t.Kind() {
140+
case reflect.String:
141+
return t.String()
142+
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
143+
return fmt.Sprintf("%v", t.Interface())
144+
default:
145+
return ""
146+
}
147+
}
136148
func parserMap(valueOf reflect.Value, t *fieldNodeTree, scene string, isSelect bool) {
137149
keys := valueOf.MapKeys()
138150
if len(keys) == 0 { //空map情况下解析为{}
@@ -152,7 +164,12 @@ func parserMap(valueOf reflect.Value, t *fieldNodeTree, scene string, isSelect b
152164
goto takeValMap
153165
}
154166
}
155-
k := keys[i].String()
167+
168+
key := isMapKey(keys[i])
169+
if key == "" {
170+
continue
171+
}
172+
k := key
156173
nodeTree := &fieldNodeTree{
157174
Key: k,
158175
ParentNode: t,

test/main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ func main() {
5555
//fmt.Println(filter.Omit("1", &list))
5656
//fmt.Println(mustJson(u))
5757

58-
for i := 0; i < 3; i++ {
59-
ExampleOmit()
60-
}
58+
TestMap()
59+
TestMap()
60+
//for i := 0; i < 3; i++ {
61+
// ExampleOmit()
62+
//}
6163
}
6264

6365
func ExampleOmit() {

test/map.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"github.com/liu-cn/json-filter/filter"
67
)
@@ -15,7 +16,26 @@ type Map struct {
1516
MPP **map[string]**string `json:"mpp,select(test)"`
1617
}
1718

19+
type IntMap struct {
20+
IntMaps map[int]string `json:"int_maps,select(IntMaps)"`
21+
UintMap map[uint]string `json:"uint_map,select(UintMap)"`
22+
StringMap map[string]string `json:"string_map,select(StringMap)"`
23+
//BoolMap map[bool]string `json:"bool_map,select(BoolMap)"`
24+
//FloatMap map[float32]string `json:"float_map,select(FloatMap)"`
25+
//ComplexMap map[complex64]string `json:"complex_map,select(ComplexMap)"`
26+
}
27+
1828
func TestMap() {
29+
//a := 1
30+
//m := map[interface{}]string{
31+
// a: "ooo",
32+
// "b": "bbb",
33+
//}
34+
//marshal, err := json.Marshal(m)
35+
//if err != nil {
36+
// panic(err)
37+
//}
38+
//fmt.Println(string(marshal))
1939

2040
str := "c++从研发到脱发"
2141
ptr := &str
@@ -28,4 +48,23 @@ func TestMap() {
2848

2949
fmt.Println("omit:", filter.Select("test", Map{M: maps, T: maps, MP: mp, MPP: mpp}))
3050
//{"m":{"test":"c++从研发到脱发"},"mp":{"test":"c++从研发到脱发"},"mpp":{"test":"c++从研发到脱发"}}
51+
52+
mmm := IntMap{
53+
IntMaps: map[int]string{-1: "一", 1: "二"},
54+
UintMap: map[uint]string{1: "一", 2: "二"},
55+
StringMap: map[string]string{"s": "s"},
56+
//FloatMap: map[float32]string{1.12: "一.12", 2.67657: "二.67657"},
57+
//BoolMap: map[bool]string{true: "true", false: "false"},
58+
//ComplexMap: map[complex64]string{complex64(1): "1", complex64(2): "2"},
59+
}
60+
marshal, err := json.Marshal(mmm)
61+
if err != nil {
62+
panic(err)
63+
}
64+
fmt.Println(string(marshal))
65+
66+
fmt.Println(filter.Select("IntMaps", mmm))
67+
fmt.Println(filter.Select("UintMap", mmm))
68+
fmt.Println(filter.Select("StringMap", mmm))
69+
3170
}

0 commit comments

Comments
 (0)