From e440b1b7ef60fb9332b878572deb0fc9d9c1068d Mon Sep 17 00:00:00 2001 From: Tom Wright Date: Thu, 31 Aug 2023 14:31:17 +0100 Subject: [PATCH] Fix an issue that caused bool-like strings to be read as bools in YAML --- CHANGELOG.md | 4 +++- dencoding/yaml_encoder.go | 8 +++++--- internal/command/put_test.go | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f092bdd..fdb13184 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -- Nothing yet +### Fixed + +- Resolved an issue with YAML parser that was causing strings to be read as booleans. ## [v2.3.6] - 2023-08-30 diff --git a/dencoding/yaml_encoder.go b/dencoding/yaml_encoder.go index de9c45ff..cb0d718a 100644 --- a/dencoding/yaml_encoder.go +++ b/dencoding/yaml_encoder.go @@ -111,10 +111,12 @@ func yamlScalarToNode(value any) (*yaml.Node, error) { } switch v := value.(type) { case string: - // If the string can be evaluated as a number, quote it. - if _, err := strconv.ParseInt(v, 0, 64); err == nil { + if v == "true" || v == "false" { + // If the string can be evaluated as a bool, quote it. + res.Style = yaml.DoubleQuotedStyle + } else if _, err := strconv.ParseInt(v, 0, 64); err == nil { + // If the string can be evaluated as a number, quote it. res.Style = yaml.DoubleQuotedStyle - return res, nil } } return res, nil diff --git a/internal/command/put_test.go b/internal/command/put_test.go index da14bb38..aa60ff23 100644 --- a/internal/command/put_test.go +++ b/internal/command/put_test.go @@ -172,4 +172,20 @@ func TestPutCommand(t *testing.T) { nil, nil, )) + + t.Run("YamlBoolLikeStringTrue", runTest( + []string{"put", "-r", "yaml", "-t", "string", "--pretty=false", "-v", "true", "t"}, + []byte(`t:`), + newline([]byte(`t: "true"`)), + nil, + nil, + )) + + t.Run("YamlBoolLikeStringFalse", runTest( + []string{"put", "-r", "yaml", "-t", "string", "--pretty=false", "-v", "false", "t"}, + []byte(`t:`), + newline([]byte(`t: "false"`)), + nil, + nil, + )) }