1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-21 20:07:04 +00:00

support more than string and int at macro functions route path input arguments: int,uint8,uint16,uint32,int8,int32,int64,slice of strings and string

Former-commit-id: d29c4fbe5926bac590151322a585f68b394ff72d
This commit is contained in:
Gerasimos (Makis) Maropoulos
2018-08-23 17:29:39 +03:00
parent 18c23e7d1e
commit ef5f383227
6 changed files with 119 additions and 60 deletions

View File

@@ -1,9 +1,7 @@
package ast
import (
"fmt"
"reflect"
"strconv"
)
// ParamType is a specific uint8 type
@@ -206,24 +204,6 @@ type ParamStatement struct {
ErrorCode int // 404
}
// ParamFuncArg represents a single parameter function's argument
type ParamFuncArg interface{}
// ParamFuncArgToInt converts and returns
// any type of "a", to an integer.
func ParamFuncArgToInt(a ParamFuncArg) (int, error) {
switch a.(type) {
case int:
return a.(int), nil
case string:
return strconv.Atoi(a.(string))
case int64:
return int(a.(int64)), nil
default:
return -1, fmt.Errorf("unexpected function argument type: %q", a)
}
}
// ParamFunc holds the name of a parameter's function
// and its arguments (values)
// A param func is declared with:
@@ -233,6 +213,6 @@ func ParamFuncArgToInt(a ParamFuncArg) (int, error) {
// the 1 and 5 are the two param function arguments
// range(1,5)
type ParamFunc struct {
Name string // range
Args []ParamFuncArg // [1,5]
Name string // range
Args []string // ["1","5"]
}

View File

@@ -82,10 +82,16 @@ const (
DefaultParamType = ast.ParamTypeString
)
func parseParamFuncArg(t token.Token) (a ast.ParamFuncArg, err error) {
if t.Type == token.INT {
return ast.ParamFuncArgToInt(t.Literal)
}
// func parseParamFuncArg(t token.Token) (a ast.ParamFuncArg, err error) {
// if t.Type == token.INT {
// return ast.ParamFuncArgToInt(t.Literal)
// }
// // act all as strings here, because of int vs int64 vs uint64 and etc.
// return t.Literal, nil
// }
func parseParamFuncArg(t token.Token) (a string, err error) {
// act all as strings here, because of int vs int64 vs uint64 and etc.
return t.Literal, nil
}
@@ -143,25 +149,14 @@ func (p *ParamParser) Parse() (*ast.ParamStatement, error) {
argValTok := l.NextDynamicToken() // catch anything from "(" and forward, until ")", because we need to
// be able to use regex expression as a macro type's func argument too.
argVal, err := parseParamFuncArg(argValTok)
if err != nil {
p.appendErr("[%d:%d] expected param func argument to be a string or number but got %s", t.Start, t.End, argValTok.Literal)
continue
}
// fmt.Printf("argValTok: %#v\n", argValTok)
// fmt.Printf("argVal: %#v\n", argVal)
lastParamFunc.Args = append(lastParamFunc.Args, argVal)
lastParamFunc.Args = append(lastParamFunc.Args, argValTok.Literal)
case token.COMMA:
argValTok := l.NextToken()
argVal, err := parseParamFuncArg(argValTok)
if err != nil {
p.appendErr("[%d:%d] expected param func argument to be a string or number type but got %s", t.Start, t.End, argValTok.Literal)
continue
}
lastParamFunc.Args = append(lastParamFunc.Args, argVal)
lastParamFunc.Args = append(lastParamFunc.Args, argValTok.Literal)
case token.RPAREN:
stmt.Funcs = append(stmt.Funcs, lastParamFunc)
lastParamFunc = ast.ParamFunc{} // reset

View File

@@ -53,10 +53,10 @@ func TestParseParam(t *testing.T) {
Funcs: []ast.ParamFunc{
{
Name: "min",
Args: []ast.ParamFuncArg{1}},
Args: []string{"1"}},
{
Name: "max",
Args: []ast.ParamFuncArg{5}},
Args: []string{"5"}},
},
ErrorCode: 404,
}}, // 0
@@ -69,7 +69,7 @@ func TestParseParam(t *testing.T) {
Funcs: []ast.ParamFunc{
{
Name: "range",
Args: []ast.ParamFuncArg{1, 5}},
Args: []string{"1", "5"}},
},
ErrorCode: 404,
}}, // 1
@@ -81,7 +81,7 @@ func TestParseParam(t *testing.T) {
Funcs: []ast.ParamFunc{
{
Name: "contains",
Args: []ast.ParamFuncArg{"."}},
Args: []string{"."}},
},
ErrorCode: 404,
}}, // 2
@@ -189,10 +189,10 @@ func TestParse(t *testing.T) {
Funcs: []ast.ParamFunc{
{
Name: "min",
Args: []ast.ParamFuncArg{1}},
Args: []string{"1"}},
{
Name: "max",
Args: []ast.ParamFuncArg{5}},
Args: []string{"5"}},
},
ErrorCode: 404,
},
@@ -205,7 +205,7 @@ func TestParse(t *testing.T) {
Funcs: []ast.ParamFunc{
{
Name: "range",
Args: []ast.ParamFuncArg{1, 5}},
Args: []string{"1", "5"}},
},
ErrorCode: 404,
},
@@ -218,7 +218,7 @@ func TestParse(t *testing.T) {
Funcs: []ast.ParamFunc{
{
Name: "contains",
Args: []ast.ParamFuncArg{"."}},
Args: []string{"."}},
},
ErrorCode: 404,
},