组件可视化配置
自定义组件通过 Config 结构体的 tag 定义可视化表单。可视化编辑器(RuleGo-Editor)会自动根据这些 tag 渲染组件配置界面。
# 可选接口
除了核心的 Node 接口,组件还可以实现以下可选接口来提供额外信息:
# ComponentDefGetter — 自定义表单定义
覆盖自动生成的组件表单配置。返回值中非空字段会覆盖自动生成的值:
type ComponentDefGetter interface {
Def() ComponentForm
}
1
2
3
2
3
示例——自定义描述和连接关系类型:
func (x *MyNode) Def() types.ComponentForm {
relationTypes := &[]string{types.Success, types.Failure, types.True, types.False}
return types.ComponentForm{
Label: "我的组件",
Desc: "这是一个自定义组件",
RelationTypes: relationTypes,
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# CategoryGetter — 自定义分类
type CategoryGetter interface {
Category() string
}
1
2
3
2
3
# DescGetter — 自定义描述
type DescGetter interface {
Desc() string
}
1
2
3
2
3
# Config 结构体 Tag
自定义组件的 Config 结构体支持以下 tag,用于可视化编辑器渲染表单:
| Tag | 说明 | 示例 |
|---|---|---|
json | 字段名 | json:"delayMs" |
label | 显示名称 | label:"延迟时间(ms)" |
desc | 字段描述 | desc:"延迟时间,单位毫秒" |
required | 是否必填 | required:"true" |
component | UI 组件类型 | component:"{\"type\":\"select\",\"options\":[...]}" |
ref | 共享节点关联 | ref:"primary" |
# component tag 示例
下拉选择:
RequestMethod string `json:"requestMethod" label:"请求方法"
component:"{\"type\":\"select\",\"options\":[{\"label\":\"GET\",\"value\":\"GET\"},{\"label\":\"POST\",\"value\":\"POST\"}]}"`
1
2
2
代码编辑器:
JsScript string `json:"jsScript" label:"脚本"
component:"{\"type\":\"codeEditor\",\"language\":\"javascript\"}"`
1
2
2
# 共享节点池(SharedNode)
对于需要共享连接或资源的组件(如 MQTT 客户端、数据库连接池),可以使用共享节点池模式。多个规则节点复用同一个底层连接实例,避免重复创建。
Config 结构体中使用 ref tag 标识字段与共享节点的关系:
type MqttClientNodeConfiguration struct {
Server string `json:"server" label:"服务器地址" required:"true" ref:"primary"` // 引用字段
Username string `json:"username" label:"用户名" ref:"shared"` // 由共享节点提供
Password string `json:"password" label:"密码" ref:"shared"` // 由共享节点提供
Topic string `json:"topic" label:"Topic" required:"true"` // 组件自身字段
}
1
2
3
4
5
6
2
3
4
5
6
ref:"primary"— 填写ref://引用地址的标识字段ref:"shared"— 由共享节点提供的配置字段,使用引用时隐藏- 无
ref— 组件自身业务字段,始终显示
# 相关文档
在 GitHub 上编辑此页 (opens new window)
上次更新: 2026/05/30, 11:18:53