/
claim_type.go
55 lines (46 loc) · 946 Bytes
/
claim_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
52
53
54
55
package types
import (
"encoding/json"
"fmt"
"strconv"
)
// ClaimType is an enum used to represent the type of claim
type ClaimType int
const (
LockText = ClaimType(iota)
BurnText
)
var ClaimTypeToString = [...]string{"lock", "burn"}
func StringToClaimType(text string) (ClaimType, error) {
switch text {
case "lock":
return LockText, nil
case "burn":
return BurnText, nil
default:
return 0, ErrInvalidClaimType
}
}
func (text ClaimType) String() string {
return ClaimTypeToString[text]
}
func (text ClaimType) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%v\"", text.String())), nil
}
func (text *ClaimType) UnmarshalJSON(b []byte) error {
var j string
err := json.Unmarshal(b, &j)
if err != nil {
return err
}
stringKey, err := strconv.Unquote(string(b))
if err != nil {
return err
}
value, err := StringToClaimType(stringKey)
if err != nil {
return err
}
*text = value
return nil
}