base

package module
v0.0.0-...-0b3fa17 Latest Latest
Warning

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

Go to latest
Published: May 24, 2024 License: MIT Imports: 5 Imported by: 0

README

more-infra/base

Github CI Go Report Card Release Go Reference

The basic structures and design modes which could be used by projects.
It includes many typical and common data structures,functions for basic using.

Packages

name description
error basic struct for error interface which has more information wrapped
runner background goroutine controller with channel sign and sync.WaitGroup
status work status controller such as starting,started,stopping,stopped
element item container supports safe thread operations and provides simple features database used, such as index,search
queue chan with dynamic capacity
reactor reactor design mode for resolving sync locking and concurrent controlling, which is similar to event loop processing
mcontext put multiple contexts into one which implements the context.Context interface
chanpool do select for multiple channels which is ambiguous
trigger pack input elements stream to batch by controlling params or function
values strings matcher with multiple regex and wildcard pre-built
varfmt format string with variable by custom syntax and variable value provider
scheduler dynamic goroutine pool for executing tasks, which could be controlled by custom options
kv convert between struct object with tag defined and map[string]interface{} like json/yaml Marshal/Unmarshal do.

Development

Now, this project is in developing stage, so the code is changing frequently. A new version will be published every week at Friday.

Documentation

Index

Constants

View Source
const (
	// ErrTypeUnknown is the default value for Error's Type field,
	// set a custom Type By NewErrorWithType or NewErrorType,WithType is recommended
	ErrTypeUnknown = "unknown"

	// ErrTypeConfig is a common error type using when the error is happened by config paring or checking.
	ErrTypeConfig = "config"
)

Variables

This section is empty.

Functions

func ErrorType

func ErrorType(err error) string

ErrorType return Type field of the Error object, it will return "unknown" when the input is not type of *base.Error

func OriginalError

func OriginalError(err error) error

OriginalError return Err field of the Error object, it will return the input object self when it's not type of *base.Error

Types

type Error

type Error struct {
	// Type defines the error's type as a self-define string, it will be set to "unknown" as default.
	// Using NewErrorWithType or NewError,WithType to create *base.Error is recommended.
	// This field could split errors by categories, it's a required field.
	// When do querying or present errors with UI, Type field is also the important tag or condition.
	Type string

	// Err is the error object return by functions, it's not typed base.Error as usual.
	Err error

	// Labels defines many labels of the error object, when used in searching scenes.
	// It will be added when call WithLabel method.
	// When do formatting, it will be split with ",".
	Labels map[string]struct{}

	// Msg defines additional comment for the error.
	// It will be added when call WithMessage method.
	// When do formatting, it will be split with "\n".
	Msg []string

	// Stack is the debug.stack information when the error is happened.
	// It will be set when call WithStack method
	Stack string

	// Fields is the additional params information of then error.
	// It will be added when call WithField or WithFields.
	// When do formatting to text, it will be joined with "=" and split with ",", such as k1=v1,k2=v2.
	// When do formatting to backend database, it will be marshals to json format.
	Fields map[string]interface{}
}

Error struct is the basic error wrapper struct, which is widely used in projects. This error object is used as standard error return value of functions in projects. Functions in projects should return this error object instead of others. Use base.NewError or base.NewErrorWithType wraps the third module's error return which is not a base.Error typed object. Use base.WrapError add more information to the Error object when the function return is a base.Error typed object. The base.Error could format in struct object(k-v format) to backend database which could present by UI.

func NewConfigError

func NewConfigError(err error) *Error

NewConfigError create an error object with "config" Type

func NewError

func NewError(err error) *Error

NewError create an error object wrapped input err, default Type is "unknown", you can set the Type by WithType method later. Using NewErrorWithType is more recommend than NewError, because a clear Type value is very important.

func NewErrorWithType

func NewErrorWithType(t string, err error) *Error

NewErrorWithType create an error object wrapped input err with the given Type field. This function is used to create error in projects typically. A clear type for the error is very important, see the Type field comments.

func WrapError

func WrapError(err error) *Error

WrapError uses to create a new error object or wrapped exist *base.Error. It's used for wrapping the exists *base.Error object typically. When the input err object is not type of *base.Error, it will create a new error object with "unknown" Type, it should be set the custom Type with WithType method.

func (*Error) Clone

func (e *Error) Clone() *Error

func (*Error) Error

func (e *Error) Error() string

Error defines the standard interface for error

func (*Error) Message

func (e *Error) Message() string

func (*Error) WithField

func (e *Error) WithField(k string, v interface{}) *Error

func (*Error) WithFields

func (e *Error) WithFields(kv map[string]interface{}) *Error

func (*Error) WithLabel

func (e *Error) WithLabel(l string) *Error

func (*Error) WithMessage

func (e *Error) WithMessage(msg string) *Error

func (*Error) WithStack

func (e *Error) WithStack() *Error

func (*Error) WithType

func (e *Error) WithType(t string) *Error

Directories

Path Synopsis
util

Jump to

Keyboard shortcuts

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