add int8, int16, int32, uint, uint16 and uint32 default-builtn parameter types and macros - no doc update - no live tests yet - time for sleep

Former-commit-id: 4a27265a9f1368c4bbecd852691155e56c875673
This commit is contained in:
Gerasimos (Makis) Maropoulos 2018-09-27 06:20:03 +03:00
parent d6d27b2605
commit f05ee872d0
4 changed files with 222 additions and 17 deletions

View File

@ -91,32 +91,59 @@ var (
},
reflect.Int: func(paramIndex int) interface{} {
return func(ctx Context) int {
v, _ := ctx.Params().GetEntryAt(paramIndex).IntDefault(0)
return v
// v, _ := ctx.Params().GetEntryAt(paramIndex).IntDefault(0)
// return v
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(int)
}
},
reflect.Int8: func(paramIndex int) interface{} {
return func(ctx Context) int8 {
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(int8)
}
},
reflect.Int16: func(paramIndex int) interface{} {
return func(ctx Context) int16 {
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(int16)
}
},
reflect.Int32: func(paramIndex int) interface{} {
return func(ctx Context) int32 {
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(int32)
}
},
reflect.Int64: func(paramIndex int) interface{} {
return func(ctx Context) int64 {
v, _ := ctx.Params().GetEntryAt(paramIndex).Int64Default(0)
return v
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(int64)
}
},
reflect.Uint: func(paramIndex int) interface{} {
return func(ctx Context) uint {
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(uint)
}
},
reflect.Uint8: func(paramIndex int) interface{} {
return func(ctx Context) uint8 {
v, _ := ctx.Params().GetEntryAt(paramIndex).Uint8Default(0)
return v
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(uint8)
}
},
reflect.Uint16: func(paramIndex int) interface{} {
return func(ctx Context) uint16 {
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(uint16)
}
},
reflect.Uint32: func(paramIndex int) interface{} {
return func(ctx Context) uint32 {
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(uint32)
}
},
reflect.Uint64: func(paramIndex int) interface{} {
return func(ctx Context) uint64 {
v, _ := ctx.Params().GetEntryAt(paramIndex).Uint64Default(0)
return v
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(uint64)
}
},
reflect.Bool: func(paramIndex int) interface{} {
return func(ctx Context) bool {
v, _ := ctx.Params().GetEntryAt(paramIndex).BoolDefault(false)
return v
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(bool)
}
},
}

View File

