Skip to content

Commit

Permalink
Fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jvcoutinho committed May 2, 2023
1 parent 03706a9 commit d4adc47
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 22 deletions.
14 changes: 14 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,22 @@ linters:
- nosnakecase
- maligned

linters-settings:
ireturn:
allow:
- anon
- error
- empty
- stdlib
- generic
- T

issues:
exclude-rules:
- path: '_test\.go'
linters:
- funlen

- source: 'ok bool'
linters:
- nonamedreturns
8 changes: 4 additions & 4 deletions internal/lists/linked_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ func (l *LinkedList[T]) ElementAt(index int) (value T, ok bool) {
return value, false
}

i := 0
for currentNode := l.head; currentNode != nil; currentNode = currentNode.next {
if i == index {
for currentNodeIndex, currentNode := 0, l.head; currentNode != nil; currentNode = currentNode.next {
if currentNodeIndex == index {
value = currentNode.value

break
}

i++
currentNodeIndex++
}

return value, true
Expand Down
1 change: 1 addition & 0 deletions internal/maps/contains_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package maps
// ContainsKey returns true if and only if elem is a key defined in _map.
func ContainsKey[T comparable, V any](_map map[T]V, elem T) bool {
_, ok := _map[elem]

return ok
}
14 changes: 7 additions & 7 deletions internal/routes/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ package routes

import "fmt"

// ErrRouteAlreadyDefined is the error for already defined routes.
type ErrRouteAlreadyDefined struct {
// RouteAlreadyDefinedError is the error for already defined routes.
type RouteAlreadyDefinedError struct {
Route Route
}

func (e ErrRouteAlreadyDefined) Error() string {
func (e RouteAlreadyDefinedError) Error() string {
return fmt.Sprintf("%s has been already defined", e.Route)
}

// ErrDuplicateArguments is the error for duplicate arguments in the same pattern.
// DuplicateArgumentsError is the error for duplicate arguments in the same pattern.
//
// Example: /users/:id/books/:id
type ErrDuplicateArguments struct {
// Example: /users/:id/books/:id.
type DuplicateArgumentsError struct {
Duplicate string
}

func (e ErrDuplicateArguments) Error() string {
func (e DuplicateArgumentsError) Error() string {
return fmt.Sprintf("a pattern can not contain two arguments with the same name (%s)", e.Duplicate)
}
12 changes: 7 additions & 5 deletions internal/routes/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ func NewGraph() Graph {
// CanBeInserted checks if route can be defined in this graph.
//
// If it can't be inserted (ok equals false), CanBeInserted returns the reason error.
func (g Graph) CanBeInserted(route Route) (reason error, ok bool) {
func (g Graph) CanBeInserted(route Route) (ok bool, reason error) {
routePath := append(route.Path(), terminalNode)

if duplicate, has := hasDuplicateArguments(routePath); has {
return ErrDuplicateArguments{duplicate}, false
return false, DuplicateArgumentsError{duplicate}
}

if !maps.ContainsKey(g, route.Method) {
return nil, true
return true, nil
}

previousNode := route.Method
Expand All @@ -38,13 +38,13 @@ func (g Graph) CanBeInserted(route Route) (reason error, ok bool) {
}

if !maps.ContainsKey(g, path) || !slices.Contains(adjacentNodes, path) {
return nil, true
return true, nil
}

previousNode = path
}

return ErrRouteAlreadyDefined{route}, false
return false, RouteAlreadyDefinedError{route}
}

// Add adds the route to this graph.
Expand Down Expand Up @@ -102,6 +102,7 @@ func (g Graph) matchAdjacentNode(parent string, path []string, pathIndex int, ma
}

match.addPathFragmentAtBeginning(child)

return true
}

Expand All @@ -116,6 +117,7 @@ func (g Graph) matchAdjacentNode(parent string, path []string, pathIndex int, ma
}

match.addPathArgumentAtBeginning(childrenArguments[i], child)

return true
}

Expand Down
10 changes: 5 additions & 5 deletions internal/routes/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestGraph_CanBeInserted(t *testing.T) {
routes.NewRoute("/users/:id", http.MethodGet),
},
routeToCheck: routes.NewRoute("/users/:user_id", http.MethodGet),
expectedError: routes.ErrRouteAlreadyDefined{
expectedError: routes.RouteAlreadyDefinedError{
Route: routes.NewRoute("/users/:user_id", http.MethodGet),
},
expectedOk: false,
Expand All @@ -62,7 +62,7 @@ func TestGraph_CanBeInserted(t *testing.T) {
name: "UndefinedRoute_DuplicateParameters",
currentRoutes: []routes.Route{},
routeToCheck: routes.NewRoute("/users/:id/books/:id", http.MethodGet),
expectedError: routes.ErrDuplicateArguments{Duplicate: ":id"},
expectedError: routes.DuplicateArgumentsError{Duplicate: ":id"},
expectedOk: false,
},
{
Expand All @@ -71,7 +71,7 @@ func TestGraph_CanBeInserted(t *testing.T) {
routes.NewRoute("/users/:id", http.MethodGet),
},
routeToCheck: routes.NewRoute("/users/:user_id", http.MethodGet),
expectedError: routes.ErrRouteAlreadyDefined{
expectedError: routes.RouteAlreadyDefinedError{
Route: routes.NewRoute("/users/:user_id", http.MethodGet),
},
expectedOk: false,
Expand All @@ -90,11 +90,11 @@ func TestGraph_CanBeInserted(t *testing.T) {
}

// Act
actualError, actualOk := graph.CanBeInserted(test.routeToCheck)
actualOk, actualError := graph.CanBeInserted(test.routeToCheck)

// Assert
require.ErrorIs(t, test.expectedError, actualError)
require.Equal(t, test.expectedOk, actualOk)
require.ErrorIs(t, test.expectedError, actualError)
})
}
}
Expand Down
1 change: 1 addition & 0 deletions internal/sets/hash_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ func (s HashSet[T]) Len() int {
// Contains returns true if and only if elem is contained in this set.
func (s HashSet[T]) Contains(elem T) bool {
_, ok := s[elem]

return ok
}
2 changes: 1 addition & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewRouter() *Router {
func (r *Router) Handle(pattern string, method string, handler HandleFunc) {
route := routes.NewRoute(pattern, method)

if err, ok := r.graph.CanBeInserted(route); !ok {
if ok, err := r.graph.CanBeInserted(route); !ok {
panic(err)
}

Expand Down

0 comments on commit d4adc47

Please sign in to comment.