mirror of
https://github.com/kataras/iris.git
synced 2026-01-21 10:56:01 +00:00
add '{date}' dynamic path parameter type
This commit is contained in:
@@ -207,7 +207,6 @@ func convertBuilderFunc(fn interface{}) ParamFuncBuilder {
|
||||
val = strings.Split(arg[1:len(arg)-1], ",") // only string slices.
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
val = arg
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"reflect"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Most important tests to look:
|
||||
@@ -484,6 +485,35 @@ func TestEmailEvaluatorRaw(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDateEvaluatorRaw(t *testing.T) {
|
||||
tests := []struct {
|
||||
pass bool
|
||||
input string
|
||||
timeStringValue string
|
||||
}{
|
||||
{true, "2022/04/21", "2022-04-21 00:00:00 +0000 UTC"}, // 0
|
||||
{true, "2022/12/05", "2022-12-05 00:00:00 +0000 UTC"}, // 1
|
||||
{false, "2022/4", ""}, // 2
|
||||
{false, "1/4/1", ""}, // 3
|
||||
{false, "2022/4/", ""}, // 4
|
||||
{false, "2022/4/21/0", ""}, // 5
|
||||
{false, "1993", ""}, // 6
|
||||
}
|
||||
for i, tt := range tests {
|
||||
testEvaluatorRaw(t, Date, tt.input, reflect.TypeOf(time.Time{}).Kind(), tt.pass, i)
|
||||
|
||||
if v, ok := Date.Evaluator(tt.input); ok {
|
||||
if value, ok := v.(time.Time); ok {
|
||||
if expected, got := tt.timeStringValue, value.String(); expected != got {
|
||||
t.Fatalf("[%d] expected: %s but got: %s", i, expected, got)
|
||||
}
|
||||
} else {
|
||||
t.Fatalf("[%d] expected to be able to cast as time.Time directly", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertBuilderFunc(t *testing.T) {
|
||||
fn := func(min uint64, slice []string) func(string) bool {
|
||||
return func(paramValue string) bool {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"net/mail"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12/macro/interpreter/ast"
|
||||
|
||||
@@ -431,6 +432,18 @@ var (
|
||||
return paramValue, true
|
||||
})
|
||||
|
||||
simpleDateLayout = "2006/01/02"
|
||||
|
||||
// Date type.
|
||||
Date = NewMacro("date", "", false, true, func(paramValue string) (interface{}, bool) {
|
||||
tt, err := time.Parse(simpleDateLayout, paramValue)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", paramValue, err), false
|
||||
}
|
||||
|
||||
return tt, true
|
||||
})
|
||||
|
||||
// Defaults contains the defaults macro and parameters types for the router.
|
||||
//
|
||||
// Read https://github.com/kataras/iris/tree/master/_examples/routing/macros for more details.
|
||||
@@ -453,6 +466,7 @@ var (
|
||||
UUID,
|
||||
Mail,
|
||||
Email,
|
||||
Date,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user