Add dynamic handler management #13

Merged
xdrm-brackets merged 12 commits from feature/dynamic-handler-signature into 0.3.0 2020-03-29 15:10:06 +00:00
3 changed files with 72 additions and 20 deletions
Showing only changes of commit ca2be1415d - Show all commits

View File

@ -481,6 +481,46 @@ func TestParseParameters(t *testing.T) {
]`,
nil,
},
// missing rename
{
`[
{
"method": "GET",
"path": "/{uri}",
"info": "info",
"in": {
"{uri}": { "info": "valid", "type": "any" }
}
}
]`,
ErrMandatoryRename,
},
{
`[
{
"method": "GET",
"path": "/",
"info": "info",
"in": {
"GET@abc": { "info": "valid", "type": "any" }
}
}
]`,
ErrMandatoryRename,
},
{
`[
{
"method": "GET",
"path": "/",
"info": "info",
"in": {
"GET@abc": { "info": "valid", "type": "any", "name": "abc" }
}
}
]`,
nil,
},
{ // URI parameter
`[
@ -616,7 +656,7 @@ func TestServiceCollision(t *testing.T) {
},
{ "method": "GET", "path": "/a/{c}",
"info": "info", "in": {
"{c}": { "info":"info", "type": "string" }
"{c}": { "info":"info", "type": "string", "name": "c" }
}
}
]`,
@ -629,7 +669,7 @@ func TestServiceCollision(t *testing.T) {
},
{ "method": "GET", "path": "/a/{c}",
"info": "info", "in": {
"{c}": { "info":"info", "type": "uint" }
"{c}": { "info":"info", "type": "uint", "name": "c" }
}
}
]`,
@ -642,7 +682,7 @@ func TestServiceCollision(t *testing.T) {
},
{ "method": "GET", "path": "/a/{c}/d",
"info": "info", "in": {
"{c}": { "info":"info", "type": "string" }
"{c}": { "info":"info", "type": "string", "name": "c" }
}
}
]`,
@ -655,7 +695,7 @@ func TestServiceCollision(t *testing.T) {
},
{ "method": "GET", "path": "/a/{c}",
"info": "info", "in": {
"{c}": { "info":"info", "type": "string" }
"{c}": { "info":"info", "type": "string", "name": "c" }
}
}
]`,
@ -668,7 +708,7 @@ func TestServiceCollision(t *testing.T) {
},
{ "method": "GET", "path": "/a/{c}",
"info": "info", "in": {
"{c}": { "info":"info", "type": "string" }
"{c}": { "info":"info", "type": "string", "name": "c" }
}
}
]`,
@ -681,7 +721,7 @@ func TestServiceCollision(t *testing.T) {
},
{ "method": "GET", "path": "/a/{c}/d",
"info": "info", "in": {
"{c}": { "info":"info", "type": "string" }
"{c}": { "info":"info", "type": "string", "name": "c" }
}
}
]`,
@ -694,7 +734,7 @@ func TestServiceCollision(t *testing.T) {
},
{ "method": "GET", "path": "/a/{c}",
"info": "info", "in": {
"{c}": { "info":"info", "type": "uint" }
"{c}": { "info":"info", "type": "uint", "name": "c" }
}
}
]`,
@ -707,7 +747,7 @@ func TestServiceCollision(t *testing.T) {
},
{ "method": "GET", "path": "/a/{c}",
"info": "info", "in": {
"{c}": { "info":"info", "type": "uint" }
"{c}": { "info":"info", "type": "uint", "name": "c" }
}
}
]`,
@ -720,7 +760,7 @@ func TestServiceCollision(t *testing.T) {
},
{ "method": "GET", "path": "/a/{c}/d",
"info": "info", "in": {
"{c}": { "info":"info", "type": "uint" }
"{c}": { "info":"info", "type": "uint", "name": "c" }
}
}
]`,
@ -733,7 +773,7 @@ func TestServiceCollision(t *testing.T) {
},
{ "method": "GET", "path": "/a/{c}/d",
"info": "info", "in": {
"{c}": { "info":"info", "type": "uint" }
"{c}": { "info":"info", "type": "uint", "name": "c" }
}
}
]`,
@ -743,12 +783,12 @@ func TestServiceCollision(t *testing.T) {
`[
{ "method": "GET", "path": "/a/{b}",
"info": "info", "in": {
"{b}": { "info":"info", "type": "uint" }
"{b}": { "info":"info", "type": "uint", "name": "b" }
}
},
{ "method": "GET", "path": "/a/{c}",
"info": "info", "in": {
"{c}": { "info":"info", "type": "uint" }
"{c}": { "info":"info", "type": "uint", "name": "c" }
}
}
]`,
@ -758,12 +798,12 @@ func TestServiceCollision(t *testing.T) {
`[
{ "method": "GET", "path": "/a/{b}",
"info": "info", "in": {
"{b}": { "info":"info", "type": "uint" }
"{b}": { "info":"info", "type": "uint", "name": "b" }
}
},
{ "method": "PUT", "path": "/a/{c}",
"info": "info", "in": {
"{c}": { "info":"info", "type": "uint" }
"{c}": { "info":"info", "type": "uint", "name": "c" }
}
}
]`,
@ -850,7 +890,8 @@ func TestMatchSimple(t *testing.T) {
"in": {
"{id}": {
"info": "info",
"type": "bool"
"type": "bool",
"name": "id"
}
}
} ]`,
@ -865,7 +906,8 @@ func TestMatchSimple(t *testing.T) {
"in": {
"{id}": {
"info": "info",
"type": "int"
"type": "int",
"name": "id"
}
}
} ]`,
@ -880,7 +922,8 @@ func TestMatchSimple(t *testing.T) {
"in": {
"{valid}": {
"info": "info",
"type": "bool"
"type": "bool",
"name": "valid"
}
}
} ]`,
@ -895,7 +938,8 @@ func TestMatchSimple(t *testing.T) {
"in": {
"{valid}": {
"info": "info",
"type": "bool"
"type": "bool",
"name": "valid"
}
}
} ]`,

