-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathvalue.go
86 lines (78 loc) · 1.92 KB
/
value.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package machine
import (
"fmt"
"reflect"
)
type Type byte
const (
TypeAccount = Type(iota + 1) // address of an account
TypeAsset // name of an asset
TypeNumber // 64bit unsigned integer
TypeString // string
TypeMonetary // [asset number]
TypePortion // rational number between 0 and 1 both inclusive
TypeAllotment // list of portions
TypeAmount // either ALL or a SPECIFIC number
TypeFunding // (asset, []{amount, account})
)
func (t Type) String() string {
switch t {
case TypeAccount:
return "account"
case TypeAsset:
return "asset"
case TypeNumber:
return "number"
case TypeString:
return "string"
case TypeMonetary:
return "monetary"
case TypePortion:
return "portion"
case TypeAllotment:
return "allotment"
case TypeAmount:
return "amount"
default:
return "invalid type"
}
}
type Value interface {
GetType() Type
}
type String string
func (String) GetType() Type { return TypeString }
func (s String) String() string {
return fmt.Sprintf("\"%v\"", string(s))
}
func ValueEquals(lhs, rhs Value) bool {
if reflect.TypeOf(lhs) != reflect.TypeOf(rhs) {
return false
}
if lhsn, ok := lhs.(*MonetaryInt); ok {
rhsn := rhs.(*MonetaryInt)
return lhsn.Equal(rhsn)
} else if lhsm, ok := lhs.(Monetary); ok {
rhsm := rhs.(Monetary)
return lhsm.Asset == rhsm.Asset && lhsm.Amount.Equal(rhsm.Amount)
} else if lhsa, ok := lhs.(Allotment); ok {
rhsa := rhs.(Allotment)
if len(lhsa) != len(rhsa) {
return false
}
for i := range lhsa {
if lhsa[i].Cmp(&rhsa[i]) != 0 {
return false
}
}
} else if lhsp, ok := lhs.(Portion); ok {
rhsp := rhs.(Portion)
return lhsp.Equals(rhsp)
} else if lhsf, ok := lhs.(Funding); ok {
rhsf := rhs.(Funding)
return lhsf.Equals(rhsf)
} else if lhs != rhs {
return false
}
return true
}