Documentation
¶
Index ¶
- Constants
- Variables
- func Fake[T any](impl any) fakeComponent
- func Run[T any, _ PointerToMain[T]](ctx context.Context, run func(context.Context, *T) error, ...) error
- func RunTest[T any](t testing.TB, body func(context.Context, T), opts ...func(*options))
- func WithConfigFile(filename string) func(*options)
- func WithFakes(fakes ...fakeComponent) func(*options)
- func WithLogWrapper(h func(slog.Handler) slog.Handler) func(*options)
- func WithRegistrations(regs ...*Registration) func(*options)
- type CallInfo
- type HandleFunc
- type Implements
- type InstanceOf
- type Interceptor
- type Kod
- type LocalStubFnInfo
- type Main
- type ObservedLogs
- type PointerToMain
- type Ref
- type Registration
- type WithConfig
Constants ¶
const (
PkgPath = "github.com/go-kod/kod"
)
Variables ¶
var NewLogObserver = kslog.NewLogObserver
var Register = registry.Register
Register registers the given component implementations.
Functions ¶
func Run ¶
func Run[T any, _ PointerToMain[T]](ctx context.Context, run func(context.Context, *T) error, opts ...func(*options)) error
Run initializes and runs the application with the provided main component and options.
func WithConfigFile ¶
func WithConfigFile(filename string) func(*options)
WithConfigFile is an option setter for specifying a configuration file.
func WithFakes ¶
func WithFakes(fakes ...fakeComponent) func(*options)
WithFakes is an option setter for specifying fake components for testing.
func WithLogWrapper ¶
WithLogWrapper is an option setter for specifying a logger.
func WithRegistrations ¶
func WithRegistrations(regs ...*Registration) func(*options)
WithRegistrations is an option setter for specifying component registrations.
Types ¶
type CallInfo ¶
type CallInfo struct { // The impl of the called component. Impl any // The component name of the called method. Component string // The full name of the called method, in the format of "package/service.method". FullMethod string // The name of the method. Method string }
CallInfo contains information about the call.
type HandleFunc ¶
HandleFunc is the type of the function invoked by Components.
type Implements ¶
type Implements[T any] struct { // contains filtered or unexported fields }
Implements[T any] provides a common structure for components, with logging capabilities and a reference to the component's interface.
type InstanceOf ¶
type InstanceOf[T any] interface { // contains filtered or unexported methods }
InstanceOf[T any] is an interface for asserting implementation of an interface T.
type Interceptor ¶
type Interceptor func(ctx context.Context, info CallInfo, req, reply []any, invoker HandleFunc) error
Interceptor is the type of the function used to intercept Components.
type Kod ¶
type Kod struct {
// contains filtered or unexported fields
}
Kod represents the core structure of the application, holding configuration and component registrations.
func FromContext ¶
FromContext returns the Kod value stored in ctx, if any.
func (*Kod) Config ¶
func (k *Kod) Config() kodConfig
Config returns the current configuration of the Kod instance.
type LocalStubFnInfo ¶
type LocalStubFnInfo = registry.LocalStubFnInfo
LocalStubFnInfo is the information passed to LocalStubFn.
type Main ¶
type Main interface{}
Main is the interface that should be implemented by an application's main component. The main component is the entry point of the application, and is expected to be a struct that embeds Implements[Main].
Example:
type app struct { kod.Implements[kod.Main] } func main() { kod.Run(context.Background(), func(ctx context.Context, main *app) error { fmt.Println("Hello, World!") return nil }) }
type ObservedLogs ¶
type ObservedLogs kslog.ObservedLogs
type PointerToMain ¶
type PointerToMain[T any] interface { *T InstanceOf[Main] }
PointerToMain is a type constraint that asserts *T is an instance of Main (i.e. T is a struct that embeds weaver.Implements[weaver.Main]).
type Ref ¶
type Ref[T any] struct { // contains filtered or unexported fields }
Ref[T any] is a reference holder to a value of type T. The reference is expected to be a field of a component struct. The value is set by the framework, and is accessible via the Get() method.
Example:
type app struct { kod.Implements[kod.Main] component kod.Ref[example.Component] } func main() { kod.Run(context.Background(), func(ctx context.Context, main *app) error { component := main.component.Get() // ... }) }
type Registration ¶
type Registration = registry.Registration
Registration is the registration information for a component.
type WithConfig ¶
type WithConfig[T any] struct { // contains filtered or unexported fields }
WithConfig[T any] is a struct to hold configuration of type T. The struct is expected to be a field of a component struct. The configuration is loaded from a file, and is accessible via the Config() method.
Example:
type app struct { kod.Implements[kod.Main] kod.WithConfig[appConfig] } type appConfig struct { Host string Port int } func main() { kod.Run(context.Background(), func(ctx context.Context, main *app) error { fmt.Println("config:", main.Config()) }) }
func (*WithConfig[T]) Config ¶
func (wc *WithConfig[T]) Config() *T
Config returns a pointer to the config.
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
cmd
|
|
kod/internal
Package files contains file-related utilities.
|
Package files contains file-related utilities. |
examples
module
|
|
ext
module
|
|
internal
|
|
tests
module
|