From d29e9632c2da595e0a0af9704ffb800e4c82d3ef Mon Sep 17 00:00:00 2001 From: subhashish-devtron <129164905+subhashish-devtron@users.noreply.github.com> Date: Wed, 4 Oct 2023 18:59:14 +0530 Subject: [PATCH] fix DT case for hcl parsing with percentage delimiter (#4012) --- pkg/variables/parsers/VariableTemplateParser.go | 9 +++++---- pkg/variables/parsers/VariableTemplateParser_test.go | 6 +++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/pkg/variables/parsers/VariableTemplateParser.go b/pkg/variables/parsers/VariableTemplateParser.go index 19b789263f9..36316ca92b1 100644 --- a/pkg/variables/parsers/VariableTemplateParser.go +++ b/pkg/variables/parsers/VariableTemplateParser.go @@ -213,12 +213,13 @@ func (impl *VariableTemplateParserImpl) convertToHclCompatible(templateType Vari } template = fmt.Sprintf(`{"root":%s}`, template) } - template = impl.diluteExistingHclVars(template) + template = impl.diluteExistingHclVars(template, "$") + template = impl.diluteExistingHclVars(template, "%") return impl.convertToHclExpression(template), nil } -func (impl *VariableTemplateParserImpl) diluteExistingHclVars(template string) string { - hclVarRegex := regexp.MustCompile(`\$\{`) +func (impl *VariableTemplateParserImpl) diluteExistingHclVars(template string, templateControlKeyword string) string { + hclVarRegex := regexp.MustCompile(templateControlKeyword + `\{`) indexesData := hclVarRegex.FindAllIndex([]byte(template), -1) var strBuilder strings.Builder strBuilder.Grow(len(template)) @@ -226,7 +227,7 @@ func (impl *VariableTemplateParserImpl) diluteExistingHclVars(template string) s for _, datum := range indexesData { startIndex := datum[0] endIndex := datum[1] - strBuilder.WriteString(template[currentIndex:startIndex] + "$" + template[startIndex:endIndex]) + strBuilder.WriteString(template[currentIndex:startIndex] + templateControlKeyword + template[startIndex:endIndex]) currentIndex = endIndex } if currentIndex <= len(template) { diff --git a/pkg/variables/parsers/VariableTemplateParser_test.go b/pkg/variables/parsers/VariableTemplateParser_test.go index e233a676f68..fa3ebf142f9 100644 --- a/pkg/variables/parsers/VariableTemplateParser_test.go +++ b/pkg/variables/parsers/VariableTemplateParser_test.go @@ -35,14 +35,14 @@ func TestVariableTemplateParserImpl_ParseTemplate(t *testing.T) { assert.Nil(t, err) templateParser := NewVariableTemplateParserImpl(logger) t.Run("parse template", func(t *testing.T) { - scopedVariables := []*models.ScopedVariableData{{VariableName: "container-port-number-new", VariableValue: models.VariableValue{Value: "1800"}}} + scopedVariables := []*models.ScopedVariableData{{VariableName: "container-port-number-new", VariableValue: &models.VariableValue{Value: "1800"}}} parserResponse := templateParser.ParseTemplate(VariableParserRequest{TemplateType: JsonVariableTemplate, Template: JsonWithIntParam, Variables: scopedVariables}) parsedTemplate := parserResponse.ResolvedTemplate assert.Equal(t, JsonWithIntParamResolvedTemplate, parsedTemplate) }) t.Run("parse stringify template", func(t *testing.T) { - scopedVariables := []*models.ScopedVariableData{{VariableName: "Variable1", VariableValue: models.VariableValue{Value: "123"}}} + scopedVariables := []*models.ScopedVariableData{{VariableName: "Variable1", VariableValue: &models.VariableValue{Value: "123"}}} parserResponse := templateParser.ParseTemplate(VariableParserRequest{TemplateType: StringVariableTemplate, Template: StringTemplate, Variables: scopedVariables}) err = parserResponse.Error assert.Nil(t, err) @@ -50,7 +50,7 @@ func TestVariableTemplateParserImpl_ParseTemplate(t *testing.T) { }) t.Run("parse stringify template with int value", func(t *testing.T) { - scopedVariables := []*models.ScopedVariableData{{VariableName: "container-port-number-new", VariableValue: models.VariableValue{Value: "1800"}}} + scopedVariables := []*models.ScopedVariableData{{VariableName: "container-port-number-new", VariableValue: &models.VariableValue{Value: "1800"}}} parserResponse := templateParser.ParseTemplate(VariableParserRequest{TemplateType: StringVariableTemplate, Template: StringTemplateWithIntParam, Variables: scopedVariables}) err = parserResponse.Error assert.Nil(t, err)