@ -83,7 +83,6 @@ func convertTmplToHandler(tmpl *macro.Template) context.Handler {
return func(tmpl macro.Template) context.Handler {
return func(ctx context.Context) {
for _, p := range tmpl.Params {
paramValue := ctx.Params().Get(p.Name)
if p.TypeEvaluator == nil {
// allow.
ctx.Next()
@ -91,7 +90,7 @@ func convertTmplToHandler(tmpl *macro.Template) context.Handler {
}
// first, check for type evaluator.
newValue, passed := p.TypeEvaluator(paramValue)
newValue, passed := p.TypeEvaluator(ctx.Parms().Get(p.Name))
if !passed {
ctx.StatusCode(p.ErrCode)
ctx.StopExecution()

View File

@ -154,6 +154,10 @@ func convertBuilderFunc(fn interface{}) ParamFuncBuilder {
v, err := strconv.ParseInt(arg, 10, 64)
panicIfErr(err)
val = v
case reflect.Uint:
v, err := strconv.ParseUint(arg, 10, strconv.IntSize)
panicIfErr(err)
val = uint(v)
case reflect.Uint8:
v, err := strconv.ParseUint(arg, 10, 8)
panicIfErr(err)
@ -245,7 +249,7 @@ type (
// and returns a function as value, its job
// is to make the macros to be registered
// by user at the most generic possible way.
ParamFuncBuilder func([]string) reflect.Value // the func
ParamFuncBuilder func([]string) reflect.Value // the func(<T>) bool
// ParamFunc represents the parsed
// parameter function, it holds

View File

@ -46,12 +46,13 @@ var (
})
simpleNumberEval = MustRegexp("^-?[0-9]+$")
// Int or int type
// Int or number type
// both positive and negative numbers, actual value can be min-max int64 or min-max int32 depends on the arch.
Int = NewMacro("int", "number", false, false, func(paramValue string) (interface{}, bool) {
if !simpleNumberEval(paramValue) {
return nil, false
}
v, err := strconv.Atoi(paramValue)
if err != nil {
return nil, false
@ -81,12 +82,100 @@ var (
}
})
// Int8 type
// -128 to 127.
Int8 = NewMacro("int8", "", false, false, func(paramValue string) (interface{}, bool) {
if !simpleNumberEval(paramValue) {
return nil, false
}
v, err := strconv.ParseInt(paramValue, 10, 8)
if err != nil {
return nil, false
}
return int8(v), true
}).
RegisterFunc("min", func(min int8) func(int8) bool {
return func(paramValue int8) bool {
return paramValue >= min
}
}).
RegisterFunc("max", func(max int8) func(int8) bool {
return func(paramValue int8) bool {
return paramValue <= max
}
}).
RegisterFunc("range", func(min, max int8) func(int8) bool {
return func(paramValue int8) bool {
return !(paramValue < min || paramValue > max)
}
})
// Int16 type
// -32768 to 32767.
Int16 = NewMacro("int16", "", false, false, func(paramValue string) (interface{}, bool) {
if !simpleNumberEval(paramValue) {
return nil, false
}
v, err := strconv.ParseInt(paramValue, 10, 16)
if err != nil {
return nil, false
}
return int16(v), true
}).
RegisterFunc("min", func(min int16) func(int16) bool {
return func(paramValue int16) bool {
return paramValue >= min
}
}).
RegisterFunc("max", func(max int16) func(int16) bool {
return func(paramValue int16) bool {
return paramValue <= max
}
}).
RegisterFunc("range", func(min, max int16) func(int16) bool {
return func(paramValue int16) bool {
return !(paramValue < min || paramValue > max)
}
})
// Int32 type
// -2147483648 to 2147483647.
Int32 = NewMacro("int32", "", false, false, func(paramValue string) (interface{}, bool) {
if !simpleNumberEval(paramValue) {
return nil, false
}
v, err := strconv.ParseInt(paramValue, 10, 32)
if err != nil {
return nil, false
}
return int32(v), true
}).
RegisterFunc("min", func(min int32) func(int32) bool {
return func(paramValue int32) bool {
return paramValue >= min
}
}).
RegisterFunc("max", func(max int32) func(int32) bool {
return func(paramValue int32) bool {
return paramValue <= max
}
}).
RegisterFunc("range", func(min, max int32) func(int32) bool {
return func(paramValue int32) bool {
return !(paramValue < min || paramValue > max)
}
})
// Int64 as int64 type
// -9223372036854775808 to 9223372036854775807.
Int64 = NewMacro("int64", "long", false, false, func(paramValue string) (interface{}, bool) {
if !simpleNumberEval(paramValue) {
return nil, false
}
v, err := strconv.ParseInt(paramValue, 10, 64)
if err != nil { // if err == strconv.ErrRange...
return nil, false
@ -115,6 +204,39 @@ var (
}
})
// Uint as uint type
// actual value can be min-max uint64 or min-max uint32 depends on the arch.
// if x64: 0 to 18446744073709551615
// if x32: 0 to 4294967295 and etc.
Uint = NewMacro("uint", "", false, false, func(paramValue string) (interface{}, bool) {
v, err := strconv.ParseUint(paramValue, 10, strconv.IntSize) // 32,64...
if err != nil {
return nil, false
}
return uint(v), true
}).
// checks if the param value's int representation is
// bigger or equal than 'min'
RegisterFunc("min", func(min uint) func(uint) bool {
return func(paramValue uint) bool {
return paramValue >= min
}
}).
// checks if the param value's int representation is
// smaller or equal than 'max'.
RegisterFunc("max", func(max uint) func(uint) bool {
return func(paramValue uint) bool {
return paramValue <= max
}
}).
// checks if the param value's int representation is
// between min and max, including 'min' and 'max'.
RegisterFunc("range", func(min, max uint) func(uint) bool {
return func(paramValue uint) bool {
return !(paramValue < min || paramValue > max)
}
})
uint8Eval = MustRegexp("^([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$")
// Uint8 as uint8 type
// 0 to 255.
@ -151,12 +273,59 @@ var (
}
})
// Uint16 as uint16 type
// 0 to 65535.
Uint16 = NewMacro("uint16", "", false, false, func(paramValue string) (interface{}, bool) {
v, err := strconv.ParseUint(paramValue, 10, 16)
if err != nil {
return nil, false
}
return uint16(v), true
}).
RegisterFunc("min", func(min uint16) func(uint16) bool {
return func(paramValue uint16) bool {
return paramValue >= min
}
}).
RegisterFunc("max", func(max uint16) func(uint16) bool {
return func(paramValue uint16) bool {
return paramValue <= max
}
}).
RegisterFunc("range", func(min, max uint16) func(uint16) bool {
return func(paramValue uint16) bool {
return !(paramValue < min || paramValue > max)
}
})
// Uint32 as uint32 type
// 0 to 4294967295.
Uint32 = NewMacro("uint32", "", false, false, func(paramValue string) (interface{}, bool) {
v, err := strconv.ParseUint(paramValue, 10, 32)
if err != nil {
return nil, false
}
return uint32(v), true
}).
RegisterFunc("min", func(min uint32) func(uint32) bool {
return func(paramValue uint32) bool {
return paramValue >= min
}
}).
RegisterFunc("max", func(max uint32) func(uint32) bool {
return func(paramValue uint32) bool {
return paramValue <= max
}
}).
RegisterFunc("range", func(min, max uint32) func(uint32) bool {
return func(paramValue uint32) bool {
return !(paramValue < min || paramValue > max)
}
})
// Uint64 as uint64 type
// 0 to 18446744073709551615.
Uint64 = NewMacro("uint64", "", false, false, func(paramValue string) (interface{}, bool) {
if !simpleNumberEval(paramValue) {
return nil, false
}
v, err := strconv.ParseUint(paramValue, 10, 64)
if err != nil {
return nil, false
@ -234,8 +403,14 @@ var (
Defaults = &Macros{
String,
Int,
Int8,
Int16,
Int32,
Int64,
Uint,
Uint8,
Uint16,
Uint32,
Uint64,
Bool,
Alphabetical,