From 60c037f57e5bd6ed960336b5286409e9b4449a57 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Wed, 15 Jun 2022 17:09:17 -0700 Subject: [PATCH] Can add string to scalars in any order #1234 --- pkg/yqlib/operator_add.go | 3 +++ pkg/yqlib/operator_add_test.go | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/pkg/yqlib/operator_add.go b/pkg/yqlib/operator_add.go index a524b9ba..3de2299e 100644 --- a/pkg/yqlib/operator_add.go +++ b/pkg/yqlib/operator_add.go @@ -103,6 +103,9 @@ func addScalars(context Context, target *CandidateNode, lhs *yaml.Node, rhs *yam } else if lhsTag == "!!str" { target.Node.Tag = lhs.Tag target.Node.Value = lhs.Value + rhs.Value + } else if rhsTag == "!!str" { + target.Node.Tag = rhs.Tag + target.Node.Value = lhs.Value + rhs.Value } else if lhsTag == "!!int" && rhsTag == "!!int" { format, lhsNum, err := parseInt64(lhs.Value) if err != nil { diff --git a/pkg/yqlib/operator_add_test.go b/pkg/yqlib/operator_add_test.go index a98e3faf..2b9c6008 100644 --- a/pkg/yqlib/operator_add_test.go +++ b/pkg/yqlib/operator_add_test.go @@ -199,6 +199,24 @@ var addOperatorScenarios = []expressionScenario{ "D0, P[], (doc)::{a: catmeow, b: meow}\n", }, }, + { + description: "String concatenation - str + int", + skipDoc: true, + document: `{a: !cool cat, b: meow}`, + expression: `.a + 3`, + expected: []string{ + "D0, P[a], (!cool)::cat3\n", + }, + }, + { + description: "String concatenation - int + str", + skipDoc: true, + document: `{a: !cool cat, b: meow}`, + expression: `3 + .a`, + expected: []string{ + "D0, P[], (!cool)::3cat\n", + }, + }, { description: "Number addition - float", subdescription: "If the lhs or rhs are floats then the expression will be calculated with floats.",