Skip to content

Commit

Permalink
Templating execution context should have its own data store
Browse files Browse the repository at this point in the history
  • Loading branch information
tommysitu committed Mar 13, 2024
1 parent ea3e3da commit 1eadd50
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
15 changes: 8 additions & 7 deletions core/templating/template_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ 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 {
Expand Down Expand Up @@ -220,17 +219,19 @@ 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)
func (t templateHelpers) addToArray(key string, value string, options *raymond.Options) string {
arrayData := options.ValueFromAllCtx("ArrayData").(map[string][]string)
if array, ok := arrayData[key]; ok {
arrayData[key] = append(array, value)
} else {
t.ArrayData[key] = []string{value}
arrayData[key] = []string{value}
}
return value
}

func (t templateHelpers) getArray(key string) []string {
if array, ok := t.ArrayData[key]; ok {
func (t templateHelpers) getArray(key string, options *raymond.Options) []string {
arrayData := options.ValueFromAllCtx("ArrayData").(map[string][]string)
if array, ok := arrayData[key]; ok {
return array
} else {
return []string{}
Expand Down
5 changes: 3 additions & 2 deletions core/templating/templating.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type TemplatingData struct {
Literals map[string]interface{}
Vars map[string]interface{}
Journal Journal
ArrayData map[string][]string
}

type Request struct {
Expand Down Expand Up @@ -63,13 +64,11 @@ 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
Expand Down Expand Up @@ -163,6 +162,7 @@ func (t *Templator) NewTemplatingData(requestDetails *models.RequestDetails, lit

}

arrayData := make(map[string][]string)
return &TemplatingData{
Request: getRequest(requestDetails),
Literals: literalMap,
Expand All @@ -172,6 +172,7 @@ func (t *Templator) NewTemplatingData(requestDetails *models.RequestDetails, lit
CurrentDateTime: func(a1, a2, a3 string) string {
return a1 + " " + a2 + " " + a3
},
ArrayData: arrayData,
}

}
Expand Down
18 changes: 15 additions & 3 deletions core/templating/templating_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,12 +637,24 @@ func Test_ApplyTemplate_divide_numbers(t *testing.T) {
func Test_ApplyTemplate_Arithmetic_Ops_With_Each_Block(t *testing.T) {
RegisterTestingT(t)

template, err := ApplyTemplate(&models.RequestDetails{
templator := templating.NewTemplator()

requestDetails := &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' }}`)
}
responseBody := `{{#each (Request.Body 'jsonpath' '$.lineitems.lineitem') }} {{ addToArray 'subtotal' (multiply (this.price) (this.quantity) '') }} {{/each}} total: {{ sum (getArray 'subtotal') '0.00' }}`

template, _ := templator.ParseTemplate(responseBody)
state := make(map[string]string)
result, err := templator.RenderTemplate(template, requestDetails, &models.Literals{}, &models.Variables{}, state, &journal.Journal{})

Expect(err).To(BeNil())
Expect(result).To(Equal(` 3.5 9 total: 12.50`))

// Running the second time should produce the same result because each execution has its own context data.
result, err = templator.RenderTemplate(template, requestDetails, &models.Literals{}, &models.Variables{}, state, &journal.Journal{})
Expect(err).To(BeNil())
Expect(template).To(Equal(` 3.5 9 total: 12.50`))
Expect(result).To(Equal(` 3.5 9 total: 12.50`))
}

func toInterfaceSlice(arguments []string) []interface{} {
Expand Down

0 comments on commit 1eadd50

Please sign in to comment.