From 0b7d4b799c1b7eee129508beac17ab183e5800dc Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Sat, 29 Jun 2024 15:03:38 +1000 Subject: [PATCH] Fixed CSV content starting with # issue #2076 --- pkg/yqlib/csv_test.go | 14 ++++++++++++++ pkg/yqlib/decoder_csv_object.go | 2 +- pkg/yqlib/lib.go | 12 +++++++++++- pkg/yqlib/lib_test.go | 10 ++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/pkg/yqlib/csv_test.go b/pkg/yqlib/csv_test.go index b56f9aea..a9310c7e 100644 --- a/pkg/yqlib/csv_test.go +++ b/pkg/yqlib/csv_test.go @@ -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, diff --git a/pkg/yqlib/decoder_csv_object.go b/pkg/yqlib/decoder_csv_object.go index 6273fcbb..21eb1369 100644 --- a/pkg/yqlib/decoder_csv_object.go +++ b/pkg/yqlib/decoder_csv_object.go @@ -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 diff --git a/pkg/yqlib/lib.go b/pkg/yqlib/lib.go index d0c81cbc..15561930 100644 --- a/pkg/yqlib/lib.go +++ b/pkg/yqlib/lib.go @@ -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 diff --git a/pkg/yqlib/lib_test.go b/pkg/yqlib/lib_test.go index d87980e7..60fdee01 100644 --- a/pkg/yqlib/lib_test.go +++ b/pkg/yqlib/lib_test.go @@ -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{