Skip to content

Commit 8e24f7a

Browse files
committed
util: add yamlutil
Add an `UnmarshalStrict()` wrapper to match the same function in yaml.v2 or earlier. Also add a `Marshal()` that indents to two spaces. Epic: none Release note: None
1 parent 99ad9a0 commit 8e24f7a

File tree

13 files changed

+108
-18
lines changed

13 files changed

+108
-18
lines changed

pkg/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,7 @@ ALL_TESTS = [
840840
"//pkg/util/uuid:uuid_test",
841841
"//pkg/util/vector:vector_test",
842842
"//pkg/util/vfsutil:vfsutil_test",
843+
"//pkg/util/yamlutil:yamlutil_test",
843844
"//pkg/util:util_test",
844845
"//pkg/workload/bank:bank_test",
845846
"//pkg/workload/cli:cli_test",
@@ -2850,6 +2851,8 @@ GO_TARGETS = [
28502851
"//pkg/util/vector:vector_test",
28512852
"//pkg/util/vfsutil:vfsutil",
28522853
"//pkg/util/vfsutil:vfsutil_test",
2854+
"//pkg/util/yamlutil:yamlutil",
2855+
"//pkg/util/yamlutil:yamlutil_test",
28532856
"//pkg/util:util",
28542857
"//pkg/util:util_test",
28552858
"//pkg/workload/bank:bank",

pkg/roachprod/install/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ go_test(
8585
"//pkg/util/randutil",
8686
"//pkg/util/retry",
8787
"//pkg/util/syncutil",
88+
"//pkg/util/yamlutil",
8889
"@com_github_cockroachdb_datadriven//:datadriven",
8990
"@com_github_cockroachdb_errors//:errors",
9091
"@com_github_stretchr_testify//require",

pkg/roachprod/install/cluster_settings_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
package install
77

88
import (
9-
"bytes"
109
"testing"
1110

11+
"github.com/cockroachdb/cockroach/pkg/util/yamlutil"
1212
"github.com/stretchr/testify/require"
1313
"gopkg.in/yaml.v3"
1414
)
@@ -22,10 +22,7 @@ func TestClusterSettingOptionListCodec(t *testing.T) {
2222
require.NoError(t, err)
2323

2424
var decOpts ClusterSettingOptionList
25-
dec := yaml.NewDecoder(bytes.NewReader(data))
26-
dec.KnownFields(true)
27-
err = dec.Decode(&decOpts)
28-
require.NoError(t, err)
25+
require.NoError(t, yamlutil.UnmarshalStrict(data, &decOpts))
2926

3027
require.Equal(t, opts, decOpts)
3128
require.Equal(t, MakeClusterSettings(opts...), MakeClusterSettings(decOpts...))

pkg/roachprod/roachprodutil/codec/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ go_test(
1616
],
1717
embed = [":codec"],
1818
deps = [
19+
"//pkg/util/yamlutil",
1920
"@com_github_stretchr_testify//require",
2021
"@in_gopkg_yaml_v3//:yaml_v3",
2122
],

pkg/roachprod/roachprodutil/codec/types_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
package codec
77

88
import (
9-
"bytes"
109
"reflect"
1110
"testing"
1211

12+
"github.com/cockroachdb/cockroach/pkg/util/yamlutil"
1313
"github.com/stretchr/testify/require"
1414
"gopkg.in/yaml.v3"
1515
)
@@ -60,10 +60,7 @@ func TestDynamicTypes(t *testing.T) {
6060
require.NoError(t, err)
6161

6262
var decoded ListWrapper[Animal]
63-
dec := yaml.NewDecoder(bytes.NewReader(data))
64-
dec.KnownFields(true)
65-
err = dec.Decode(&decoded)
66-
require.NoError(t, err)
63+
require.NoError(t, yamlutil.UnmarshalStrict(data, &decoded))
6764

6865
// Verify equality of the decoded list and the original list.
6966
require.Equal(t, animals, decoded.Get())

pkg/sql/schemachanger/scbuild/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ go_test(
101101
"//pkg/util/log",
102102
"//pkg/util/mon",
103103
"//pkg/util/randutil",
104+
"//pkg/util/yamlutil",
104105
"@com_github_cockroachdb_datadriven//:datadriven",
105106
"@com_github_stretchr_testify//require",
106107
"@in_gopkg_yaml_v3//:yaml_v3",

pkg/sql/schemachanger/scbuild/builder_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
4040
"github.com/cockroachdb/cockroach/pkg/util/log"
4141
"github.com/cockroachdb/cockroach/pkg/util/mon"
42+
"github.com/cockroachdb/cockroach/pkg/util/yamlutil"
4243
"github.com/cockroachdb/datadriven"
4344
"github.com/stretchr/testify/require"
4445
"gopkg.in/yaml.v3"
@@ -272,10 +273,8 @@ var _ sort.Interface = nodeEntries{}
272273
func formatElementForDisplay(t *testing.T, e scpb.Element) []byte {
273274
marshaled, err := sctestutils.ProtoToYAML(e, false /* emitDefaults */)
274275
require.NoError(t, err)
275-
dec := yaml.NewDecoder(strings.NewReader(marshaled))
276-
dec.KnownFields(true)
277276
var n yaml.Node
278-
require.NoError(t, dec.Decode(&n))
277+
require.NoError(t, yamlutil.UnmarshalStrict([]byte(marshaled), &n))
279278
walkYaml(&n, func(node *yaml.Node) { node.Style = yaml.FlowStyle })
280279
data, err := yaml.Marshal(&n)
281280
require.NoError(t, err)

pkg/storage/storageconfig/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ go_test(
3333
deps = [
3434
"//pkg/testutils/datapathutils",
3535
"//pkg/util/leaktest",
36+
"//pkg/util/yamlutil",
3637
"@com_github_cockroachdb_datadriven//:datadriven",
3738
"@com_github_stretchr_testify//require",
3839
"@in_gopkg_yaml_v3//:yaml_v3",

pkg/storage/storageconfig/store_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
package storageconfig
77

88
import (
9-
"bytes"
109
"fmt"
1110
"math"
1211
"math/rand/v2"
1312
"testing"
1413

1514
"github.com/cockroachdb/cockroach/pkg/testutils/datapathutils"
15+
"github.com/cockroachdb/cockroach/pkg/util/yamlutil"
1616
"github.com/cockroachdb/datadriven"
1717
"github.com/stretchr/testify/require"
1818
"gopkg.in/yaml.v3"
@@ -165,9 +165,7 @@ func randSize(rng *rand.Rand, minBytes, maxBytes int64, minPercent, maxPercent f
165165

166166
func unmarshal(data []byte) (Store, error) {
167167
var s Store
168-
dec := yaml.NewDecoder(bytes.NewReader(data))
169-
dec.KnownFields(true)
170-
if err := dec.Decode(&s); err != nil {
168+
if err := yamlutil.UnmarshalStrict(data, &s); err != nil {
171169
return Store{}, err
172170
}
173171
return s, nil

pkg/testutils/lint/lint_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1674,7 +1674,7 @@ func TestLint(t *testing.T) {
16741674
filter,
16751675
stream.GrepNot(`nolint:yaml`),
16761676
), func(s string) {
1677-
t.Errorf("\n%s <- forbidden; use 'yaml.UnmarshalStrict' instead", s)
1677+
t.Errorf("\n%s <- forbidden; use 'yamlutil.UnmarshalStrict' instead", s)
16781678
}); err != nil {
16791679
t.Error(err)
16801680
}

0 commit comments

Comments
 (0)