Skip to content

Commit c7d58a5

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

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-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: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
"reflect"
27+
28+
"github.com/google/go-cmp/cmp"
29+
yaml_v2 "gopkg.in/yaml.v2"
30+
yaml_v3 "gopkg.in/yaml.v3"
31+
32+
"github.com/sapcc/go-bits/logg"
33+
)
34+
35+
func Marshal[T any](in T) ([]byte, error) {
36+
out, err := yaml_v2.Marshal(in)
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
outV3, err := yaml_v3.Marshal(in)
42+
if err != nil {
43+
logg.Error("gopkg.in/yaml.v3.Marshal() returned an error: %w,", err)
44+
}
45+
46+
if !reflect.DeepEqual(out, outV3) {
47+
logg.Error("gopkg.in/yaml.v2 and gopkg.in/yaml.v3 Marshal() are not equal. Turn on debug logging to see the difference.")
48+
if logg.ShowDebug {
49+
logg.Debug(cmp.Diff(out, outV3))
50+
}
51+
}
52+
53+
return out, nil
54+
}
55+
56+
func Unmarshal[T any](in []byte, out *T) error {
57+
err := yaml_v2.Unmarshal(in, out)
58+
if err != nil {
59+
return err
60+
}
61+
62+
var outV3 T
63+
err = yaml_v3.Unmarshal(in, outV3)
64+
if err != nil {
65+
logg.Error("gopkg.in/yaml.v3.Unmarshal() returned an error: %w,", err)
66+
}
67+
68+
if !reflect.DeepEqual(out, outV3) {
69+
logg.Error("gopkg.in/yaml.v2 and gopkg.in/yaml.v3 Unmarshal() are not equal. Turn on debug logging to see the difference.")
70+
if logg.ShowDebug {
71+
logg.Debug(cmp.Diff(out, outV3))
72+
}
73+
}
74+
75+
return nil
76+
}
77+
78+
func UnmarshalStrict[T any](in []byte, out *T) error {
79+
err := yaml_v2.UnmarshalStrict(in, out)
80+
if err != nil {
81+
return err
82+
}
83+
84+
var outV3 T
85+
dec := yaml_v3.NewDecoder(bytes.NewReader(in))
86+
dec.KnownFields(true)
87+
err = dec.Decode(&outV3)
88+
if err != nil {
89+
logg.Error("gopkg.in/yaml.v3.UnmarshalStrict() returned an error: %w,", err)
90+
}
91+
92+
if !reflect.DeepEqual(out, outV3) {
93+
logg.Error("gopkg.in/yaml.v2 and gopkg.in/yaml.v3 UnmarshalStrict() are not equal. Turn on debug logging to see the difference.")
94+
if logg.ShowDebug {
95+
logg.Debug(cmp.Diff(out, outV3))
96+
}
97+
}
98+
99+
return nil
100+
}

0 commit comments

Comments
 (0)