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, err := bindly.NewInjector(opts...)
if err != nil {
panic(err)
}
service := &Service{}
err = bindly.WithState[Service](injector, dependencies).Inject(context.Background(), service)
fmt.Println(service, err)
}
Output:
Index ¶
- type BindOption
- type Binding
- type BindingCache
- type BindingContext
- type BindingError
- type BindingEvent
- type BindingObserver
- type BindingOption
- type BindingSpec
- type BindingType
- type Bindings
- type DynamicContext
- type Injector
- func (i *Injector) Bind(ctx context.Context, target any, options ...BindOption) error
- func (i *Injector) CompilePlan(target reflect.Type, specs ...BindingSpec) (*Plan, error)
- func (i *Injector) ForScope(providers ...locator.Provider) (*Injector, error)
- func (i *Injector) ReadResource(reference string) ([]byte, error)
- func (i *Injector) ResourceFS(name string) (fs.FS, bool)
- func (i *Injector) Resources() *resource.Store
- func (b *Injector) TransformerRegistry() *xform.Registry
- type InjectorOption
- func WithBindingTag(tag string) InjectorOption
- func WithLocators(registry *locator.Registry) InjectorOption
- func WithProviders(providers ...locator.Provider) InjectorOption
- func WithResourceFS(name string, source fs.FS) InjectorOption
- func WithResources(store *resource.Store) InjectorOption
- func WithTransformerTag(tag string) InjectorOption
- type Plan
- func (p *Plan) Presence(target any, path string) (bool, error)
- func (p *Plan) Projection(fields ...ProjectionField) (*Projection, error)
- func (p *Plan) Replay(paths ...string) (*ReplayPlan, error)
- func (p *Plan) SetPresence(target any, path string, present bool) error
- func (p *Plan) TargetType() reflect.Type
- type Projection
- type ProjectionField
- type Replay
- type ReplayBinding
- type ReplayPlan
- func (p *ReplayPlan) Capture(input any) (*Replay, error)
- func (p *ReplayPlan) CaptureSources(ctx context.Context, providers []locator.Provider) (*Replay, error)
- func (p *ReplayPlan) DecodeJSON(data []byte) (*Replay, error)
- func (p *ReplayPlan) Prepare(projection *Projection, paths ...string) (*ReplayPlan, error)
- type StructTypeCache
- type ValueCache
- type ValueResolver
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BindOption ¶ added in v0.2.0
type BindOption func(*bindOptions)
func WithBindingObserver ¶ added in v0.2.0
func WithBindingObserver(observer BindingObserver) BindOption
func WithPlan ¶ added in v0.2.0
func WithPlan(plan *Plan) BindOption
func WithReplay ¶ added in v0.2.0
func WithReplay(binding ReplayBinding) BindOption
func WithSource ¶ added in v0.2.0
func WithSource(source any) BindOption
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 BindingError ¶ added in v0.2.0
func (*BindingError) Error ¶ added in v0.2.0
func (e *BindingError) Error() string
func (*BindingError) StatusCode ¶ added in v0.2.0
func (e *BindingError) StatusCode() int
func (*BindingError) Unwrap ¶ added in v0.2.0
func (e *BindingError) Unwrap() error
type BindingEvent ¶ added in v0.2.0
BindingEvent describes one successfully assigned target field. Target is the actual Bind/BindTarget destination, not necessarily the initial input. Path is that target's full planned field selector. Value is the assigned value; Metadata is nil when its provenance is unknown or an opaque conversion ran. Later bindings served by the explicit persistent value cache also have nil metadata: opaque evidence is never persisted or reconstructed from a value. No values or metadata are logged automatically.
type BindingObserver ¶ added in v0.2.0
type BindingObserver func(context.Context, BindingEvent) error
BindingObserver runs after field and presence-marker assignment. An error aborts binding through BindingError; already applied assignments are not rolled back. Observers own any longer-lived recording or publication policy.
type BindingOption ¶
type BindingOption[T any] func(ctx *BindingContext[T])
func WithCache ¶
func WithCache[T any](cache *ValueCache) BindingOption[T]
type BindingSpec ¶ added in v0.2.0
type BindingSpec struct {
MarkerField string
Path, Name string
Location state.Location
SourceType reflect.Type
Required, Cacheable *bool
MinAllowedRecords, MaxAllowedRecords, ExpectedReturned *int
When, Scope, With, URI, ResourceRef, DataType, ErrorMessage, Cardinality string
Async bool
DefaultValue, Extension any
ErrorCode int
Transformer xform.Transformer
}
func BindingSpecFromField ¶ added in v0.2.0
func BindingSpecFromField(field reflect.StructField) (BindingSpec, bool, error)
type BindingType ¶
type BindingType struct {
Bindings []Bindings
Type *structology.StateType
}
type DynamicContext ¶ added in v0.2.0
type DynamicContext struct {
// contains filtered or unexported fields
}
DynamicContext binds runtime-owned targets through the canonical invocation.
func WithDynamicState ¶ added in v0.2.0
func WithDynamicState(injector *Injector, stateValue interface{}) *DynamicContext
func (*DynamicContext) Assign ¶ added in v0.2.0
func (c *DynamicContext) Assign(ctx context.Context, target interface{}, location *state.Location) error
Assign resolves a typed location and writes its value into a nonnil pointer.
type Injector ¶
type Injector struct {
// contains filtered or unexported fields
}
Injector represents dependency injector
func NewInjector ¶
func NewInjector(options ...InjectorOption) (*Injector, error)
NewInjector creates injector
func (*Injector) CompilePlan ¶ added in v0.2.0
func (*Injector) ReadResource ¶ added in v0.2.0
func (*Injector) ResourceFS ¶ added in v0.2.0
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 WithResourceFS ¶ added in v0.2.0
func WithResourceFS(name string, source fs.FS) InjectorOption
func WithResources ¶ added in v0.2.0
func WithResources(store *resource.Store) InjectorOption
func WithTransformerTag ¶
func WithTransformerTag(tag string) InjectorOption
type Plan ¶ added in v0.2.0
type Plan struct {
// contains filtered or unexported fields
}
Plan contains only immutable target metadata; providers are resolved per bind.
func (*Plan) Presence ¶ added in v0.2.0
Presence reads a binding's compiled presence marker. Inputs without a marker carry values unconditionally. Missing marker pointers represent absence.
func (*Plan) Projection ¶ added in v0.2.0
func (p *Plan) Projection(fields ...ProjectionField) (*Projection, error)
func (*Plan) SetPresence ¶ added in v0.2.0
SetPresence restores presence through the same compiled marker used by Bind.
func (*Plan) TargetType ¶ added in v0.2.0
type Projection ¶ added in v0.2.0
type Projection struct {
// contains filtered or unexported fields
}
func (*Projection) Resolver ¶ added in v0.2.0
func (p *Projection) Resolver(target any) ValueResolver
func (*Projection) TargetType ¶ added in v0.2.0
func (p *Projection) TargetType() reflect.Type
type ProjectionField ¶ added in v0.2.0
type Replay ¶ added in v0.2.0
type Replay struct {
// contains filtered or unexported fields
}
Replay holds one detached set of source values. Prepared values can only be produced by successful native binding. It is invocation-owned, not shareable.
func (*Replay) MarshalJSON ¶ added in v0.2.0
type ReplayBinding ¶ added in v0.2.0
type ReplayPlan ¶ added in v0.2.0
type ReplayPlan struct {
// contains filtered or unexported fields
}
ReplayPlan is an immutable selection of external fields from one canonical binding plan. It retains no invocation values or dependency-read evidence.
func (*ReplayPlan) Capture ¶ added in v0.2.0
func (p *ReplayPlan) Capture(input any) (*Replay, error)
func (*ReplayPlan) CaptureSources ¶ added in v0.2.0
func (p *ReplayPlan) CaptureSources(ctx context.Context, providers []locator.Provider) (*Replay, error)
CaptureSources snapshots only this plan's selected source bindings. Providers are invocation-scoped; no target is initialized, transformer applied, default materialized or prerequisite/dependency evaluated here. Raw-source hooks retain JSON presence and exact numbers before the normal ReplayBinding preflight.
func (*ReplayPlan) DecodeJSON ¶ added in v0.2.0
func (p *ReplayPlan) DecodeJSON(data []byte) (*Replay, error)
func (*ReplayPlan) Prepare ¶ added in v0.2.0
func (p *ReplayPlan) Prepare(projection *Projection, paths ...string) (*ReplayPlan, error)
Prepare includes fresh, non-persisted bindings needed to authorize external input. Param dependencies are resolved through the plan's canonical aliases.
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
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
field
Package field applies immutable field indexes compiled by x/shape.
|
Package field applies immutable field indexes compiled by x/shape. |
|
provider
|
|
|
body
Package body resolves whole or named request bodies to planned Go types.
|
Package body resolves whole or named request bodies to planned Go types. |
|
request
Package request composes independently scoped HTTP value providers while preserving the original request pointer for handler capabilities.
|
Package request composes independently scoped HTTP value providers while preserving the original request pointer for handler capabilities. |
|
values
Package values supplies an invocation-scoped source of authored values.
|
Package values supplies an invocation-scoped source of authored values. |
|
Package resource owns named package filesystems shared by compilation and invocation.
|
Package resource owns named package filesystems shared by compilation and invocation. |