-
Notifications
You must be signed in to change notification settings - Fork 1
/
pk_mode.go
50 lines (42 loc) · 870 Bytes
/
pk_mode.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
package specs
import (
"bytes"
"encoding/json"
"fmt"
)
type PKMode int
const (
PKModeDefaultKeys PKMode = iota
PKModeCQID
)
var (
pkModeStrings = []string{"default", "cq-id-only"}
)
func (m PKMode) String() string {
return pkModeStrings[m]
}
func (m PKMode) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBufferString(`"`)
buffer.WriteString(m.String())
buffer.WriteString(`"`)
return buffer.Bytes(), nil
}
func (m *PKMode) UnmarshalJSON(data []byte) (err error) {
var pkMode string
if err := json.Unmarshal(data, &pkMode); err != nil {
return err
}
if *m, err = PKModeFromString(pkMode); err != nil {
return err
}
return nil
}
func PKModeFromString(s string) (PKMode, error) {
switch s {
case "default":
return PKModeDefaultKeys, nil
case "cq-id-only":
return PKModeCQID, nil
}
return 0, fmt.Errorf("invalid pk mode: %s", s)
}