Skip to content

Commit 173df62

Browse files
committed
refactor:add InterfaceToString
1 parent 2be365a commit 173df62

File tree

2 files changed

+70
-11
lines changed

2 files changed

+70
-11
lines changed

common/util.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ package common
88
import (
99
"crypto/md5"
1010
"encoding/hex"
11+
"encoding/json"
1112
"fmt"
1213
"log"
1314
"math/rand"
1415
"os"
1516
"path"
1617
"path/filepath"
1718
"runtime"
19+
"strconv"
1820
"strings"
1921
"time"
2022
)
@@ -132,3 +134,67 @@ func CompareSlice(first []string, second []string) (add []string, incre []string
132134

133135
return
134136
}
137+
138+
//interface 转字符串
139+
func InterfaceToString(value interface{}) (s string) {
140+
var key string
141+
if value == nil {
142+
return key
143+
}
144+
145+
switch value.(type) {
146+
147+
case float64:
148+
ft := value.(float64)
149+
key = strconv.FormatFloat(ft, 'f', -1, 64)
150+
case float32:
151+
ft := value.(float32)
152+
key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
153+
case int:
154+
it := value.(int)
155+
key = strconv.Itoa(it)
156+
case uint:
157+
it := value.(uint)
158+
key = strconv.Itoa(int(it))
159+
case int8:
160+
it := value.(int8)
161+
key = strconv.Itoa(int(it))
162+
case uint8:
163+
it := value.(uint8)
164+
key = strconv.Itoa(int(it))
165+
case int16:
166+
it := value.(int16)
167+
key = strconv.Itoa(int(it))
168+
case uint16:
169+
it := value.(uint16)
170+
key = strconv.Itoa(int(it))
171+
case int32:
172+
it := value.(int32)
173+
key = strconv.Itoa(int(it))
174+
case uint32:
175+
it := value.(uint32)
176+
key = strconv.Itoa(int(it))
177+
case int64:
178+
it := value.(int64)
179+
key = strconv.FormatInt(it, 10)
180+
case uint64:
181+
it := value.(uint64)
182+
key = strconv.FormatUint(it, 10)
183+
case string:
184+
key = value.(string)
185+
case bool:
186+
val, _ := value.(bool)
187+
if val {
188+
key = "True"
189+
} else {
190+
key = "False"
191+
}
192+
case []byte:
193+
key = string(value.([]byte))
194+
default:
195+
newValue, _ := json.Marshal(value)
196+
key = string(newValue)
197+
}
198+
199+
return key
200+
}

controllers/ws_controller.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8+
"grm/common"
89
"grm/global"
910
"grm/service"
1011
"log"
@@ -163,20 +164,12 @@ func (con wsController) Ws(c *gin.Context) {
163164

164165
var resultPut string
165166
switch val := result.(type) {
166-
case string:
167-
resultPut = val
168-
case int:
169-
resultPut = strconv.Itoa(val)
170167
case []interface{}:
171168
for _, v := range val {
172-
resultPut += fmt.Sprintf("%s \r\n", v.(string))
173-
}
174-
case bool:
175-
if val {
176-
resultPut = "True"
177-
} else {
178-
resultPut = "False"
169+
resultPut += fmt.Sprintf("%s \r\n", common.InterfaceToString(v))
179170
}
171+
default:
172+
resultPut = common.InterfaceToString(val)
180173
}
181174

182175
err = ws.WriteMessage(mt, ReturnResp(resultPut, 1, uint8(cmd.Db)))

0 commit comments

Comments
 (0)