Documentation
¶
Overview ¶
Example (Inject) ¶
package main
import (
"context"
"fmt"
"github.com/viant/bindly"
"github.com/viant/bindly/locator/buildin"
"github.com/viant/structology"
)
type AppConfig struct {
ServerPort int
BaseURL string
}
// Logger is a simple logging service
type Logger interface {
Log(message string)
}
// SimpleLogger implements Logger
type SimpleLogger struct{}
func (l *SimpleLogger) Log(message string) {
fmt.Println(message)
}
type DependencySetup struct {
Config *AppConfig
Settings map[string]interface{}
Interfaces map[string]interface{}
}
// Service uses the configuration
type Service struct {
Debug bool `bind:"kind=setting,in=debug"`
ServerPort int `bind:"in=Config.ServerPort"` //state kind is a default kind
Logger Logger //interface bind by defaul to interface kind
}
func main() {
var iLogger Logger
appLoger := &SimpleLogger{}
dependencies := &DependencySetup{
Config: &AppConfig{
ServerPort: 8080,
BaseURL: "http://localhost:8080",
},
Interfaces: map[string]interface{}{
structology.InterfaceTypeOf(&iLogger).String(): appLoger,
},
Settings: map[string]interface{}{
"debug": true,
"port": 8080,
},
}
var opts = append([]bindly.InjectorOption{}, bindly.WithProviders(
buildin.Struct("state", "", 1), //empty kind
buildin.Map("setting", "Settings", 1),
buildin.Map("interface", "Interfaces", 1)))
injector := bindly.NewInjector(opts...)
service := &Service{}
err := bindly.WithState[Service](injector, dependencies).Inject(context.Background(), service)
fmt.Println(service, err)
}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Binding ¶
type Binding struct {
// contains filtered or unexported fields
}
Binding represents a binding
type BindingCache ¶
type BindingCache struct {
internal.Map[reflect.Type, *BindingType]
}
func NewBindingCache ¶
func NewBindingCache() *BindingCache
type BindingContext ¶
type BindingContext[T any] struct { // contains filtered or unexported fields }
BindingContext represents binding context with state
func WithState ¶
func WithState[T any](binder *Injector, state interface{}, opt ...BindingOption[T]) *BindingContext[T]
type BindingOption ¶
type BindingOption[T any] func(ctx *BindingContext[T])
func WithCache ¶
func WithCache[T any](cache *ValueCache) BindingOption[T]
type BindingType ¶
type BindingType struct {
Bindings []Bindings
Type *structology.StateType
}
type Injector ¶
type Injector struct {
// contains filtered or unexported fields
}
Injector represents dependency injector
func NewInjector ¶
func NewInjector(options ...InjectorOption) *Injector
NewInjector creates injector
func (*Injector) TransformerRegistry ¶
TransformerRegistry returns the transformer registry
type InjectorOption ¶
type InjectorOption func(*Injector)
func WithBindingTag ¶
func WithBindingTag(tag string) InjectorOption
func WithLocators ¶
func WithLocators(registry *locator.Registry) InjectorOption
func WithProviders ¶
func WithProviders(providers ...locator.Provider) InjectorOption
func WithTransformerTag ¶
func WithTransformerTag(tag string) InjectorOption
type StructTypeCache ¶
func NewStructTypeCache ¶
func NewStructTypeCache() *StructTypeCache
type ValueCache ¶
type ValueCache struct {
internal.Map[string, interface{}]
// contains filtered or unexported fields
}
func NewValueCache ¶
func NewValueCache() *ValueCache
Source Files
¶
Click to show internal directories.
Click to hide internal directories.