-
Notifications
You must be signed in to change notification settings - Fork 22
/
registry.go
49 lines (43 loc) · 929 Bytes
/
registry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package specs
import (
"bytes"
"encoding/json"
"fmt"
)
type Registry int
const (
RegistryGithub Registry = iota
RegistryLocal
RegistryGrpc
)
func (r Registry) String() string {
return [...]string{"github", "local", "grpc"}[r]
}
func (r Registry) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBufferString(`"`)
buffer.WriteString(r.String())
buffer.WriteString(`"`)
return buffer.Bytes(), nil
}
func (r *Registry) UnmarshalJSON(data []byte) (err error) {
var registry string
if err := json.Unmarshal(data, ®istry); err != nil {
return err
}
if *r, err = RegistryFromString(registry); err != nil {
return err
}
return nil
}
func RegistryFromString(s string) (Registry, error) {
switch s {
case "github":
return RegistryGithub, nil
case "local":
return RegistryLocal, nil
case "grpc":
return RegistryGrpc, nil
default:
return RegistryGithub, fmt.Errorf("unknown registry %s", s)
}
}