@@ -31,49 +31,18 @@ func toStringMap(i any) map[string]any {
3131func toString (i any ) string {
3232 i = indirectToStringerOrError (i )
3333
34+ res , ok := toIntString (i )
35+ if ok {
36+ return res
37+ }
38+
3439 switch s := i .(type ) {
3540 case []byte :
3641 return string (s )
3742 case string :
3843 return s
3944 case bool :
4045 return strconv .FormatBool (s )
41- }
42-
43- switch s := i .(type ) {
44- case float64 :
45- return strconv .FormatFloat (s , 'f' , - 1 , 64 )
46- case float32 :
47- return strconv .FormatFloat (float64 (s ), 'f' , - 1 , 32 )
48- }
49-
50- switch s := i .(type ) {
51- case int :
52- return strconv .Itoa (s )
53- case int64 :
54- return strconv .FormatInt (s , 10 )
55- case int32 :
56- return strconv .Itoa (int (s ))
57- case int16 :
58- return strconv .FormatInt (int64 (s ), 10 )
59- case int8 :
60- return strconv .FormatInt (int64 (s ), 10 )
61- }
62-
63- switch s := i .(type ) {
64- case uint :
65- return strconv .FormatUint (uint64 (s ), 10 )
66- case uint64 :
67- return strconv .FormatUint (uint64 (s ), 10 )
68- case uint32 :
69- return strconv .FormatUint (uint64 (s ), 10 )
70- case uint16 :
71- return strconv .FormatUint (uint64 (s ), 10 )
72- case uint8 :
73- return strconv .FormatUint (uint64 (s ), 10 )
74- }
75-
76- switch s := i .(type ) {
7746 case template.HTML :
7847 return string (s )
7948 case template.URL :
@@ -84,9 +53,6 @@ func toString(i any) string {
8453 return string (s )
8554 case template.HTMLAttr :
8655 return string (s )
87- }
88-
89- switch s := i .(type ) {
9056 case nil :
9157 return ""
9258 case fmt.Stringer :
@@ -98,6 +64,37 @@ func toString(i any) string {
9864 }
9965}
10066
67+ func toIntString (i any ) (string , bool ) {
68+ switch s := i .(type ) {
69+ case float64 :
70+ return strconv .FormatFloat (s , 'f' , - 1 , 64 ), true
71+ case float32 :
72+ return strconv .FormatFloat (float64 (s ), 'f' , - 1 , 32 ), true
73+ case int :
74+ return strconv .Itoa (s ), true
75+ case int64 :
76+ return strconv .FormatInt (s , 10 ), true
77+ case int32 :
78+ return strconv .Itoa (int (s )), true
79+ case int16 :
80+ return strconv .FormatInt (int64 (s ), 10 ), true
81+ case int8 :
82+ return strconv .FormatInt (int64 (s ), 10 ), true
83+ case uint :
84+ return strconv .FormatUint (uint64 (s ), 10 ), true
85+ case uint64 :
86+ return strconv .FormatUint (uint64 (s ), 10 ), true
87+ case uint32 :
88+ return strconv .FormatUint (uint64 (s ), 10 ), true
89+ case uint16 :
90+ return strconv .FormatUint (uint64 (s ), 10 ), true
91+ case uint8 :
92+ return strconv .FormatUint (uint64 (s ), 10 ), true
93+ }
94+
95+ return "" , false
96+ }
97+
10198// json 转换
10299func jsonStringToObject (s string , v any ) error {
103100 data := []byte (s )
0 commit comments