github.com/infraboard/keyauth@v0.8.1/apps/token/token.pb_enum_generate.go (about) 1 // Code generated by github.com/infraboard/mcube 2 // DO NOT EDIT 3 4 package token 5 6 import ( 7 "bytes" 8 "fmt" 9 "strings" 10 ) 11 12 // ParseGrantTypeFromString Parse GrantType from string 13 func ParseGrantTypeFromString(str string) (GrantType, error) { 14 key := strings.Trim(string(str), `"`) 15 v, ok := GrantType_value[strings.ToUpper(key)] 16 if !ok { 17 return 0, fmt.Errorf("unknown GrantType: %s", str) 18 } 19 20 return GrantType(v), nil 21 } 22 23 // Equal type compare 24 func (t GrantType) Equal(target GrantType) bool { 25 return t == target 26 } 27 28 // IsIn todo 29 func (t GrantType) IsIn(targets ...GrantType) bool { 30 for _, target := range targets { 31 if t.Equal(target) { 32 return true 33 } 34 } 35 36 return false 37 } 38 39 // MarshalJSON todo 40 func (t GrantType) MarshalJSON() ([]byte, error) { 41 b := bytes.NewBufferString(`"`) 42 b.WriteString(strings.ToUpper(t.String())) 43 b.WriteString(`"`) 44 return b.Bytes(), nil 45 } 46 47 // UnmarshalJSON todo 48 func (t *GrantType) UnmarshalJSON(b []byte) error { 49 ins, err := ParseGrantTypeFromString(string(b)) 50 if err != nil { 51 return err 52 } 53 *t = ins 54 return nil 55 } 56 57 // ParseTokenTypeFromString Parse TokenType from string 58 func ParseTokenTypeFromString(str string) (TokenType, error) { 59 key := strings.Trim(string(str), `"`) 60 v, ok := TokenType_value[strings.ToUpper(key)] 61 if !ok { 62 return 0, fmt.Errorf("unknown TokenType: %s", str) 63 } 64 65 return TokenType(v), nil 66 } 67 68 // Equal type compare 69 func (t TokenType) Equal(target TokenType) bool { 70 return t == target 71 } 72 73 // IsIn todo 74 func (t TokenType) IsIn(targets ...TokenType) bool { 75 for _, target := range targets { 76 if t.Equal(target) { 77 return true 78 } 79 } 80 81 return false 82 } 83 84 // MarshalJSON todo 85 func (t TokenType) MarshalJSON() ([]byte, error) { 86 b := bytes.NewBufferString(`"`) 87 b.WriteString(strings.ToUpper(t.String())) 88 b.WriteString(`"`) 89 return b.Bytes(), nil 90 } 91 92 // UnmarshalJSON todo 93 func (t *TokenType) UnmarshalJSON(b []byte) error { 94 ins, err := ParseTokenTypeFromString(string(b)) 95 if err != nil { 96 return err 97 } 98 *t = ins 99 return nil 100 } 101 102 // ParseBlockTypeFromString Parse BlockType from string 103 func ParseBlockTypeFromString(str string) (BlockType, error) { 104 key := strings.Trim(string(str), `"`) 105 v, ok := BlockType_value[strings.ToUpper(key)] 106 if !ok { 107 return 0, fmt.Errorf("unknown BlockType: %s", str) 108 } 109 110 return BlockType(v), nil 111 } 112 113 // Equal type compare 114 func (t BlockType) Equal(target BlockType) bool { 115 return t == target 116 } 117 118 // IsIn todo 119 func (t BlockType) IsIn(targets ...BlockType) bool { 120 for _, target := range targets { 121 if t.Equal(target) { 122 return true 123 } 124 } 125 126 return false 127 } 128 129 // MarshalJSON todo 130 func (t BlockType) MarshalJSON() ([]byte, error) { 131 b := bytes.NewBufferString(`"`) 132 b.WriteString(strings.ToUpper(t.String())) 133 b.WriteString(`"`) 134 return b.Bytes(), nil 135 } 136 137 // UnmarshalJSON todo 138 func (t *BlockType) UnmarshalJSON(b []byte) error { 139 ins, err := ParseBlockTypeFromString(string(b)) 140 if err != nil { 141 return err 142 } 143 *t = ins 144 return nil 145 }