diff options
author | Michael Muré <batolettre@gmail.com> | 2022-08-18 23:34:05 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2022-08-18 23:44:06 +0200 |
commit | 5511c230b678a181cc596238bf6669428d1b1902 (patch) | |
tree | 8701efc87732439f993eb4f1d00585fc419b87ab /entities/bug/status.go | |
parent | 5ca686b59751e3c87740b84108c54fc675a074cf (diff) | |
download | git-bug-5511c230b678a181cc596238bf6669428d1b1902.tar.gz |
move {bug,identity} to /entities, move input to /commands
Diffstat (limited to 'entities/bug/status.go')
-rw-r--r-- | entities/bug/status.go | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/entities/bug/status.go b/entities/bug/status.go new file mode 100644 index 00000000..b8fba609 --- /dev/null +++ b/entities/bug/status.go @@ -0,0 +1,86 @@ +package bug + +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 +} |