Skip to content

Commit

Permalink
Address some linter issues
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Balogh <[email protected]>
  • Loading branch information
javaducky committed Jan 20, 2024
1 parent efc085a commit c419d9c
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 42 deletions.
16 changes: 8 additions & 8 deletions api/api.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package api provides service access from external HTTP clients.
package api

import (
Expand Down Expand Up @@ -33,13 +34,10 @@ type API struct {
}

// New creates a new API instance.
func New(a *app.App) (api *API, err error) {
func New(a *app.App) (api *API) {
api = &API{App: a}
api.Config, err = initConfig()
if err != nil {
return nil, err
}
return api, nil
api.Config = initConfig()
return api
}

// Init is where we define the routes our API will support.
Expand Down Expand Up @@ -125,8 +123,10 @@ func (a *API) handler(f func(*app.Context, http.ResponseWriter, *http.Request) e
})
}

func (a *API) helloHandler(ctx *app.Context, w http.ResponseWriter, r *http.Request) error {
_, err := w.Write([]byte(fmt.Sprintf(`{"hello":"world","remote_address":%q,"trace_id":%q}`, ctx.RemoteAddress, ctx.TraceID)))
func (a *API) helloHandler(ctx *app.Context, w http.ResponseWriter, _ *http.Request) error {
_, err := w.Write([]byte(
fmt.Sprintf(`{"hello":"world","remote_address":%q,"trace_id":%q}`,
ctx.RemoteAddress, ctx.TraceID)))
return err
}

Expand Down
2 changes: 2 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
)

