github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/types/decimal.go (about) 1 // Copyright 2020 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package types 16 17 import ( 18 "context" 19 20 "github.com/shopspring/decimal" 21 22 "github.com/dolthub/dolt/go/store/hash" 23 ) 24 25 type Decimal decimal.Decimal 26 27 func (v Decimal) Value(ctx context.Context) (Value, error) { 28 return v, nil 29 } 30 31 func (v Decimal) Equals(other Value) bool { 32 v2, ok := other.(Decimal) 33 if !ok { 34 return false 35 } 36 37 return decimal.Decimal(v).Equal(decimal.Decimal(v2)) 38 } 39 40 func (v Decimal) Less(nbf *NomsBinFormat, other LesserValuable) (bool, error) { 41 if v2, ok := other.(Decimal); ok { 42 return decimal.Decimal(v).LessThan(decimal.Decimal(v2)), nil 43 } 44 return DecimalKind < other.Kind(), nil 45 } 46 47 func (v Decimal) Hash(nbf *NomsBinFormat) (hash.Hash, error) { 48 return getHash(v, nbf) 49 } 50 51 func (v Decimal) isPrimitive() bool { 52 return true 53 } 54 55 func (v Decimal) WalkValues(ctx context.Context, cb ValueCallback) error { 56 return nil 57 } 58 59 func (v Decimal) WalkRefs(nbf *NomsBinFormat, cb RefCallback) error { 60 return nil 61 } 62 63 func (v Decimal) typeOf() (*Type, error) { 64 return PrimitiveTypeMap[DecimalKind], nil 65 } 66 67 func (v Decimal) Kind() NomsKind { 68 return DecimalKind 69 } 70 71 func (v Decimal) valueReadWriter() ValueReadWriter { 72 return nil 73 } 74 75 func (v Decimal) writeTo(w nomsWriter, nbf *NomsBinFormat) error { 76 encodedDecimal, err := decimal.Decimal(v).GobEncode() 77 if err != nil { 78 return err 79 } 80 81 err = DecimalKind.writeTo(w, nbf) 82 if err != nil { 83 return err 84 } 85 86 w.writeUint16(uint16(len(encodedDecimal))) 87 w.writeRaw(encodedDecimal) 88 return nil 89 } 90 91 func (v Decimal) readFrom(nbf *NomsBinFormat, b *binaryNomsReader) (Value, error) { 92 dec, err := b.ReadDecimal() 93 if err != nil { 94 return nil, err 95 } 96 return Decimal(dec), nil 97 } 98 99 func (v Decimal) skip(nbf *NomsBinFormat, b *binaryNomsReader) { 100 size := uint32(b.readUint16()) 101 b.skipBytes(size) 102 } 103 104 func (v Decimal) HumanReadableString() string { 105 return decimal.Decimal(v).String() 106 }