Skip to content

Commit

Permalink
Merge pull request #353 from TomWright/yaml-strings
Browse files Browse the repository at this point in the history
Fix an issue that caused yaml strings to be read as numbers
  • Loading branch information
TomWright authored Aug 29, 2023
2 parents 7baf21b + 9907ef7 commit d882423
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
14 changes: 12 additions & 2 deletions dencoding/yaml_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/tomwright/dasel/v2/util"
"gopkg.in/yaml.v3"
"io"
"strconv"
)

// YAMLEncoder wraps a standard yaml encoder to implement custom ordering logic.
Expand Down Expand Up @@ -104,8 +105,17 @@ func yamlSliceToNode(value []any) (*yaml.Node, error) {
}

func yamlScalarToNode(value any) (*yaml.Node, error) {
return &yaml.Node{
res := &yaml.Node{
Kind: yaml.ScalarNode,
Value: util.ToString(value),
}, nil
}
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 {
res.Style = yaml.DoubleQuotedStyle
return res, nil
}
}
return res, nil
}
9 changes: 9 additions & 0 deletions internal/command/put_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,13 @@ func TestPutCommand(t *testing.T) {
nil,
nil,
))

// https://github.com/TomWright/dasel/issues/327
t.Run("Yaml0xStringQuoted", runTest(
[]string{"put", "-r", "yaml", "-t", "string", "--pretty=false", "-v", "0x12_11", "t"},
[]byte(`t:`),
newline([]byte(`t: "0x12_11"`)),
nil,
nil,
))
}

0 comments on commit d882423

Please sign in to comment.