-
Notifications
You must be signed in to change notification settings - Fork 245
/
decimal.go
69 lines (50 loc) · 1.35 KB
/
decimal.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
package revision
import (
"github.com/shopspring/decimal"
"github.com/authzed/spicedb/pkg/datastore"
)
type Decimal struct {
decimal.Decimal
}
var NoRevision Decimal
func NewFromDecimal(d decimal.Decimal) Decimal {
return Decimal{d}
}
func (d Decimal) Equal(rhs datastore.Revision) bool {
if rhs == datastore.NoRevision {
return false
}
rhsD := rhs.(Decimal)
return d.Decimal.Equal(rhsD.Decimal)
}
func (d Decimal) GreaterThan(rhs datastore.Revision) bool {
if rhs == datastore.NoRevision {
rhs = Decimal{decimal.Zero}
}
rhsD := rhs.(Decimal)
return d.Decimal.GreaterThan(rhsD.Decimal)
}
func (d Decimal) LessThan(rhs datastore.Revision) bool {
if rhs == datastore.NoRevision {
rhs = Decimal{decimal.Zero}
}
rhsD := rhs.(Decimal)
return d.Decimal.LessThan(rhsD.Decimal)
}
var _ datastore.Revision = Decimal{}
type DecimalDecoder struct{}
func (DecimalDecoder) RevisionFromString(s string) (datastore.Revision, error) {
parsed, err := decimal.NewFromString(s)
if err != nil {
return datastore.NoRevision, err
}
return Decimal{parsed}, nil
}
// DecimalKeyFunc is used to convert a simple Decimal to an int64 for use in maps.
func DecimalKeyFunc(r Decimal) int64 {
return r.IntPart()
}
// DecimalKeyLessThanFunc is used to compare keys created by the DecimalKeyFunc.
func DecimalKeyLessThanFunc(lhs, rhs int64) bool {
return lhs < rhs
}