Skip to content

Commit 1e46a3d

Browse files
committed
update
1 parent 8f52d81 commit 1e46a3d

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

json.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,40 @@ func ListToJSONArray(list string, unique ...bool) string {
113113
list, _ = SetJSON(result)
114114
return list
115115
}
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+
}

transform.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,71 @@
1919
package com
2020

2121
import (
22+
"bytes"
23+
"encoding/gob"
2224
"fmt"
2325
"log"
2426
"math"
2527
"strconv"
2628
"strings"
2729
)
2830

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+
2987
func Int64(i interface{}) int64 {
3088
switch v := i.(type) {
3189
case int:

0 commit comments

Comments
 (0)