From 8639729bd80a1f614f04bdecba7ca2709b6e5a11 Mon Sep 17 00:00:00 2001 From: Luke Massa Date: Thu, 26 Jun 2025 16:26:39 -0400 Subject: [PATCH] chore(deps): Convert go yaml to goccy yaml (#5579) Signed-off-by: Luke Massa Co-authored-by: Rui Chen --- cmd/server_test.go | 2 +- go.mod | 3 +- go.sum | 2 ++ server/core/config/parser_validator.go | 8 ++---- server/core/config/parser_validator_test.go | 18 ++++++++---- server/core/config/raw/policies_test.go | 2 +- server/core/config/raw/raw_test.go | 5 ++-- server/core/config/raw/repo_cfg_test.go | 29 ++++++++++++++++---- server/core/config/raw/step_test.go | 8 ++++-- server/core/config/raw/workflow_step_test.go | 8 ++++-- 10 files changed, 59 insertions(+), 26 deletions(-) diff --git a/cmd/server_test.go b/cmd/server_test.go index daef8709b..273ac69b2 100644 --- a/cmd/server_test.go +++ b/cmd/server_test.go @@ -24,10 +24,10 @@ import ( "strings" "testing" + yaml "github.com/goccy/go-yaml" homedir "github.com/mitchellh/go-homedir" "github.com/spf13/cobra" "github.com/spf13/viper" - "gopkg.in/yaml.v3" "github.com/runatlantis/atlantis/server" "github.com/runatlantis/atlantis/server/events/vcs/testdata" diff --git a/go.mod b/go.mod index fda5d4b8f..9f9e32325 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/go-ozzo/ozzo-validation v3.6.0+incompatible github.com/go-playground/validator/v10 v10.26.0 github.com/go-test/deep v1.1.1 + github.com/goccy/go-yaml v1.17.1 github.com/gofri/go-github-ratelimit v1.1.1 github.com/golang-jwt/jwt/v5 v5.2.2 github.com/google/go-github/v71 v71.0.0 @@ -51,7 +52,6 @@ require ( go.uber.org/zap v1.27.0 golang.org/x/term v0.31.0 golang.org/x/text v0.24.0 - gopkg.in/yaml.v3 v3.0.1 ) require ( @@ -141,4 +141,5 @@ require ( golang.org/x/time v0.8.0 // indirect golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect google.golang.org/protobuf v1.36.1 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 021e29ad5..b3231fdee 100644 --- a/go.sum +++ b/go.sum @@ -170,6 +170,8 @@ github.com/go-test/deep v1.1.1 h1:0r/53hagsehfO4bzD2Pgr/+RgHqhmf+k1Bpse2cTu1U= github.com/go-test/deep v1.1.1/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE= github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss= github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= +github.com/goccy/go-yaml v1.17.1 h1:LI34wktB2xEE3ONG/2Ar54+/HJVBriAGJ55PHls4YuY= +github.com/goccy/go-yaml v1.17.1/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= github.com/gofri/go-github-ratelimit v1.1.1 h1:5TCOtFf45M2PjSYU17txqbiYBEzjOuK1+OhivbW69W0= github.com/gofri/go-github-ratelimit v1.1.1/go.mod h1:wGZlBbzHmIVjwDR3pZgKY7RBTV6gsQWxLVkpfwhcMJM= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= diff --git a/server/core/config/parser_validator.go b/server/core/config/parser_validator.go index 0c12b7aeb..b9a819091 100644 --- a/server/core/config/parser_validator.go +++ b/server/core/config/parser_validator.go @@ -13,9 +13,9 @@ import ( validation "github.com/go-ozzo/ozzo-validation" shlex "github.com/google/shlex" + yaml "github.com/goccy/go-yaml" "github.com/runatlantis/atlantis/server/core/config/raw" "github.com/runatlantis/atlantis/server/core/config/valid" - yaml "gopkg.in/yaml.v3" ) // ParserValidator parses and validates server-side repo config files and @@ -57,8 +57,7 @@ func (p *ParserValidator) ParseRepoCfg(absRepoDir string, globalCfg valid.Global func (p *ParserValidator) ParseRepoCfgData(repoCfgData []byte, globalCfg valid.GlobalCfg, repoID string, branch string) (valid.RepoCfg, error) { var rawConfig raw.RepoCfg - decoder := yaml.NewDecoder(bytes.NewReader(repoCfgData)) - decoder.KnownFields(true) + decoder := yaml.NewDecoder(bytes.NewReader(repoCfgData), yaml.Strict()) err := decoder.Decode(&rawConfig) if err != nil && !errors.Is(err, io.EOF) { @@ -119,8 +118,7 @@ func (p *ParserValidator) ParseGlobalCfg(configFile string, defaultCfg valid.Glo var rawCfg raw.GlobalCfg - decoder := yaml.NewDecoder(bytes.NewReader(configData)) - decoder.KnownFields(true) + decoder := yaml.NewDecoder(bytes.NewReader(configData), yaml.Strict()) err = decoder.Decode(&rawCfg) if err != nil && !errors.Is(err, io.EOF) { diff --git a/server/core/config/parser_validator_test.go b/server/core/config/parser_validator_test.go index abe6f9893..eef1503dc 100644 --- a/server/core/config/parser_validator_test.go +++ b/server/core/config/parser_validator_test.go @@ -84,12 +84,12 @@ func TestParseCfgs_InvalidYAML(t *testing.T) { { "random characters", "slkjds", - "yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `slkjds` into", + "string was used where mapping is expected", }, { "just a colon", ":", - "yaml: did not find expected key", + "unexpected key name", }, } @@ -670,7 +670,12 @@ projects: version: 3 projects: - unknown: value`, - expErr: "yaml: unmarshal errors:\n line 4: field unknown not found in type raw.Project", + expErr: `[4:3] unknown field "unknown" + 2 | version: 3 + 3 | projects: +> 4 | - unknown: value + ^ +`, }, { description: "referencing workflow that doesn't exist", @@ -1272,8 +1277,11 @@ func TestParseGlobalCfg(t *testing.T) { expErr: "file was empty", }, "invalid fields": { - input: "invalid: key", - expErr: "yaml: unmarshal errors:\n line 1: field invalid not found in type raw.GlobalCfg", + input: "invalid: key", + expErr: `[1:1] unknown field "invalid" +> 1 | invalid: key + ^ +`, }, "no id specified": { input: `repos: diff --git a/server/core/config/raw/policies_test.go b/server/core/config/raw/policies_test.go index 4f8f6a313..a04e50d4c 100644 --- a/server/core/config/raw/policies_test.go +++ b/server/core/config/raw/policies_test.go @@ -3,11 +3,11 @@ package raw_test import ( "testing" + yaml "github.com/goccy/go-yaml" "github.com/hashicorp/go-version" "github.com/runatlantis/atlantis/server/core/config/raw" "github.com/runatlantis/atlantis/server/core/config/valid" . "github.com/runatlantis/atlantis/testing" - yaml "gopkg.in/yaml.v3" ) func TestPolicySetsConfig_YAMLMarshalling(t *testing.T) { diff --git a/server/core/config/raw/raw_test.go b/server/core/config/raw/raw_test.go index 0d8fdca9a..88a16fe4c 100644 --- a/server/core/config/raw/raw_test.go +++ b/server/core/config/raw/raw_test.go @@ -6,7 +6,7 @@ import ( "errors" - "gopkg.in/yaml.v3" + yaml "github.com/goccy/go-yaml" ) // Bool is a helper routine that allocates a new bool value @@ -23,8 +23,7 @@ func String(v string) *string { return &v } // Helper function to unmarshal from strings func unmarshalString(in string, out interface{}) error { - decoder := yaml.NewDecoder(strings.NewReader(in)) - decoder.KnownFields(true) + decoder := yaml.NewDecoder(strings.NewReader(in), yaml.Strict()) err := decoder.Decode(out) if errors.Is(err, io.EOF) { diff --git a/server/core/config/raw/repo_cfg_test.go b/server/core/config/raw/repo_cfg_test.go index 2c405da34..52d81203a 100644 --- a/server/core/config/raw/repo_cfg_test.go +++ b/server/core/config/raw/repo_cfg_test.go @@ -45,7 +45,10 @@ func TestConfig_UnmarshalYAML(t *testing.T) { Projects: nil, Workflows: nil, }, - expErr: "yaml: unmarshal errors:\n line 1: field invalid not found in type raw.RepoCfg", + expErr: `[1:1] unknown field "invalid" +> 1 | invalid: key + ^ +`, }, { description: "version set to 2", @@ -91,7 +94,11 @@ func TestConfig_UnmarshalYAML(t *testing.T) { Projects: nil, Workflows: nil, }, - expErr: "yaml: unmarshal errors:\n line 2: cannot unmarshal !!map into []raw.Project", + expErr: `[2:6] mapping was used where sequence is expected + 1 | projects: +> 2 | key: value + ^ +`, }, { description: "projects with a scalar", @@ -101,7 +108,10 @@ func TestConfig_UnmarshalYAML(t *testing.T) { Projects: nil, Workflows: nil, }, - expErr: "yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `value` into []raw.Project", + expErr: `[1:11] string was used where sequence is expected +> 1 | projects: value + ^ +`, }, { description: "automerge not a boolean", @@ -111,7 +121,11 @@ func TestConfig_UnmarshalYAML(t *testing.T) { Projects: nil, Workflows: nil, }, - expErr: "yaml: unmarshal errors:\n line 2: cannot unmarshal !!str `notabool` into bool", + expErr: `[2:12] cannot unmarshal string into Go struct field RepoCfg.Automerge of type bool + 1 | version: 3 +> 2 | automerge: notabool + ^ +`, }, { description: "parallel apply not a boolean", @@ -121,8 +135,11 @@ func TestConfig_UnmarshalYAML(t *testing.T) { Projects: nil, Workflows: nil, }, - expErr: "yaml: unmarshal errors:\n line 2: cannot unmarshal !!str `notabool` into bool", - }, + expErr: `[2:17] cannot unmarshal string into Go struct field RepoCfg.ParallelApply of type bool + 1 | version: 3 +> 2 | parallel_apply: notabool + ^ +`}, { description: "should use values if set", input: ` diff --git a/server/core/config/raw/step_test.go b/server/core/config/raw/step_test.go index c7373ad14..e244c4da5 100644 --- a/server/core/config/raw/step_test.go +++ b/server/core/config/raw/step_test.go @@ -3,10 +3,10 @@ package raw_test import ( "testing" + yaml "github.com/goccy/go-yaml" "github.com/runatlantis/atlantis/server/core/config/raw" "github.com/runatlantis/atlantis/server/core/config/valid" . "github.com/runatlantis/atlantis/testing" - yaml "gopkg.in/yaml.v3" ) func TestStepConfig_YAMLMarshalling(t *testing.T) { @@ -148,7 +148,11 @@ key: value`, key: - value: another: map`, - expErr: "yaml: unmarshal errors:\n line 3: cannot unmarshal !!seq into map[string]interface {}", + expErr: `[3:2] sequence was used where mapping is expected + 2 | key: +> 3 | - value: + ^ + 4 | another: map`, }, } diff --git a/server/core/config/raw/workflow_step_test.go b/server/core/config/raw/workflow_step_test.go index 60b6f1552..3d4c0c0c3 100644 --- a/server/core/config/raw/workflow_step_test.go +++ b/server/core/config/raw/workflow_step_test.go @@ -3,10 +3,10 @@ package raw_test import ( "testing" + yaml "github.com/goccy/go-yaml" "github.com/runatlantis/atlantis/server/core/config/raw" "github.com/runatlantis/atlantis/server/core/config/valid" . "github.com/runatlantis/atlantis/testing" - yaml "gopkg.in/yaml.v3" ) func TestWorkflowHook_YAMLMarshalling(t *testing.T) { @@ -47,7 +47,11 @@ key: value`, key: value: another: map`, - expErr: "yaml: unmarshal errors:\n line 3: cannot unmarshal !!map into string", + expErr: `[3:8] cannot unmarshal map[string]interface {} into Go value of type string + 2 | key: +> 3 | value: + ^ + 4 | another: map`, }, }