inithook

package module
v0.0.0-...-e4bfffd Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2023 License: Apache-2.0 Imports: 7 Imported by: 2

README

inithook

inithook used to register attribute setter, which usually used in library code, for example, if a render lib need to set app_name to fill output fields, and also some others libs need to set app_name, thus in these libs, can call inithook.RegisterAttrSetter like this:

// e.g. in apierrors lib
apierrorsLibAppName := ""
err := inithook.RegisterAttrSetter(inithook.AppName, "apierrors", func(ctx context.Context, value string) error {
    apierrorsLibAppName = value
    return nil
})
// e.g. in render lib
renderLibAppName := ""
err = inithook.RegisterAttrSetter(inithook.AppName, "render", func(ctx context.Context, value string) error {
    renderLibAppName = value
    return nil
})

then, in your app code, you can call ExecuteAttrSetters to set the app_name once, then all libs's app_name will be set, also, maybe the attr values comes from some config file, you can call the GetAttrConstructor to get the attr value constructor to assist the unmarshal procedure.

// e.g. in app
data := map[string]json.RawMessage{
    inithook.AppName: []byte(`"xxx"`),
    "your-other-attr": ...,
}
err = inithook.ExecuteMapAttrSetters(context.Background(), data)

Documentation

Index

Constants

View Source
const (
	AppName = "app_name"
	Version = "version"
)

some builtin attrs

Variables

View Source
var (
	// ErrNotFound defines not found error
	ErrNotFound = errors.New("not found")

	// ErrAlreadyExists defines already exists error
	ErrAlreadyExists = errors.New("already exists")
)

Functions

func ExecuteAttrSetters

func ExecuteAttrSetters(ctx context.Context, attr string, value any) error

ExecuteAttrSetters execute attr setters, used in app code

func ExecuteMapAttrSetters

func ExecuteMapAttrSetters(ctx context.Context, attrsData map[Attr]json.RawMessage) error

ExecuteMapAttrSetters execute a map of attr setters with json format value, used in app code

func GetAttrConstructor

func GetAttrConstructor(attr string) func() any

GetAttrConstructor return attr value constructor used to assist the serialization procedure

func NewConstructor

func NewConstructor[T any]() func() any

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!

func Zero

func Zero[V any]() V

Zero create a new V's instance, and New will indirect reflect.Ptr recursively to ensure not return nil pointer

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

type AttrSetter[T any] func(ctx context.Context, value T) error

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

type DefaultLoader[V any] interface {
	LoadDefault(ctx context.Context, key any) (V, error)
}

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 NewMap

func NewMap[K comparable, V any]() *Map[K, V]

NewMap creates a new map

func (*Map[K, V]) Clear

func (m *Map[K, V]) Clear(ctx context.Context) error

Clear clear all V's instances

func (*Map[K, V]) Default

func (m *Map[K, V]) Default(ctx context.Context, key K) (V, error)

Default returns V's default value if it implement the `DefaultLoader` or `Default`, otherwise return `Zero[V]()`

func (*Map[K, V]) Delete

func (m *Map[K, V]) Delete(ctx context.Context, key K) error

Delete delete a V's instance specified by key

func (*Map[K, V]) Get

func (m *Map[K, V]) Get(ctx context.Context, key K) (V, error)

GetDefault get a V's instance by key, if not found return `NotFound` error(use `errors.Is` to assert)

func (*Map[K, V]) GetDefault

func (m *Map[K, V]) GetDefault(ctx context.Context, key K) (V, error)

GetDefault get a V's instance by key, if not found, then try to returns a default one

func (*Map[K, V]) Has

func (m *Map[K, V]) Has(ctx context.Context, key K) bool

Has tells if map has key

func (*Map[K, V]) Keys

func (m *Map[K, V]) Keys(ctx context.Context) []K

Keys return all keys

func (*Map[K, V]) Map

func (m *Map[K, V]) Map(ctx context.Context) map[K]V

Map return map with all items

func (*Map[K, V]) MustClear

func (m *Map[K, V]) MustClear(ctx context.Context)

MustClear clear all V's instances, if failed then panic

func (*Map[K, V]) MustDelete

func (m *Map[K, V]) MustDelete(ctx context.Context, key K)

MustDelete delete a V's instance specified by key, if failed then panic

func (*Map[K, V]) MustRegister

func (m *Map[K, V]) MustRegister(ctx context.Context, key K, value V)

MustRegister register a V's instance with key, if failed(e.g. already exists) then panic

func (*Map[K, V]) MustSet

func (m *Map[K, V]) MustSet(ctx context.Context, key K, value V)

MustSet set a V's instance with key, if exists then override, if failed then panic

func (*Map[K, V]) Range

func (m *Map[K, V]) Range(ctx context.Context, fn func(key, value any) bool)

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

func (m *Map[K, V]) Register(ctx context.Context, key K, value V) error

Register register a V's instance with key, if exists then return `ErrAlreadyExists` error(use `errors.Is` to assert)

func (*Map[K, V]) Set

func (m *Map[K, V]) Set(ctx context.Context, key K, value V) error

Set set a V's instance with key, if exists then override

func (*Map[K, V]) Values

func (m *Map[K, V]) Values(ctx context.Context) []V

Values return all values

type SetterName

type SetterName = string

Jump to

Keyboard shortcuts

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