Skip to content

Commit

Permalink
Merge pull request #366 from TomWright/csv-writer-map
Browse files Browse the repository at this point in the history
Fix an parsing issue in the CSV formatter
  • Loading branch information
TomWright committed Oct 18, 2023
2 parents 2b1bf11 + 3f489f6 commit 155933f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Resolved an issue with YAML parser that was causing strings to be read as booleans.
- Fix a parsing issue with CSV types that forced you to expand and merge in order for it selects to work [Issue 364](https://github.com/TomWright/dasel/issues/364).

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

Expand Down
24 changes: 24 additions & 0 deletions internal/command/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,28 @@ octal: 8`)),
nil,
))

t.Run("Issue364 - CSV root element part 1", runTest(
[]string{"-r", "csv", "-w", "csv", "all().merge()"},
[]byte(`A,B,C
a,b,c
d,e,f`),
newline([]byte(`A,B,C
a,b,c
d,e,f`)),
nil,
nil,
))

t.Run("Issue364 - CSV root element part 2", runTest(
[]string{"-r", "csv", "-w", "csv"},
[]byte(`A,B,C
a,b,c
d,e,f`),
newline([]byte(`A,B,C
a,b,c
d,e,f`)),
nil,
nil,
))

}
30 changes: 27 additions & 3 deletions storage/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (p *CSVParser) FromBytes(byteData []byte) (dasel.Value, error) {
}

reader := csv.NewReader(bytes.NewBuffer(byteData))
res := make([]map[string]interface{}, 0)
res := make([]*dencoding.Map, 0)
records, err := reader.ReadAll()
if err != nil {
return dasel.Value{}, fmt.Errorf("could not read csv file: %w", err)
Expand All @@ -47,13 +47,13 @@ func (p *CSVParser) FromBytes(byteData []byte) (dasel.Value, error) {
headers = row
continue
}
rowRes := make(map[string]interface{})
rowRes := dencoding.NewMap()
allEmpty := true
for index, val := range row {
if val != "" {
allEmpty = false
}
rowRes[headers[index]] = val
rowRes.Set(headers[index], val)
}
if !allEmpty {
res = append(res, rowRes)
Expand Down Expand Up @@ -124,6 +124,30 @@ func interfaceToCSVDocument(val interface{}) (*CSVDocument, error) {
Headers: headers,
}, nil

case []*dencoding.Map:
mapVals := make([]map[string]interface{}, 0)
headers := make([]string, 0)
for _, val := range v {
mapVals = append(mapVals, val.UnorderedData())
for _, objectKey := range val.Keys() {
found := false
for _, existingHeader := range headers {
if existingHeader == objectKey {
found = true
break
}
}
if !found {
headers = append(headers, objectKey)
}
}
}
sort.Strings(headers)
return &CSVDocument{
Value: mapVals,
Headers: headers,
}, nil

default:
return nil, fmt.Errorf("CSVParser.toBytes cannot handle type %T", val)
}
Expand Down
17 changes: 8 additions & 9 deletions storage/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package storage_test

import (
"github.com/tomwright/dasel/v2"
"github.com/tomwright/dasel/v2/dencoding"
"github.com/tomwright/dasel/v2/storage"
"reflect"
"testing"
Expand All @@ -11,15 +12,13 @@ var csvBytes = []byte(`id,name
1,Tom
2,Jim
`)
var csvMap = []map[string]interface{}{
{
"id": "1",
"name": "Tom",
},
{
"id": "2",
"name": "Jim",
},
var csvMap = []*dencoding.Map{
dencoding.NewMap().
Set("id", "1").
Set("name", "Tom"),
dencoding.NewMap().
Set("id", "2").
Set("name", "Jim"),
}

func TestCSVParser_FromBytes(t *testing.T) {
Expand Down

0 comments on commit 155933f

Please sign in to comment.