refactor-test #15
|
@ -20,11 +20,11 @@ type Server struct {
|
||||||
// make datatypes available when checking and formatting the read configuration.
|
// make datatypes available when checking and formatting the read configuration.
|
||||||
func (srv *Server) Parse(r io.Reader) error {
|
func (srv *Server) Parse(r io.Reader) error {
|
||||||
if err := json.NewDecoder(r).Decode(&srv.Services); err != nil {
|
if err := json.NewDecoder(r).Decode(&srv.Services); err != nil {
|
||||||
return fmt.Errorf("%s: %w", ErrRead, err)
|
return fmt.Errorf("%s: %w", errRead, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := srv.validate(); err != nil {
|
if err := srv.validate(); err != nil {
|
||||||
return fmt.Errorf("%s: %w", ErrFormat, err)
|
return fmt.Errorf("%s: %w", errFormat, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -41,7 +41,7 @@ func (server Server) validate(datatypes ...datatype.T) error {
|
||||||
|
|
||||||
// check for collisions
|
// check for collisions
|
||||||
if err := server.collide(); err != nil {
|
if err := server.collide(); err != nil {
|
||||||
return fmt.Errorf("%s: %w", ErrFormat, err)
|
return fmt.Errorf("%s: %w", errFormat, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -92,14 +92,14 @@ func (server *Server) collide() error {
|
||||||
|
|
||||||
// both captures -> as we cannot check, consider a collision
|
// both captures -> as we cannot check, consider a collision
|
||||||
if aIsCapture && bIsCapture {
|
if aIsCapture && bIsCapture {
|
||||||
partErrors = append(partErrors, fmt.Errorf("(%s '%s') vs (%s '%s'): %w (path %s and %s)", aService.Method, aService.Pattern, bService.Method, bService.Pattern, ErrPatternCollision, aPart, bPart))
|
partErrors = append(partErrors, fmt.Errorf("(%s '%s') vs (%s '%s'): %w (path %s and %s)", aService.Method, aService.Pattern, bService.Method, bService.Pattern, errPatternCollision, aPart, bPart))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// no capture -> check equal
|
// no capture -> check equal
|
||||||
if !aIsCapture && !bIsCapture {
|
if !aIsCapture && !bIsCapture {
|
||||||
if aPart == bPart {
|
if aPart == bPart {
|
||||||
partErrors = append(partErrors, fmt.Errorf("(%s '%s') vs (%s '%s'): %w (same path '%s')", aService.Method, aService.Pattern, bService.Method, bService.Pattern, ErrPatternCollision, aPart))
|
partErrors = append(partErrors, fmt.Errorf("(%s '%s') vs (%s '%s'): %w (same path '%s')", aService.Method, aService.Pattern, bService.Method, bService.Pattern, errPatternCollision, aPart))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -110,13 +110,13 @@ func (server *Server) collide() error {
|
||||||
|
|
||||||
// fail if no type or no validator
|
// fail if no type or no validator
|
||||||
if !exists || input.Validator == nil {
|
if !exists || input.Validator == nil {
|
||||||
partErrors = append(partErrors, fmt.Errorf("(%s '%s') vs (%s '%s'): %w (invalid type for %s)", aService.Method, aService.Pattern, bService.Method, bService.Pattern, ErrPatternCollision, aPart))
|
partErrors = append(partErrors, fmt.Errorf("(%s '%s') vs (%s '%s'): %w (invalid type for %s)", aService.Method, aService.Pattern, bService.Method, bService.Pattern, errPatternCollision, aPart))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// fail if not valid
|
// fail if not valid
|
||||||
if _, valid := input.Validator(bPart); valid {
|
if _, valid := input.Validator(bPart); valid {
|
||||||
partErrors = append(partErrors, fmt.Errorf("(%s '%s') vs (%s '%s'): %w (%s captures '%s')", aService.Method, aService.Pattern, bService.Method, bService.Pattern, ErrPatternCollision, aPart, bPart))
|
partErrors = append(partErrors, fmt.Errorf("(%s '%s') vs (%s '%s'): %w (%s captures '%s')", aService.Method, aService.Pattern, bService.Method, bService.Pattern, errPatternCollision, aPart, bPart))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,13 +126,13 @@ func (server *Server) collide() error {
|
||||||
|
|
||||||
// fail if no type or no validator
|
// fail if no type or no validator
|
||||||
if !exists || input.Validator == nil {
|
if !exists || input.Validator == nil {
|
||||||
partErrors = append(partErrors, fmt.Errorf("(%s '%s') vs (%s '%s'): %w (invalid type for %s)", aService.Method, aService.Pattern, bService.Method, bService.Pattern, ErrPatternCollision, bPart))
|
partErrors = append(partErrors, fmt.Errorf("(%s '%s') vs (%s '%s'): %w (invalid type for %s)", aService.Method, aService.Pattern, bService.Method, bService.Pattern, errPatternCollision, bPart))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// fail if not valid
|
// fail if not valid
|
||||||
if _, valid := input.Validator(aPart); valid {
|
if _, valid := input.Validator(aPart); valid {
|
||||||
partErrors = append(partErrors, fmt.Errorf("(%s '%s') vs (%s '%s'): %w (%s captures '%s')", aService.Method, aService.Pattern, bService.Method, bService.Pattern, ErrPatternCollision, bPart, aPart))
|
partErrors = append(partErrors, fmt.Errorf("(%s '%s') vs (%s '%s'): %w (%s captures '%s')", aService.Method, aService.Pattern, bService.Method, bService.Pattern, errPatternCollision, bPart, aPart))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,15 +21,15 @@ func TestLegalServiceName(t *testing.T) {
|
||||||
// empty
|
// empty
|
||||||
{
|
{
|
||||||
`[ { "method": "GET", "info": "a", "path": "" } ]`,
|
`[ { "method": "GET", "info": "a", "path": "" } ]`,
|
||||||
ErrInvalidPattern,
|
errInvalidPattern,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
`[ { "method": "GET", "info": "a", "path": "no-starting-slash" } ]`,
|
`[ { "method": "GET", "info": "a", "path": "no-starting-slash" } ]`,
|
||||||
ErrInvalidPattern,
|
errInvalidPattern,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
`[ { "method": "GET", "info": "a", "path": "ending-slash/" } ]`,
|
`[ { "method": "GET", "info": "a", "path": "ending-slash/" } ]`,
|
||||||
ErrInvalidPattern,
|
errInvalidPattern,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
`[ { "method": "GET", "info": "a", "path": "/" } ]`,
|
`[ { "method": "GET", "info": "a", "path": "/" } ]`,
|
||||||
|
@ -45,35 +45,35 @@ func TestLegalServiceName(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
`[ { "method": "GET", "info": "a", "path": "/invalid/s{braces}" } ]`,
|
`[ { "method": "GET", "info": "a", "path": "/invalid/s{braces}" } ]`,
|
||||||
ErrInvalidPatternBraceCapture,
|
errInvalidPatternBraceCapture,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
`[ { "method": "GET", "info": "a", "path": "/invalid/{braces}a" } ]`,
|
`[ { "method": "GET", "info": "a", "path": "/invalid/{braces}a" } ]`,
|
||||||
ErrInvalidPatternBraceCapture,
|
errInvalidPatternBraceCapture,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
`[ { "method": "GET", "info": "a", "path": "/invalid/{braces}" } ]`,
|
`[ { "method": "GET", "info": "a", "path": "/invalid/{braces}" } ]`,
|
||||||
ErrUndefinedBraceCapture,
|
errUndefinedBraceCapture,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
`[ { "method": "GET", "info": "a", "path": "/invalid/s{braces}/abc" } ]`,
|
`[ { "method": "GET", "info": "a", "path": "/invalid/s{braces}/abc" } ]`,
|
||||||
ErrInvalidPatternBraceCapture,
|
errInvalidPatternBraceCapture,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
`[ { "method": "GET", "info": "a", "path": "/invalid/{braces}s/abc" } ]`,
|
`[ { "method": "GET", "info": "a", "path": "/invalid/{braces}s/abc" } ]`,
|
||||||
ErrInvalidPatternBraceCapture,
|
errInvalidPatternBraceCapture,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
`[ { "method": "GET", "info": "a", "path": "/invalid/{braces}/abc" } ]`,
|
`[ { "method": "GET", "info": "a", "path": "/invalid/{braces}/abc" } ]`,
|
||||||
ErrUndefinedBraceCapture,
|
errUndefinedBraceCapture,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
`[ { "method": "GET", "info": "a", "path": "/invalid/{b{races}s/abc" } ]`,
|
`[ { "method": "GET", "info": "a", "path": "/invalid/{b{races}s/abc" } ]`,
|
||||||
ErrInvalidPatternBraceCapture,
|
errInvalidPatternBraceCapture,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
`[ { "method": "GET", "info": "a", "path": "/invalid/{braces}/}abc" } ]`,
|
`[ { "method": "GET", "info": "a", "path": "/invalid/{braces}/}abc" } ]`,
|
||||||
ErrInvalidPatternBraceCapture,
|
errInvalidPatternBraceCapture,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,8 +143,8 @@ func TestAvailableMethods(t *testing.T) {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
|
|
||||||
if !test.ValidMethod && !errors.Is(err, ErrUnknownMethod) {
|
if !test.ValidMethod && !errors.Is(err, errUnknownMethod) {
|
||||||
t.Errorf("expected error <%s> got <%s>", ErrUnknownMethod, err)
|
t.Errorf("expected error <%s> got <%s>", errUnknownMethod, err)
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -217,8 +217,8 @@ func TestParseMissingMethodDescription(t *testing.T) {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
|
|
||||||
if !test.ValidDescription && !errors.Is(err, ErrMissingDescription) {
|
if !test.ValidDescription && !errors.Is(err, errMissingDescription) {
|
||||||
t.Errorf("expected error <%s> got <%s>", ErrMissingDescription, err)
|
t.Errorf("expected error <%s> got <%s>", errMissingDescription, err)
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -321,7 +321,7 @@ func TestParseParameters(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
ErrMissingParamDesc,
|
errMissingParamDesc,
|
||||||
},
|
},
|
||||||
{ // invalid param name suffix
|
{ // invalid param name suffix
|
||||||
`[
|
`[
|
||||||
|
@ -334,7 +334,7 @@ func TestParseParameters(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
ErrMissingParamDesc,
|
errMissingParamDesc,
|
||||||
},
|
},
|
||||||
|
|
||||||
{ // missing param description
|
{ // missing param description
|
||||||
|
@ -348,7 +348,7 @@ func TestParseParameters(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
ErrMissingParamDesc,
|
errMissingParamDesc,
|
||||||
},
|
},
|
||||||
{ // empty param description
|
{ // empty param description
|
||||||
`[
|
`[
|
||||||
|
@ -361,7 +361,7 @@ func TestParseParameters(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
ErrMissingParamDesc,
|
errMissingParamDesc,
|
||||||
},
|
},
|
||||||
|
|
||||||
{ // missing param type
|
{ // missing param type
|
||||||
|
@ -375,7 +375,7 @@ func TestParseParameters(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
ErrMissingParamType,
|
errMissingParamType,
|
||||||
},
|
},
|
||||||
{ // empty param type
|
{ // empty param type
|
||||||
`[
|
`[
|
||||||
|
@ -388,7 +388,7 @@ func TestParseParameters(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
ErrMissingParamType,
|
errMissingParamType,
|
||||||
},
|
},
|
||||||
{ // invalid type (optional mark only)
|
{ // invalid type (optional mark only)
|
||||||
`[
|
`[
|
||||||
|
@ -402,7 +402,7 @@ func TestParseParameters(t *testing.T) {
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
|
|
||||||
ErrMissingParamType,
|
errMissingParamType,
|
||||||
},
|
},
|
||||||
{ // valid description + valid type
|
{ // valid description + valid type
|
||||||
`[
|
`[
|
||||||
|
@ -444,7 +444,7 @@ func TestParseParameters(t *testing.T) {
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
// 2 possible errors as map order is not deterministic
|
// 2 possible errors as map order is not deterministic
|
||||||
ErrParamNameConflict,
|
errParamNameConflict,
|
||||||
},
|
},
|
||||||
{ // rename conflict with name
|
{ // rename conflict with name
|
||||||
`[
|
`[
|
||||||
|
@ -459,7 +459,7 @@ func TestParseParameters(t *testing.T) {
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
// 2 possible errors as map order is not deterministic
|
// 2 possible errors as map order is not deterministic
|
||||||
ErrParamNameConflict,
|
errParamNameConflict,
|
||||||
},
|
},
|
||||||
{ // rename conflict with rename
|
{ // rename conflict with rename
|
||||||
`[
|
`[
|
||||||
|
@ -474,7 +474,7 @@ func TestParseParameters(t *testing.T) {
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
// 2 possible errors as map order is not deterministic
|
// 2 possible errors as map order is not deterministic
|
||||||
ErrParamNameConflict,
|
errParamNameConflict,
|
||||||
},
|
},
|
||||||
|
|
||||||
{ // both renamed with no conflict
|
{ // both renamed with no conflict
|
||||||
|
@ -503,7 +503,7 @@ func TestParseParameters(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
ErrMandatoryRename,
|
errMandatoryRename,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
`[
|
`[
|
||||||
|
@ -516,7 +516,7 @@ func TestParseParameters(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
ErrMandatoryRename,
|
errMandatoryRename,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
`[
|
`[
|
||||||
|
@ -556,7 +556,7 @@ func TestParseParameters(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
ErrIllegalOptionalURIParam,
|
errIllegalOptionalURIParam,
|
||||||
},
|
},
|
||||||
{ // URI parameter not specified
|
{ // URI parameter not specified
|
||||||
`[
|
`[
|
||||||
|
@ -569,7 +569,7 @@ func TestParseParameters(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
ErrUnspecifiedBraceCapture,
|
errUnspecifiedBraceCapture,
|
||||||
},
|
},
|
||||||
{ // URI parameter not defined
|
{ // URI parameter not defined
|
||||||
`[
|
`[
|
||||||
|
@ -580,7 +580,7 @@ func TestParseParameters(t *testing.T) {
|
||||||
"in": { }
|
"in": { }
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
ErrUndefinedBraceCapture,
|
errUndefinedBraceCapture,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -637,7 +637,7 @@ func TestServiceCollision(t *testing.T) {
|
||||||
"info": "info", "in": {}
|
"info": "info", "in": {}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
ErrPatternCollision,
|
errPatternCollision,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
`[
|
`[
|
||||||
|
@ -672,7 +672,7 @@ func TestServiceCollision(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
ErrPatternCollision,
|
errPatternCollision,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
`[
|
`[
|
||||||
|
@ -698,7 +698,7 @@ func TestServiceCollision(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
ErrPatternCollision,
|
errPatternCollision,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
`[
|
`[
|
||||||
|
@ -711,7 +711,7 @@ func TestServiceCollision(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
ErrPatternCollision,
|
errPatternCollision,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
`[
|
`[
|
||||||
|
@ -750,7 +750,7 @@ func TestServiceCollision(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
ErrPatternCollision,
|
errPatternCollision,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
`[
|
`[
|
||||||
|
@ -789,7 +789,7 @@ func TestServiceCollision(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
ErrPatternCollision,
|
errPatternCollision,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
`[
|
`[
|
||||||
|
@ -804,7 +804,7 @@ func TestServiceCollision(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
ErrPatternCollision,
|
errPatternCollision,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
`[
|
`[
|
||||||
|
|
|
@ -7,53 +7,53 @@ func (err cerr) Error() string {
|
||||||
return string(err)
|
return string(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrRead - a problem ocurred when trying to read the configuration file
|
// errRead - a problem ocurred when trying to read the configuration file
|
||||||
const ErrRead = cerr("cannot read config")
|
const errRead = cerr("cannot read config")
|
||||||
|
|
||||||
// ErrUnknownMethod - invalid http method
|
// errUnknownMethod - invalid http method
|
||||||
const ErrUnknownMethod = cerr("unknown HTTP method")
|
const errUnknownMethod = cerr("unknown HTTP method")
|
||||||
|
|
||||||
// ErrFormat - a invalid format has been detected
|
// errFormat - a invalid format has been detected
|
||||||
const ErrFormat = cerr("invalid config format")
|
const errFormat = cerr("invalid config format")
|
||||||
|
|
||||||
// ErrPatternCollision - there is a collision between 2 services' patterns (same method)
|
// errPatternCollision - there is a collision between 2 services' patterns (same method)
|
||||||
const ErrPatternCollision = cerr("pattern collision")
|
const errPatternCollision = cerr("pattern collision")
|
||||||
|
|
||||||
// ErrInvalidPattern - a service pattern is malformed
|
// errInvalidPattern - a service pattern is malformed
|
||||||
const ErrInvalidPattern = cerr("must begin with a '/' and not end with")
|
const errInvalidPattern = cerr("must begin with a '/' and not end with")
|
||||||
|
|
||||||
// ErrInvalidPatternBraceCapture - a service pattern brace capture is invalid
|
// errInvalidPatternBraceCapture - a service pattern brace capture is invalid
|
||||||
const ErrInvalidPatternBraceCapture = cerr("invalid uri capturing braces")
|
const errInvalidPatternBraceCapture = cerr("invalid uri capturing braces")
|
||||||
|
|
||||||
// ErrUnspecifiedBraceCapture - a parameter brace capture is not specified in the pattern
|
// errUnspecifiedBraceCapture - a parameter brace capture is not specified in the pattern
|
||||||
const ErrUnspecifiedBraceCapture = cerr("capturing brace missing in the path")
|
const errUnspecifiedBraceCapture = cerr("capturing brace missing in the path")
|
||||||
|
|
||||||
// ErrMandatoryRename - capture/query parameters must have a rename
|
// errMandatoryRename - capture/query parameters must have a rename
|
||||||
const ErrMandatoryRename = cerr("capture and query parameters must have a 'name'")
|
const errMandatoryRename = cerr("capture and query parameters must have a 'name'")
|
||||||
|
|
||||||
// ErrUndefinedBraceCapture - a parameter brace capture in the pattern is not defined in parameters
|
// errUndefinedBraceCapture - a parameter brace capture in the pattern is not defined in parameters
|
||||||
const ErrUndefinedBraceCapture = cerr("capturing brace missing input definition")
|
const errUndefinedBraceCapture = cerr("capturing brace missing input definition")
|
||||||
|
|
||||||
// ErrMissingDescription - a service is missing its description
|
// errMissingDescription - a service is missing its description
|
||||||
const ErrMissingDescription = cerr("missing description")
|
const errMissingDescription = cerr("missing description")
|
||||||
|
|
||||||
// ErrIllegalOptionalURIParam - an URI parameter cannot be optional
|
// errIllegalOptionalURIParam - an URI parameter cannot be optional
|
||||||
const ErrIllegalOptionalURIParam = cerr("URI parameter cannot be optional")
|
const errIllegalOptionalURIParam = cerr("URI parameter cannot be optional")
|
||||||
|
|
||||||
// ErrOptionalOption - an output is optional
|
// errOptionalOption - an output is optional
|
||||||
const ErrOptionalOption = cerr("output cannot be optional")
|
const errOptionalOption = cerr("output cannot be optional")
|
||||||
|
|
||||||
// ErrMissingParamDesc - a parameter is missing its description
|
// errMissingParamDesc - a parameter is missing its description
|
||||||
const ErrMissingParamDesc = cerr("missing parameter description")
|
const errMissingParamDesc = cerr("missing parameter description")
|
||||||
|
|
||||||
// ErrUnknownDataType - a parameter has an unknown datatype name
|
// errUnknownDataType - a parameter has an unknown datatype name
|
||||||
const ErrUnknownDataType = cerr("unknown data type")
|
const errUnknownDataType = cerr("unknown data type")
|
||||||
|
|
||||||
// ErrIllegalParamName - a parameter has an illegal name
|
// errIllegalParamName - a parameter has an illegal name
|
||||||
const ErrIllegalParamName = cerr("illegal parameter name")
|
const errIllegalParamName = cerr("illegal parameter name")
|
||||||
|
|
||||||
// ErrMissingParamType - a parameter has an illegal type
|
// errMissingParamType - a parameter has an illegal type
|
||||||
const ErrMissingParamType = cerr("missing parameter type")
|
const errMissingParamType = cerr("missing parameter type")
|
||||||
|
|
||||||
// ErrParamNameConflict - a parameter has a conflict with its name/rename field
|
// errParamNameConflict - a parameter has a conflict with its name/rename field
|
||||||
const ErrParamNameConflict = cerr("name conflict for parameter")
|
const errParamNameConflict = cerr("name conflict for parameter")
|
||||||
|
|
|
@ -23,12 +23,12 @@ type Parameter struct {
|
||||||
func (param *Parameter) validate(datatypes ...datatype.T) error {
|
func (param *Parameter) validate(datatypes ...datatype.T) error {
|
||||||
// missing description
|
// missing description
|
||||||
if len(param.Description) < 1 {
|
if len(param.Description) < 1 {
|
||||||
return ErrMissingParamDesc
|
return errMissingParamDesc
|
||||||
}
|
}
|
||||||
|
|
||||||
// invalid type
|
// invalid type
|
||||||
if len(param.Type) < 1 || param.Type == "?" {
|
if len(param.Type) < 1 || param.Type == "?" {
|
||||||
return ErrMissingParamType
|
return errMissingParamType
|
||||||
}
|
}
|
||||||
|
|
||||||
// optional type transform
|
// optional type transform
|
||||||
|
@ -46,7 +46,7 @@ func (param *Parameter) validate(datatypes ...datatype.T) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if param.Validator == nil {
|
if param.Validator == nil {
|
||||||
return ErrUnknownDataType
|
return errUnknownDataType
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -118,7 +118,7 @@ func (svc *Service) validate(datatypes ...datatype.T) error {
|
||||||
|
|
||||||
// check description
|
// check description
|
||||||
if len(strings.Trim(svc.Description, " \t\r\n")) < 1 {
|
if len(strings.Trim(svc.Description, " \t\r\n")) < 1 {
|
||||||
return fmt.Errorf("field 'description': %w", ErrMissingDescription)
|
return fmt.Errorf("field 'description': %w", errMissingDescription)
|
||||||
}
|
}
|
||||||
|
|
||||||
// check input parameters
|
// check input parameters
|
||||||
|
@ -130,7 +130,7 @@ func (svc *Service) validate(datatypes ...datatype.T) error {
|
||||||
// fail if a brace capture remains undefined
|
// fail if a brace capture remains undefined
|
||||||
for _, capture := range svc.Captures {
|
for _, capture := range svc.Captures {
|
||||||
if capture.Ref == nil {
|
if capture.Ref == nil {
|
||||||
return fmt.Errorf("field 'in': %s: %w", capture.Name, ErrUndefinedBraceCapture)
|
return fmt.Errorf("field 'in': %s: %w", capture.Name, errUndefinedBraceCapture)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,7 +149,7 @@ func (svc *Service) isMethodAvailable() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ErrUnknownMethod
|
return errUnknownMethod
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svc *Service) isPatternValid() error {
|
func (svc *Service) isPatternValid() error {
|
||||||
|
@ -157,13 +157,13 @@ func (svc *Service) isPatternValid() error {
|
||||||
|
|
||||||
// empty pattern
|
// empty pattern
|
||||||
if length < 1 {
|
if length < 1 {
|
||||||
return ErrInvalidPattern
|
return errInvalidPattern
|
||||||
}
|
}
|
||||||
|
|
||||||
if length > 1 {
|
if length > 1 {
|
||||||
// pattern not starting with '/' or ending with '/'
|
// pattern not starting with '/' or ending with '/'
|
||||||
if svc.Pattern[0] != '/' || svc.Pattern[length-1] == '/' {
|
if svc.Pattern[0] != '/' || svc.Pattern[length-1] == '/' {
|
||||||
return ErrInvalidPattern
|
return errInvalidPattern
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -171,7 +171,7 @@ func (svc *Service) isPatternValid() error {
|
||||||
parts := SplitURL(svc.Pattern)
|
parts := SplitURL(svc.Pattern)
|
||||||
for i, part := range parts {
|
for i, part := range parts {
|
||||||
if len(part) < 1 {
|
if len(part) < 1 {
|
||||||
return ErrInvalidPattern
|
return errInvalidPattern
|
||||||
}
|
}
|
||||||
|
|
||||||
// if brace capture
|
// if brace capture
|
||||||
|
@ -192,7 +192,7 @@ func (svc *Service) isPatternValid() error {
|
||||||
|
|
||||||
// fail on invalid format
|
// fail on invalid format
|
||||||
if strings.ContainsAny(part, "{}") {
|
if strings.ContainsAny(part, "{}") {
|
||||||
return ErrInvalidPatternBraceCapture
|
return errInvalidPatternBraceCapture
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -211,7 +211,7 @@ func (svc *Service) validateInput(types []datatype.T) error {
|
||||||
// for each parameter
|
// for each parameter
|
||||||
for paramName, param := range svc.Input {
|
for paramName, param := range svc.Input {
|
||||||
if len(paramName) < 1 {
|
if len(paramName) < 1 {
|
||||||
return fmt.Errorf("%s: %w", paramName, ErrIllegalParamName)
|
return fmt.Errorf("%s: %w", paramName, errIllegalParamName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// fail if brace capture does not exists in pattern
|
// fail if brace capture does not exists in pattern
|
||||||
|
@ -228,7 +228,7 @@ func (svc *Service) validateInput(types []datatype.T) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !found {
|
if !found {
|
||||||
return fmt.Errorf("%s: %w", paramName, ErrUnspecifiedBraceCapture)
|
return fmt.Errorf("%s: %w", paramName, errUnspecifiedBraceCapture)
|
||||||
}
|
}
|
||||||
iscapture = true
|
iscapture = true
|
||||||
|
|
||||||
|
@ -251,7 +251,7 @@ func (svc *Service) validateInput(types []datatype.T) error {
|
||||||
|
|
||||||
// fail if capture or query without rename
|
// fail if capture or query without rename
|
||||||
if len(param.Rename) < 1 && (iscapture || isquery) {
|
if len(param.Rename) < 1 && (iscapture || isquery) {
|
||||||
return fmt.Errorf("%s: %w", paramName, ErrMandatoryRename)
|
return fmt.Errorf("%s: %w", paramName, errMandatoryRename)
|
||||||
}
|
}
|
||||||
|
|
||||||
// use param name if no rename
|
// use param name if no rename
|
||||||
|
@ -266,7 +266,7 @@ func (svc *Service) validateInput(types []datatype.T) error {
|
||||||
|
|
||||||
// capture parameter cannot be optional
|
// capture parameter cannot be optional
|
||||||
if iscapture && param.Optional {
|
if iscapture && param.Optional {
|
||||||
return fmt.Errorf("%s: %w", paramName, ErrIllegalOptionalURIParam)
|
return fmt.Errorf("%s: %w", paramName, errIllegalOptionalURIParam)
|
||||||
}
|
}
|
||||||
|
|
||||||
// fail on name/rename conflict
|
// fail on name/rename conflict
|
||||||
|
@ -280,7 +280,7 @@ func (svc *Service) validateInput(types []datatype.T) error {
|
||||||
// 3.2.2. Not-renamed field matches a renamed field
|
// 3.2.2. Not-renamed field matches a renamed field
|
||||||
// 3.2.3. Renamed field matches name
|
// 3.2.3. Renamed field matches name
|
||||||
if param.Rename == param2.Rename || paramName == param2.Rename || paramName2 == param.Rename {
|
if param.Rename == param2.Rename || paramName == param2.Rename || paramName2 == param.Rename {
|
||||||
return fmt.Errorf("%s: %w", paramName, ErrParamNameConflict)
|
return fmt.Errorf("%s: %w", paramName, errParamNameConflict)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -301,7 +301,7 @@ func (svc *Service) validateOutput(types []datatype.T) error {
|
||||||
// for each parameter
|
// for each parameter
|
||||||
for paramName, param := range svc.Output {
|
for paramName, param := range svc.Output {
|
||||||
if len(paramName) < 1 {
|
if len(paramName) < 1 {
|
||||||
return fmt.Errorf("%s: %w", paramName, ErrIllegalParamName)
|
return fmt.Errorf("%s: %w", paramName, errIllegalParamName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// use param name if no rename
|
// use param name if no rename
|
||||||
|
@ -315,7 +315,7 @@ func (svc *Service) validateOutput(types []datatype.T) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if param.Optional {
|
if param.Optional {
|
||||||
return fmt.Errorf("%s: %w", paramName, ErrOptionalOption)
|
return fmt.Errorf("%s: %w", paramName, errOptionalOption)
|
||||||
}
|
}
|
||||||
|
|
||||||
// fail on name/rename conflict
|
// fail on name/rename conflict
|
||||||
|
@ -329,7 +329,7 @@ func (svc *Service) validateOutput(types []datatype.T) error {
|
||||||
// 3.2.2. Not-renamed field matches a renamed field
|
// 3.2.2. Not-renamed field matches a renamed field
|
||||||
// 3.2.3. Renamed field matches name
|
// 3.2.3. Renamed field matches name
|
||||||
if param.Rename == param2.Rename || paramName == param2.Rename || paramName2 == param.Rename {
|
if param.Rename == param2.Rename || paramName == param2.Rename || paramName2 == param.Rename {
|
||||||
return fmt.Errorf("%s: %w", paramName, ErrParamNameConflict)
|
return fmt.Errorf("%s: %w", paramName, errParamNameConflict)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue