forked from benthosdev/benthos
/
bloblang_numbers.go
142 lines (133 loc) · 5.24 KB
/
bloblang_numbers.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
package pure
import (
"github.com/benthosdev/benthos/v4/internal/bloblang/query"
"github.com/benthosdev/benthos/v4/public/bloblang"
)
func init() {
if err := bloblang.RegisterMethodV2("int64",
bloblang.NewPluginSpec().
Category(query.MethodCategoryNumbers).
Description(`
Converts a numerical type into a 64-bit signed integer, this is for advanced use cases where a specific data type is needed for a given component (such as the ClickHouse SQL driver).
If the value is a string then an attempt will be made to parse it as a 64-bit integer. If the target value exceeds the capacity of an integer or contains decimal values then this method will throw an error. In order to convert a floating point number containing decimals first use `+"[`.round()`](#round)"+` on the value first. Please refer to the [`+"`strconv.ParseInt`"+` documentation](https://pkg.go.dev/strconv#ParseInt) for details regarding the supported formats.`).
Example("", `
root.a = this.a.int64()
root.b = this.b.round().int64()
root.c = this.c.int64()
`,
[2]string{
`{"a":12,"b":12.34,"c":"12"}`,
`{"a":12,"b":12,"c":12}`,
},
).
Example("", `
root = this.int64()
`,
[2]string{
`"0xDEADBEEF"`,
`3735928559`,
},
),
func(args *bloblang.ParsedParams) (bloblang.Method, error) {
return func(input any) (any, error) {
return query.IToInt(input)
}, nil
}); err != nil {
panic(err)
}
if err := bloblang.RegisterMethodV2("int32",
bloblang.NewPluginSpec().
Category(query.MethodCategoryNumbers).
Description(`
Converts a numerical type into a 32-bit signed integer, this is for advanced use cases where a specific data type is needed for a given component (such as the ClickHouse SQL driver).
If the value is a string then an attempt will be made to parse it as a 32-bit integer. If the target value exceeds the capacity of an integer or contains decimal values then this method will throw an error. In order to convert a floating point number containing decimals first use `+"[`.round()`](#round)"+` on the value first. Please refer to the [`+"`strconv.ParseInt`"+` documentation](https://pkg.go.dev/strconv#ParseInt) for details regarding the supported formats.`).
Example("", `
root.a = this.a.int32()
root.b = this.b.round().int32()
root.c = this.c.int32()
`,
[2]string{
`{"a":12,"b":12.34,"c":"12"}`,
`{"a":12,"b":12,"c":12}`,
},
).
Example("", `
root = this.int32()
`,
[2]string{
`"0xB70B"`,
`46859`,
},
),
func(args *bloblang.ParsedParams) (bloblang.Method, error) {
return func(input any) (any, error) {
return query.IToInt32(input)
}, nil
}); err != nil {
panic(err)
}
if err := bloblang.RegisterMethodV2("uint64",
bloblang.NewPluginSpec().
Category(query.MethodCategoryNumbers).
Description(`
Converts a numerical type into a 64-bit unsigned integer, this is for advanced use cases where a specific data type is needed for a given component (such as the ClickHouse SQL driver).
If the value is a string then an attempt will be made to parse it as a 64-bit unsigned integer. If the target value exceeds the capacity of an integer or contains decimal values then this method will throw an error. In order to convert a floating point number containing decimals first use `+"[`.round()`](#round)"+` on the value first. Please refer to the [`+"`strconv.ParseInt`"+` documentation](https://pkg.go.dev/strconv#ParseInt) for details regarding the supported formats.`).
Example("", `
root.a = this.a.uint64()
root.b = this.b.round().uint64()
root.c = this.c.uint64()
root.d = this.d.uint64().catch(0)
`,
[2]string{
`{"a":12,"b":12.34,"c":"12","d":-12}`,
`{"a":12,"b":12,"c":12,"d":0}`,
},
).
Example("", `
root = this.uint64()
`,
[2]string{
`"0xDEADBEEF"`,
`3735928559`,
},
),
func(args *bloblang.ParsedParams) (bloblang.Method, error) {
return func(input any) (any, error) {
return query.IToUint(input)
}, nil
}); err != nil {
panic(err)
}
if err := bloblang.RegisterMethodV2("uint32",
bloblang.NewPluginSpec().
Category(query.MethodCategoryNumbers).
Description(`
Converts a numerical type into a 32-bit unsigned integer, this is for advanced use cases where a specific data type is needed for a given component (such as the ClickHouse SQL driver).
If the value is a string then an attempt will be made to parse it as a 32-bit unsigned integer. If the target value exceeds the capacity of an integer or contains decimal values then this method will throw an error. In order to convert a floating point number containing decimals first use `+"[`.round()`](#round)"+` on the value first. Please refer to the [`+"`strconv.ParseInt`"+` documentation](https://pkg.go.dev/strconv#ParseInt) for details regarding the supported formats.`).
Example("", `
root.a = this.a.uint32()
root.b = this.b.round().uint32()
root.c = this.c.uint32()
root.d = this.d.uint32().catch(0)
`,
[2]string{
`{"a":12,"b":12.34,"c":"12","d":-12}`,
`{"a":12,"b":12,"c":12,"d":0}`,
},
).
Example("", `
root = this.uint32()
`,
[2]string{
`"0xB70B"`,
`46859`,
},
),
func(args *bloblang.ParsedParams) (bloblang.Method, error) {
return func(input any) (any, error) {
return query.IToUint32(input)
}, nil
}); err != nil {
panic(err)
}
}