Skip to content

Commit

Permalink
Merge pull request #6 from kolesa-team/default-values
Browse files Browse the repository at this point in the history
Allows to get the default value for the expression
  • Loading branch information
antonsergeyev authored Jun 1, 2022
2 parents 8e7843e + b3c6cad commit c17b03c
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ Request data:
- storage.SetValue(key, value) - stores a value corresponding to a given key and returns the value. This is useful if you have some entities requested by both id and name, so that you can store the mapping between than and later retrieve it. You can check the samples in [storage](config/persistence/storage/) folder
- storage.GetValue(key) - returns the value corresponding to the given key
- dynamic.Calc(expression) - returns the result of a mathematical or logical expression
- default.Get(value, default) - returns the first value if it's not empty or the default second

> Regex: The regex should contain a group named **value** which will be matched and its value will be returned. E.g. if we want to match the id from this url **`/your/path/4`** the regex should look like **`/your/path/(?P<value>\\d+)`**. Note that in *golang* the named regex group match need to contain a **P** symbol after the question mark. The regex should be prefixed either with **request.url.**, **request.body.** or **response.body.** considering your input. When setting the Persist.Collection field the regex can match multiple records from it's input, which is useful for cases like [users-delete-passingids.json](config/persistence/users-delete-passingids.json)
Expand Down
11 changes: 11 additions & 0 deletions config/getDefault.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"description": "Handle default values. Returns the second (default) value if the first on is empty",
"request": {
"method": "GET",
"path": "/get-default-value"
},
"response": {
"statusCode": 200,
"body": "{{ default.Get(cat, dog) }}"
}
}
60 changes: 60 additions & 0 deletions vars/default_values_filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package vars

import (
"github.com/kolesa-team/http-api-mock/definition"
"github.com/kolesa-team/http-api-mock/persist"
"github.com/kolesa-team/http-api-mock/utils"
"regexp"
"strings"
)

type DefaultValuesFilter struct {
Engines *persist.PersistEngineBag
RegexHelper utils.RegexHelper
}

func (dvf DefaultValuesFilter) Fill(_ *definition.Mock, input string, _ bool) string {
r := regexp.MustCompile(`\{\{\s*default\.([^{]+?)\s*}}`)
if r.MatchString(input) {
input = dvf.Process(r, input)
}

return input
}

func (dvf DefaultValuesFilter) Process(r *regexp.Regexp, input string) string {
return r.ReplaceAllStringFunc(input, func(raw string) string {
found := false
s := ""
tag := strings.Trim(raw[2:len(raw)-2], " ")
if i := strings.Index(tag, "default.Get"); i == 0 {
s, found = dvf.getValue(tag[len("default.Get"):])
}

if !found {
return raw
}
return s
})
}

func (dvf DefaultValuesFilter) getValue(parameters string) (string, bool) {
regexPattern := `\(\s*(?:'|")?(?P<expression>.+?)(?:'|")?\s*\)`

expression, found := dvf.RegexHelper.GetStringPart(parameters, regexPattern, "expression")
if !found {
return "", false
}

values := strings.Split(expression, ",")

if len(values) != 2 {
return "", false
}

if values[0] != "" {
return strings.TrimSpace(values[0]), true
}

return strings.TrimSpace(values[1]), true
}
5 changes: 5 additions & 0 deletions vars/filler_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type FillerFactory interface {
CreateStorageFiller(Engines *persist.PersistEngineBag) Filler
CreatePersistFiller(Engines *persist.PersistEngineBag) Filler
CreateDynamicVarsFiller(Engines *persist.PersistEngineBag) Filler
CreateDefaultValuesFilter(Engines *persist.PersistEngineBag) Filler
}

type MockFillerFactory struct{}
Expand All @@ -36,3 +37,7 @@ func (mff MockFillerFactory) CreatePersistFiller(engines *persist.PersistEngineB
func (mff MockFillerFactory) CreateDynamicVarsFiller(engines *persist.PersistEngineBag) Filler {
return DynamicVarsFiller{Engines: engines, RegexHelper: utils.RegexHelper{}}
}

func (mff MockFillerFactory) CreateDefaultValuesFilter(engines *persist.PersistEngineBag) Filler {
return DefaultValuesFilter{Engines: engines, RegexHelper: utils.RegexHelper{}}
}
2 changes: 2 additions & 0 deletions vars/vars_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func (fp VarsProcessor) Eval(req *definition.Request, m *definition.Mock) {
storageFiller := fp.FillerFactory.CreateStorageFiller(fp.PersistEngines)
persistFiller := fp.FillerFactory.CreatePersistFiller(fp.PersistEngines)
dynamicVarsFiller := fp.FillerFactory.CreateDynamicVarsFiller(fp.PersistEngines)
defaultValuesVarsFiller := fp.FillerFactory.CreateDefaultValuesFilter(fp.PersistEngines)
entityActions := persist.EntityActions{fp.PersistEngines}

fp.walkAndFill(requestFiller, m, true)
Expand All @@ -29,6 +30,7 @@ func (fp VarsProcessor) Eval(req *definition.Request, m *definition.Mock) {

// handle possible dynamic values and use it in storage again
fp.walkAndFill(dynamicVarsFiller, m, true)
fp.walkAndFill(defaultValuesVarsFiller, m, true)
fp.walkAndFill(storageFiller, m, true)

entityActions.ApplyActions(m)
Expand Down

0 comments on commit c17b03c

Please sign in to comment.