-
Notifications
You must be signed in to change notification settings - Fork 0
/
getter_builtin.go
178 lines (165 loc) · 3.87 KB
/
getter_builtin.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package decoder
import (
"hash/crc32"
"strconv"
"time"
"github.com/koykov/byteconv"
"github.com/koykov/inspector/testobj"
"github.com/koykov/vector"
"github.com/koykov/x2bytes"
)
type atoxT int
const (
atoi atoxT = iota
atou
atof
atob
)
// Calculate common crc32 hash of given arguments.
func getterCrc32(ctx *Ctx, buf *any, args []any) (err error) {
n := len(args)
if n == 0 {
err = ErrGetterPoorArgs
return
}
ctx.BufAcc.StakeOut()
_ = args[n-1]
for i := 0; i < n; i++ {
a := args[i]
switch a.(type) {
case []byte:
ctx.BufAcc.Write(a.([]byte))
case *[]byte:
ctx.BufAcc.Write(*a.(*[]byte))
case string:
ctx.BufAcc.WriteString(a.(string))
case *string:
ctx.BufAcc.WriteString(*a.(*string))
case *vector.Node:
node := a.(*vector.Node)
if node != nil {
ctx.BufAcc.Write(node.Bytes())
}
}
}
if ctx.BufAcc.StakedLen() > 0 {
ctx.bufI = int64(crc32.ChecksumIEEE(ctx.BufAcc.StakedBytes()))
*buf = &ctx.bufI
}
return
}
// Convert string to int.
func getterAtoi(ctx *Ctx, buf *any, args []any) (err error) {
return atox(ctx, buf, args, atoi)
}
// Convert string to uint.
func getterAtou(ctx *Ctx, buf *any, args []any) (err error) {
return atox(ctx, buf, args, atou)
}
// Convert string to float.
func getterAtof(ctx *Ctx, buf *any, args []any) (err error) {
return atox(ctx, buf, args, atof)
}
// Convert string to bool.
func getterAtob(ctx *Ctx, buf *any, args []any) (err error) {
return atox(ctx, buf, args, atob)
}
func atox(ctx *Ctx, buf *any, args []any, target atoxT) (err error) {
if len(args) < 1 {
err = ErrGetterPoorArgs
return
}
var raw string
ok := true
switch args[0].(type) {
case *vector.Node:
raw = args[0].(*vector.Node).String()
case string:
raw = args[0].(string)
case *string:
raw = *args[0].(*string)
case *[]byte:
raw = byteconv.B2S(*args[0].(*[]byte))
case []byte:
raw = byteconv.B2S(args[0].([]byte))
default:
ok = false
}
if ok {
switch target {
case atoi:
if ctx.bufI, err = strconv.ParseInt(raw, 10, 64); err == nil {
*buf = &ctx.bufI
}
case atou:
if ctx.bufU, err = strconv.ParseUint(raw, 10, 64); err == nil {
*buf = &ctx.bufU
}
case atof:
if ctx.bufF, err = strconv.ParseFloat(raw, 64); err == nil {
*buf = &ctx.bufF
}
case atob:
if ctx.bufBl, err = strconv.ParseBool(raw); err == nil {
*buf = &ctx.bufBl
}
}
}
return
}
func getterItoa(ctx *Ctx, buf *any, args []any) (err error) {
if len(args) < 1 {
err = ErrGetterPoorArgs
return
}
i := ctx.reserveBB()
if node, ok := args[0].(*vector.Node); ok {
ctx.bufBB[i] = append(ctx.bufBB[i], node.ForceBytes()...)
*buf = &ctx.bufBB[i]
} else if ctx.bufBB[i], err = x2bytes.IntToBytes(ctx.bufBB[i], args[0]); err == nil {
*buf = &ctx.bufBB[i]
}
return
}
func getterUtoa(ctx *Ctx, buf *any, args []any) (err error) {
if len(args) < 1 {
err = ErrGetterPoorArgs
return
}
i := ctx.reserveBB()
if node, ok := args[0].(*vector.Node); ok {
ctx.bufBB[i] = append(ctx.bufBB[i], node.ForceBytes()...)
*buf = &ctx.bufBB[i]
} else if ctx.bufBB[i], err = x2bytes.UintToBytes(ctx.bufBB[i], args[0]); err == nil {
*buf = &ctx.bufBB[i]
}
return
}
// Example of getter callback that demonstrates how callbacks works.
func getterAppendTestHistory(_ *Ctx, buf *any, args []any) (err error) {
if len(args) < 3 {
err = ErrGetterPoorArgs
return
}
if h, ok := args[0].(*[]testobj.TestHistory); ok || args[0] == nil {
if h == nil {
h = &[]testobj.TestHistory{}
}
hr := testobj.TestHistory{DateUnix: time.Now().Unix()}
switch args[1].(type) {
case *[]byte:
hr.Cost, _ = strconv.ParseFloat(byteconv.B2S(*args[1].(*[]byte)), 64)
case *vector.Node:
hr.Cost, _ = args[1].(*vector.Node).Float()
}
switch args[2].(type) {
case *[]byte:
hr.Comment = *args[2].(*[]byte)
case *vector.Node:
hr.Comment = args[2].(*vector.Node).Bytes()
}
*h = append(*h, hr)
*buf = h
}
return
}