-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
complement.go
113 lines (99 loc) · 2.31 KB
/
complement.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package config
import (
"errors"
"github.com/suzuki-shunsuke/tfcmt/pkg/domain"
)
type Complement struct {
PR []domain.ComplementEntry
Owner []domain.ComplementEntry
Repo []domain.ComplementEntry
SHA []domain.ComplementEntry
Link []domain.ComplementEntry
Vars map[string][]domain.ComplementEntry
}
type rawComplement struct {
PR []map[string]interface{}
Owner []map[string]interface{}
Repo []map[string]interface{}
SHA []map[string]interface{}
Link []map[string]interface{}
Vars map[string][]map[string]interface{}
}
func convComplementEntries(maps []map[string]interface{}) ([]domain.ComplementEntry, error) {
entries := make([]domain.ComplementEntry, len(maps))
for i, m := range maps {
entry, err := convComplementEntry(m)
if err != nil {
return nil, err
}
entries[i] = entry
}
return entries, nil
}
func convComplementEntry(m map[string]interface{}) (domain.ComplementEntry, error) {
t, ok := m["type"]
if !ok {
return nil, errors.New(`"type" is required`)
}
typ, ok := t.(string)
if !ok {
return nil, errors.New(`"type" must be string`)
}
switch typ {
case "envsubst":
entry := ComplementEnvsubstEntry{}
if err := newComplementEnvsubstEntry(m, &entry); err != nil {
return nil, err
}
return &entry, nil
case "template":
entry := ComplementTemplateEntry{}
if err := newComplementTemplateEntry(m, &entry); err != nil {
return nil, err
}
return &entry, nil
default:
return nil, errors.New(`unsupported type: ` + typ)
}
}
func (cpl *Complement) UnmarshalYAML(unmarshal func(interface{}) error) error {
var val rawComplement
if err := unmarshal(&val); err != nil {
return err
}
pr, err := convComplementEntries(val.PR)
if err != nil {
return err
}
cpl.PR = pr
owner, err := convComplementEntries(val.Owner)
if err != nil {
return err
}
cpl.Owner = owner
repo, err := convComplementEntries(val.Repo)
if err != nil {
return err
}
cpl.Repo = repo
sha, err := convComplementEntries(val.SHA)
if err != nil {
return err
}
cpl.SHA = sha
link, err := convComplementEntries(val.Link)
if err != nil {
return err
}
cpl.Link = link
vars := make(map[string][]domain.ComplementEntry, len(val.Vars))
for k, v := range val.Vars {
a, err := convComplementEntries(v)
if err != nil {
return err
}
vars[k] = a
}
cpl.Vars = vars
return nil
}