Skip to content

Commit 7f269c8

Browse files
authored
Merge pull request #1436 from laskoviymishka/fix-1435
feat: expose `DecimalValueFromString`
2 parents 2322fd0 + 7747a98 commit 7f269c8

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Added `table/types.DecimalValueFromString` decimal type constructor
2+
13
## v3.77.1
24
* Added log topic writer ack
35
* Replaced `operation.Client.List` to five methods for listing operations `operation.List{BuildIndex,ImportFromS3,ExportToS3,ExportToYT,ExecuteQuery}`

examples/decimal/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ func main() {
9393

9494
x := big.NewInt(42 * 1000000000)
9595
x.Mul(x, big.NewInt(2))
96+
parsedDecimal, err := types.DecimalValueFromString("42.00", 22, 9)
97+
if err != nil {
98+
panic(err)
99+
}
96100

97101
_, _, err = s.Execute(ctx, txc, render(writeQuery, templateConfig{
98102
TablePathPrefix: prefix,
@@ -103,6 +107,10 @@ func main() {
103107
types.StructFieldValue("id", types.Uint32Value(42)),
104108
types.StructFieldValue("value", types.DecimalValueFromBigInt(x, 22, 9)),
105109
),
110+
types.StructValue(
111+
types.StructFieldValue("id", types.Uint32Value(43)),
112+
types.StructFieldValue("value", parsedDecimal),
113+
),
106114
),
107115
),
108116
))

table/types/cast_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package types
22

33
import (
44
"math/big"
5+
"strconv"
56
"testing"
67

78
"github.com/stretchr/testify/require"
@@ -87,3 +88,32 @@ func TestToDecimal(t *testing.T) {
8788
})
8889
}
8990
}
91+
92+
func TestDecimalParse(t *testing.T) {
93+
for i, tt := range []struct {
94+
raw string
95+
literal string
96+
err bool
97+
}{
98+
{
99+
raw: "-1234567.890123456",
100+
literal: `Decimal("-1234567.890123456",22,9)`,
101+
err: false,
102+
},
103+
{
104+
raw: "not a decimal",
105+
literal: ``,
106+
err: true,
107+
},
108+
} {
109+
t.Run(strconv.Itoa(i)+"."+tt.raw, func(t *testing.T) {
110+
dec, err := DecimalValueFromString(tt.raw, 22, 9)
111+
if err != nil {
112+
require.True(t, tt.err)
113+
require.Nil(t, dec)
114+
} else {
115+
require.Equal(t, tt.literal, dec.Yql())
116+
}
117+
})
118+
}
119+
}

table/types/value.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@ func DecimalValueFromBigInt(v *big.Int, precision, scale uint32) Value {
189189
return value.DecimalValueFromBigInt(v, precision, scale)
190190
}
191191

192+
func DecimalValueFromString(str string, precision, scale uint32) (Value, error) {
193+
bigI, err := decimal.Parse(str, precision, scale)
194+
if err != nil {
195+
return nil, err
196+
}
197+
198+
return value.DecimalValueFromBigInt(bigI, precision, scale), nil
199+
}
200+
192201
func TupleValue(vs ...Value) Value {
193202
return value.TupleValue(vs...)
194203
}

0 commit comments

Comments
 (0)