-
Notifications
You must be signed in to change notification settings - Fork 929
/
process.go
83 lines (69 loc) · 2.54 KB
/
process.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package manifestparser
import (
"errors"
"reflect"
"strings"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
)
type Process struct {
DiskQuota string `yaml:"disk-quota,omitempty"`
HealthCheckEndpoint string `yaml:"health-check-http-endpoint,omitempty"`
HealthCheckType constant.HealthCheckType `yaml:"health-check-type,omitempty"`
HealthCheckTimeout int64 `yaml:"timeout,omitempty"`
Instances *int `yaml:"instances,omitempty"`
Memory string `yaml:"memory,omitempty"`
Type string `yaml:"type"`
RemainingManifestFields map[string]interface{} `yaml:"-,inline"`
}
func (process *Process) SetStartCommand(command string) {
if process.RemainingManifestFields == nil {
process.RemainingManifestFields = map[string]interface{}{}
}
if command == "" {
process.RemainingManifestFields["command"] = nil
} else {
process.RemainingManifestFields["command"] = command
}
}
func (process *Process) UnmarshalYAML(unmarshal func(v interface{}) error) error {
// This prevents infinite recursion. The Alias type does not implement the unmarshaller interface
// so by casting application to a alias pointer, it will unmarshal in to the same memory without calling
// UnmarshalYAML on itself infinite times
type Alias Process
aliasPntr := (*Alias)(process)
err := unmarshal(aliasPntr)
if err != nil {
return err
}
err = unmarshal(&process.RemainingManifestFields)
if err != nil {
return err
}
value := reflect.ValueOf(*process)
removeDuplicateMapKeys(value, process.RemainingManifestFields)
// old style was `disk_quota` (underscore not hyphen)
// we maintain backwards-compatibility by supporting both flavors
if process.RemainingManifestFields["disk_quota"] != nil {
if process.DiskQuota != "" {
return errors.New("cannot define both `disk_quota` and `disk-quota`")
}
diskQuota, ok := process.RemainingManifestFields["disk_quota"].(string)
if !ok {
return errors.New("`disk_quota` must be a string")
}
process.DiskQuota = diskQuota
delete(process.RemainingManifestFields, "disk_quota")
}
return nil
}
func removeDuplicateMapKeys(model reflect.Value, fieldMap map[string]interface{}) {
for i := 0; i < model.NumField(); i++ {
structField := model.Type().Field(i)
yamlTag := strings.Split(structField.Tag.Get("yaml"), ",")
yamlKey := yamlTag[0]
if yamlKey == "" {
yamlKey = structField.Name
}
delete(fieldMap, yamlKey)
}
}