Merge pull request #1463 from stevvooe/switch-snapshot-kind

snapshot: use switch to parse snapshot.Kind
This commit is contained in:
Derek McGowan 2017-09-05 12:44:57 -07:00 committed by GitHub
commit 887f97f2ff

View File

@ -14,24 +14,37 @@ type Kind uint8
// definitions of snapshot kinds // definitions of snapshot kinds
const ( const (
KindView Kind = iota + 1 KindUnknown Kind = iota
KindView
KindActive KindActive
KindCommitted KindCommitted
) )
var ( func ParseKind(s string) Kind {
kindStrings = map[Kind]string{ s = strings.ToLower(s)
KindView: "view", switch s {
KindActive: "active", case "view":
KindCommitted: "committed", return KindView
case "active":
return KindActive
case "committed":
return KindCommitted
}
return KindUnknown
} }
)
func (k Kind) String() string { func (k Kind) String() string {
if s, ok := kindStrings[k]; ok { switch k {
return s case KindView:
return "View"
case KindActive:
return "Active"
case KindCommitted:
return "Committed"
} }
return "unknown"
return "Unknown"
} }
func (k Kind) MarshalJSON() ([]byte, error) { func (k Kind) MarshalJSON() ([]byte, error) {
@ -43,12 +56,8 @@ func (k *Kind) UnmarshalJSON(b []byte) error {
if err := json.Unmarshal(b, &s); err != nil { if err := json.Unmarshal(b, &s); err != nil {
return err return err
} }
for kk, ks := range kindStrings {
if strings.EqualFold(s, ks) { *k = ParseKind(s)
*k = kk
return nil
}
}
return nil return nil
} }