diff options
author | Steve Moyer <smoyer1@selesy.com> | 2022-08-23 10:33:19 -0400 |
---|---|---|
committer | Steve Moyer <smoyer1@selesy.com> | 2022-08-23 10:33:19 -0400 |
commit | 7ba98bfadc2c4c7fd3e777074976b78d6171b163 (patch) | |
tree | 4865a84837a016e9a18b566046a2aa7a1992acc7 /entities/common/status.go | |
parent | 2c2c449187557384fd1e5b9333e808451be86041 (diff) | |
parent | 5a70e8b3a2e0fe3d1a1dcd4c24bb6bf64633cb7f (diff) | |
download | git-bug-7ba98bfadc2c4c7fd3e777074976b78d6171b163.tar.gz |
Merge branch 'master' into fix-850-ineffective-comment-edit
Diffstat (limited to 'entities/common/status.go')
-rw-r--r-- | entities/common/status.go | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/entities/common/status.go b/entities/common/status.go new file mode 100644 index 00000000..6859891a --- /dev/null +++ b/entities/common/status.go @@ -0,0 +1,86 @@ +package common + +import ( + "fmt" + "io" + "strconv" + "strings" +) + +type Status int + +const ( + _ Status = iota + OpenStatus + ClosedStatus +) + +func (s Status) String() string { + switch s { + case OpenStatus: + return "open" + case ClosedStatus: + return "closed" + default: + return "unknown status" + } +} + +func (s Status) Action() string { + switch s { + case OpenStatus: + return "opened" + case ClosedStatus: + return "closed" + default: + return "unknown status" + } +} + +func StatusFromString(str string) (Status, error) { + cleaned := strings.ToLower(strings.TrimSpace(str)) + + switch cleaned { + case "open": + return OpenStatus, nil + case "closed": + return ClosedStatus, nil + default: + return 0, fmt.Errorf("unknown status") + } +} + +func (s Status) Validate() error { + if s != OpenStatus && s != ClosedStatus { + return fmt.Errorf("invalid") + } + + return nil +} + +func (s Status) MarshalGQL(w io.Writer) { + switch s { + case OpenStatus: + _, _ = fmt.Fprintf(w, strconv.Quote("OPEN")) + case ClosedStatus: + _, _ = fmt.Fprintf(w, strconv.Quote("CLOSED")) + default: + panic("missing case") + } +} + +func (s *Status) UnmarshalGQL(v interface{}) error { + str, ok := v.(string) + if !ok { + return fmt.Errorf("enums must be strings") + } + switch str { + case "OPEN": + *s = OpenStatus + case "CLOSED": + *s = ClosedStatus + default: + return fmt.Errorf("%s is not a valid Status", str) + } + return nil +} |