Handle scalars in csv, xml files

This commit is contained in:
Mike Farah 2023-03-27 13:54:24 +11:00
parent 1b0a62d08d
commit 8d516ce535
3 changed files with 20 additions and 0 deletions

View File

@ -126,6 +126,14 @@ var csvScenarios = []formatScenario{
expected: expectedYamlFromCSV,
scenarioType: "decode-csv-object",
},
{
description: "Scalar roundtrip",
skipDoc: true,
input: "mike\ncat",
expression: ".[0].mike",
expected: "cat\n",
scenarioType: "roundtrip-csv",
},
{
description: "Parse TSV into an array of objects",
subdescription: "First row is assumed to be the header row.",

View File

@ -103,6 +103,10 @@ func (e *csvEncoder) encodeObjects(csvWriter *csv.Writer, content []*yaml.Node)
}
func (e *csvEncoder) Encode(writer io.Writer, originalNode *yaml.Node) error {
if originalNode.Kind == yaml.ScalarNode {
return writeString(writer, originalNode.Value+"\n")
}
csvWriter := csv.NewWriter(writer)
csvWriter.Comma = e.separator

View File

@ -301,6 +301,14 @@ var xmlScenarios = []formatScenario{
expected: expectedXmlProcInstAndHeadComment,
scenarioType: "encode",
},
{
description: "Scalar roundtrip",
skipDoc: true,
input: "<mike>cat</mike>",
expression: ".mike",
expected: "cat",
scenarioType: "roundtrip",
},
{
description: "ProcInst with head comment round trip",
skipDoc: true,