diff --git a/internal/config/config.go b/internal/config/config.go index 55089bb..259d70e 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -20,11 +20,11 @@ type Server struct { // make datatypes available when checking and formatting the read configuration. func (srv *Server) Parse(r io.Reader) error { 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 { - return fmt.Errorf("%s: %w", ErrFormat, err) + return fmt.Errorf("%s: %w", errFormat, err) } return nil @@ -41,7 +41,7 @@ func (server Server) validate(datatypes ...datatype.T) error { // check for collisions if err := server.collide(); err != nil { - return fmt.Errorf("%s: %w", ErrFormat, err) + return fmt.Errorf("%s: %w", errFormat, err) } return nil @@ -92,14 +92,14 @@ func (server *Server) collide() error { // both captures -> as we cannot check, consider a collision 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 } // no capture -> check equal if !aIsCapture && !bIsCapture { 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 } } @@ -110,13 +110,13 @@ func (server *Server) collide() error { // fail if no type or no validator 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 } // fail if not 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 } @@ -126,13 +126,13 @@ func (server *Server) collide() error { // fail if no type or no validator 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 } // fail if not 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 } } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 6ff7dcd..11e9ad7 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -21,15 +21,15 @@ func TestLegalServiceName(t *testing.T) { // empty { `[ { "method": "GET", "info": "a", "path": "" } ]`, - ErrInvalidPattern, + errInvalidPattern, }, { `[ { "method": "GET", "info": "a", "path": "no-starting-slash" } ]`, - ErrInvalidPattern, + errInvalidPattern, }, { `[ { "method": "GET", "info": "a", "path": "ending-slash/" } ]`, - ErrInvalidPattern, + errInvalidPattern, }, { `[ { "method": "GET", "info": "a", "path": "/" } ]`, @@ -45,35 +45,35 @@ func TestLegalServiceName(t *testing.T) { }, { `[ { "method": "GET", "info": "a", "path": "/invalid/s{braces}" } ]`, - ErrInvalidPatternBraceCapture, + errInvalidPatternBraceCapture, }, { `[ { "method": "GET", "info": "a", "path": "/invalid/{braces}a" } ]`, - ErrInvalidPatternBraceCapture, + errInvalidPatternBraceCapture, }, { `[ { "method": "GET", "info": "a", "path": "/invalid/{braces}" } ]`, - ErrUndefinedBraceCapture, + errUndefinedBraceCapture, }, { `[ { "method": "GET", "info": "a", "path": "/invalid/s{braces}/abc" } ]`, - ErrInvalidPatternBraceCapture, + errInvalidPatternBraceCapture, }, { `[ { "method": "GET", "info": "a", "path": "/invalid/{braces}s/abc" } ]`, - ErrInvalidPatternBraceCapture, + errInvalidPatternBraceCapture, }, { `[ { "method": "GET", "info": "a", "path": "/invalid/{braces}/abc" } ]`, - ErrUndefinedBraceCapture, + errUndefinedBraceCapture, }, { `[ { "method": "GET", "info": "a", "path": "/invalid/{b{races}s/abc" } ]`, - ErrInvalidPatternBraceCapture, + errInvalidPatternBraceCapture, }, { `[ { "method": "GET", "info": "a", "path": "/invalid/{braces}/}abc" } ]`, - ErrInvalidPatternBraceCapture, + errInvalidPatternBraceCapture, }, } @@ -143,8 +143,8 @@ func TestAvailableMethods(t *testing.T) { t.FailNow() } - if !test.ValidMethod && !errors.Is(err, ErrUnknownMethod) { - t.Errorf("expected error <%s> got <%s>", ErrUnknownMethod, err) + if !test.ValidMethod && !errors.Is(err, errUnknownMethod) { + t.Errorf("expected error <%s> got <%s>", errUnknownMethod, err) t.FailNow() } }) @@ -217,8 +217,8 @@ func TestParseMissingMethodDescription(t *testing.T) { t.FailNow() } - if !test.ValidDescription && !errors.Is(err, ErrMissingDescription) { - t.Errorf("expected error <%s> got <%s>", ErrMissingDescription, err) + if !test.ValidDescription && !errors.Is(err, errMissingDescription) { + t.Errorf("expected error <%s> got <%s>", errMissingDescription, err) t.FailNow() } }) @@ -321,7 +321,7 @@ func TestParseParameters(t *testing.T) { } } ]`, - ErrMissingParamDesc, + errMissingParamDesc, }, { // invalid param name suffix `[ @@ -334,7 +334,7 @@ func TestParseParameters(t *testing.T) { } } ]`, - ErrMissingParamDesc, + errMissingParamDesc, }, { // missing param description @@ -348,7 +348,7 @@ func TestParseParameters(t *testing.T) { } } ]`, - ErrMissingParamDesc, + errMissingParamDesc, }, { // empty param description `[ @@ -361,7 +361,7 @@ func TestParseParameters(t *testing.T) { } } ]`, - ErrMissingParamDesc, + errMissingParamDesc, }, { // missing param type @@ -375,7 +375,7 @@ func TestParseParameters(t *testing.T) { } } ]`, - ErrMissingParamType, + errMissingParamType, }, { // empty param type `[ @@ -388,7 +388,7 @@ func TestParseParameters(t *testing.T) { } } ]`, - ErrMissingParamType, + errMissingParamType, }, { // invalid type (optional mark only) `[ @@ -402,7 +402,7 @@ func TestParseParameters(t *testing.T) { } ]`, - ErrMissingParamType, + errMissingParamType, }, { // valid description + valid type `[ @@ -444,7 +444,7 @@ func TestParseParameters(t *testing.T) { } ]`, // 2 possible errors as map order is not deterministic - ErrParamNameConflict, + errParamNameConflict, }, { // rename conflict with name `[ @@ -459,7 +459,7 @@ func TestParseParameters(t *testing.T) { } ]`, // 2 possible errors as map order is not deterministic - ErrParamNameConflict, + errParamNameConflict, }, { // rename conflict with rename `[ @@ -474,7 +474,7 @@ func TestParseParameters(t *testing.T) { } ]`, // 2 possible errors as map order is not deterministic - ErrParamNameConflict, + errParamNameConflict, }, { // 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 `[ @@ -569,7 +569,7 @@ func TestParseParameters(t *testing.T) { } } ]`, - ErrUnspecifiedBraceCapture, + errUnspecifiedBraceCapture, }, { // URI parameter not defined `[ @@ -580,7 +580,7 @@ func TestParseParameters(t *testing.T) { "in": { } } ]`, - ErrUndefinedBraceCapture, + errUndefinedBraceCapture, }, } @@ -637,7 +637,7 @@ func TestServiceCollision(t *testing.T) { "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, }, { `[ diff --git a/internal/config/errors.go b/internal/config/errors.go index 9e2cf17..a11596d 100644 --- a/internal/config/errors.go +++ b/internal/config/errors.go @@ -7,53 +7,53 @@ func (err cerr) Error() string { return string(err) } -// ErrRead - a problem ocurred when trying to read the configuration file -const ErrRead = cerr("cannot read config") +// errRead - a problem ocurred when trying to read the configuration file +const errRead = cerr("cannot read config") -// ErrUnknownMethod - invalid http method -const ErrUnknownMethod = cerr("unknown HTTP method") +// errUnknownMethod - invalid http method +const errUnknownMethod = cerr("unknown HTTP method") -// ErrFormat - a invalid format has been detected -const ErrFormat = cerr("invalid config format") +// errFormat - a invalid format has been detected +const errFormat = cerr("invalid config format") -// ErrPatternCollision - there is a collision between 2 services' patterns (same method) -const ErrPatternCollision = cerr("pattern collision") +// errPatternCollision - there is a collision between 2 services' patterns (same method) +const errPatternCollision = cerr("pattern collision") -// ErrInvalidPattern - a service pattern is malformed -const ErrInvalidPattern = cerr("must begin with a '/' and not end with") +// errInvalidPattern - a service pattern is malformed +const errInvalidPattern = cerr("must begin with a '/' and not end with") -// ErrInvalidPatternBraceCapture - a service pattern brace capture is invalid -const ErrInvalidPatternBraceCapture = cerr("invalid uri capturing braces") +// errInvalidPatternBraceCapture - a service pattern brace capture is invalid +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") +// 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'") +// 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") +// errUndefinedBraceCapture - a parameter brace capture in the pattern is not defined in parameters +const errUndefinedBraceCapture = cerr("capturing brace missing input definition") -// ErrMissingDescription - a service is missing its description -const ErrMissingDescription = cerr("missing description") +// errMissingDescription - a service is missing its description +const errMissingDescription = cerr("missing description") -// ErrIllegalOptionalURIParam - an URI parameter cannot be optional -const ErrIllegalOptionalURIParam = cerr("URI parameter cannot be optional") +// errIllegalOptionalURIParam - an URI parameter cannot be optional +const errIllegalOptionalURIParam = cerr("URI parameter cannot be optional") -// ErrOptionalOption - an output is optional -const ErrOptionalOption = cerr("output cannot be optional") +// errOptionalOption - an output is optional +const errOptionalOption = cerr("output cannot be optional") -// ErrMissingParamDesc - a parameter is missing its description -const ErrMissingParamDesc = cerr("missing parameter description") +// errMissingParamDesc - a parameter is missing its description +const errMissingParamDesc = cerr("missing parameter description") -// ErrUnknownDataType - a parameter has an unknown datatype name -const ErrUnknownDataType = cerr("unknown data type") +// errUnknownDataType - a parameter has an unknown datatype name +const errUnknownDataType = cerr("unknown data type") -// ErrIllegalParamName - a parameter has an illegal name -const ErrIllegalParamName = cerr("illegal parameter name") +// errIllegalParamName - a parameter has an illegal name +const errIllegalParamName = cerr("illegal parameter name") -// ErrMissingParamType - a parameter has an illegal type -const ErrMissingParamType = cerr("missing parameter type") +// errMissingParamType - a parameter has an illegal type +const errMissingParamType = cerr("missing parameter type") -// ErrParamNameConflict - a parameter has a conflict with its name/rename field -const ErrParamNameConflict = cerr("name conflict for parameter") +// errParamNameConflict - a parameter has a conflict with its name/rename field +const errParamNameConflict = cerr("name conflict for parameter") diff --git a/internal/config/parameter.go b/internal/config/parameter.go index ca36a1c..2a0a0cd 100644 --- a/internal/config/parameter.go +++ b/internal/config/parameter.go @@ -23,12 +23,12 @@ type Parameter struct { func (param *Parameter) validate(datatypes ...datatype.T) error { // missing description if len(param.Description) < 1 { - return ErrMissingParamDesc + return errMissingParamDesc } // invalid type if len(param.Type) < 1 || param.Type == "?" { - return ErrMissingParamType + return errMissingParamType } // optional type transform @@ -46,7 +46,7 @@ func (param *Parameter) validate(datatypes ...datatype.T) error { } } if param.Validator == nil { - return ErrUnknownDataType + return errUnknownDataType } return nil diff --git a/internal/config/service.go b/internal/config/service.go index 0f79764..1d67456 100644 --- a/internal/config/service.go +++ b/internal/config/service.go @@ -118,7 +118,7 @@ func (svc *Service) validate(datatypes ...datatype.T) error { // check description 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 @@ -130,7 +130,7 @@ func (svc *Service) validate(datatypes ...datatype.T) error { // fail if a brace capture remains undefined for _, capture := range svc.Captures { 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 ErrUnknownMethod + return errUnknownMethod } func (svc *Service) isPatternValid() error { @@ -157,13 +157,13 @@ func (svc *Service) isPatternValid() error { // empty pattern if length < 1 { - return ErrInvalidPattern + return errInvalidPattern } if length > 1 { // pattern not starting with '/' or ending with '/' 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) for i, part := range parts { if len(part) < 1 { - return ErrInvalidPattern + return errInvalidPattern } // if brace capture @@ -192,7 +192,7 @@ func (svc *Service) isPatternValid() error { // fail on invalid format if strings.ContainsAny(part, "{}") { - return ErrInvalidPatternBraceCapture + return errInvalidPatternBraceCapture } } @@ -211,7 +211,7 @@ func (svc *Service) validateInput(types []datatype.T) error { // for each parameter for paramName, param := range svc.Input { 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 @@ -228,7 +228,7 @@ func (svc *Service) validateInput(types []datatype.T) error { } } if !found { - return fmt.Errorf("%s: %w", paramName, ErrUnspecifiedBraceCapture) + return fmt.Errorf("%s: %w", paramName, errUnspecifiedBraceCapture) } iscapture = true @@ -251,7 +251,7 @@ func (svc *Service) validateInput(types []datatype.T) error { // fail if capture or query without rename 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 @@ -266,7 +266,7 @@ func (svc *Service) validateInput(types []datatype.T) error { // capture parameter cannot be optional if iscapture && param.Optional { - return fmt.Errorf("%s: %w", paramName, ErrIllegalOptionalURIParam) + return fmt.Errorf("%s: %w", paramName, errIllegalOptionalURIParam) } // 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.3. Renamed field matches name 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 paramName, param := range svc.Output { if len(paramName) < 1 { - return fmt.Errorf("%s: %w", paramName, ErrIllegalParamName) + return fmt.Errorf("%s: %w", paramName, errIllegalParamName) } // use param name if no rename @@ -315,7 +315,7 @@ func (svc *Service) validateOutput(types []datatype.T) error { } if param.Optional { - return fmt.Errorf("%s: %w", paramName, ErrOptionalOption) + return fmt.Errorf("%s: %w", paramName, errOptionalOption) } // 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.3. Renamed field matches name 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) } }