-
Notifications
You must be signed in to change notification settings - Fork 0
/
tarray_encode_decode.go
87 lines (82 loc) · 2.07 KB
/
tarray_encode_decode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package gosql
import (
"bytes"
"strconv"
"strings"
"golang.org/x/exp/constraints"
)
// Number general type
type Number interface {
constraints.Integer | constraints.Float
}
// ArrayNumberDecode decodes array of type int
func ArrayNumberDecode[T Number](data any, begin, end byte) (result []T, err error) {
var arr string
switch vdata := data.(type) {
case []byte:
arr = string(vdata)
case string:
arr = vdata
case []T:
return vdata, nil
case nil:
return nil, nil
default:
return nil, ErrInvalidScan
}
if strings.EqualFold(arr, "null") {
return nil, nil
}
if arr == "{}" || arr == "[]" || len(arr) == 0 {
return []T{}, err
}
if vals := strings.Split(strings.Trim(arr, "{}[]"), ","); vals != nil {
result = make([]T, 0, len(vals))
for _, cid := range vals {
switch any(T(0)).(type) {
case int, int8, int16, int32, int64:
if v, err := strconv.ParseInt(strings.Trim(cid, "'\""), 10, 64); err == nil {
result = append(result, T(v))
} else {
return nil, err
}
case uint, uint8, uint16, uint32, uint64:
if v, err := strconv.ParseUint(strings.Trim(cid, "'\""), 10, 64); err == nil {
result = append(result, T(v))
} else {
return nil, err
}
case float32, float64:
if v, err := strconv.ParseFloat(strings.Trim(cid, "'\""), 64); err == nil {
switch sv := any(v).(type) {
case T: // NOTE: Temporary hack of type casting
result = append(result, sv)
}
} else {
return nil, err
}
}
} // end for
}
return result, err
}
// ArrayEncode encodes array of type int
func ArrayNumberEncode[T Number](begin, end byte, arr []T) *bytes.Buffer {
var buff bytes.Buffer
buff.WriteByte(begin)
for i, v := range arr {
if i > 0 {
buff.WriteByte(',')
}
switch any(v).(type) {
case int, int8, int16, int32, int64:
buff.WriteString(strconv.FormatInt(int64(v), 10))
case uint, uint8, uint16, uint32, uint64:
buff.WriteString(strconv.FormatUint(uint64(v), 10))
case float32, float64:
buff.WriteString(strconv.FormatFloat(float64(v), 'G', -1, 64))
}
}
buff.WriteByte(end)
return &buff
}