diff --git a/pkg/yqlib/csv_test.go b/pkg/yqlib/csv_test.go index b56f9aea54..a9310c7ec1 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 6273fcbb92..21eb136987 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 d0c81cbc0e..155619307b 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 d87980e734..60fdee0190 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{