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 ¶
- func HandleHttpError(w http.ResponseWriter, r *http.Request, status int, err error)
- func IntFromStringOrDefault(actual string, defaultValue int) int
- func RenderJSON(w http.ResponseWriter, r *http.Request, status int, data interface{})
- func RenderNoContent(w http.ResponseWriter, r *http.Request)
- type EmptyResponse
- type HttpService
- type PosgresRepo
- type Repo
- type RepoConfig
- type RepoInfo
- type RepoItem
- type SqlRepo
- func (repo *SqlRepo) Check() error
- func (repo *SqlRepo) Close() error
- func (repo *SqlRepo) Create(item *RepoItem) (*RepoItem, error)
- func (repo *SqlRepo) Delete(item *RepoItem) error
- func (repo *SqlRepo) DeleteAll() error
- func (repo *SqlRepo) Fetch(item *RepoItem) (*RepoItem, error)
- func (repo *SqlRepo) Info() (RepoInfo, error)
- func (repo *SqlRepo) Init() error
- func (repo *SqlRepo) IsConflict(err error) bool
- func (repo *SqlRepo) IsNotFound(err error) bool
- func (repo *SqlRepo) List(offset int, limit int) ([]*RepoItem, error)
- func (repo *SqlRepo) Update(item *RepoItem) (*RepoItem, error)
- type Sqlite3Repo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HandleHttpError ¶
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 ¶
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 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 ¶
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) Delete ¶
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 ¶
DeleteAll hard delete all items. This operation cannot be recovered, so use with care
func (*SqlRepo) IsConflict ¶
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 ¶
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)
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