Skip to content

Commit

Permalink
Fix an issue when parsing empty input documents
Browse files Browse the repository at this point in the history
  • Loading branch information
TomWright committed Nov 20, 2023
1 parent db905a2 commit 09ffcb6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add `man` that generates manpages for all dasel subcommands.

### Fixed

- Fixed an issue when [parsing empty input documents](https://github.com/TomWright/dasel/issues/374).

## [v2.4.1] - 2023-10-18

### Fixed
Expand Down
16 changes: 15 additions & 1 deletion internal/command/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/spf13/cobra"
"github.com/tomwright/dasel/v2"
"github.com/tomwright/dasel/v2/dencoding"
"github.com/tomwright/dasel/v2/storage"
"io"
"os"
Expand Down Expand Up @@ -75,7 +76,20 @@ func (o *readOptions) rootValue(cmd *cobra.Command) (dasel.Value, error) {
}
}

return storage.Load(parser, reader, options...)
rootNode, err := storage.Load(parser, reader, options...)
if err != nil {
return rootNode, err
}

if !rootNode.Value.IsValid() {
var defaultValue any = dencoding.NewMap()
if _, ok := parser.(*storage.CSVParser); ok {
defaultValue = []any{}
}
rootNode = dasel.ValueOf(defaultValue)
}

return rootNode, nil
}

type writeOptions struct {
Expand Down
7 changes: 0 additions & 7 deletions internal/command/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,4 @@ func runSelectCommand(opts *selectOptions, cmd *cobra.Command) error {
}

return nil

// todo : check if this is still needed.
// if !rootNode.Value.IsValid() {
// rootNode = dasel.New(&storage.BasicSingleDocument{
// Value: map[string]interface{}{},
// })
// }
}
39 changes: 39 additions & 0 deletions internal/command/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,43 @@ d.e.f`)),
nil,
))

t.Run("Issue 374 empty input", func(t *testing.T) {
tests := []struct {
format string
exp []byte
}{
{
format: "json",
exp: []byte("{}\n"),
},
{
format: "toml",
exp: []byte("\n"),
},
{
format: "yaml",
exp: []byte("{}\n"),
},
{
format: "xml",
exp: []byte("<doc/>\n"),
},
{
format: "csv",
exp: []byte(""),
},
}

for _, test := range tests {
tc := test
t.Run(tc.format, runTest(
[]string{"-r", tc.format},
[]byte(``),
tc.exp,
nil,
nil,
))
}
})

}

0 comments on commit 09ffcb6

Please sign in to comment.