-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
88 lines (84 loc) · 2.36 KB
/
types.go
File metadata and controls
88 lines (84 loc) · 2.36 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import "encoding/json"
type Registry struct {
Name string `yaml:"name" json:"name"`
RegistryHost string `yaml:"url,omitempty" json:"registry,omitempty"`
AuthStrategy string `yaml:"auth_strategy" json:"auth_strategy,omitempty"`
Default bool `yaml:"default" json:"default"`
ImageTypes []string `yaml:"image_types" json:"image_types"`
BasePaths map[string]string `yaml:"base_paths" json:"base_paths"`
}
// Custom YAML unmarshaling to support both 'registry' and 'url' mapping to RegistryHost
func (r *Registry) UnmarshalYAML(unmarshal func(interface{}) error) error {
var aux map[string]interface{}
if err := unmarshal(&aux); err != nil {
return err
}
if name, ok := aux["name"].(string); ok {
r.Name = name
}
if reg, ok := aux["registry"].(string); ok {
r.RegistryHost = reg
} else if url, ok := aux["url"].(string); ok {
r.RegistryHost = url
}
if auth, ok := aux["auth_strategy"].(string); ok {
r.AuthStrategy = auth
}
if def, ok := aux["default"].(bool); ok {
r.Default = def
}
if img, ok := aux["image_types"].([]interface{}); ok {
r.ImageTypes = make([]string, len(img))
for i, v := range img {
if s, ok := v.(string); ok {
r.ImageTypes[i] = s
}
}
}
if bp, ok := aux["base_paths"].(map[string]interface{}); ok {
r.BasePaths = make(map[string]string)
for k, v := range bp {
if s, ok := v.(string); ok {
r.BasePaths[k] = s
}
}
}
return nil
}
// Custom JSON unmarshaling to support both 'registry' and 'url' mapping to Url
func (r *Registry) UnmarshalJSON(data []byte) error {
var aux map[string]interface{}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
if name, ok := aux["name"].(string); ok {
r.Name = name
}
if reg, ok := aux["registry"].(string); ok {
r.RegistryHost = reg
}
if auth, ok := aux["auth_strategy"].(string); ok {
r.AuthStrategy = auth
}
if def, ok := aux["default"].(bool); ok {
r.Default = def
}
if img, ok := aux["image_types"].([]interface{}); ok {
r.ImageTypes = make([]string, len(img))
for i, v := range img {
if s, ok := v.(string); ok {
r.ImageTypes[i] = s
}
}
}
if bp, ok := aux["base_paths"].(map[string]interface{}); ok {
r.BasePaths = make(map[string]string)
for k, v := range bp {
if s, ok := v.(string); ok {
r.BasePaths[k] = s
}
}
}
return nil
}