consider collision only if every part is matching

This commit is contained in:
Adrien Marquès 2020-03-21 15:48:59 +01:00
parent 0e6dfbe580
commit e3adbf48ca
Signed by: xdrm-brackets
GPG Key ID: D75243CA236D825E
1 changed files with 35 additions and 8 deletions

View File

@ -74,6 +74,8 @@ func (server *Server) collide() error {
continue continue
} }
partErrors := make([]error, 0)
// for each part // for each part
for pi, aPart := range aParts { for pi, aPart := range aParts {
bPart := bParts[pi] bPart := bParts[pi]
@ -83,13 +85,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 {
return fmt.Errorf("%s: %s '%s'", ErrPatternCollision, aService.Method, aService.Pattern) partErrors = append(partErrors, fmt.Errorf("%s '%s': %w (path %s and %s)", aService.Method, aService.Pattern, ErrPatternCollision, aPart, bPart))
continue
} }
// no capture -> check equal // no capture -> check equal
if !aIsCapture && !bIsCapture { if !aIsCapture && !bIsCapture {
if aPart == bPart { if aPart == bPart {
return fmt.Errorf("%s: %s '%s'", ErrPatternCollision, aService.Method, aService.Pattern) partErrors = append(partErrors, fmt.Errorf("%s '%s': %w (same path '%s')", aService.Method, aService.Pattern, ErrPatternCollision, aPart))
} }
continue continue
} }
@ -100,12 +103,14 @@ 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 {
return fmt.Errorf("%s: %s '%s'", ErrPatternCollision, aService.Method, aService.Pattern) partErrors = append(partErrors, fmt.Errorf("%s '%s': %w (invalid type for %s)", aService.Method, aService.Pattern, ErrPatternCollision, aPart))
continue
} }
// fail if not valid // fail if not valid
if _, valid := input.Validator(aPart); !valid { if _, valid := input.Validator(bPart); valid {
return fmt.Errorf("%s: %s '%s'", ErrPatternCollision, aService.Method, aService.Pattern) partErrors = append(partErrors, fmt.Errorf("%s '%s': %w (%s captures '%s')", aService.Method, aService.Pattern, ErrPatternCollision, aPart, bPart))
continue
} }
// B captures A -> check type (A is B ?) // B captures A -> check type (A is B ?)
@ -114,15 +119,37 @@ 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 {
return fmt.Errorf("%s: %s '%s'", ErrPatternCollision, aService.Method, aService.Pattern) partErrors = append(partErrors, fmt.Errorf("%s '%s': %w (invalid type for %s)", bService.Method, bService.Pattern, ErrPatternCollision, bPart))
continue
} }
// fail if not valid // fail if not valid
if _, valid := input.Validator(bPart); !valid { if _, valid := input.Validator(aPart); valid {
return fmt.Errorf("%s: %s '%s'", ErrPatternCollision, aService.Method, aService.Pattern) partErrors = append(partErrors, fmt.Errorf("%s '%s': %w (%s captures '%s')", bService.Method, bService.Pattern, ErrPatternCollision, bPart, aPart))
continue
} }
} }
partErrors = append(partErrors, nil)
}
// if at least 1 url part does not match -> ok
var firstError error
oneMismatch := false
for _, err := range partErrors {
if err != nil && firstError == nil {
firstError = err
}
if err == nil {
oneMismatch = true
continue
}
}
if !oneMismatch {
return firstError
} }
} }