From e02bb7194897edb916a6787fb8f8e5a7f432dd94 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Mon, 14 Nov 2022 16:40:41 +1100 Subject: [PATCH] Fixed parsing of newline character in string expression #1430 --- pkg/yqlib/lexer_participle.go | 4 ++++ pkg/yqlib/lexer_participle_test.go | 21 +++++++++++++++++++++ pkg/yqlib/operator_strings.go | 1 + 3 files changed, 26 insertions(+) diff --git a/pkg/yqlib/lexer_participle.go b/pkg/yqlib/lexer_participle.go index 04f6213a54..41cf7fd7d9 100644 --- a/pkg/yqlib/lexer_participle.go +++ b/pkg/yqlib/lexer_participle.go @@ -352,8 +352,12 @@ func nullValue() yqAction { func stringValue() yqAction { return func(rawToken lexer.Token) (*token, error) { + log.Debug("rawTokenvalue: %v", rawToken.Value) value := unwrap(rawToken.Value) + log.Debug("unwrapped: %v", value) value = strings.ReplaceAll(value, "\\\"", "\"") + value = strings.ReplaceAll(value, "\\n", "\n") + log.Debug("replaced: %v", value) return &token{TokenType: operationToken, Operation: createValueOperation(value, value)}, nil } } diff --git a/pkg/yqlib/lexer_participle_test.go b/pkg/yqlib/lexer_participle_test.go index a1defe41bd..29d65adcf4 100644 --- a/pkg/yqlib/lexer_participle_test.go +++ b/pkg/yqlib/lexer_participle_test.go @@ -620,6 +620,27 @@ var participleLexerScenarios = []participleLexerScenario{ }, }, }, + { + expression: `"string with a\n"`, + tokens: []*token{ + { + TokenType: operationToken, + Operation: &Operation{ + OperationType: valueOpType, + Value: "string with a\n", + StringValue: "string with a\n", + Preferences: nil, + CandidateNode: &CandidateNode{ + Node: &yaml.Node{ + Kind: yaml.ScalarNode, + Tag: "!!str", + Value: "string with a\n", + }, + }, + }, + }, + }, + }, { expression: `"string with a \""`, tokens: []*token{ diff --git a/pkg/yqlib/operator_strings.go b/pkg/yqlib/operator_strings.go index 0ccfbfec8e..716620fe61 100644 --- a/pkg/yqlib/operator_strings.go +++ b/pkg/yqlib/operator_strings.go @@ -432,6 +432,7 @@ func split(value string, spltStr string) *yaml.Node { var contents []*yaml.Node if value != "" { + log.Debug("going to spltStr[%v]", spltStr) var newStrings = strings.Split(value, spltStr) contents = make([]*yaml.Node, len(newStrings))