Documentation
¶
Index ¶
- Constants
- Variables
- func ExecuteAttrSetters(ctx context.Context, attr string, value any) error
- func ExecuteMapAttrSetters(ctx context.Context, attrsData map[Attr]json.RawMessage) error
- func GetAttrConstructor(attr string) func() any
- func NewConstructor[T any]() func() any
- func RegisterAttrSetter[T any](attr, setterName string, setter AttrSetter[T]) error
- func Zero[V any]() V
- type Attr
- type AttrSetter
- type Default
- type DefaultLoader
- type Map
- func (m *Map[K, V]) Clear(ctx context.Context) error
- func (m *Map[K, V]) Default(ctx context.Context, key K) (V, error)
- func (m *Map[K, V]) Delete(ctx context.Context, key K) error
- func (m *Map[K, V]) Get(ctx context.Context, key K) (V, error)
- func (m *Map[K, V]) GetDefault(ctx context.Context, key K) (V, error)
- func (m *Map[K, V]) Has(ctx context.Context, key K) bool
- func (m *Map[K, V]) Keys(ctx context.Context) []K
- func (m *Map[K, V]) Map(ctx context.Context) map[K]V
- func (m *Map[K, V]) MustClear(ctx context.Context)
- func (m *Map[K, V]) MustDelete(ctx context.Context, key K)
- func (m *Map[K, V]) MustRegister(ctx context.Context, key K, value V)
- func (m *Map[K, V]) MustSet(ctx context.Context, key K, value V)
- func (m *Map[K, V]) Range(ctx context.Context, fn func(key, value any) bool)
- func (m *Map[K, V]) Register(ctx context.Context, key K, value V) error
- func (m *Map[K, V]) Set(ctx context.Context, key K, value V) error
- func (m *Map[K, V]) Values(ctx context.Context) []V
- type SetterName
Constants ¶
const ( AppName = "app_name" Version = "version" )
some builtin attrs
Variables ¶
var ( // ErrNotFound defines not found error ErrNotFound = errors.New("not found") // ErrAlreadyExists defines already exists error ErrAlreadyExists = errors.New("already exists") )
Functions ¶
func ExecuteAttrSetters ¶
ExecuteAttrSetters execute attr setters, used in app code
func ExecuteMapAttrSetters ¶
ExecuteMapAttrSetters execute a map of attr setters with json format value, used in app code
func GetAttrConstructor ¶
GetAttrConstructor return attr value constructor used to assist the serialization procedure
func NewConstructor ¶
NewConstructor returns a constructor which return a new T's instance constructor which is a new created or a cached constructor, it's used to assist the serialization procedure, and the constructor will indirect reflect.Ptr recursively to ensure not return nil pointer,
func RegisterAttrSetter ¶
func RegisterAttrSetter[T any](attr, setterName string, setter AttrSetter[T]) error
RegisterAttrSetter used to register AttrSetter, and should be used in library init NOTE: the execution order of setters cannot be guaranteed!
Types ¶
type Attr ¶
type Attr = string
used for doc
func AttrsNotSetted ¶
func AttrsNotSetted() []Attr
AttrsNotSetted return a slice of attr which has not seted, used to alert in app
type AttrSetter ¶
AttrSetter used to set attr in library
type Default ¶
type Default[V any] interface { Default() V }
Default giving a type a useful default value.
type DefaultLoader ¶
DefaultLoader load default instance of V according to key
type Map ¶
type Map[K comparable, V any] struct { // contains filtered or unexported fields }
Map is a instances map of specified Type
func (*Map[K, V]) Default ¶
Default returns V's default value if it implement the `DefaultLoader` or `Default`, otherwise return `Zero[V]()`
func (*Map[K, V]) Get ¶
GetDefault get a V's instance by key, if not found return `NotFound` error(use `errors.Is` to assert)
func (*Map[K, V]) GetDefault ¶
GetDefault get a V's instance by key, if not found, then try to returns a default one
func (*Map[K, V]) MustDelete ¶
MustDelete delete a V's instance specified by key, if failed then panic
func (*Map[K, V]) MustRegister ¶
MustRegister register a V's instance with key, if failed(e.g. already exists) then panic
func (*Map[K, V]) MustSet ¶
MustSet set a V's instance with key, if exists then override, if failed then panic
func (*Map[K, V]) Range ¶
Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.
func (*Map[K, V]) Register ¶
Register register a V's instance with key, if exists then return `ErrAlreadyExists` error(use `errors.Is` to assert)
type SetterName ¶
type SetterName = string