Skip to content

Commit

Permalink
Add godoc to exported and packages (#17)
Browse files Browse the repository at this point in the history
* Added godoc to exported methods/structs and to different packages
* Updated ``revive`` lint rules.
* Fixed magic number lint errors.
  • Loading branch information
ribeirohugo committed Apr 8, 2022
1 parent 7c3e941 commit 03edbe3
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 8 deletions.
17 changes: 17 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ linters-settings:
# tab width in spaces. Default to 1.
tab-width: 1

revive:
rules:
- name: exported
severity: error

linters:
# Enable extra linters besides the default ones
enable:
Expand All @@ -32,6 +37,7 @@ linters:
- gocyclo
- gofmt
- goimports
- gomnd
- gosec
- gosimple
- govet
Expand All @@ -43,6 +49,7 @@ linters:
- rowserrcheck
- staticcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
Expand All @@ -51,12 +58,22 @@ linters:
- wsl

issues:
# Independently from option `exclude` we use default exclude patterns,
# it can be disabled by this option. To list all
# excluded by default patterns execute `golangci-lint run --help`.
# Default value for this option is true.
exclude-use-default: false

# The list of ids of default excludes to include or disable. By default it's empty.
include:
- EXC0002 # disable excluding of issues about comments from golint

exclude-rules:
# Exclude some linters from running on tests files.
- path: _test\.go
linters:
- dupl
- errcheck
- gomnd
- gosec
- lll
6 changes: 6 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package config holds configuration global data to be initialized.
package config

import (
Expand All @@ -15,22 +16,26 @@ const (
migrationsPathEnv = "MIGRATIONS_PATH"
)

// Database - database related config data
type Database struct {
Address string
Name string
MigrationsPath string
}

// Server - server related config data
type Server struct {
Host string
Port string
}

// Config - holds global configs
type Config struct {
Database Database
Server Server
}

// Load - loads configurations data
func Load() Config {
return Config{
Database: Database{
Expand All @@ -45,6 +50,7 @@ func Load() Config {
}
}

// GetServerAddress - returns server address based on Server configs
func (c *Config) GetServerAddress() string {
return fmt.Sprintf("%s:%s", c.Server.Host, c.Server.Port)
}
4 changes: 4 additions & 0 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//go:generate mockgen -package controller -source=controller.go -destination controller_mock.go

// Package controller holds application controllers.
package controller

import (
Expand All @@ -18,11 +19,13 @@ type Service interface {
DeleteUser(ctx context.Context, id string) error
}

// Controller - controller related struct
type Controller struct {
mux *mux.Router
service Service
}

// New - Instantiates a new Controller. Requires a Service.
func New(service Service) *Controller {
c := &Controller{
mux: mux.NewRouter(),
Expand All @@ -34,6 +37,7 @@ func New(service Service) *Controller {
return c
}

// Mux - Initializes routing and returns a Router.
func (c *Controller) Mux() *mux.Router {
c.routing()

Expand Down
12 changes: 12 additions & 0 deletions internal/controller/controller_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const (
userDeletedMessage = "user successfully removed"
)

// GetUser - Handles a get user request.
// Requires a URL parameter ID.
// Returns a user ID or an error in case of failure.
func (c *Controller) GetUser(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
userID := vars["id"]
Expand All @@ -35,6 +38,9 @@ func (c *Controller) GetUser(w http.ResponseWriter, r *http.Request) {
}
}

// NewUser - Handles a user creation request.
// Requires a user data JSON body.
// Returns the created user or an error in case of failure.
func (c *Controller) NewUser(w http.ResponseWriter, r *http.Request) {
var user model.User

Expand All @@ -58,6 +64,9 @@ func (c *Controller) NewUser(w http.ResponseWriter, r *http.Request) {
}
}

// UpdateUser - Handles a user update request.
// Requires the user ID to update and a user data JSON body.
// Returns the updated user or an error in case of failure.
func (c *Controller) UpdateUser(w http.ResponseWriter, r *http.Request) {
var user model.User

Expand All @@ -81,6 +90,9 @@ func (c *Controller) UpdateUser(w http.ResponseWriter, r *http.Request) {
}
}

// DeleteUser - Handles a user deletion.
// Requires the user ID to be removed.
// Returns OK or an error in case of failure.
func (c *Controller) DeleteUser(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
userID := vars["id"]
Expand Down
7 changes: 6 additions & 1 deletion internal/database/mongodb/database.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package mongodb holds MongoDB database and repository methods.
package mongodb

import (
Expand All @@ -10,11 +11,15 @@ import (
"go.mongodb.org/mongo-driver/mongo/readpref"
)

const timeoutDefaultDuration = 5

// Database - MongoDB database struct
type Database struct {
client *mongo.Client
database string
}

// New creates a new MongoDB database struct
func New(ctx context.Context, address string, database string) (*Database, error) {
clientOptions := options.Client().ApplyURI(address)

Expand All @@ -23,7 +28,7 @@ func New(ctx context.Context, address string, database string) (*Database, error
log.Fatal(err)
}

ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), timeoutDefaultDuration*time.Second)
defer cancel()

err = client.Ping(ctx, readpref.Primary())
Expand Down
9 changes: 5 additions & 4 deletions internal/database/mongodb/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"
)

const (
userCollection = "users"
)
const userCollection = "users"

// FindUser(ctx context.Context, id string) (model.User, error)
// FindUser - Returns a user for a given ID or an error if anything fails
func (db *Database) FindUser(ctx context.Context, id string) (model.User, error) {
var user model.User

Expand All @@ -39,6 +37,7 @@ func (db *Database) FindUser(ctx context.Context, id string) (model.User, error)
return user, nil
}

// UpdateUser - Updates a user and returns an error if anything fails
func (db *Database) UpdateUser(ctx context.Context, user model.User) error {
collection := db.client.Database(db.database).Collection(userCollection)

Expand All @@ -49,6 +48,7 @@ func (db *Database) UpdateUser(ctx context.Context, user model.User) error {
return err
}

// CreateUser - Creates a user and returns its ID or an error, if anything fails
func (db *Database) CreateUser(ctx context.Context, user model.User) (string, error) {
collection := db.client.Database(db.database).Collection(userCollection)

Expand All @@ -61,6 +61,7 @@ func (db *Database) CreateUser(ctx context.Context, user model.User) (string, er
return id, err
}

// DeleteUser - Deletes a User for a given ID and could return an error if anything fails
func (db *Database) DeleteUser(ctx context.Context, id string) error {
collection := db.client.Database(db.database).Collection(userCollection)

Expand Down
5 changes: 5 additions & 0 deletions internal/database/mysql/database.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package mysql holds MySQL database and repository methods.
package mysql

import (
Expand All @@ -16,10 +17,12 @@ const (
migrationsTable = "migrations"
)

// Database - MySQL database struct
type Database struct {
client *sql.DB
}

// New creates a new MySQL database struct
func New(address string) (*Database, error) {
db, err := sql.Open(mysqlDriveName, address)
if err != nil {
Expand All @@ -34,6 +37,8 @@ func New(address string) (*Database, error) {
return &Database{client: db}, nil
}

// Migrate - Runs a database migration for a given database and migrations path with the SQL files
// It returns an error in case of failure
func (db *Database) Migrate(databaseName string, migrationsPath string) error {
postgresConfig := mysqlMigration.Config{
MigrationsTable: migrationsTable,
Expand Down
8 changes: 7 additions & 1 deletion internal/database/mysql/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
"github.com/ribeirohugo/golang_startup/internal/model"
)

const baseNumber = 10

// FindUser - Returns a user for a given ID or an error if anything fails
func (db *Database) FindUser(ctx context.Context, id string) (model.User, error) {
row := db.client.QueryRowContext(ctx, `
SELECT id, username, email
Expand All @@ -33,6 +36,7 @@ func (db *Database) FindUser(ctx context.Context, id string) (model.User, error)
return user, nil
}

// UpdateUser - Updates a user and returns an error if anything fails
func (db *Database) UpdateUser(ctx context.Context, user model.User) error {
err := db.client.QueryRowContext(ctx, `
UPDATE users
Expand All @@ -46,6 +50,7 @@ func (db *Database) UpdateUser(ctx context.Context, user model.User) error {
return nil
}

// CreateUser - Creates a user and returns its ID or an error, if anything fails
func (db *Database) CreateUser(ctx context.Context, user model.User) (string, error) {
res, err := db.client.ExecContext(ctx, `
INSERT INTO users (username, email)
Expand All @@ -62,9 +67,10 @@ func (db *Database) CreateUser(ctx context.Context, user model.User) (string, er

log.Println(lastInsertedID)

return strconv.FormatInt(lastInsertedID, 10), nil
return strconv.FormatInt(lastInsertedID, baseNumber), nil
}

// DeleteUser - Deletes a User for a given ID and could return an error if anything fails
func (db *Database) DeleteUser(ctx context.Context, id string) error {
err := db.client.QueryRowContext(ctx, `
DELETE FROM users
Expand Down
6 changes: 5 additions & 1 deletion internal/database/postgres/database.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package postgres holds PostgreSQL database and repository methods.
package postgres

import (
Expand All @@ -18,11 +19,12 @@ const (
schemaName = "public"
)

// Database represents an initialised client to the database.
// Database - PostgreSQL database struct
type Database struct {
sql *sql.DB
}

// New creates a new PostgreSQL database struct
func New(address string) (*Database, error) {
db, err := sql.Open(postgresDriveName, address)
if err != nil {
Expand All @@ -37,6 +39,8 @@ func New(address string) (*Database, error) {
return &Database{sql: db}, nil
}

// Migrate - Runs a database migration for a given database and migrations path with the SQL files
// It returns an error in case of failure
func (db *Database) Migrate(databaseName string, migrationsPath string) error {
postgresConfig := postgresMigration.Config{
SchemaName: schemaName,
Expand Down
4 changes: 4 additions & 0 deletions internal/database/postgres/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/ribeirohugo/golang_startup/internal/model"
)

// FindUser - Returns a user for a given ID or an error if anything fails
func (db *Database) FindUser(ctx context.Context, id string) (model.User, error) {
row := db.sql.QueryRowContext(ctx, `
SELECT id, username, email
Expand All @@ -30,6 +31,7 @@ func (db *Database) FindUser(ctx context.Context, id string) (model.User, error)
return user, nil
}

// UpdateUser - Updates a user and returns an error if anything fails
func (db *Database) UpdateUser(ctx context.Context, user model.User) error {
err := db.sql.QueryRowContext(ctx, `
UPDATE users
Expand All @@ -43,6 +45,7 @@ func (db *Database) UpdateUser(ctx context.Context, user model.User) error {
return nil
}

// CreateUser - Creates a user and returns its ID or an error, if anything fails
func (db *Database) CreateUser(ctx context.Context, user model.User) (string, error) {
lastInsertedID := ""

Expand All @@ -58,6 +61,7 @@ func (db *Database) CreateUser(ctx context.Context, user model.User) (string, er
return lastInsertedID, nil
}

// DeleteUser - Deletes a User for a given ID and could return an error if anything fails
func (db *Database) DeleteUser(ctx context.Context, id string) error {
err := db.sql.QueryRowContext(ctx, `
DELETE FROM users
Expand Down
2 changes: 2 additions & 0 deletions internal/model/user.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Package model holds domain model structs and methods.
package model

// User - domain model for a user
type User struct {
ID string `bson:"_id,omitempty" json:"id,omitempty"`
Name string `bson:"name,omitempty" json:"name,omitempty"`
Expand Down
Loading

0 comments on commit 03edbe3

Please sign in to comment.