Documentation
¶
Overview ¶
Package res — регистрация и хранение ресурсов приложения.
Центральный тип — Registry (Global, New): add, query, transform, remove. Ресурс в pool представлен Entry — handle на slot (Type, Value, system tags, custom tags). Один type может иметь несколько Entry; Registry.GetByType и Registry.GetByInterface возвращают все совпадения (0..N), Registry.GetOneByType и Registry.GetOneByInterface — первое в порядке регистрации или ErrNotFound.
Tag — system metadata (TagRegular, TagReplaceable, TagFixed); задаётся через Registry.AddWithTags. Интерпретация policy — в github.com/omcrgnt/res/unique.
Custom tags — map[string]any на entry; registry хранит, не интерпретирует. Чтение: Entry.GetCustomTag. Запись — через github.com/omcrgnt/res/unique.AddWithCustomTag или AddWithTagsAndCustomTags (для unique).
Entry.ChangeValue заменяет value in-place, сохраняя tags и custom tags. Unique policy (one concrete type per registry) — в github.com/omcrgnt/res/unique.
Type-unique registry (composition root): github.com/omcrgnt/res/unique.
Global и AddToGlobalWithTags — legacy; prefer unique.Global и unique.MustAddReplaceable.
Subpackage github.com/omcrgnt/res/unique — type-unique registry for app composition root. Subpackage github.com/omcrgnt/res/restest — optional test helpers over the same Registry API.
Index ¶
- Variables
- func AddToGlobalWithTags(v any, tags ...Tag) error
- func AddWithTagsAndCustomTags(r Registry, v any, customTags map[string]any, tags ...Tag) error
- func EntryCustomTags(e Entry) map[string]any
- func ReplaceAtType(r Registry, v any, tags ...Tag) error
- func ReplaceAtTypeWithCustomTags(r Registry, v any, customTags map[string]any, tags ...Tag) error
- type Entry
- type Registry
- type Tag
- type TransformFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("resource not found")
ErrNotFound is returned by Registry.GetOneByType and Registry.GetOneByInterface when no matching entry exists.
var TagFixed = Tag{/* contains filtered or unexported fields */}
TagFixed marks a resource that must not be replaced or deduplicated away. If another entry matches the same dependency type, sdi.Resolve fails.
var TagRegular = Tag{/* contains filtered or unexported fields */}
TagRegular marks a normal application resource (not a library default).
var TagReplaceable = Tag{/* contains filtered or unexported fields */}
TagReplaceable marks a resource as a fallback candidate when a caller chooses one of several matching entries.
Functions ¶
func AddToGlobalWithTags ¶ added in v0.20.1
AddToGlobalWithTags is Registry.AddWithTags on Global.
func AddWithTagsAndCustomTags ¶ added in v0.22.0
AddWithTagsAndCustomTags registers v with system tags and custom tags. Intended for github.com/omcrgnt/res/unique.
func EntryCustomTags ¶ added in v0.22.0
EntryCustomTags returns a copy of custom tags on e, or nil.
func ReplaceAtType ¶ added in v0.21.0
ReplaceAtType replaces the first entry whose type equals reflect.TypeOf(v), preserving registration order. If none exists, appends like Add or AddWithTags. Storage-only: does not interpret tags. Custom tags on the replaced entry are preserved.
func ReplaceAtTypeWithCustomTags ¶ added in v0.22.0
ReplaceAtTypeWithCustomTags replaces the first entry of v's type, preserving custom tags from the replaced entry when the new entry does not supply custom tags.
Types ¶
type Entry ¶ added in v0.5.0
type Entry interface {
Type() reflect.Type
Value() any
Has(tag Tag) bool
Regular() bool
Replaceable() bool
Fixed() bool
Tags() []Tag
GetCustomTag(key string) (any, bool)
ChangeValue(new any) error
}
Entry is a handle to one resource slot in a Registry.
type Registry ¶ added in v0.6.0
type Registry interface {
// Add registers a resource without tags.
Add(v any) error
// AddWithTags registers a resource with the given tags (duplicates ignored).
AddWithTags(v any, tags ...Tag) error
// WalkEntries visits entries in registration order.
WalkEntries(fn func(Entry) bool)
// GetByType returns entries whose concrete type equals t.
GetByType(t reflect.Type) []Entry
// GetByInterface returns entries whose concrete type implements iface.
GetByInterface(iface reflect.Type) []Entry
// GetOneByType returns the first [Entry.Value] for t in registration order.
GetOneByType(t reflect.Type) (any, error)
// GetOneByInterface returns the first matching [Entry.Value] in registration order.
GetOneByInterface(iface reflect.Type) (any, error)
// Transform applies [TransformFunc] to every stored resource in place.
Transform(...TransformFunc) error
// Remove unregisters a resource by Value identity (==).
Remove(v any) error
}
Registry stores application resources as Entry values.
func Global ¶ added in v0.20.1
func Global() Registry
Global returns the shared application Registry populated by library use init (via AddToGlobalWithTags) and used as the composition-root registry.
func New ¶ added in v0.6.0
func New() Registry
New returns an empty Registry.
Example ¶
package main
import (
"fmt"
"reflect"
"github.com/omcrgnt/res"
)
type Logger struct {
Level string
}
func main() {
reg := res.New()
_ = reg.Add(&Logger{Level: "DEBUG"})
entries := reg.GetByType(reflect.TypeFor[*Logger]())
if len(entries) > 0 {
fmt.Println(entries[0].Value().(*Logger).Level)
}
}
Output: DEBUG
Example (AddWithTags) ¶
package main
import (
"fmt"
"reflect"
"github.com/omcrgnt/res"
)
func main() {
reg := res.New()
_ = reg.AddWithTags("default-key", res.TagReplaceable)
_ = reg.Add("my-secret-key")
entries := reg.GetByType(reflect.TypeFor[string]())
fmt.Println("count:", len(entries))
}
Output: count: 2
func ResetGlobalForRestest ¶ added in v0.20.1
func ResetGlobalForRestest() Registry
ResetGlobalForRestest replaces Global with an empty registry.
type Tag ¶ added in v0.6.0
type Tag struct {
// contains filtered or unexported fields
}
Tag is metadata on a registered resource (Entry), set by Registry.AddWithTags. The registry does not interpret tags; callers read them from Entry.
type TransformFunc ¶ added in v0.4.0
TransformFunc transforms a single resource during Registry.Transform.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package restest provides optional helpers for tests that use the production github.com/omcrgnt/res.Registry implementation (res.New, res.Global).
|
Package restest provides optional helpers for tests that use the production github.com/omcrgnt/res.Registry implementation (res.New, res.Global). |
|
Package unique is a type-unique registry: at most one res.Entry per concrete type.
|
Package unique is a type-unique registry: at most one res.Entry per concrete type. |