Created
March 28, 2021 07:21
-
-
Save yngvark/d272e00cdc2d2a1ec2ce00c128a1e70d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var stackStatusValues []StackStatus | |
type StackStatus string | |
//goland:noinspection GoUnusedGlobalVariable | |
var ( | |
StackStatusDeleteInProgress = newStackStatus("DELETE_IN_PROGRESS") | |
StackStatusCreateComplete = newStackStatus("CREATE_COMPLETE") | |
StackStatusDeleteFailed = newStackStatus("DELETE_FAILED") | |
StackStatusDeleteComplete = newStackStatus("DELETE_COMPLETE") | |
) | |
func newStackStatus(s string) StackStatus { | |
ss := StackStatus(s) | |
stackStatusValues = append(stackStatusValues, ss) | |
return ss | |
} | |
func (c *StackStatus) String() string { | |
return string(*c) | |
} | |
func ParseStackStatus(s string) (StackStatus, error) { | |
for _, v := range stackStatusValues { | |
if s == v.String() { | |
return StackStatus(v), nil | |
} | |
} | |
return "", fmt.Errorf("no StackStatus exists for value '%s'", s) | |
} | |
// --- | |
// DEMO | |
// --- | |
func main() { | |
myenum := StackStatusDeleteInProgress | |
fmt.Println(myenum) | |
my2 := "DELETE_FAILED" | |
ss2, err := ParseStackStatus(my2) | |
if err != nil { | |
fmt.Printf("ERR: %s\n", err) | |
} | |
fmt.Println(ss2) | |
my3 := "DELETE_FAILED2" | |
ss3 , err := ParseStackStatus(my3) | |
if err != nil { | |
fmt.Printf("ERR: %s\n", err) | |
} | |
fmt.Println(ss3) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment