Skip to content

Add helper to compare gopk.in/yaml.v{2,3} #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/databus23/goslo.policy v0.0.0-20210929125152-81bf2876dbdb
github.com/gofrs/uuid/v5 v5.1.0
github.com/golang-migrate/migrate/v4 v4.17.0
github.com/google/go-cmp v0.6.0
github.com/gophercloud/gophercloud v1.11.0
github.com/gorilla/mux v1.8.1
github.com/hashicorp/golang-lru/v2 v2.0.7
Expand Down
104 changes: 104 additions & 0 deletions yaml/yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*******************************************************************************
*
* Copyright 2024 SAP SE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You should have received a copy of the License along with this
* program. If not, 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.
*
*******************************************************************************/

// This package is just an aid to migrate from gopkg.in/yaml.v2 to gopkg.in/yaml.v2
// It provides a helper to parse a given yaml document with both major versions and log if there is a difference.
package yaml

import (
"bytes"
"fmt"
"reflect"

"github.com/google/go-cmp/cmp"
yaml_v2 "gopkg.in/yaml.v2"
yaml_v3 "gopkg.in/yaml.v3"

"github.com/sapcc/go-bits/logg"
)

func Marshal[T any](in T) ([]byte, error) {
out, err := yaml_v2.Marshal(in)
if err != nil {
return nil, err
}

outV3 := new(bytes.Buffer)
dec := yaml_v3.NewEncoder(outV3)
dec.SetIndent(2)
err = dec.Encode(in)
if err != nil {
logg.Error("gopkg.in/yaml.v3.Marshal() returned an error: %w,", err)
}

if !reflect.DeepEqual(out, outV3.Bytes()) {
logg.Error("gopkg.in/yaml.v2 and gopkg.in/yaml.v3 Marshal() are not equal. Turn on debug logging to see the difference.")
if logg.ShowDebug {
fmt.Print(cmp.Diff(out, outV3.Bytes()))
}
}

return out, nil
}

func Unmarshal[T any](in []byte, out *T) error {
err := yaml_v2.Unmarshal(in, out)
if err != nil {
return err
}

var outV3 *T
err = yaml_v3.Unmarshal(in, outV3)
if err != nil {
logg.Error("gopkg.in/yaml.v3.Unmarshal() returned an error: %w,", err)
}

if !reflect.DeepEqual(out, outV3) {
logg.Error("gopkg.in/yaml.v2 and gopkg.in/yaml.v3 Unmarshal() are not equal. Turn on debug logging to see the difference.")
if logg.ShowDebug {
fmt.Print(cmp.Diff(out, outV3))
}
}

return nil
}

func UnmarshalStrict[T any](in []byte, out *T) error {
err := yaml_v2.UnmarshalStrict(in, out)
if err != nil {
return err
}

var outV3 *T
dec := yaml_v3.NewDecoder(bytes.NewReader(in))
dec.KnownFields(true)
err = dec.Decode(&outV3)
if err != nil {
logg.Error("gopkg.in/yaml.v3.UnmarshalStrict() returned an error: %w,", err)
}

if !reflect.DeepEqual(out, outV3) {
logg.Error("gopkg.in/yaml.v2 and gopkg.in/yaml.v3 UnmarshalStrict() are not equal. Turn on debug logging to see the difference.")
if logg.ShowDebug {
fmt.Print(cmp.Diff(out, outV3))
}
}

return nil
}
Loading