/
type.go
58 lines (48 loc) · 1.3 KB
/
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
56
57
58
package profile
import (
"encoding/json"
"fmt"
)
// Type enumerates different types of peers
type Type int
const (
// TypePeer is a single person
TypePeer Type = iota
// TypeOrganization represents a group of people
TypeOrganization
)
// String implements the Stringer interface for Type
func (t Type) String() string {
switch t {
case TypePeer:
return "peer"
case TypeOrganization:
return "organization"
}
return "unknown"
}
// ParseType decodes a peer type from a string
func ParseType(t string) (Type, error) {
got, ok := map[string]Type{"": TypePeer, "user": TypePeer, "peer": TypePeer, "organization": TypeOrganization}[t]
if !ok {
return TypePeer, fmt.Errorf("invalid Type %q", t)
}
return got, nil
}
// MarshalJSON implements the json.Marshaler interface for Type
func (t Type) MarshalJSON() ([]byte, error) {
s, ok := map[Type]string{TypePeer: "peer", TypeOrganization: "organization"}[t]
if !ok {
return nil, fmt.Errorf("invalid Type %d", t)
}
return []byte(fmt.Sprintf(`"%s"`, s)), nil
}
// UnmarshalJSON implements the json.Unmarshaler interface for Type
func (t *Type) UnmarshalJSON(data []byte) (err error) {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return fmt.Errorf("Peer type should be a string, got %s", data)
}
*t, err = ParseType(s)
return err
}