snapshot: use switch to parse snapshot.Kind

Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
Stephen J Day 2017-09-01 14:31:23 -07:00
parent 378e3343fe
commit 1c31199797
No known key found for this signature in database
GPG Key ID: 67B3DED84EDC823F

View File

@ -14,24 +14,37 @@ type Kind uint8
// definitions of snapshot kinds
const (
KindView Kind = iota + 1
KindUnknown Kind = iota
KindView
KindActive
KindCommitted
)
var (
kindStrings = map[Kind]string{
KindView: "view",
KindActive: "active",
KindCommitted: "committed",
func ParseKind(s string) Kind {
s = strings.ToLower(s)
switch s {
case "view":
return KindView
case "active":
return KindActive
case "committed":
return KindCommitted
}
return KindUnknown
}
)
func (k Kind) String() string {
if s, ok := kindStrings[k]; ok {
return s
switch k {
case KindView:
return "View"
case KindActive:
return "Active"
case KindCommitted:
return "Committed"
}
return "unknown"
return "Unknown"
}
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 {
return err
}
for kk, ks := range kindStrings {
if strings.EqualFold(s, ks) {
*k = kk
return nil
}
}
*k = ParseKind(s)
return nil
}