Fixed CSV content starting with # issue #2076

This commit is contained in:
Mike Farah 2024-06-29 15:03:38 +10:00
parent 7e1722510a
commit 0b7d4b799c
4 changed files with 36 additions and 2 deletions

View File

@ -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,

View File

@ -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

View File

@ -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

View File

@ -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{