Skip to content

Commit

Permalink
Handle scalars in csv, xml files
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Mar 27, 2023
1 parent 1b0a62d commit 8d516ce
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/yqlib/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
4 changes: 4 additions & 0 deletions pkg/yqlib/encoder_csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions pkg/yqlib/xml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 8d516ce

Please sign in to comment.