Skip to content

Commit

Permalink
修复map 用uint int为key时转义问题
Browse files Browse the repository at this point in the history
  • Loading branch information
liu-cn committed Aug 16, 2023
1 parent 55cfc45 commit 7fbf064
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
19 changes: 18 additions & 1 deletion filter/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package filter

import (
"encoding"
"fmt"
"reflect"
)

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

// map的key为数值 bool 和字符串
func isMapKey(t reflect.Value) string {
switch t.Kind() {
case reflect.String:
return t.String()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return fmt.Sprintf("%v", t.Interface())
default:
return ""
}
}
func parserMap(valueOf reflect.Value, t *fieldNodeTree, scene string, isSelect bool) {
keys := valueOf.MapKeys()
if len(keys) == 0 { //空map情况下解析为{}
Expand All @@ -152,7 +164,12 @@ func parserMap(valueOf reflect.Value, t *fieldNodeTree, scene string, isSelect b
goto takeValMap
}
}
k := keys[i].String()

key := isMapKey(keys[i])
if key == "" {
continue
}
k := key
nodeTree := &fieldNodeTree{
Key: k,
ParentNode: t,
Expand Down
8 changes: 5 additions & 3 deletions test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ func main() {
//fmt.Println(filter.Omit("1", &list))
//fmt.Println(mustJson(u))

for i := 0; i < 3; i++ {
ExampleOmit()
}
TestMap()
TestMap()
//for i := 0; i < 3; i++ {
// ExampleOmit()
//}
}

func ExampleOmit() {
Expand Down
39 changes: 39 additions & 0 deletions test/map.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"fmt"
"github.com/liu-cn/json-filter/filter"
)
Expand All @@ -15,7 +16,26 @@ type Map struct {
MPP **map[string]**string `json:"mpp,select(test)"`
}

type IntMap struct {
IntMaps map[int]string `json:"int_maps,select(IntMaps)"`
UintMap map[uint]string `json:"uint_map,select(UintMap)"`
StringMap map[string]string `json:"string_map,select(StringMap)"`
//BoolMap map[bool]string `json:"bool_map,select(BoolMap)"`
//FloatMap map[float32]string `json:"float_map,select(FloatMap)"`
//ComplexMap map[complex64]string `json:"complex_map,select(ComplexMap)"`
}

func TestMap() {
//a := 1
//m := map[interface{}]string{
// a: "ooo",
// "b": "bbb",
//}
//marshal, err := json.Marshal(m)
//if err != nil {
// panic(err)
//}
//fmt.Println(string(marshal))

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

fmt.Println("omit:", filter.Select("test", Map{M: maps, T: maps, MP: mp, MPP: mpp}))
//{"m":{"test":"c++从研发到脱发"},"mp":{"test":"c++从研发到脱发"},"mpp":{"test":"c++从研发到脱发"}}

mmm := IntMap{
IntMaps: map[int]string{-1: "一", 1: "二"},
UintMap: map[uint]string{1: "一", 2: "二"},
StringMap: map[string]string{"s": "s"},
//FloatMap: map[float32]string{1.12: "一.12", 2.67657: "二.67657"},
//BoolMap: map[bool]string{true: "true", false: "false"},
//ComplexMap: map[complex64]string{complex64(1): "1", complex64(2): "2"},
}
marshal, err := json.Marshal(mmm)
if err != nil {
panic(err)
}
fmt.Println(string(marshal))

fmt.Println(filter.Select("IntMaps", mmm))
fmt.Println(filter.Select("UintMap", mmm))
fmt.Println(filter.Select("StringMap", mmm))

}

0 comments on commit 7fbf064

Please sign in to comment.