mirror of
https://github.com/kataras/iris.git
synced 2026-01-08 20:41:57 +00:00
add uint8 parameter type, and mvc and hero - this commit may be a point of tutorial on how to add a completely new type from scratch to the hero for future contributors
Former-commit-id: dc7a7e6c97ef0c644a22e92072e4bdb98ae10582
This commit is contained in:
@@ -26,6 +26,10 @@ const (
|
||||
// Allows only -9223372036854775808 to 9223372036854775807.
|
||||
// Declaration: /mypath/{myparam:int64} or {myparam:long}
|
||||
ParamTypeInt64
|
||||
// ParamTypeUint8 a number type.
|
||||
// Allows only 0 to 255.
|
||||
// Declaration: /mypath/{myparam:uint8}
|
||||
ParamTypeUint8
|
||||
// ParamTypeUint64 a number type.
|
||||
// Allows only 0 to 18446744073709551615.
|
||||
// Declaration: /mypath/{myparam:uint64}
|
||||
@@ -88,6 +92,8 @@ func (pt ParamType) Kind() reflect.Kind {
|
||||
return reflect.Int
|
||||
case ParamTypeInt64:
|
||||
return reflect.Int64
|
||||
case ParamTypeUint8:
|
||||
return reflect.Uint8
|
||||
case ParamTypeUint64:
|
||||
return reflect.Uint64
|
||||
case ParamTypeBoolean:
|
||||
@@ -106,6 +112,8 @@ func ValidKind(k reflect.Kind) bool {
|
||||
fallthrough
|
||||
case reflect.Int64:
|
||||
fallthrough
|
||||
case reflect.Uint8:
|
||||
fallthrough
|
||||
case reflect.Uint64:
|
||||
fallthrough
|
||||
case reflect.Bool:
|
||||
@@ -128,6 +136,7 @@ var paramTypes = map[string]ParamType{
|
||||
"int": ParamTypeNumber, // same as number.
|
||||
"long": ParamTypeInt64,
|
||||
"int64": ParamTypeInt64, // same as long.
|
||||
"uint8": ParamTypeUint8,
|
||||
"uint64": ParamTypeUint64,
|
||||
|
||||
"boolean": ParamTypeBoolean,
|
||||
@@ -149,6 +158,7 @@ var paramTypes = map[string]ParamType{
|
||||
// "string"
|
||||
// "number" or "int"
|
||||
// "long" or "int64"
|
||||
// "uint8"
|
||||
// "uint64"
|
||||
// "boolean" or "bool"
|
||||
// "alphabetical"
|
||||
@@ -179,6 +189,8 @@ func LookupParamTypeFromStd(goType string) ParamType {
|
||||
return ParamTypeNumber
|
||||
case "int64":
|
||||
return ParamTypeInt64
|
||||
case "uint8":
|
||||
return ParamTypeUint8
|
||||
case "uint64":
|
||||
return ParamTypeUint64
|
||||
case "bool":
|
||||
|
||||
@@ -271,6 +271,9 @@ type Map struct {
|
||||
// int64 as int64 type
|
||||
// -9223372036854775808 to 9223372036854775807.
|
||||
Int64 *Macro
|
||||
// uint8 as uint8 type
|
||||
// 0 to 255.
|
||||
Uint8 *Macro
|
||||
// uint64 as uint64 type
|
||||
// 0 to 18446744073709551615.
|
||||
Uint64 *Macro
|
||||
@@ -313,6 +316,7 @@ func NewMap() *Map {
|
||||
// if err == strconv.ErrRange...
|
||||
return err == nil
|
||||
}), //("^-[1-9]|-?[1-9][0-9]{1,14}|-?1000000000000000|-?10000000000000000|-?100000000000000000|-?[1-9]000000000000000000|-?9[0-2]00000000000000000|-?92[0-2]0000000000000000|-?922[0-3]000000000000000|-?9223[0-3]00000000000000|-?92233[0-7]0000000000000|-?922337[0-2]000000000000|-?92233720[0-3]0000000000|-?922337203[0-6]000000000|-?9223372036[0-8]00000000|-?92233720368[0-5]0000000|-?922337203685[0-4]000000|-?9223372036854[0-7]00000|-?92233720368547[0-7]0000|-?922337203685477[0-5]000|-?922337203685477[56]000|[0-9]$")),
|
||||
Uint8: newMacro(MustNewEvaluatorFromRegexp("^([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$")),
|
||||
Uint64: newMacro(func(paramValue string) bool {
|
||||
if !simpleNumberEvalutator(paramValue) {
|
||||
return false
|
||||
@@ -346,6 +350,8 @@ func (m *Map) Lookup(typ ast.ParamType) *Macro {
|
||||
return m.Number
|
||||
case ast.ParamTypeInt64:
|
||||
return m.Int64
|
||||
case ast.ParamTypeUint8:
|
||||
return m.Uint8
|
||||
case ast.ParamTypeUint64:
|
||||
return m.Uint64
|
||||
case ast.ParamTypeBoolean:
|
||||
|
||||
@@ -142,6 +142,37 @@ func TestInt64EvaluatorRaw(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUint8EvaluatorRaw(t *testing.T) {
|
||||
f := NewMap()
|
||||
|
||||
tests := []struct {
|
||||
pass bool
|
||||
input string
|
||||
}{
|
||||
{false, "astring"}, // 0
|
||||
{false, "astringwith_numb3rS_and_symbol$"}, // 1
|
||||
{false, "-9223372036854775808"}, // 2
|
||||
{false, "main.css"}, // 3
|
||||
{false, "/assets/main.css"}, // 4
|
||||
{false, "92233720368547758079223372036854775807"}, // 5
|
||||
{false, "9223372036854775808 9223372036854775808"}, // 6
|
||||
{false, "-1"}, // 7
|
||||
{false, "-0"}, // 8
|
||||
{false, "+1"}, // 9
|
||||
{false, "18446744073709551615"}, // 10
|
||||
{false, "9223372036854775807"}, // 11
|
||||
{false, "021"}, // 12 - no leading zeroes are allowed.
|
||||
{false, "300"}, // 13
|
||||
{true, "0"}, // 14
|
||||
{true, "255"}, // 15
|
||||
{true, "21"}, // 16
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
testEvaluatorRaw(t, f.Uint8, tt.input, tt.pass, i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUint64EvaluatorRaw(t *testing.T) {
|
||||
f := NewMap()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user