sdk

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 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/bincooo/sdk"
)
// @Gen(target="wire")
func main() {
    container := sdk.NewContainer()
    err := wire.Injects(container)
    if err != nil {
        panic(err)
    }

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

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

func main() {
    gen.Process()
}

使用代理

// model.go
type A struct {

}

// @Proxy(target="invoke")
type IEcho interface {
    Echo()
}

func invoke(ctx sdk.Context) {
    // 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/bincooo/sdk"
)

// @Gen(target="wire")
func main() {
    container := sdk.NewContainer()
    err := wire.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()
}

执行命令生成代码:

go run cmd/main.go

参考示例

  1. tests
  2. sdk-examples

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InvokeAlias

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

func InvokeAs

func InvokeAs[T any](container *Container, name string) (t T, err 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, err error)

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 Proxy

func Proxy[T any](container *Container, name string, f func(T) T)

Types

type Container

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

func NewContainer

func NewContainer() *Container

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 Context

type Context struct {
	In,
	Out []any
	Name     string
	Receiver any
	Do       func()
}

type Initializer

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

func InitializedWrapper

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

Directories

Path Synopsis
gen

Jump to

Keyboard shortcuts

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