util

package
v0.0.0-...-84d4e1c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 3, 2019 License: MIT-0 Imports: 14 Imported by: 0

Documentation

Overview

util provides with simple utility types and functions so that our main application package is less cluttered

util provides with simple utility types and functions so that our main application package is less cluttered

util provides with simple utility types and functions so that our main application package is less cluttered

util provides with simple utility types and functions so that our main application package is less cluttered

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleHttpError

func HandleHttpError(w http.ResponseWriter, r *http.Request, status int, err error)

HttpError handles a http error by returning a JSON response with the appropiate status code, and logging the root cause to the console

func IntFromStringOrDefault

func IntFromStringOrDefault(actual string, defaultValue int) int

IntFromString tries to parse the given string, as an int. In case of error, the default int value is returned instead

func RenderJSON

func RenderJSON(w http.ResponseWriter, r *http.Request, status int, data interface{})

RenderJSON is a convenience function that marshalls the given interface value as json, with the given http status code

func RenderNoContent

func RenderNoContent(w http.ResponseWriter, r *http.Request)

RenderNoContent returns a 204 and an empty response body

Types

type EmptyResponse

type EmptyResponse struct{}

EmptyResponse represents an empty JSON response

type HttpService

type HttpService struct {
	BaseUrl string
}

HttpService is a simple base type for Http services Provides with some convenience functions that can be reused by more concrete implementations

func (*HttpService) UrlFor

func (s *HttpService) UrlFor(path string) string

urlFor builds a new url by prefixing the given path with the base url configured for the service

type PosgresRepo

type PosgresRepo struct {
	SqlRepo
	// contains filtered or unexported fields
}

A Postgres Repo inherits the behavior from a generic sql repo.

func (*PosgresRepo) Description

func (repo *PosgresRepo) Description() string

Description returns this database storage type

type Repo

type Repo interface {

	// Init initializes the repo.
	Init() error

	// A simple description, for logging
	// purposes
	Description() string

	// Repository info
	Info() (RepoInfo, error)

	// A simple function that returns an error if
	// the database is not in a healthy state
	Check() error

	// Close the database
	Close() error

	// Return a finite list of db items
	List(offset int, limit int) ([]*RepoItem, error)

	// Create a new database item
	Create(item *RepoItem) (*RepoItem, error)

	// Update an existing database item
	// and return the new version
	Update(item *RepoItem) (*RepoItem, error)

	// Get all the information for the given
	// repo item. Note: the repo item passed as argument
	// does not need to hold every info about the
	// the item we are interested in, just basic
	// identification data (id, version,etc..)
	Fetch(item *RepoItem) (*RepoItem, error)

	// Delete a single repo item
	Delete(item *RepoItem) error

	// Delete all items from this repo
	DeleteAll() error

	// Defines an abstract way of determining
	// whether the given error represents a database
	// conflict
	IsConflict(err error) bool

	// Defines an abstract way of determining
	// whether the given error represents
	// a item that was not found
	IsNotFound(err error) bool
}

Repo is a small abstraction of a database so that we can easily switch between vendors or even storage technology

func NewPostgresRepo

func NewPostgresRepo(config RepoConfig) (Repo, error)

NewPostgresRepo creates a new Postgres backed repo from the given configuration

func NewRepo

func NewRepo(config RepoConfig) (Repo, error)

NewRepo returns a new repo implementation for the given configuration or error if not supported. The caller is in charge of closing the repo when it is no longer needed.

func NewSqlite3Repo

func NewSqlite3Repo(config RepoConfig) (Repo, error)

NewSqlite3Repo creates a new Sqlite3 backed repo from the given configuration

type RepoConfig

type RepoConfig struct {
	Driver     string
	Uri        string
	Migrations string
	Schema     string
}

RepoConfig is a simple container for database configuration

type RepoInfo

type RepoInfo struct {
	// The total number of non-deleted items in the repo
	Count int `json:"count"`
}

Basic repository live information Could be useful for audit or monitoring purposes

type RepoItem

type RepoItem struct {
	Id           string `db:"id"`
	Version      int    `db:"version"`
	Organisation string `db:"organisation"`
	Attributes   string `db:"attributes"`
}

RepoItem represents a generic repo item record that can be persisted

type SqlRepo

type SqlRepo struct {
	// contains filtered or unexported fields
}

A Generic SQL rep. Defines the schema it operates on (a database table) and the set of sql statement it executes

func (*SqlRepo) Check

func (repo *SqlRepo) Check() error

Check performs a simple check on the database

func (*SqlRepo) Close

func (repo *SqlRepo) Close() error

Close the database

func (*SqlRepo) Create

func (repo *SqlRepo) Create(item *RepoItem) (*RepoItem, error)

Create a new item in the database

func (*SqlRepo) Delete

func (repo *SqlRepo) Delete(item *RepoItem) error

Delete deletes the item from the repo. In this implementation, We simply mark the item as deleted. This is to make sure it's id is not reused by future payments

func (*SqlRepo) DeleteAll

func (repo *SqlRepo) DeleteAll() error

DeleteAll hard delete all items. This operation cannot be recovered, so use with care

func (*SqlRepo) Fetch

func (repo *SqlRepo) Fetch(item *RepoItem) (*RepoItem, error)

Fetch tries to find a repo item by its id. Returns an error if not found

func (*SqlRepo) Info

func (repo *SqlRepo) Info() (RepoInfo, error)

Info returns basic information about the current status of the repo

func (*SqlRepo) Init

func (repo *SqlRepo) Init() error

Init initializes all sql statements with the proper database table

func (*SqlRepo) IsConflict

func (repo *SqlRepo) IsConflict(err error) bool

IsConflict returns true, if the given error denotes a database conflict. Note: this kind of error is something we generate and send to the web layer (it is not coming from the underlying sql library)

func (*SqlRepo) IsNotFound

func (repo *SqlRepo) IsNotFound(err error) bool

IsNotFound Ireturns true, if the given error denotes an item that was not found. Note: this kind of error is something we generate and send to the web layer (it is not coming from the underlying sql library)

func (*SqlRepo) List

func (repo *SqlRepo) List(offset int, limit int) ([]*RepoItem, error)

List Return a list of db items. Ignore items marked as deleted

func (*SqlRepo) Update

func (repo *SqlRepo) Update(item *RepoItem) (*RepoItem, error)

Update an existing item in the database. Returns the updated db item, or an error

type Sqlite3Repo

type Sqlite3Repo struct {
	SqlRepo
	// contains filtered or unexported fields
}

A Sqlite3 Repo inherits the behavior from a generic sql repo.

func (*Sqlite3Repo) Description

func (repo *Sqlite3Repo) Description() string

Description returns this database storage type

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL