Fixing null csv bug #1404

This commit is contained in:
Mike Farah 2022-10-30 16:29:42 +11:00
parent 0cc5e75432
commit c887042a1b
5 changed files with 90 additions and 4 deletions

View File

@ -1,3 +1,3 @@
name,numberOfCats,likesApples,height
Gary,1,true,168.8
,1,true,168.8
Samantha's Rabbit,2,false,-188.8
1 name numberOfCats likesApples height
2 Gary 1 true 168.8
3 Samantha's Rabbit 2 false -188.8

View File

@ -12,7 +12,9 @@ const csvSimple = `name,numberOfCats,likesApples,height
Gary,1,true,168.8
Samantha's Rabbit,2,false,-188.8
`
const csvMissing = `name,numberOfCats,likesApples,height
,null,,168.8
`
const expectedUpdatedSimpleCsv = `name,numberOfCats,likesApples,height
Gary,3,true,168.8
Samantha's Rabbit,2,false,-188.8
@ -110,6 +112,13 @@ var csvScenarios = []formatScenario{
expected: csvSimpleMissingData,
scenarioType: "encode-csv",
},
{
description: "decode csv missing",
skipDoc: true,
input: csvMissing,
expected: csvMissing,
scenarioType: "roundtrip-csv",
},
{
description: "Parse CSV into an array of objects",
subdescription: "First row is assumed to be the header row.",

View File

@ -249,6 +249,12 @@ func guessTagFromCustomType(node *yaml.Node) string {
}
func parseSnippet(value string) (*yaml.Node, error) {
if value == "" {
return &yaml.Node{
Kind: yaml.ScalarNode,
Tag: "!!null",
}, nil
}
decoder := NewYamlDecoder(ConfiguredYamlPreferences)
err := decoder.Init(strings.NewReader(value))
if err != nil {

View File

@ -1,6 +1,11 @@
package yqlib
import "testing"
import (
"testing"
"github.com/mikefarah/yq/v4/test"
yaml "gopkg.in/yaml.v3"
)
func TestGetLogger(t *testing.T) {
l := GetLogger()
@ -8,3 +13,69 @@ func TestGetLogger(t *testing.T) {
t.Fatal("GetLogger should return the yq logger instance, not a copy")
}
}
type parseSnippetScenario struct {
snippet string
expected *yaml.Node
}
var parseSnippetScenarios = []parseSnippetScenario{
{
snippet: "",
expected: &yaml.Node{
Kind: yaml.ScalarNode,
Tag: "!!null",
},
},
{
snippet: "3",
expected: &yaml.Node{
Kind: yaml.ScalarNode,
Tag: "!!int",
Value: "3",
Line: 1,
Column: 1,
},
},
{
snippet: "cat",
expected: &yaml.Node{
Kind: yaml.ScalarNode,
Tag: "!!str",
Value: "cat",
Line: 1,
Column: 1,
},
},
{
snippet: "3.1",
expected: &yaml.Node{
Kind: yaml.ScalarNode,
Tag: "!!float",
Value: "3.1",
Line: 1,
Column: 1,
},
},
{
snippet: "true",
expected: &yaml.Node{
Kind: yaml.ScalarNode,
Tag: "!!bool",
Value: "true",
Line: 1,
Column: 1,
},
},
}
func TestParseSnippet(t *testing.T) {
for _, tt := range parseSnippetScenarios {
actual, err := parseSnippet(tt.snippet)
if err != nil {
t.Error(tt.snippet)
t.Error(err)
}
test.AssertResultComplexWithContext(t, tt.expected, actual, tt.snippet)
}
}

View File

@ -31,7 +31,7 @@ type expressionScenario struct {
}
func TestMain(m *testing.M) {
logging.SetLevel(logging.DEBUG, "")
logging.SetLevel(logging.ERROR, "")
Now = func() time.Time {
return time.Date(2021, time.May, 19, 1, 2, 3, 4, time.UTC)
}