View File

@ -29,6 +29,9 @@ const ErrInvalidPatternBraceCapture = cerr("invalid uri capturing braces")
// ErrUnspecifiedBraceCapture - a parameter brace capture is not specified in the pattern
const ErrUnspecifiedBraceCapture = cerr("capturing brace missing in the path")
// ErrMandatoryRename - capture/query parameters must have a rename
const ErrMandatoryRename = cerr("capture and query parameters must have a 'name'")
// ErrUndefinedBraceCapture - a parameter brace capture in the pattern is not defined in parameters
const ErrUndefinedBraceCapture = cerr("capturing brace missing input definition")

View File

@ -183,7 +183,7 @@ func (svc *Service) validateInput(types []datatype.T) error {
}
// fail if brace capture does not exists in pattern
iscapture := false
var iscapture, isquery bool
if matches := braceRegex.FindAllStringSubmatch(paramName, -1); len(matches) > 0 && len(matches[0]) > 1 {
braceName := matches[0][1]
@ -209,7 +209,7 @@ func (svc *Service) validateInput(types []datatype.T) error {
svc.Query = make(map[string]*Parameter)
}
svc.Query[queryName] = param
isquery = true
} else {
if svc.Form == nil {
svc.Form = make(map[string]*Parameter)
@ -217,6 +217,11 @@ func (svc *Service) validateInput(types []datatype.T) error {
svc.Form[paramName] = param
}
// fail if capture or query without rename
if len(param.Rename) < 1 && (iscapture || isquery) {
return fmt.Errorf("%s: %w", paramName, ErrMandatoryRename)
}
// use param name if no rename
if len(param.Rename) < 1 {
param.Rename = paramName