fairy

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2021 License: MIT Imports: 17 Imported by: 0

README

Fairy

Go Report Card Go Reference

Fairy contains several general tools for easier and simpler development.

All these tools are simplified version of the original ones and for general use. Your case may need a more complex one but I hope these tools can help you or at least become a reference for your own tools.

Good luck.


Installation

go get github.com/rl404/fairy

Usage

Please go to the documentation or example folder.

License

MIT License

Copyright (c) 2021 Axel

Documentation

Overview

Package fairy contains several general tools for easier and simpler development.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidCacheType = errors.New("invalid cache type")

ErrInvalidCacheType is error for invalid cache type.

View Source
var ErrInvalidLogType = errors.New("invalid log type")

ErrInvalidLogType is error for invalid log type.

View Source
var ErrInvalidPubsubType = errors.New("invalid pubsub type")

ErrInvalidPubsubType is error for invalid pubsub type.

Functions

func HandlerFuncWithLog

func HandlerFuncWithLog(logger Logger, next http.HandlerFunc) http.HandlerFunc

HandlerFuncWithLog is http handler func with log.

func HandlerWithLog

func HandlerWithLog(logger Logger, next http.Handler) http.Handler

HandlerWithLog is http handler with log. Also includes error stack tracing feature if you use it.

func MiddlewareWithLog

func MiddlewareWithLog(logger Logger) func(http.Handler) http.Handler

MiddlewareWithLog is http middleware that will log the request and response.

Types

type CacheType

type CacheType int8

CacheType is type for cache.

const (
	NoCache CacheType = iota
	InMemory
	RedisCache
	Memcache
)

Available types for cache.

type Cacher

type Cacher interface {
	// Get data from cache. The returned value will be
	// assigned to param `data`. Param `data` should
	// be a pointer just like when using json.Unmarshal.
	Get(key string, data interface{}) error
	// Save data to cache. Set and Get should be using
	// the same encoding method. For example, json.Marshal
	// for Set and json.Unmarshal for Get.
	Set(key string, data interface{}) error
	// Delete data from cache.
	Delete(key string) error
	// Close cache connection.
	Close() error
}

Cacher is caching interface.

See usage example in example folder.

func NewCache

func NewCache(cacheType CacheType, address string, password string, expiredTime time.Duration) (Cacher, error)

NewCache to create new cache client depends on the type.

type Channel

type Channel interface {
	// Read and process incoming message. Param `data` should
	// be a pointer just like when using json.Unmarshal.
	Read(data interface{}) (<-chan interface{}, <-chan error)
	// Close subscription.
	Close() error
}

Channel is channel interface.

See usage example in example folder.

type ErrStacker added in v0.2.0

type ErrStacker interface {
	// Init the context so it can be used for stack.
	Init(ctx context.Context) context.Context
	// Wrap the error and put it in the error stack.
	Wrap(ctx context.Context, err error, errs ...error) error
	// Get the error stack.
	Get(ctx context.Context) interface{}
}

ErrStacker is error stack interface.

See usage example in example folder.

func NewErrStacker added in v0.2.0

func NewErrStacker() ErrStacker

NewErrStacker to create new error stack.

type LogLevel

type LogLevel int8

LogLevel is level of log that will be printed. Will print level that is higher than your chosen one.

const (
	TraceLevel LogLevel = iota - 1
	DebugLevel
	InfoLevel
	WarnLevel
	ErrorLevel
	FatalLevel
	PanicLevel
	Disabled
)

Available log level.

type LogType

type LogType int8

LogType is type for logging library.

const (
	NoLog LogType = iota
	BuiltIn
	Zerolog
	Logrus
)

Available types for logging.

type Logger

type Logger interface {
	Trace(format string, args ...interface{})
	Debug(format string, args ...interface{})
	Info(format string, args ...interface{})
	Warn(format string, args ...interface{})
	Error(format string, args ...interface{})
	Fatal(format string, args ...interface{})
	Panic(format string, args ...interface{})

	// General log with key value.
	Log(fields map[string]interface{})
}

Logger is logging interface.

See usage example in example folder.

func NewLog

func NewLog(logType LogType, level LogLevel, jsonFormat bool, color bool) (Logger, error)

NewLog to create new log client depends on the type. Color is not working in json format.

type PubSub

type PubSub interface {
	// Publish message to specific topic/channel.
	// Data will be encoded first before publishing.
	Publish(topic string, data interface{}) error
	// Subscribe to specific topic/channel.
	Subscribe(topic string) (interface{}, error)
	// Close pubsub client connection.
	Close() error
}

PubSub is pubsub interface.

For subscribe function, you have to convert the return type to Channel.

See usage example in example folder.

func NewPubSub

func NewPubSub(pubsubType PubsubType, address string, password string) (PubSub, error)

NewPubSub to create new pubsub client depends on the type.

type PubsubType

type PubsubType int8

PubsubType is type for pubsub.

const (
	RedisPubsub PubsubType = iota + 1
	RabbitMQ
	NSQ
)

Available types for pubsub.

type Validator

type Validator interface {
	// Register custom modifier.
	RegisterModifier(name string, fn func(in string) (out string)) error
	// Modify struct field value according to modifier tag.
	// Param `data` should be a pointer.
	Modify(data interface{}) error

	// Register custom validator.
	RegisterValidator(name string, fn func(value interface{}, param ...string) (ok bool)) error
	// Register error message handler.
	RegisterValidatorError(name string, fn func(field string, param ...string) (msg error)) error
	// Validate struct field value according to validator tag.
	// Param `data` should be a pointer.
	Validate(data interface{}) error
}

Validator is validating interface.

See usage example in example folder.

func NewValidator

func NewValidator(mod bool) Validator

NewValidator to create new validator. Pass true if you want to modify the data automatically before validate.

Directories

Path Synopsis
cache
bigcache
Package bigcache is a wrapper of the original "github.com/allegro/bigcache" library.
Package bigcache is a wrapper of the original "github.com/allegro/bigcache" library.
memcache
Package memcache is a wrapper of the original "github.com/bradfitz/gomemcache/memcache" library.
Package memcache is a wrapper of the original "github.com/bradfitz/gomemcache/memcache" library.
nocache
Package nocache is a mock of caching.
Package nocache is a mock of caching.
redis
Package redis is a wrapper of the original "github.com/go-redis/redis" library.
Package redis is a wrapper of the original "github.com/go-redis/redis" library.
errors
stack
Package stack is an error wrapper and put it to a stack using passed context.
Package stack is an error wrapper and put it to a stack using passed context.
example
log
log
zerolog
Package zerolog is a wrapper of the original "github.com/rs/zerolog" library.
Package zerolog is a wrapper of the original "github.com/rs/zerolog" library.
pubsub
nsq
Package nsq is a wrapper of the original "github.com/nsqio/go-nsq" library.
Package nsq is a wrapper of the original "github.com/nsqio/go-nsq" library.
rabbitmq
Package rabbitmq is a wrapper of the original "github.com/streadway/amqp" library.
Package rabbitmq is a wrapper of the original "github.com/streadway/amqp" library.
redis
Package redis is a wrapper of the original "github.com/go-redis/redis/v8" library.
Package redis is a wrapper of the original "github.com/go-redis/redis/v8" library.
validation
playground
Package playground is a wrapper of the original "github.com/go-playground/validator" and "github.com/go-playground/mold" library.
Package playground is a wrapper of the original "github.com/go-playground/validator" and "github.com/go-playground/mold" library.

Jump to

Keyboard shortcuts

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