Documentation
¶
Overview ¶
Package registry provides a minimal interface-based provider registry for breaking import cycles in the codebase.
This is a fallback mechanism for situations where import cycles block development.
See README.md in this directory for detailed rationale, usage guidelines, and examples.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Get ¶
Get retrieves the registered implementation for interface type T.
Returns (implementation, true) if a provider is registered for type T, or (zero value, false) if no provider is registered.
This function is thread-safe and optimized for concurrent access.
Example:
if provider, ok := registry.Get[MyProvider](); ok {
provider.DoSomething()
} else {
// handle missing provider
}
func RegisterAs ¶
func RegisterAs[T any](impl T)
RegisterAs registers an implementation for an interface type T.
The type parameter T must be an interface type, and impl must be a non-nil implementation of that interface. If T is not an interface or impl is nil, this function panics with a descriptive error message.
If a provider is registered multiple times, the last registration wins. This is intentional to allow test code to override providers.
Example:
type MyProvider interface {
DoSomething() error
}
type myImpl struct{}
func (m *myImpl) DoSomething() error { return nil }
registry.RegisterAs[MyProvider](&myImpl{})
func Restore ¶
func Restore(snapshot RegistrySnapshot)
Restore replaces the current registry state with a saved snapshot.
This function is intended for testing only. It restores the registry to the exact state it was in when Snapshot() was called.
Example in tests:
func TestSomething(t *testing.T) {
snapshot := registry.Snapshot()
defer registry.Restore(snapshot) // Restore original state
registry.RegisterAs[MyProvider](mockImpl)
// ... test code
}
Types ¶
type RegistrySnapshot ¶
type RegistrySnapshot struct {
// contains filtered or unexported fields
}
RegistrySnapshot represents a saved state of the registry. Use with Restore() to save and restore registry state in tests.
func Snapshot ¶
func Snapshot() RegistrySnapshot
Snapshot creates a copy of the current registry state.
This is useful in tests to save the registry state (including bootstrap providers), make temporary changes, and then restore the original state.
Example in tests:
func TestSomething(t *testing.T) {
snapshot := registry.Snapshot()
defer registry.Restore(snapshot)
registry.RegisterAs[MyProvider](mockImpl)
// ... test code that uses mockImpl
} // Restore() brings back all original providers including bootstrap ones