github.com/sandwich-go/boost@v1.3.29/xmap/internal/template2/walk_template.go (about) 1 package template2 2 3 import "sort" 4 5 type WalkConfig struct { 6 Key, Value, Sort string 7 } 8 9 func GetWalkTPLArgs() interface{} { 10 var walkConfigs = make([]WalkConfig, 0) 11 var types = []string{ 12 "int", "int8", "int16", "int32", "int64", 13 "uint", "uint8", "uint16", "uint32", "uint64", 14 "float32", "float64", 15 "string", 16 } 17 var sorts = map[string]string{ 18 "int": "sort.Ints", 19 "string": "sort.Strings", 20 } 21 for _, tk := range types { 22 for _, tv := range append(types, "interface{}") { 23 walkConfigs = append(walkConfigs, WalkConfig{ 24 Key: tk, 25 Value: tv, 26 Sort: sorts[tk], 27 }) 28 } 29 } 30 sort.Slice(walkConfigs, func(i, j int) bool { 31 if walkConfigs[i].Key == walkConfigs[j].Key { 32 return walkConfigs[i].Value < walkConfigs[j].Value 33 } 34 return walkConfigs[i].Key < walkConfigs[j].Key 35 }) 36 return map[string]interface{}{"WalkConfigs": walkConfigs} 37 } 38 39 const WalkTestTPL = `// Code generated by tools. DO NOT EDIT. 40 package xmap 41 42 import ( 43 . "github.com/smartystreets/goconvey/convey" 44 "sort" 45 "strconv" 46 "testing" 47 ) 48 49 func TestWalkMapDeterministic(t *testing.T) { 50 var n = 100 51 {{- range $walkConfig := .WalkConfigs }} 52 {{- $camelCaseKey := $walkConfig.Key | CamelCase }} 53 {{- $camelCaseValue := $walkConfig.Value | CamelCase | trimSuffix "{}" }} 54 {{- $walkMapName := print "Walk" $camelCaseKey $camelCaseValue "MapDeterministic" }} 55 Convey("{{ $walkMapName }}", t, func() { 56 var keys = make([]{{ $walkConfig.Key }}, 0, n) 57 var value {{ $walkConfig.Value }} 58 var tm = make(map[{{ $walkConfig.Key }}]{{ $walkConfig.Value }}) 59 for i := 0; i {{ "<" | Unescaped }} n; i++ { 60 var key {{ $walkConfig.Key }} 61 {{- if eq $walkConfig.Key "string" }} 62 key = strconv.FormatInt(int64(i), 10) 63 {{- else if eq $walkConfig.Key "int" }} 64 key = i 65 {{- else }} 66 key = {{ $walkConfig.Key }}(i) 67 {{- end }} 68 tm[key] = value 69 keys = append(keys, key) 70 } 71 {{- if eq $walkConfig.Sort "" }} 72 sort.Slice(keys, func(i, j int) bool { return keys[i] {{ "<" | Unescaped }} keys[j] }) 73 {{- else }} 74 {{ $walkConfig.Sort }}(keys) 75 {{- end }} 76 77 var dest = make([]{{ $walkConfig.Key }}, 0, n) 78 {{ $walkMapName }}(tm, func(k {{ $walkConfig.Key }}, v {{ $walkConfig.Value }}) bool { 79 dest = append(dest, k) 80 return true 81 }) 82 83 for k, v := range keys { 84 So(v, ShouldEqual, dest[k]) 85 } 86 }) 87 {{ end }}} 88 ` 89 90 const WalkTPL = `// Code generated by tools. DO NOT EDIT. 91 package xmap 92 93 import "sort" 94 95 var _ sort.Interface 96 97 {{- range $walkConfig := .WalkConfigs }} 98 {{- $camelCaseKey := $walkConfig.Key | CamelCase }} 99 {{- $camelCaseValue := $walkConfig.Value | CamelCase | trimSuffix "{}" }} 100 {{- $walkMapName := print "Walk" $camelCaseKey $camelCaseValue "MapDeterministic" }} 101 // {{ $walkMapName }} 有序遍历map 102 // walkFunc 函数返回 false,停止遍历 103 func {{ $walkMapName }}(in map[{{ $walkConfig.Key }}]{{ $walkConfig.Value }}, walkFunc func(k {{ $walkConfig.Key }}, v {{ $walkConfig.Value }}) bool) { 104 var keys = make([]{{ $walkConfig.Key }}, 0, len(in)) 105 for k := range in { 106 keys = append(keys, k) 107 } 108 {{- if eq $walkConfig.Sort "" }} 109 sort.Slice(keys, func(i, j int) bool { return keys[i] {{ "<" | Unescaped }} keys[j] }) 110 {{- else }} 111 {{ $walkConfig.Sort }}(keys) 112 {{- end }} 113 for _, k := range keys { 114 if walkFunc(k, in[k]) { 115 continue 116 } 117 break 118 } 119 } 120 {{ end }} 121 `