github.com/interconnectedcloud/qdr-operator@v0.0.0-20210826174505-576d2b33dac7/test/e2e/framework/qdrmanagement/entities/common/role.go (about) 1 package common 2 3 import ( 4 "encoding/json" 5 "strconv" 6 "strings" 7 ) 8 9 // RoleType 10 type RoleType int 11 12 const ( 13 RoleNormal RoleType = iota 14 RoleInterRouter 15 RoleRouteContainer 16 RoleEdge 17 ) 18 19 // UnmarshalJSON returns the appropriate RoleType for parsed string 20 func (a *RoleType) UnmarshalJSON(b []byte) error { 21 var s string 22 23 if len(b) == 0 { 24 return nil 25 } 26 if b[0] != '"' { 27 b = []byte(strconv.Quote(string(b))) 28 } 29 if err := json.Unmarshal(b, &s); err != nil { 30 return err 31 } 32 switch strings.ToLower(s) { 33 case "inter-router": 34 *a = RoleInterRouter 35 case "route-container": 36 *a = RoleRouteContainer 37 case "edge": 38 *a = RoleEdge 39 default: 40 *a = RoleNormal 41 } 42 return nil 43 } 44 45 // MarshalJSON returns the string representation of RoleType 46 func (a RoleType) MarshalJSON() ([]byte, error) { 47 var s string 48 switch a { 49 case RoleInterRouter: 50 s = "inter-router" 51 case RoleRouteContainer: 52 s = "route-container" 53 case RoleEdge: 54 s = "edge" 55 default: 56 s = "normal" 57 } 58 return json.Marshal(s) 59 }