|
| 1 | +// Copyright 2024 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package osv |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | +) |
| 11 | + |
| 12 | +type ReviewStatus int |
| 13 | + |
| 14 | +const ( |
| 15 | + ReviewStatusUnknown ReviewStatus = iota |
| 16 | + ReviewStatusUnreviewed |
| 17 | + ReviewStatusReviewed |
| 18 | +) |
| 19 | + |
| 20 | +var statusStrs = []string{ |
| 21 | + ReviewStatusUnknown: "", |
| 22 | + ReviewStatusUnreviewed: "UNREVIEWED", |
| 23 | + ReviewStatusReviewed: "REVIEWED", |
| 24 | +} |
| 25 | + |
| 26 | +func (r ReviewStatus) String() string { |
| 27 | + if !r.IsValid() { |
| 28 | + return fmt.Sprintf("INVALID(%d)", r) |
| 29 | + } |
| 30 | + return statusStrs[r] |
| 31 | +} |
| 32 | + |
| 33 | +func ReviewStatusValues() []string { |
| 34 | + return statusStrs[1:] |
| 35 | +} |
| 36 | + |
| 37 | +func (r ReviewStatus) IsValid() bool { |
| 38 | + return int(r) >= 0 && int(r) < len(statusStrs) |
| 39 | +} |
| 40 | + |
| 41 | +func ToReviewStatus(s string) (ReviewStatus, bool) { |
| 42 | + for stat, str := range statusStrs { |
| 43 | + if s == str { |
| 44 | + return ReviewStatus(stat), true |
| 45 | + } |
| 46 | + } |
| 47 | + return 0, false |
| 48 | +} |
| 49 | + |
| 50 | +func (r ReviewStatus) MarshalJSON() ([]byte, error) { |
| 51 | + if !r.IsValid() { |
| 52 | + return nil, fmt.Errorf("MarshalJSON: unrecognized review status: %d", r) |
| 53 | + } |
| 54 | + return json.Marshal(r.String()) |
| 55 | +} |
| 56 | + |
| 57 | +func (r *ReviewStatus) UnmarshalJSON(b []byte) error { |
| 58 | + var s string |
| 59 | + if err := json.Unmarshal(b, &s); err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + if rs, ok := ToReviewStatus(s); ok { |
| 63 | + *r = rs |
| 64 | + return nil |
| 65 | + } |
| 66 | + return fmt.Errorf("UnmarshalJSON: unrecognized review status: %s", s) |
| 67 | +} |
0 commit comments