Skip to content

Commit 02e52b6

Browse files
Add helper to compare gopk.in/yaml.v{2,3}
1 parent b837fce commit 02e52b6

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/databus23/goslo.policy v0.0.0-20210929125152-81bf2876dbdb
77
github.com/gofrs/uuid/v5 v5.1.0
88
github.com/golang-migrate/migrate/v4 v4.17.0
9+
github.com/google/go-cmp v0.6.0
910
github.com/gophercloud/gophercloud v1.11.0
1011
github.com/gorilla/mux v1.8.1
1112
github.com/hashicorp/golang-lru/v2 v2.0.7

yaml/yaml.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*******************************************************************************
2+
*
3+
* Copyright 2024 SAP SE
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You should have received a copy of the License along with this
8+
* program. If not, you may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
*******************************************************************************/
19+
20+
// This package is just an aid to migrate from gopkg.in/yaml.v2 to gopkg.in/yaml.v2
21+
// It provides a helper to parse a given yaml document with both major versions and log if there is a difference.
22+
package yaml
23+
24+
import (
25+
"bytes"
26+
"fmt"
27+
"reflect"
28+
29+
"github.com/google/go-cmp/cmp"
30+
yaml_v2 "gopkg.in/yaml.v2"
31+
yaml_v3 "gopkg.in/yaml.v3"
32+
33+
"github.com/sapcc/go-bits/logg"
34+
)
35+
36+
func Marshal[T any](in T) ([]byte, error) {
37+
out, err := yaml_v2.Marshal(in)
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
outV3 := new(bytes.Buffer)
43+
dec := yaml_v3.NewEncoder(outV3)
44+
dec.SetIndent(2)
45+
err = dec.Encode(in)
46+
if err != nil {
47+
logg.Error("gopkg.in/yaml.v3.Marshal() returned an error: %w,", err)
48+
}
49+
50+
if !reflect.DeepEqual(out, outV3.Bytes()) {
51+
logg.Error("gopkg.in/yaml.v2 and gopkg.in/yaml.v3 Marshal() are not equal. Turn on debug logging to see the difference.")
52+
if logg.ShowDebug {
53+
fmt.Print(cmp.Diff(out, outV3.Bytes()))
54+
}
55+
}
56+
57+
return out, nil
58+
}
59+
60+
func Unmarshal[T any](in []byte, out *T) error {
61+
err := yaml_v2.Unmarshal(in, out)
62+
if err != nil {
63+
return err
64+
}
65+
66+
var outV3 *T
67+
err = yaml_v3.Unmarshal(in, outV3)
68+
if err != nil {
69+
logg.Error("gopkg.in/yaml.v3.Unmarshal() returned an error: %w,", err)
70+
}
71+
72+
if !reflect.DeepEqual(out, outV3) {
73+
logg.Error("gopkg.in/yaml.v2 and gopkg.in/yaml.v3 Unmarshal() are not equal. Turn on debug logging to see the difference.")
74+
if logg.ShowDebug {
75+
fmt.Print(cmp.Diff(out, outV3))
76+
}
77+
}
78+
79+
return nil
80+
}
81+
82+
func UnmarshalStrict[T any](in []byte, out *T) error {
83+
err := yaml_v2.UnmarshalStrict(in, out)
84+
if err != nil {
85+
return err
86+
}
87+
88+
var outV3 *T
89+
dec := yaml_v3.NewDecoder(bytes.NewReader(in))
90+
dec.KnownFields(true)
91+
err = dec.Decode(&outV3)
92+
if err != nil {
93+
logg.Error("gopkg.in/yaml.v3.UnmarshalStrict() returned an error: %w,", err)
94+
}
95+
96+
if !reflect.DeepEqual(out, outV3) {
97+
logg.Error("gopkg.in/yaml.v2 and gopkg.in/yaml.v3 UnmarshalStrict() are not equal. Turn on debug logging to see the difference.")
98+
if logg.ShowDebug {
99+
fmt.Print(cmp.Diff(out, outV3))
100+
}
101+
}
102+
103+
return nil
104+
}

0 commit comments

Comments
 (0)