Skip to content

Commit

Permalink
Fixed parsing of newline character in string expression #1430
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Nov 14, 2022
1 parent 1fd96e1 commit e02bb71
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/yqlib/lexer_participle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/yqlib/lexer_participle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
1 change: 1 addition & 0 deletions pkg/yqlib/operator_strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down

0 comments on commit e02bb71

Please sign in to comment.