xb

Custom 快速开始

本文档是 xb/doc/CUSTOM_QUICKSTART.md 的中文版本。它引导你在不到 30 分钟内实现新适配器。


1. 骨架

type MyVectorCustom struct {
    Endpoint string
}

func NewMyVectorCustom(endpoint string) *MyVectorCustom {
    return &MyVectorCustom{Endpoint: endpoint}
}

func (c *MyVectorCustom) Generate(built *xb.Built) (any, error) {
    req := buildPayload(built) // 将条件转换为 JSON
    return req, nil
}

2. 构建器辅助方法

在你的自定义结构体上暴露辅助方法,以便调用者不接触原始字段。


3. 测试

func TestMyVectorCustom_Select(t *testing.T) {
    built := xb.Of("vectors").
        Eq("tenant_id", 42).
        VectorSearch("embedding", xb.Vector{0.1, 0.2}, 5).
        Build()

    custom := NewMyVectorCustom("http://localhost:9000")
    payload, err := custom.Generate(built)
    require.NoError(t, err)
    snapshot.AssertJSON(t, payload)
}

4. 文档


5. 下一步