Newer
Older
package model
import (
"encoding/json"
"fmt"
"gopkg.in/yaml.v3"
)
// TokeninfoAction is an enum like type for tokeninfo actions
type TokeninfoAction int
// AllTokeninfoActions holds all defined TokenInfo strings
var AllTokeninfoActions = [...]string{"introspect", "event_history", "subtoken_tree", "list_mytokens"}
// TokeninfoActions
const (
TokeninfoActionIntrospect TokeninfoAction = iota
TokeninfoActionEventHistory
TokeninfoActionSubtokenTree
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
maxTokeninfoAction
)
// NewTokeninfoAction creates a new TokeninfoAction from the tokeninfo action string
func NewTokeninfoAction(s string) TokeninfoAction {
for i, f := range AllTokeninfoActions {
if f == s {
return TokeninfoAction(i)
}
}
return -1
}
func (a *TokeninfoAction) String() string {
if *a < 0 || int(*a) >= len(AllTokeninfoActions) {
return ""
}
return AllTokeninfoActions[*a]
}
// Valid checks that TokeninfoAction is a defined tokeninfo action
func (a *TokeninfoAction) Valid() bool {
return *a < maxTokeninfoAction && *a >= 0
}
// UnmarshalYAML implements the yaml.Unmarshaler interface
func (a *TokeninfoAction) UnmarshalYAML(value *yaml.Node) error {
s := value.Value
if s == "" {
return fmt.Errorf("empty value in unmarshal TokeninfoAction")
}
*a = NewTokeninfoAction(s)
if !a.Valid() {
return fmt.Errorf("value '%s' not valid for TokeninfoAction", s)
}
return nil
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (a *TokeninfoAction) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
*a = NewTokeninfoAction(s)
if !a.Valid() {
return fmt.Errorf("value '%s' not valid for TokeninfoAction", s)
}
return nil
}
// MarshalJSON implements the json.Marshaler interface
func (a TokeninfoAction) MarshalJSON() ([]byte, error) {
return json.Marshal(a.String())
}
// AddToSliceIfNotFound adds the TokeninfoAction to a slice s if it is not already there
func (a TokeninfoAction) AddToSliceIfNotFound(s *[]TokeninfoAction) {
for _, ss := range *s {
if ss == a {
return
}
}
*s = append(*s, a)
}