Skip to content

Commit 06dffc7

Browse files
authored
Merge pull request #356 from TomWright/bool-like-strings
Fix an issue that caused bool-like strings to be read as bools in YAML
2 parents 501ce1a + e440b1b commit 06dffc7

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
- Nothing yet
10+
### Fixed
11+
12+
- Resolved an issue with YAML parser that was causing strings to be read as booleans.
1113

1214
## [v2.3.6] - 2023-08-30
1315

dencoding/yaml_encoder.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,12 @@ func yamlScalarToNode(value any) (*yaml.Node, error) {
111111
}
112112
switch v := value.(type) {
113113
case string:
114-
// If the string can be evaluated as a number, quote it.
115-
if _, err := strconv.ParseInt(v, 0, 64); err == nil {
114+
if v == "true" || v == "false" {
115+
// If the string can be evaluated as a bool, quote it.
116+
res.Style = yaml.DoubleQuotedStyle
117+
} else if _, err := strconv.ParseInt(v, 0, 64); err == nil {
118+
// If the string can be evaluated as a number, quote it.
116119
res.Style = yaml.DoubleQuotedStyle
117-
return res, nil
118120
}
119121
}
120122
return res, nil

internal/command/put_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,20 @@ func TestPutCommand(t *testing.T) {
172172
nil,
173173
nil,
174174
))
175+
176+
t.Run("YamlBoolLikeStringTrue", runTest(
177+
[]string{"put", "-r", "yaml", "-t", "string", "--pretty=false", "-v", "true", "t"},
178+
[]byte(`t:`),
179+
newline([]byte(`t: "true"`)),
180+
nil,
181+
nil,
182+
))
183+
184+
t.Run("YamlBoolLikeStringFalse", runTest(
185+
[]string{"put", "-r", "yaml", "-t", "string", "--pretty=false", "-v", "false", "t"},
186+
[]byte(`t:`),
187+
newline([]byte(`t: "false"`)),
188+
nil,
189+
nil,
190+
))
175191
}

0 commit comments

Comments
 (0)