From 98d183a0293e6a625abef23f3099bf052f228d82 Mon Sep 17 00:00:00 2001 From: Tommy Situ Date: Wed, 17 Jan 2024 22:51:32 +0000 Subject: [PATCH] Add array data store for templating --- core/templating/template_helpers.go | 18 ++++++++++++++++++ core/templating/templating.go | 5 +++++ core/templating/templating_test.go | 25 ++++++++++++++++++------- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/core/templating/template_helpers.go b/core/templating/template_helpers.go index a6bfc043e..846eac5c3 100644 --- a/core/templating/template_helpers.go +++ b/core/templating/template_helpers.go @@ -24,6 +24,7 @@ type templateHelpers struct { now func() time.Time fakerSource *gofakeit.Faker TemplateDataSource *TemplateDataSource + ArrayData map[string][]string } func (t templateHelpers) nowHelper(offset string, format string) string { @@ -219,6 +220,23 @@ func (t templateHelpers) divide(val1 string, val2 string, format string) string return formatNumber(f1/f2, format) } +func (t templateHelpers) addToArray(key string, value string) string { + if array, ok := t.ArrayData[key]; ok { + t.ArrayData[key] = append(array, value) + } else { + t.ArrayData[key] = []string{value} + } + return value +} + +func (t templateHelpers) getArray(key string) []string { + if array, ok := t.ArrayData[key]; ok { + return array + } else { + return []string{} + } +} + func sumNumbers(numbers []string, format string) string { var sum float64 = 0 for _, number := range numbers { diff --git a/core/templating/templating.go b/core/templating/templating.go index f9424c29b..70fe469c4 100644 --- a/core/templating/templating.go +++ b/core/templating/templating.go @@ -63,10 +63,13 @@ var helpersRegistered = false func NewTemplator() *Templator { templateDataSource := NewTemplateDataSource() + arrayData := make(map[string][]string) + t := templateHelpers{ now: time.Now, fakerSource: gofakeit.New(0), TemplateDataSource: templateDataSource, + ArrayData: arrayData, } helperMethodMap := make(map[string]interface{}) helperMethodMap["now"] = t.nowHelper @@ -92,6 +95,8 @@ func NewTemplator() *Templator { helperMethodMap["subtract"] = t.subtract helperMethodMap["multiply"] = t.multiply helperMethodMap["divide"] = t.divide + helperMethodMap["addToArray"] = t.addToArray + helperMethodMap["getArray"] = t.getArray if !helpersRegistered { raymond.RegisterHelpers(helperMethodMap) helpersRegistered = true diff --git a/core/templating/templating_test.go b/core/templating/templating_test.go index 57df99a5b..b06ef9da7 100644 --- a/core/templating/templating_test.go +++ b/core/templating/templating_test.go @@ -557,7 +557,7 @@ func Test_VarSetToProperValueInCaseOfRequestDetailsPassedAsArgument(t *testing.T func Test_ApplyTemplate_add_integers(t *testing.T) { RegisterTestingT(t) - template, err := ApplyTemplate(&models.RequestDetails{}, make(map[string]string), `{{ add '1' '2' '0'}}`) + template, err := ApplyTemplate(&models.RequestDetails{}, make(map[string]string), `{{ add 1 2 '0'}}`) Expect(err).To(BeNil()) @@ -567,7 +567,7 @@ func Test_ApplyTemplate_add_integers(t *testing.T) { func Test_ApplyTemplate_add_floats(t *testing.T) { RegisterTestingT(t) - template, err := ApplyTemplate(&models.RequestDetails{}, make(map[string]string), `{{ add '0.1' '1.34' '0.00'}}`) + template, err := ApplyTemplate(&models.RequestDetails{}, make(map[string]string), `{{ add 0.1 1.34 '0.00'}}`) Expect(err).To(BeNil()) @@ -577,7 +577,7 @@ func Test_ApplyTemplate_add_floats(t *testing.T) { func Test_ApplyTemplate_add_floats_withRoundUp(t *testing.T) { RegisterTestingT(t) - template, err := ApplyTemplate(&models.RequestDetails{}, make(map[string]string), `{{ add '0.1' '1.34' '0.0'}} and {{ add '0.1' '1.56' '0.0'}}`) + template, err := ApplyTemplate(&models.RequestDetails{}, make(map[string]string), `{{ add 0.1 1.34 '0.0'}} and {{ add 0.1 1.56 '0.0'}}`) Expect(err).To(BeNil()) @@ -587,7 +587,7 @@ func Test_ApplyTemplate_add_floats_withRoundUp(t *testing.T) { func Test_ApplyTemplate_add_number_without_format(t *testing.T) { RegisterTestingT(t) - template, err := ApplyTemplate(&models.RequestDetails{}, make(map[string]string), `{{ add '0.1' '1.34' ''}} and {{ add '1' '2' ''}} and {{ add '0' '0' ''}}`) + template, err := ApplyTemplate(&models.RequestDetails{}, make(map[string]string), `{{ add 0.1 1.34 ''}} and {{ add 1 2 ''}} and {{ add 0 0 ''}}`) Expect(err).To(BeNil()) @@ -607,7 +607,7 @@ func Test_ApplyTemplate_add_NotNumber(t *testing.T) { func Test_ApplyTemplate_subtract_numbers(t *testing.T) { RegisterTestingT(t) - template, err := ApplyTemplate(&models.RequestDetails{}, make(map[string]string), `{{ subtract '10' '0.99' ''}}`) + template, err := ApplyTemplate(&models.RequestDetails{}, make(map[string]string), `{{ subtract 10 0.99 ''}}`) Expect(err).To(BeNil()) @@ -617,7 +617,7 @@ func Test_ApplyTemplate_subtract_numbers(t *testing.T) { func Test_ApplyTemplate_mutiply_numbers(t *testing.T) { RegisterTestingT(t) - template, err := ApplyTemplate(&models.RequestDetails{}, make(map[string]string), `{{ multiply '10' '0.99' ''}}`) + template, err := ApplyTemplate(&models.RequestDetails{}, make(map[string]string), `{{ multiply 10 0.99 ''}}`) Expect(err).To(BeNil()) @@ -627,13 +627,24 @@ func Test_ApplyTemplate_mutiply_numbers(t *testing.T) { func Test_ApplyTemplate_divide_numbers(t *testing.T) { RegisterTestingT(t) - template, err := ApplyTemplate(&models.RequestDetails{}, make(map[string]string), `{{ divide '10' '2.5' ''}}`) + template, err := ApplyTemplate(&models.RequestDetails{}, make(map[string]string), `{{ divide 10 2.5 ''}}`) Expect(err).To(BeNil()) Expect(template).To(Equal("4")) } +func Test_ApplyTemplate_Arithmetic_Ops_With_Each_Block(t *testing.T) { + RegisterTestingT(t) + + template, err := ApplyTemplate(&models.RequestDetails{ + Body: `{"lineitems":{"lineitem":[{"upc":"1001","quantity":"1","price":"3.50"},{"upc":"1002","quantity":"2","price":"4.50"}]}}`, + }, make(map[string]string), `{{#each (Request.Body 'jsonpath' '$.lineitems.lineitem') }} {{ addToArray 'subtotal' (multiply (this.price) (this.quantity) '') }} {{/each}} total: {{ sum (getArray 'subtotal') '0.00' }}`) + + Expect(err).To(BeNil()) + Expect(template).To(Equal(` 3.5 9 total: 12.50`)) +} + func toInterfaceSlice(arguments []string) []interface{} { argumentsArray := make([]interface{}, len(arguments))