Skip to content

Commit

Permalink
Add array data store for templating
Browse files Browse the repository at this point in the history
  • Loading branch information
tommysitu committed Mar 12, 2024
1 parent e8ce758 commit 98d183a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
18 changes: 18 additions & 0 deletions core/templating/template_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions core/templating/templating.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
25 changes: 18 additions & 7 deletions core/templating/templating_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand All @@ -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())

Expand All @@ -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())

Expand All @@ -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())

Expand All @@ -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())

Expand All @@ -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())

Expand All @@ -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))

Expand Down

0 comments on commit 98d183a

Please sign in to comment.