/
data_convert.go
214 lines (202 loc) · 5.42 KB
/
data_convert.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package elem
import (
"encoding/binary"
"fmt"
"math"
"strconv"
"github.com/Bitspark/slang/pkg/core"
"github.com/google/uuid"
)
func numberToBinary(value float64) core.Binary {
bits := math.Float64bits(value)
bytes := make(core.Binary, 8)
binary.LittleEndian.PutUint64(bytes, bits)
return bytes
}
func numberToString(value float64) string {
return strconv.FormatFloat(value, 'f', -1, 64)
}
func stringToNumber(value string) float64 {
result, _ := strconv.ParseFloat(value, 64)
return result
}
func boolToNumber(value bool) float64 {
if value {
return 1.0
} else {
return 0.0
}
}
func binaryToNumber(value core.Binary) float64 {
bits := binary.LittleEndian.Uint64(value)
return math.Float64frombits(bits)
}
func binaryToBool(value core.Binary) bool {
return stringToBool(string(value))
}
func stringToBool(value string) bool {
result, _ := strconv.ParseBool(value)
return result
}
var dataConvertId = uuid.MustParse("d1191456-3583-4eaf-8ec1-e486c3818c60")
var dataConvertCfg = &builtinConfig{
safe: true,
blueprint: core.Blueprint{
Id: dataConvertId,
Meta: core.BlueprintMetaDef{
Name: "convert",
ShortDescription: "converts the type of a value",
Icon: "arrow-alt-right",
Tags: []string{"data"},
DocURL: "https://bitspark.de/slang/docs/operator/convert",
},
ServiceDefs: map[string]*core.ServiceDef{
core.MAIN_SERVICE: {
In: core.TypeDef{
Type: "generic",
Generic: "fromType",
},
Out: core.TypeDef{
Type: "generic",
Generic: "toType",
},
},
},
DelegateDefs: map[string]*core.DelegateDef{},
},
opFunc: func(op *core.Operator) {
in := op.Main().In()
out := op.Main().Out()
for !op.CheckStop() {
i := in.Pull()
if core.IsMarker(i) || i == nil {
out.Push(i)
continue
}
if in.Type() == out.Type() {
out.Push(i)
continue
}
switch in.Type() {
case core.TYPE_NUMBER:
value := i.(float64)
switch out.Type() {
case core.TYPE_STRING: // number -> string
out.Push(numberToString(value))
case core.TYPE_BINARY: // number -> binary
out.Push(numberToBinary(value))
case core.TYPE_BOOLEAN: // number -> bool
out.Push(value != 0.0)
default:
panic("not supported yet")
}
case core.TYPE_BOOLEAN:
value := i.(bool)
switch out.Type() {
case core.TYPE_STRING: // bool -> string
out.Push(strconv.FormatBool(value))
case core.TYPE_BINARY: // bool -> binary
out.Push(core.Binary(strconv.FormatBool(value)))
case core.TYPE_NUMBER: // bool -> number
out.Push(boolToNumber(value))
default:
panic("not supported yet")
}
case core.TYPE_STRING:
value := i.(string)
switch out.Type() {
case core.TYPE_BINARY: // string -> binary
out.Push(core.Binary(value))
case core.TYPE_NUMBER: // string -> number
out.Push(stringToNumber(value))
case core.TYPE_BOOLEAN: // string -> bool
out.Push(stringToBool(value))
default:
panic("not supported yet")
}
case core.TYPE_BINARY:
value := i.(core.Binary)
switch out.Type() {
case core.TYPE_STRING: // binary -> string
out.Push(string(value))
case core.TYPE_NUMBER: // binary -> number
out.Push(binaryToNumber(value))
case core.TYPE_BOOLEAN: // binary -> bool
out.Push(binaryToBool(value))
default:
panic("not supported yet")
}
case core.TYPE_STREAM:
value := i.([]interface{})
switch out.Type() {
case core.TYPE_STRING: // stream -> string
out.Push(fmt.Sprintf("%v", value))
case core.TYPE_BINARY: // stream -> binary
out.Push(core.Binary(fmt.Sprintf("%v", value)))
default:
panic("not supported yet")
}
case core.TYPE_PRIMITIVE:
switch out.Type() {
case core.TYPE_STRING:
switch value := i.(type) {
case float64: // number -> string
out.Push(numberToString(value))
case string: // string -> string
out.Push(value)
case bool: // bool -> string
out.Push(strconv.FormatBool(value))
case core.Binary: // binary -> string
out.Push(string(value))
default:
panic("not supported yet")
}
case core.TYPE_NUMBER:
switch value := i.(type) {
case float64: // number -> number
out.Push(value)
case string: // string -> number
out.Push(stringToNumber(value))
case bool: // bool -> number
out.Push(boolToNumber(value))
case core.Binary: // binary -> number
out.Push(binaryToNumber(value))
default:
panic("not supported yet")
}
case core.TYPE_BOOLEAN:
switch value := i.(type) {
case float64: // number -> bool
out.Push(value != 0.0)
case string: // string -> bool
out.Push(stringToBool(value))
case bool: // bool -> bool
out.Push(value)
case core.Binary: // binary -> bool
out.Push(binaryToBool(value))
default:
panic("not supported yet")
}
case core.TYPE_BINARY:
switch value := i.(type) {
case float64: // number -> binary
out.Push(numberToBinary(value))
case string: // string -> binary
out.Push(core.Binary(value))
case bool: // bool -> binary
stringBool := strconv.FormatBool(value)
out.Push(core.Binary(stringBool))
case core.Binary: // binary -> binary
out.Push(value)
default:
panic("not supported yet")
}
default:
panic("not supported yet")
}
default:
panic("not supported yet")
}
}
},
}