Skip to content

Commit 0b7d4b7

Browse files
committed
Fixed CSV content starting with # issue #2076
1 parent 7e17225 commit 0b7d4b7

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

pkg/yqlib/csv_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,20 @@ var csvScenarios = []formatScenario{
186186
expected: expectedYamlFromCSVNoParsing,
187187
scenarioType: "decode-csv-no-auto",
188188
},
189+
{
190+
description: "values starting with #, no auto parse",
191+
skipDoc: true,
192+
input: "value\n#ffff",
193+
expected: "- value: '#ffff'\n",
194+
scenarioType: "decode-csv-no-auto",
195+
},
196+
{
197+
description: "values starting with #",
198+
skipDoc: true,
199+
input: "value\n#ffff",
200+
expected: "- value: #ffff\n",
201+
scenarioType: "decode-csv",
202+
},
189203
{
190204
description: "Scalar roundtrip",
191205
skipDoc: true,

pkg/yqlib/decoder_csv_object.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (dec *csvObjectDecoder) convertToNode(content string) *CandidateNode {
3131
node, err := parseSnippet(content)
3232
// if we're not auto-parsing, then we wont put in parsed objects or arrays
3333
// but we still parse scalars
34-
if err != nil || (!dec.prefs.AutoParse && node.Kind != ScalarNode) {
34+
if err != nil || (!dec.prefs.AutoParse && (node.Kind != ScalarNode || node.Value != content)) {
3535
return createScalarNode(content, content)
3636
}
3737
return node

pkg/yqlib/lib.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,20 @@ func parseSnippet(value string) (*CandidateNode, error) {
9595
if err != nil {
9696
return nil, err
9797
}
98+
99+
if result.Kind == ScalarNode {
100+
result.LineComment = result.LeadingContent
101+
} else {
102+
result.HeadComment = result.LeadingContent
103+
}
104+
result.LeadingContent = ""
105+
98106
if result.Tag == "!!str" {
99107
// use the original string value, as
100108
// decoding drops new lines
101-
return createScalarNode(value, value), nil
109+
newNode := createScalarNode(value, value)
110+
newNode.LineComment = result.LineComment
111+
return newNode, nil
102112
}
103113
result.Line = 0
104114
result.Column = 0

pkg/yqlib/lib_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ var parseSnippetScenarios = []parseSnippetScenario{
6262
Column: 0,
6363
},
6464
},
65+
{
66+
snippet: "# things",
67+
expected: &CandidateNode{
68+
Kind: ScalarNode,
69+
Tag: "!!null",
70+
LineComment: "# things",
71+
Line: 0,
72+
Column: 0,
73+
},
74+
},
6575
{
6676
snippet: "3.1",
6777
expected: &CandidateNode{

0 commit comments

Comments
 (0)