sdk

package module
v0.0.0-...-de20e9d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 12, 2024 License: GPL-3.0 Imports: 11 Imported by: 0

README

注解生成 & ioc 容器库

使用Ioc

// model.go
type A struct {

}

type B struct {
    *A
}

// @Inject(lazy="false")
func NewA() *A {
    return &A{}
}

// @Inject
func NewB(a *A) *B, error {
    return &B{a}, nil
}

// main.go
import (
    "github.com/iocgo/sdk"
)

// @Gen
func Injects(container sdk.Container) error {
   panic("auto implements")
}

func main() {
    container := sdk.NewContainer()
    err := Injects(container)
    if err != nil {
        panic(err)
    }

    err = container.Run()
    if err != nil {
        panic(err)
    }
}

// cmd/main.go
import (
    "github.com/iocgo/sdk/gen/tool"
)

func main() {
    tool.Exec()
}

使用代理

// model.go
type A struct {

}

type IEcho interface {
    Echo()
}

// @Proxy(target="IEcho")
func IEchoInvocationHandler(ctx sdk.Context[IEcho]) {
    // before
    fmt.Println(ctx.In...)

    // instance & method name
    fmt.Println(ctx.Receiver, ctx.Name)

    // do method
    ctx.Do()

    // after
    fmt.Println(ctx.Out...)
}

func (A) Echo() {
    fmt.Println("A.Echo()")
}

// @Inject(name="model.A", lazy="false", proxy="model.IEcho")
func NewA() *A {
    return &A{}
}

// main.go
import (
    "github.com/iocgo/sdk"
)

// @Gen()
func Injects(container sdk.Container) error {
   panic("auto implements")
}

func main() {
    container := sdk.NewContainer()
    err := Injects(container)
    if err != nil {
        panic(err)
    }

    err = container.Run()
    if err != nil {
        panic(err)
    }

    bean, err := sdk.InvokeAs[model.Echo](container, "model.A")
    if err != nil {
        panic(err)
    }
    bean.Echo()
}

参考示例

  1. examples
  2. sdk-examples

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assert

func Assert[T any](t interface{}) T

func AssertToError

func AssertToError[T any](t interface{}) (T, error)

func InvokeBean

func InvokeBean[T any](container *Container, name string) (t T, err error)

func ListInvokeAs

func ListInvokeAs[T any](container *Container) (re []T)

func NameOf

func NameOf[T any]() string

func OverrideBean

func OverrideBean[T any](container *Container, name string, provider func() (T, error))

func ProvideBean

func ProvideBean[T any](container *Container, name string, provider func() (T, error))

func ProvideTransient

func ProvideTransient[T any](container *Container, name string, provider func() (T, error))

Types

type Container

type Container struct {
	// contains filtered or unexported fields
}

func NewContainer

func NewContainer() *Container

func (*Container) AddInitialized

func (c *Container) AddInitialized(i func() error)

func (*Container) Alias

func (c *Container) Alias(name, fullName string)

func (*Container) HealthLogger

func (c *Container) HealthLogger() string

func (*Container) Inject

func (c *Container) Inject() *do.RootScope

func (*Container) Run

func (c *Container) Run(signals ...os.Signal) (err error)

func (*Container) Stop

func (c *Container) Stop() (err error)

type Initializer

type Initializer interface {
	Init(*Container) error
	Order() int
}

func InitializedWrapper

func InitializedWrapper(order int, init func(*Container) error) Initializer

Directories

Path Synopsis
cmd
scan
该包下仅提供给iocgo工具使用的,不需要理会 Injects 的错误,在编译过程中生成
该包下仅提供给iocgo工具使用的,不需要理会 Injects 的错误,在编译过程中生成
gen
该包下仅提供给iocgo工具使用的,不需要理会 Injects 的错误,在编译过程中生成
该包下仅提供给iocgo工具使用的,不需要理会 Injects 的错误,在编译过程中生成

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL