github.com/Cloud-Foundations/Dominator@v0.3.4/proto/sub/methods.go (about) 1 package sub 2 3 import ( 4 "fmt" 5 ) 6 7 const stateUnknown = "UNKNOWN State" 8 9 var ( 10 disruptionStateToText = map[DisruptionState]string{ 11 DisruptionStateAnytime: "anytime", 12 DisruptionStatePermitted: "permitted", 13 DisruptionStateRequested: "requested", 14 DisruptionStateDenied: "denied", 15 } 16 textToDisruptionState map[string]DisruptionState 17 ) 18 19 func init() { 20 textToDisruptionState = make(map[string]DisruptionState, 21 len(disruptionStateToText)) 22 for state, text := range disruptionStateToText { 23 textToDisruptionState[text] = state 24 } 25 } 26 27 func VerifyDisruptionState(state DisruptionState) bool { 28 _, ok := disruptionStateToText[state] 29 return ok 30 } 31 32 func (state DisruptionState) String() string { 33 if text, ok := disruptionStateToText[state]; ok { 34 return text 35 } else { 36 return stateUnknown 37 } 38 } 39 40 func (state DisruptionState) MarshalText() (text []byte, err error) { 41 if text, ok := disruptionStateToText[state]; ok { 42 return []byte(text), nil 43 } else { 44 return nil, fmt.Errorf("invalid DisruptionState: %d", state) 45 } 46 } 47 48 func (state *DisruptionState) UnmarshalText(text []byte) error { 49 txt := string(text) 50 if val, ok := textToDisruptionState[txt]; ok { 51 *state = val 52 return nil 53 } else { 54 return fmt.Errorf("unknown DisruptionState: %s", txt) 55 } 56 }