1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-23 12:57:05 +00:00
Fix some things on an experimental future feature.
Still dirty code ofc, it will take a long to be adopted, but new
developers can watch the backstage's step-by-step test-driven solutions.


Former-commit-id: 739260b8662bff7164ba75ef36c81a6b564d53cc
This commit is contained in:
Gerasimos (Makis) Maropoulos
2017-03-22 05:07:30 +02:00
parent 1ac6f08a97
commit 5d518d4440
4 changed files with 115 additions and 48 deletions

View File

@@ -73,12 +73,15 @@ func ParseParam(source string) (*ParamTmpl, error) {
// id:int min(1) max(5)
// id:int range(1,5)!404 or !404, space doesn't matters on fail error code.
cursor := 0
// hasFuncs setted to true when we validate that we have macro's functions
// waitForFunc setted to true when we validate that we have macro's functions
// so we can check for parenthesis.
// We need that check because the user may add a regexp with parenthesis.
// Although this will not be recommended, user is better to create a macro for its regexp
// in order to use it everywhere and reduce code duplication.
hasFuncs := false
waitForFunc := false
// when inside macro func we don't need to check for anything else, because it could
// break the tmpl, i.e FuncSeperator (space) if "contains( )".
insideFunc := false
for i := 0; i < len(source); i++ {
if source[i] == ParamNameSeperator {
if i+1 >= len(source) {
@@ -101,20 +104,26 @@ func ParseParam(source string) (*ParamTmpl, error) {
cursor = i + 1
continue
}
// TODO: find a better way instead of introducing variables like waitForFunc, insideFunc,
// one way is to move the functions with the reverse order but this can fix the problem for now
// later it will introduce new bugs, we can find a better static way to check these things, tomorrow.
// int ...
if source[i] == FuncSeperator {
if !waitForFunc && source[i] == FuncSeperator {
// take the left part: int if it's the first
// space after the param name
if t.Macro.Name == t.Expression {
hasFuncs = true
t.Macro.Name = source[cursor:i]
} // else we have one or more functions, skip.
waitForFunc = true
cursor = i + 1
continue
}
if hasFuncs && source[i] == FuncStart {
// if not inside a func body
// the cursor is a point which can receive a func
// starts with (
if !insideFunc && waitForFunc && source[i] == FuncStart {
insideFunc = true
// take the left part: range
funcName := source[cursor:i]
t.Macro.Funcs = append(t.Macro.Funcs, MacroFuncTmpl{Name: funcName})
@@ -123,7 +132,8 @@ func ParseParam(source string) (*ParamTmpl, error) {
continue
}
// 1,5)
if hasFuncs && source[i] == FuncEnd {
// we are inside func and )
if insideFunc && source[i] == FuncEnd {
// check if we have end parenthesis but not start
if len(t.Macro.Funcs) == 0 {
return nil, fmt.Errorf("missing start macro's '%s' function, on source '%s'", t.Macro.Name, source)
@@ -131,11 +141,15 @@ func ParseParam(source string) (*ParamTmpl, error) {
// take the left part, between Start and End: 1,5
funcParamsStr := source[cursor:i]
println("param_parser.go:41: '" + funcParamsStr + "'")
funcParams := strings.SplitN(funcParamsStr, string(FuncParamSeperator), -1)
t.Macro.Funcs[len(t.Macro.Funcs)-1].Params = funcParams
cursor = i + 1
insideFunc = false // ignore ')' until new '('
waitForFunc = false // wait for the next space to not ignore '('
continue
}