forked from hyperledger-archives/burrow
/
type.go
51 lines (40 loc) · 847 Bytes
/
type.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
package balance
type Type uint32
const (
TypeNative Type = 1
TypePower Type = 2
)
var nameFromType = map[Type]string{
TypeNative: "Native",
TypePower: "Power",
}
var typeFromName = make(map[string]Type)
func init() {
for t, n := range nameFromType {
typeFromName[n] = t
}
}
func TypeFromString(name string) Type {
return typeFromName[name]
}
func (typ Type) String() string {
name, ok := nameFromType[typ]
if ok {
return name
}
return "UnknownBalanceType"
}
func (typ Type) MarshalText() ([]byte, error) {
return []byte(typ.String()), nil
}
func (typ *Type) UnmarshalText(data []byte) error {
*typ = TypeFromString(string(data))
return nil
}
// Protobuf support
func (typ Type) Marshal() ([]byte, error) {
return typ.MarshalText()
}
func (typ *Type) Unmarshal(data []byte) error {
return typ.UnmarshalText(data)
}