Skip to content

Commit

Permalink
Merge pull request #356 from TomWright/bool-like-strings
Browse files Browse the repository at this point in the history
Fix an issue that caused bool-like strings to be read as bools in YAML
  • Loading branch information
TomWright authored Aug 31, 2023
2 parents 501ce1a + e440b1b commit 06dffc7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 5 additions & 3 deletions dencoding/yaml_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions internal/command/put_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
))
}

0 comments on commit 06dffc7

Please sign in to comment.