Skip to content

Commit 97128eb

Browse files
committed
* Added table/types.ToDecimal() converter from table/types.Value to table/types.Decimal
1 parent 8823220 commit 97128eb

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
* Added `table/types.ToDecimal()` converter from `table/types.Value` to `table/types.Decimal`
2+
13
## v3.46.1
2-
* Implemented `xcontext.With{Cancel,Timeout}` with stack record and switched all usages from standard `context.With{Cancel,Timeout}`
4+
* Implemented `internal/xcontext.With{Cancel,Timeout}` with stack record and switched all usages from standard `context.With{Cancel,Timeout}`
35

46
## v3.46.0
57
* Refactored package `log` for support typed fields in log messages

internal/value/value.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,31 @@ func DatetimeValueFromTime(t time.Time) datetimeValue {
410410
return datetimeValue(t.Unix())
411411
}
412412

413+
var _ DecimalValuer = (*decimalValue)(nil)
414+
413415
type decimalValue struct {
414416
value [16]byte
415417
innerType *DecimalType
416418
}
417419

420+
func (v *decimalValue) Value() [16]byte {
421+
return v.value
422+
}
423+
424+
func (v *decimalValue) Precision() uint32 {
425+
return v.innerType.Precision
426+
}
427+
428+
func (v *decimalValue) Scale() uint32 {
429+
return v.innerType.Scale
430+
}
431+
432+
type DecimalValuer interface {
433+
Value() [16]byte
434+
Precision() uint32
435+
Scale() uint32
436+
}
437+
418438
func (v *decimalValue) castTo(dst interface{}) error {
419439
return xerrors.WithStackTrace(fmt.Errorf("cannot cast '%+v' to '%T' destination", v, dst))
420440
}

table/types/cast.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ func CastTo(v Value, dst interface{}) error {
1818
return value.CastTo(v, dst)
1919
}
2020

21+
func ToDecimal(v Value) (*Decimal, error) {
22+
if valuer, isDecimalValuer := v.(value.DecimalValuer); isDecimalValuer {
23+
return &Decimal{
24+
Bytes: valuer.Value(),
25+
Precision: valuer.Precision(),
26+
Scale: valuer.Scale(),
27+
}, nil
28+
}
29+
return nil, xerrors.WithStackTrace(fmt.Errorf("value type '%s' is not decimal type", v.Type().Yql()))
30+
}
31+
2132
func TupleItems(v Value) ([]Value, error) {
2233
if vv, has := v.(interface {
2334
Items() []Value

0 commit comments

Comments
 (0)