forked from Arrim/http-api-mock
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from kolesa-team/default-values
Allows to get the default value for the expression
- Loading branch information
Showing
5 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) }}" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters