@@ -35,9 +35,8 @@ const (
3535 maxDecimalScale uint32 = 76
3636)
3737
38- // ScaledDecimal represents a decimal value as a two's complement big-endian byte slice and a scale.
39- // NULL decimals are represented by an offset of 32.
40- type ScaledDecimal struct {
38+ // Decimal represents a decimal value as a two's complement big-endian byte slice and a scale.
39+ type Decimal struct {
4140 scale uint32
4241 unscaled [32 ]byte
4342 offset uint8
@@ -48,19 +47,20 @@ type ShopspringDecimal interface {
4847 Exponent () int32
4948}
5049
51- // NewScaledDecimal constructs a decimal from a two's complement big-endian unscaled value and a scale.
50+ // NewDecimalUnsafe constructs a decimal from a two's complement big-endian unscaled value and a scale.
5251// A nil/empty unscaled slice produces a NULL decimal.
53- func NewScaledDecimal (unscaled []byte , scale uint32 ) (ScaledDecimal , error ) {
52+ func NewDecimalUnsafe (unscaled []byte , scale uint32 ) (Decimal , error ) {
5453 if len (unscaled ) == 0 {
55- return ScaledDecimal {
54+ // NULL decimals are represented by an offset of 32.
55+ return Decimal {
5656 offset : 32 ,
5757 }, nil
5858 }
5959 normalized , offset , err := normalizeTwosComplement (unscaled )
6060 if err != nil {
61- return ScaledDecimal {}, err
61+ return Decimal {}, err
6262 }
63- return ScaledDecimal {
63+ return Decimal {
6464 scale : scale ,
6565 unscaled : normalized ,
6666 offset : offset ,
@@ -69,43 +69,43 @@ func NewScaledDecimal(unscaled []byte, scale uint32) (ScaledDecimal, error) {
6969
7070// NewDecimal constructs a decimal from an arbitrary-precision integer and a scale.
7171// Providing a nil unscaled value produces a NULL decimal.
72- func NewDecimal (unscaled * big.Int , scale uint32 ) (ScaledDecimal , error ) {
72+ func NewDecimal (unscaled * big.Int , scale uint32 ) (Decimal , error ) {
7373 if unscaled == nil {
74- return ScaledDecimal {
74+ return Decimal {
7575 offset : 32 ,
7676 }, nil
7777 }
7878 unscaledRaw , offset , err := bigIntToTwosComplement (unscaled )
7979 if err != nil {
80- return ScaledDecimal {}, err
80+ return Decimal {}, err
8181 }
82- return ScaledDecimal {
82+ return Decimal {
8383 scale : scale ,
8484 unscaled : unscaledRaw ,
8585 offset : offset ,
8686 }, nil
8787}
8888
8989// NewDecimalFromInt64 constructs a decimal from a 64-bit integer and a scale.
90- func NewDecimalFromInt64 (unscaled int64 , scale uint32 ) ScaledDecimal {
90+ func NewDecimalFromInt64 (unscaled int64 , scale uint32 ) Decimal {
9191 var be [8 ]byte
9292 binary .BigEndian .PutUint64 (be [:], uint64 (unscaled ))
9393 offset := trimTwosComplement (be [:])
9494 payload := [32 ]byte {}
9595 copy (payload [32 - (8 - offset ):], be [offset :])
96- return ScaledDecimal {
96+ return Decimal {
9797 scale : scale ,
9898 unscaled : payload ,
9999 offset : uint8 (32 - (8 - offset )),
100100 }
101101}
102102
103103// isNull reports whether the decimal represents NULL.
104- func (d ScaledDecimal ) isNull () bool {
104+ func (d Decimal ) isNull () bool {
105105 return d .offset >= 32
106106}
107107
108- func (d ScaledDecimal ) ensureValidScale () error {
108+ func (d Decimal ) ensureValidScale () error {
109109 if d .isNull () {
110110 return nil
111111 }
@@ -115,10 +115,10 @@ func (d ScaledDecimal) ensureValidScale() error {
115115 return nil
116116}
117117
118- func convertShopspringDecimal (value ShopspringDecimal ) (ScaledDecimal , error ) {
118+ func convertShopspringDecimal (value ShopspringDecimal ) (Decimal , error ) {
119119 coeff := value .Coefficient ()
120120 if coeff == nil {
121- return ScaledDecimal {
121+ return Decimal {
122122 offset : 32 ,
123123 }, nil
124124 }
0 commit comments