forked from istio/operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml.go
205 lines (183 loc) · 5.18 KB
/
yaml.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2019 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package util
import (
"bytes"
"fmt"
"strings"
jsonpatch "github.com/evanphx/json-patch"
yaml2 "github.com/ghodss/yaml"
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
jsonpb2 "github.com/golang/protobuf/jsonpb"
"github.com/kylelemons/godebug/diff"
"sigs.k8s.io/yaml"
)
// ToYAML returns a YAML string representation of val, or the error string if an error occurs.
func ToYAML(val interface{}) string {
y, err := yaml.Marshal(val)
if err != nil {
return err.Error()
}
return string(y)
}
// ToYAMLWithJSONPB returns a YAML string representation of val (using jsonpb), or the error string if an error occurs.
func ToYAMLWithJSONPB(val proto.Message) string {
m := jsonpb.Marshaler{EnumsAsInts: true}
js, err := m.MarshalToString(val)
if err != nil {
return err.Error()
}
yb, err := yaml.JSONToYAML([]byte(js))
if err != nil {
return err.Error()
}
return string(yb)
}
// MarshalWithJSONPB returns a YAML string representation of val (using jsonpb).
func MarshalWithJSONPB(val proto.Message) (string, error) {
m := jsonpb.Marshaler{EnumsAsInts: true}
js, err := m.MarshalToString(val)
if err != nil {
return "", err
}
yb, err := yaml.JSONToYAML([]byte(js))
if err != nil {
return "", err
}
return string(yb), nil
}
// UnmarshalWithJSONPB unmarshals y into out using gogo jsonpb (required for many proto defined structs).
func UnmarshalWithJSONPB(y string, out proto.Message) error {
jb, err := yaml.YAMLToJSON([]byte(y))
if err != nil {
return err
}
u := jsonpb.Unmarshaler{AllowUnknownFields: false}
err = u.Unmarshal(bytes.NewReader(jb), out)
if err != nil {
return err
}
return nil
}
// UnmarshalValuesWithJSONPB unmarshals y into out using golang jsonpb.
func UnmarshalValuesWithJSONPB(y string, out proto.Message, allowUnknown bool) error {
jb, err := yaml.YAMLToJSON([]byte(y))
if err != nil {
return err
}
u := jsonpb2.Unmarshaler{AllowUnknownFields: allowUnknown}
err = u.Unmarshal(bytes.NewReader(jb), out)
if err != nil {
return err
}
return nil
}
/*func ObjectsInManifest(mstr string) string {
ao, err := manifest.ParseObjectsFromYAMLManifest(mstr)
if err != nil {
return err.Error()
}
var out []string
for _, v := range ao {
out = append(out, v.Hash())
}
return strings.Join(out, "\n")
}*/
// OverlayTrees performs a sequential JSON strategic of overlays over base.
func OverlayTrees(base map[string]interface{}, overlays ...map[string]interface{}) (map[string]interface{}, error) {
bby, err := yaml.Marshal(base)
if err != nil {
return nil, err
}
by := string(bby)
for _, o := range overlays {
oy, err := yaml.Marshal(o)
if err != nil {
return nil, err
}
by, err = OverlayYAML(by, string(oy))
if err != nil {
return nil, err
}
}
out := make(map[string]interface{})
err = yaml.Unmarshal([]byte(by), &out)
if err != nil {
return nil, err
}
return out, nil
}
// OverlayYAML patches the overlay tree over the base tree and returns the result. All trees are expressed as YAML
// strings.
func OverlayYAML(base, overlay string) (string, error) {
if overlay == "" {
return base, nil
}
bj, err := yaml2.YAMLToJSON([]byte(base))
if err != nil {
return "", fmt.Errorf("yamlToJSON error in base: %s\n%s", err, bj)
}
oj, err := yaml2.YAMLToJSON([]byte(overlay))
if err != nil {
return "", fmt.Errorf("yamlToJSON error in overlay: %s\n%s", err, oj)
}
if overlay == "" {
oj = []byte("{}")
}
merged, err := jsonpatch.MergePatch(bj, oj)
if err != nil {
return "", fmt.Errorf("json merge error (%s) for base object: \n%s\n override object: \n%s", err, bj, oj)
}
my, err := yaml2.JSONToYAML(merged)
if err != nil {
return "", fmt.Errorf("jsonToYAML error (%s) for merged object: \n%s", err, merged)
}
return string(my), nil
}
func YAMLDiff(a, b string) string {
ao, bo := make(map[string]interface{}), make(map[string]interface{})
if err := yaml.Unmarshal([]byte(a), &ao); err != nil {
return err.Error()
}
if err := yaml.Unmarshal([]byte(b), &bo); err != nil {
return err.Error()
}
ay, err := yaml.Marshal(ao)
if err != nil {
return err.Error()
}
by, err := yaml.Marshal(bo)
if err != nil {
return err.Error()
}
return diff.Diff(string(ay), string(by))
}
// IsYAMLEqual reports whether the YAML in strings a and b are equal.
func IsYAMLEqual(a, b string) bool {
if strings.TrimSpace(a) == "" && strings.TrimSpace(b) == "" {
return true
}
ajb, err := yaml.YAMLToJSON([]byte(a))
if err != nil {
scope.Debugf("bad YAML in isYAMLEqual:\n%s", a)
return false
}
bjb, err := yaml.YAMLToJSON([]byte(b))
if err != nil {
scope.Debugf("bad YAML in isYAMLEqual:\n%s", b)
return false
}
return string(ajb) == string(bjb)
}