Skip to content

Commit

Permalink
Fixed CSV content starting with # issue #2076
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Jun 29, 2024
1 parent 7e17225 commit 0b7d4b7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
14 changes: 14 additions & 0 deletions pkg/yqlib/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,20 @@ var csvScenarios = []formatScenario{
expected: expectedYamlFromCSVNoParsing,
scenarioType: "decode-csv-no-auto",
},
{
description: "values starting with #, no auto parse",
skipDoc: true,
input: "value\n#ffff",
expected: "- value: '#ffff'\n",
scenarioType: "decode-csv-no-auto",
},
{
description: "values starting with #",
skipDoc: true,
input: "value\n#ffff",
expected: "- value: #ffff\n",
scenarioType: "decode-csv",
},
{
description: "Scalar roundtrip",
skipDoc: true,
Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/decoder_csv_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (dec *csvObjectDecoder) convertToNode(content string) *CandidateNode {
node, err := parseSnippet(content)
// if we're not auto-parsing, then we wont put in parsed objects or arrays
// but we still parse scalars
if err != nil || (!dec.prefs.AutoParse && node.Kind != ScalarNode) {
if err != nil || (!dec.prefs.AutoParse && (node.Kind != ScalarNode || node.Value != content)) {
return createScalarNode(content, content)
}
return node
Expand Down
12 changes: 11 additions & 1 deletion pkg/yqlib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,20 @@ func parseSnippet(value string) (*CandidateNode, error) {
if err != nil {
return nil, err
}

if result.Kind == ScalarNode {
result.LineComment = result.LeadingContent
} else {
result.HeadComment = result.LeadingContent
}
result.LeadingContent = ""

if result.Tag == "!!str" {
// use the original string value, as
// decoding drops new lines
return createScalarNode(value, value), nil
newNode := createScalarNode(value, value)
newNode.LineComment = result.LineComment
return newNode, nil
}
result.Line = 0
result.Column = 0
Expand Down
10 changes: 10 additions & 0 deletions pkg/yqlib/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ var parseSnippetScenarios = []parseSnippetScenario{
Column: 0,
},
},
{
snippet: "# things",
expected: &CandidateNode{
Kind: ScalarNode,
Tag: "!!null",
LineComment: "# things",
Line: 0,
Column: 0,
},
},
{
snippet: "3.1",
expected: &CandidateNode{
Expand Down

0 comments on commit 0b7d4b7

Please sign in to comment.