-
Notifications
You must be signed in to change notification settings - Fork 79
/
type_platform.go
62 lines (51 loc) · 1.27 KB
/
type_platform.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
50
51
52
53
54
55
56
57
58
59
60
61
62
package v1alpha2
import (
"encoding/json"
"errors"
)
// DefaultPlatformArchitecture defines the default
// architecture used by mirroring platform
// release payloads.
const DefaultPlatformArchitecture = "amd64"
// PlatformType defines the content type for platforms
type PlatformType int
// TypeOCP is default
const (
TypeOCP PlatformType = iota
TypeOKD
)
var platformTypeStrings = map[PlatformType]string{
TypeOCP: "ocp",
TypeOKD: "okd",
}
var platformStringsType = map[string]PlatformType{
"ocp": TypeOCP,
"okd": TypeOKD,
}
// String returns the string representation
// of an PlatformType
func (pt PlatformType) String() string {
return platformTypeStrings[pt]
}
// MarshalJSON marshals the PlatformType as a quoted json string
func (pt PlatformType) MarshalJSON() ([]byte, error) {
if err := pt.validate(); err != nil {
return nil, err
}
return json.Marshal(pt.String())
}
// UnmarshalJSON unmarshals a quoted json string to the PlatformType
func (pt *PlatformType) UnmarshalJSON(b []byte) error {
var j string
if err := json.Unmarshal(b, &j); err != nil {
return err
}
*pt = platformStringsType[j]
return nil
}
func (pt PlatformType) validate() error {
if _, ok := platformTypeStrings[pt]; !ok {
return errors.New("unknown platform type")
}
return nil
}