Skip to content

Commit

Permalink
Merge pull request #103 from TomWright/issue-102
Browse files Browse the repository at this point in the history
Issue 102
  • Loading branch information
TomWright authored Mar 11, 2021
2 parents fe4da3c + a4cf1e8 commit 69e9072
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 24 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Nothing yet.

## [v1.13.4] - 2021-03-11

### Fixed

- Empty document input is now treated different in select and put commands.
- https://github.com/TomWright/dasel/issues/99
- https://github.com/TomWright/dasel/issues/102

## [v1.13.3] - 2021-03-05

### Fixed
Expand Down Expand Up @@ -268,7 +276,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Everything!

[unreleased]: https://github.com/TomWright/dasel/compare/v1.13.3...HEAD
[unreleased]: https://github.com/TomWright/dasel/compare/v1.13.4...HEAD
[v1.13.4]: https://github.com/TomWright/dasel/compare/v1.13.3...v1.13.4
[v1.13.3]: https://github.com/TomWright/dasel/compare/v1.13.2...v1.13.3
[v1.13.2]: https://github.com/TomWright/dasel/compare/v1.13.1...v1.13.2
[v1.13.1]: https://github.com/TomWright/dasel/compare/v1.13.0...v1.13.1
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ An important note is that if no sub-command is given, dasel will default to `sel
dasel select -f <file> -p <parser> -r <read_parser> -w <write_parser> -m <selector>
```
Select treats blank input as an empty map.
#### Arguments
##### `-f`, `--file`
Expand Down
5 changes: 5 additions & 0 deletions internal/command/root_put_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,11 @@ metadata:
labels:
node.longhorn.io/create-default-disk: config
`, nil))

// https://github.com/TomWright/dasel/issues/102
// Worked in v1.13.2
t.Run("BlankInput", putStringTest(``, "yaml", `[0].job_name`, "logging", `- job_name: logging
`, nil))
}

func TestRootCMD_Put_TOML(t *testing.T) {
Expand Down
11 changes: 11 additions & 0 deletions internal/command/root_select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,11 @@ func TestRootCmd_Select_JSON(t *testing.T) {
6
4`), nil, "--length", "-m"))

t.Run("NullInput", selectTest(`null`, "json", `.`, newline("{}"), nil))
t.Run("EmptyDocument", selectTest(`{}`, "json", `.`, newline("{}"), nil))
t.Run("EmptyArray", selectTest(`[]`, "json", `.`, newline("[]"), nil))
t.Run("BlankInput", selectTest(``, "json", `.`, newline("{}"), nil))

}

func TestRootCmd_Select_YAML(t *testing.T) {
Expand Down Expand Up @@ -492,6 +497,12 @@ spec:
timeoutSeconds: 5
periodSeconds: 5
`, "yaml", "spec.template.spec.containers.(name=harbor-exporter).env.(name=HARBOR_URI).value", newline(`http://harbor-core.harbor`), nil))

// https://github.com/TomWright/dasel/issues/99
// Worked in v1.13.3
t.Run("NullInput", selectTest(`null`, "yaml", `.`, newline("{}"), nil))
t.Run("EmptyDocument", selectTest(`---`, "yaml", `.`, newline("{}"), nil))
t.Run("BlankInput", selectTest(``, "yaml", `.`, newline("{}"), nil))
}

func TestRootCmd_Select_TOML(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions internal/command/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ func runSelectCommand(opts selectOptions, cmd *cobra.Command) error {
return err
}

if !rootNode.Value.IsValid() {
rootNode = dasel.New(&storage.BasicSingleDocument{
Value: map[string]interface{}{},
})
}

if opts.Writer == nil {
opts.Writer = cmd.OutOrStdout()
}
Expand Down
6 changes: 1 addition & 5 deletions internal/storage/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@ func (p *CSVParser) FromBytes(byteData []byte) (interface{}, error) {
return nil, fmt.Errorf("could not read csv file: %w", err)
}
if len(records) == 0 {
// If there's no content then return an empty CSV document.
return &CSVDocument{
Value: res,
Headers: []string{},
}, nil
return nil, nil
}
var headers []string
for i, row := range records {
Expand Down
3 changes: 1 addition & 2 deletions internal/storage/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ docLoop:
}
switch len(res) {
case 0:
// If no document is found return an empty single document.
return &BasicSingleDocument{Value: map[string]interface{}{}}, nil
return nil, nil
case 1:
return &BasicSingleDocument{Value: res[0]}, nil
default:
Expand Down
7 changes: 2 additions & 5 deletions internal/storage/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,8 @@ func TestJSONParser_FromBytes(t *testing.T) {
t.Errorf("unexpected error: %s", err)
return
}
exp := &storage.BasicSingleDocument{
Value: map[string]interface{}{},
}
if !reflect.DeepEqual(exp, got) {
t.Errorf("expected %v, got %v", exp, got)
if !reflect.DeepEqual(nil, got) {
t.Errorf("expected %v, got %v", nil, got)
}
})
}
Expand Down
7 changes: 1 addition & 6 deletions internal/storage/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,11 @@ docLoop:

formattedDocData := cleanupYamlMapValue(docData)

// Insert empty documents instead of nils.
if formattedDocData == nil {
formattedDocData = map[string]interface{}{}
}
res = append(res, formattedDocData)
}
switch len(res) {
case 0:
// If no document is found return an empty single document.
return &BasicSingleDocument{Value: map[string]interface{}{}}, nil
return nil, nil
case 1:
return &BasicSingleDocument{Value: res[0]}, nil
default:
Expand Down
7 changes: 2 additions & 5 deletions internal/storage/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,8 @@ func TestYAMLParser_FromBytes(t *testing.T) {
t.Errorf("unexpected error: %s", err)
return
}
exp := &storage.BasicSingleDocument{
Value: map[string]interface{}{},
}
if !reflect.DeepEqual(exp, got) {
t.Errorf("expected %v, got %v", exp, got)
if !reflect.DeepEqual(nil, got) {
t.Errorf("expected %v, got %v", nil, got)
}
})
}
Expand Down

0 comments on commit 69e9072

Please sign in to comment.