build

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package build 在启动期按配置构造插件实例图。

Build 按 root id 构造一个类型化根实例;BuildInto 按目标 struct 填充多个扩展点。 Scaffold / ScaffoldYAML 从目标 struct 反向生成 BuildInto 配置,默认引用所有兼容插件。 包内会先编译装配计划,再按依赖顺序执行构造。 Collect 和 CollectInstances 可从 Result 中按类型筛选已构造实例。 WireContributions 和 WireSetter 可把贡献者实例后置装配到收集器,避免构建图循环依赖。 有贡献者但找不到可装配的收集器时返回 ErrNoContributionsCollector。 运行期不应再调用本包;使用方只使用已经构造好的实例。

Index

Constants

This section is empty.

Variables

View Source
var ErrNoContributionsCollector = errors.New("pluginkit wire: contributors found but no collector wired")

ErrNoContributionsCollector is returned when result contains contributors but WireSetter or WireContributions could not wire any non-nil collector.

Functions

func Collect

func Collect[T any](result *Result) []T

Collect 返回 result 中所有值可断言为 T 的实例。 result 为 nil 时返回 nil。

func GetByID

func GetByID[T any](result *Result, id string) (T, bool)

GetByID 按实例 id 取出指定类型的值。

func RequireByID

func RequireByID[T any](result *Result, field, id string) (T, error)

RequireByID 按实例 id 取出指定类型的值,并在不存在或类型不匹配时返回装配错误。

field 用于错误信息,例如 "workflow.steps[2]"。

func Scaffold added in v0.1.2

func Scaffold(target any, opts ScaffoldOptions) (map[string]any, error)

Scaffold 根据 target struct 的扩展点字段,自动生成 BuildInto 可接受的 plugins 配置。 slice 字段会包含所有兼容插件;单值字段默认选第一个兼容插件。 插件实例 id 默认使用 kind,冲突时追加 -2、-3。

func ScaffoldYAML added in v0.1.2

func ScaffoldYAML(target any, opts ScaffoldOptions) ([]byte, error)

ScaffoldYAML 根据 target struct 生成带注释的 YAML 配置。 单值字段存在多个兼容插件时,会在 use 行附加 default/alternatives 注释。

func ValidatePlan

func ValidatePlan(graph map[string]any, rootID string, want reflect.Type) error

ValidatePlan 只执行 resolve / typecheck / deps 规划,不调用插件 New。

func WireContributions added in v0.1.1

func WireContributions[Contributor, Collector any](
	result *Result,
	attach func(Collector, []Contributor) error,
) error

WireContributions collects every instance assignable to Contributor from result and calls attach once per non-nil instance assignable to Collector. When there are no contributors, attach is not called. When contributors exist but no collector is wired, ErrNoContributionsCollector is returned. nil result or attach is a no-op.

func WireSetter added in v0.1.1

func WireSetter[T any](result *Result) error

WireSetter collects contributions of type T and calls SetContributions on every non-nil instance that implements ContributionsSetter[T]. When there are no contributors, SetContributions is not called. When contributors exist but no collector is wired, ErrNoContributionsCollector is returned. nil result is a no-op.

Types

type ContributionsSetter added in v0.1.1

type ContributionsSetter[T any] interface {
	SetContributions([]T) error
}

ContributionsSetter receives contributions of type T collected from result. Host frameworks can adopt this naming convention and wire with WireSetter.

type Error

type Error struct {
	Field string
	Use   string
	ID    string
	Stage Stage
	Err   error
}

Error 是装配错误,包含字段名、插件 use、实例 id 与阶段。

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

type Instance

type Instance struct {
	ID    string
	Use   string
	Value any
}

Instance 是一次构造成功的插件实例。

type Result

type Result struct {
	Instances []Instance
}

Result 保存本次 Build 构造出的全部实例。

func Build

func Build[T any](ctx context.Context, graph map[string]any, rootID string) (T, *Result, error)

Build 从实例图中按 rootID 构造指定类型的根实例。 deps 可以引用已有实例 id,也可以内联私有插件对象;内联实例会按路径生成 id。

func BuildInto

func BuildInto(ctx context.Context, plugins map[string]any, target any) (*Result, error)

BuildInto 按 plugins 配置构造实例并写入 target。 target 必须是指向 struct 的指针;struct 字段的 json tag、类型与是否为 slice 构成扩展点。

type ScaffoldOptions added in v0.1.2

type ScaffoldOptions struct {
	Whitelist []string
	Blacklist []string
}

ScaffoldOptions 控制从 target struct 生成配置时的插件筛选。 Whitelist 非空时只保留名单内的 kind;Blacklist 始终排除对应 kind。

type Stage

type Stage string

Stage 表示装配失败发生的阶段。

const (
	StageResolve   Stage = "resolve"
	StageDecode    Stage = "decode"
	StageDeps      Stage = "deps"
	StageConstruct Stage = "construct"
	StageTypeCheck Stage = "typecheck"
)

type TypedInstance

type TypedInstance[T any] struct {
	ID    string
	Use   string
	Value T
}

TypedInstance 是已构造实例及其图 id 和 kind。

func CollectInstances

func CollectInstances[T any](result *Result) []TypedInstance[T]

CollectInstances 返回所有匹配实例及其 id 和 kind。 result 为 nil 时返回 nil。

Jump to

Keyboard shortcuts

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