func TestAddressForRequest(t *testing.T) {
t.Parallel()
testCases := []struct {
remoteAddr string
expected string
Expand All @@ -23,6 +24,7 @@ func TestAddressForRequest(t *testing.T) {
for _, tc := range testCases {
tc := tc // pin
t.Run(tc.remoteAddr, func(t *testing.T) {
t.Parallel()
request := mockRequest(tc.remoteAddr)
actual := fixture.addressForRequest(&request)
if actual != tc.expected {
Expand Down
4 changes: 2 additions & 2 deletions api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ type Config struct {
Port int
}

func initConfig() (*Config, error) {
func initConfig() *Config {
config := &Config{
Port: viper.GetInt("Port"),
}
if config.Port == 0 {
config.Port = 9092
}
return config, nil
return config
}
11 changes: 7 additions & 4 deletions api/place.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/weesvc/weesvc-gorilla/model"
)

func (a *API) getPlaces(ctx *app.Context, w http.ResponseWriter, r *http.Request) error {
func (a *API) getPlaces(ctx *app.Context, w http.ResponseWriter, _ *http.Request) error {
places, err := ctx.GetPlaces()
if err != nil {
return err
Expand Down Expand Up @@ -42,7 +42,9 @@ type createPlaceResponse struct {
func (a *API) createPlace(ctx *app.Context, w http.ResponseWriter, r *http.Request) error {
var input createPlaceInput

defer r.Body.Close()
defer func() {
_ = r.Body.Close()
}()
body, err := io.ReadAll(r.Body)
if err != nil {
return err
Expand Down Expand Up @@ -100,7 +102,9 @@ func (a *API) updatePlaceByID(ctx *app.Context, w http.ResponseWriter, r *http.R

var input updatePlaceInput

defer r.Body.Close()
defer func() {
_ = r.Body.Close()
}()
body, err := io.ReadAll(r.Body)
if err != nil {
return err
Expand Down Expand Up @@ -145,7 +149,6 @@ func (a *API) updatePlaceByID(ctx *app.Context, w http.ResponseWriter, r *http.R
func (a *API) deletePlaceByID(ctx *app.Context, w http.ResponseWriter, r *http.Request) error {
id := getIDFromRequest(r)
err := ctx.DeletePlaceByID(id)

if err != nil {
return handleError(w, r, err)
}
Expand Down
4 changes: 3 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package app provides the core service implementations.
package app

import (
Expand Down Expand Up @@ -46,7 +47,7 @@ type ValidationError struct {
Message string `json:"message"`
}

// Here is a poorly formatted function!
// Error creates an error message from the underlying ValidationError.
func (e *ValidationError) Error() string {
return e.Message
}
Expand All @@ -57,6 +58,7 @@ type UserError struct {
StatusCode int `json:"-"`
}

// Error creates an error message from the underlying UserError.
func (e *UserError) Error() string {
return e.Message
}
2 changes: 1 addition & 1 deletion app/place.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (ctx *Context) validatePlace(place *model.Place) *ValidationError {
// UpdatePlace saves changes made to the provided place.
func (ctx *Context) UpdatePlace(place *model.Place) error {
if err := ctx.validatePlace(place); err != nil {
return nil
return err
}

return ctx.Database.UpdatePlace(place)
Expand Down
6 changes: 4 additions & 2 deletions cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ var migrateCmd = &cobra.Command{
if err != nil {
return err
}
defer a.Close()
defer func() {
_ = a.Close()
}()

// Make sure Migration table is there
logrus.Debug("ensuring migrations table is present")
Expand Down Expand Up @@ -61,7 +63,7 @@ var migrateCmd = &cobra.Command{
}

if uint(number) <= latest.Number && latest.Number > 0 {
logrus.Info("no migrations to apply, specified number is less than or equal to latest migration; backwards migrations are not supported")
logrus.Info("no migrations to apply; number is less than or equal to latest migration")
return nil
}

Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package cmd contains available commands for command line access.
package cmd

import (
Expand Down
5 changes: 1 addition & 4 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ var serveCmd = &cobra.Command{
return err
}

api, err := api.New(a)
if err != nil {
return err
}
api := api.New(a)

ctx, cancel := context.WithCancel(context.Background())

Expand Down
5 changes: 4 additions & 1 deletion db/db.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// Package db contains implementations for accessing back-end databases.
package db

import (
"github.com/jinzhu/gorm"
"github.com/pkg/errors"

// Initialize supported dialects
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/pkg/errors"
)

// Database represents the data access object.
Expand Down
38 changes: 19 additions & 19 deletions db/place_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ import (

func TestDatabase_GetPlaces(t *testing.T) {
t.Parallel()
placeDb := setupDatabase(t)
placeDB := setupDatabase(t)

places, err := placeDb.GetPlaces()
places, err := placeDB.GetPlaces()
assert.NoError(t, err)
assert.Equal(t, 10, len(places))
}

func TestDatabase_GetPlaceByID(t *testing.T) {
t.Parallel()
placeDb := setupDatabase(t)
placeDB := setupDatabase(t)

fetchId := uint(6)
place, err := placeDb.GetPlaceByID(fetchId)
fetchID := uint(6)
place, err := placeDB.GetPlaceByID(fetchID)
if assert.NoError(t, err) {
assert.Equal(t, fetchId, place.ID)
assert.Equal(t, fetchID, place.ID)
assert.Equal(t, "MIA", place.Name)
assert.Equal(t, "Miami International Airport, FL, USA", place.Description)
assert.Equal(t, 25.79516, place.Latitude)
Expand All @@ -39,7 +39,7 @@ func TestDatabase_GetPlaceByID(t *testing.T) {

func TestDatabase_CreatePlace(t *testing.T) {
t.Parallel()
placeDb := setupDatabase(t)
placeDB := setupDatabase(t)

newPlace := &model.Place{
ID: 20,
Expand All @@ -48,10 +48,10 @@ func TestDatabase_CreatePlace(t *testing.T) {
Latitude: 64.04126,
Longitude: -20.88530,
}
err := placeDb.CreatePlace(newPlace)
err := placeDB.CreatePlace(newPlace)
if assert.NoError(t, err) {
// Verify our inserted place
created, err := placeDb.GetPlaceByID(newPlace.ID)
created, err := placeDB.GetPlaceByID(newPlace.ID)
if assert.NoError(t, err) {
assert.Equal(t, newPlace.ID, created.ID)
assert.Equal(t, newPlace.Name, created.Name)
Expand All @@ -66,9 +66,9 @@ func TestDatabase_CreatePlace(t *testing.T) {

func TestDatabase_UpdatePlace(t *testing.T) {
t.Parallel()
placeDb := setupDatabase(t)
placeDB := setupDatabase(t)

original, err := placeDb.GetPlaceByID(7)
original, err := placeDB.GetPlaceByID(7)
if assert.NoError(t, err) {
changes := &model.Place{
ID: original.ID,
Expand All @@ -77,9 +77,9 @@ func TestDatabase_UpdatePlace(t *testing.T) {
Latitude: 29.42590,
Longitude: -98.48625,
}
if assert.NoError(t, placeDb.UpdatePlace(changes)) {
if assert.NoError(t, placeDB.UpdatePlace(changes)) {
// Verify the updated place
updated, err := placeDb.GetPlaceByID(original.ID)
updated, err := placeDB.GetPlaceByID(original.ID)
if assert.NoError(t, err) {
assert.Equal(t, original.ID, updated.ID)
assert.Equal(t, changes.Name, updated.Name)
Expand All @@ -95,14 +95,14 @@ func TestDatabase_UpdatePlace(t *testing.T) {

func TestDatabase_DeletePlaceByID(t *testing.T) {
t.Parallel()
placeDb := setupDatabase(t)
placeDB := setupDatabase(t)

deleteID := uint(1)
_, err := placeDb.GetPlaceByID(deleteID)
_, err := placeDB.GetPlaceByID(deleteID)
if assert.NoError(t, err) {
if assert.NoError(t, placeDb.DeletePlaceByID(deleteID)) {
if assert.NoError(t, placeDB.DeletePlaceByID(deleteID)) {
// Verify no longer retrievable
_, err = placeDb.GetPlaceByID(deleteID)
_, err = placeDB.GetPlaceByID(deleteID)
assert.EqualError(t, err, "unable to get place: record not found")
}
}
Expand All @@ -117,7 +117,7 @@ func setupDatabase(t *testing.T) *Database {
log.Fatal(err)
}

placeDb, err := New(&Config{
placeDB, err := New(&Config{
DatabaseURI: pgContainer.ConnectionString,
Dialect: "postgres",
Verbose: true,
Expand All @@ -132,5 +132,5 @@ func setupDatabase(t *testing.T) *Database {
}
})

return placeDb
return placeDB
}
1 change: 1 addition & 0 deletions env/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package env provides system-wide build information.
package env

var (
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package main provides the main entrypoint for the service.
package main

import "github.com/weesvc/weesvc-gorilla/cmd"
Expand Down
2 changes: 2 additions & 0 deletions migrations/migrate.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Package migrations contains updates to evolve database schemas
// as the application evolves.
package migrations

import "github.com/jinzhu/gorm"
Expand Down
1 change: 1 addition & 0 deletions model/place.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package model provides common data structures utilized by services.
package model

import "time"
Expand Down
1 change: 1 addition & 0 deletions testhelpers/containers.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package testhelpers contains utilities for unit and integration testing.
package testhelpers

import (
Expand Down

0 comments on commit c419d9c

Please sign in to comment.