File tree 2 files changed +95
-0
lines changed 2 files changed +95
-0
lines changed Original file line number Diff line number Diff line change @@ -113,3 +113,40 @@ func ListToJSONArray(list string, unique ...bool) string {
113
113
list , _ = SetJSON (result )
114
114
return list
115
115
}
116
+
117
+ func ListToJSONUintArray (list string , unique ... bool ) string {
118
+ items := strings .Split (list , `,` )
119
+ result := make ([]uint64 , 0 , len (items ))
120
+ if len (unique ) > 0 && unique [0 ] {
121
+ uniqMap := map [uint64 ]struct {}{}
122
+ for _ , item := range items {
123
+ item = strings .TrimSpace (item )
124
+ if len (item ) == 0 {
125
+ continue
126
+ }
127
+ i := Uint64 (item )
128
+ if i == 0 {
129
+ continue
130
+ }
131
+ if _ , ok := uniqMap [i ]; ok {
132
+ continue
133
+ }
134
+ result = append (result , i )
135
+ uniqMap [i ] = struct {}{}
136
+ }
137
+ list , _ = SetJSON (result )
138
+ return list
139
+ }
140
+ for _ , item := range items {
141
+ item = strings .TrimSpace (item )
142
+ if len (item ) == 0 {
143
+ continue
144
+ }
145
+ i := Uint64 (item )
146
+ if i == 0 {
147
+ continue
148
+ }
149
+ result = append (result , i )
150
+ }
151
+ return `[` + JoinNumbers (result , `,` ) + `]`
152
+ }
Original file line number Diff line number Diff line change 19
19
package com
20
20
21
21
import (
22
+ "bytes"
23
+ "encoding/gob"
22
24
"fmt"
23
25
"log"
24
26
"math"
25
27
"strconv"
26
28
"strings"
27
29
)
28
30
31
+ func AsType (typ string , val interface {}) interface {} {
32
+ switch typ {
33
+ case `string` :
34
+ return String (val )
35
+ case `bytes` , `[]byte` :
36
+ return Bytes (val )
37
+ case `bool` :
38
+ return Bool (val )
39
+ case `float64` :
40
+ return Float64 (val )
41
+ case `float32` :
42
+ return Float32 (val )
43
+ case `int8` :
44
+ return Int8 (val )
45
+ case `int16` :
46
+ return Int16 (val )
47
+ case `int` :
48
+ return Int (val )
49
+ case `int32` :
50
+ return Int32 (val )
51
+ case `int64` :
52
+ return Int64 (val )
53
+ case `uint8` :
54
+ return Uint8 (val )
55
+ case `uint16` :
56
+ return Uint16 (val )
57
+ case `uint` :
58
+ return Uint (val )
59
+ case `uint32` :
60
+ return Uint32 (val )
61
+ case `uint64` :
62
+ return Uint64 (val )
63
+ default :
64
+ return val
65
+ }
66
+ }
67
+
68
+ func Bytes (val interface {}) []byte {
69
+ switch v := val .(type ) {
70
+ case []byte :
71
+ return v
72
+ case nil :
73
+ return nil
74
+ case string :
75
+ return []byte (v )
76
+ default :
77
+ var buf bytes.Buffer
78
+ enc := gob .NewEncoder (& buf )
79
+ err := enc .Encode (val )
80
+ if err != nil {
81
+ return nil
82
+ }
83
+ return buf .Bytes ()
84
+ }
85
+ }
86
+
29
87
func Int64 (i interface {}) int64 {
30
88
switch v := i .(type ) {
31
89
case int :
You can’t perform that action at this time.
0 commit comments