
DevOpsCI/CD后端CLI云原生【免费下载链接】daggerAutomation engine to build, test and ship any codebase. Runs locally, in CI, or directly in the cloud项目地址https://gitcode.com/GitHub_Trending/da/dagger点击查看免费下载本文基于 Dagger 0.21 版 TypeScript SDK 官方 API 参考中ModuleConfigClientID类型别名的定义页展开说明这一标识符类型在模块 SDK 生成体系中的位置、它与ModuleConfigClient对象的对应关系以及底层 GraphQL Schema 如何通过loadModuleConfigClientFromID查询支持基于 ID 的对象重建。读完本文你将理解 Dagger TypeScript SDK 中 ID 类型别名的设计模式、模块clients配置的源码实现以及如何在模块工程中实际使用这些 API。1. 类型定义一个带“对象标识”语义的字符串别名ModuleConfigClientID是 Dagger TypeScript SDKclient.gen.ts生成产物中由 GraphQL Schema 自动映射出来的类型别名。按 0.21 版官方参考页 的定义/** * A unique identifier for an object. */ export type ModuleConfigClientID string object /** * __ModuleConfigClientID: never */ export type __ModuleConfigClientID never这段声明包含两层信息ModuleConfigClientIDstringobject从运行时角度看它就是一个字符串交叉上object类型是为了给 TypeScript 提供唯一性branded type 技巧使不同类型的 ID 在编译期不能互相赋值。比如ContainerID与ModuleConfigClientID虽然底层都是字符串但类型系统不允许互换从而避免把容器 ID 误传给模块客户端的加载函数。__ModuleConfigClientIDnever这是该参考系列为每个 ID 别名附带的内部辅助类型占位声明没有实际取值属于代码生成的固定模式同目录下 ModuleID 等所有*ID类型别名的页面都遵循相同结构。同名的 Go 侧生成代码给出了等价的类型约束可以印证这一模式在 TypeScript 运行时嵌入的生成文件中dagger.gen.go 声明为type ModuleConfigClientID string并配有注释// A unique identifier for an object.而在模块工程脚手架生成的 Go SDK如 testdata 中的模块中则写作type ModuleConfigClientID ID即 ID 的具名别名。无论哪种形式语义一致它是ModuleConfigClient对象的唯一标识符。2. 标识符对应的对象ModuleConfigClient 与模块clients配置ModuleConfigClientID标识的对象是ModuleConfigClient即“为模块生成的 SDK 客户端”。GraphQL Schema见 base_schema.graphqls中它的定义非常简洁The client generated for the module. type ModuleConfigClient implements Node { A unique identifier for this ModuleConfigClient. id: ID! The generator to use generator: String! The directory the client is generated in. directory: String! } A unique identifier for an object. scalar ModuleConfigClientID注意implements NodeModuleConfigClient实现了 Dagger 的Node接口这正是所有可按 ID 加载的对象类型的共同前提。ModuleConfigClientID作为一个 GraphQL scalar就是Node.id的类型化版本。每个对象只有两个业务字段与核心 Go 结构体一一对应。core/modules/config.go 中定义了服务端的数据模型type ModuleConfigClient struct { // The generator the client uses to be generated. Generator string field:true name:generator json:generator toml:generator doc:The generator to use // The directory the client is generated in. Directory string field:true name:directory json:directory toml:directory doc:The directory the client is generated in. }结构体上的toml标签直接决定了模块工程配置文件dagger.toml中clients数组的写法clients [ { generator go, directory internal/dagger }, { generator typescript, directory clients/typescript }, ]也就是说ModuleConfigClientID指向的对象本质上就是dagger.toml中clients条目的一份运行时视图用哪个generator生成、生成到哪个directory。从源码结构看clients的入口在 core/schema/modulesource.go 的withConfigClient逻辑中新增客户端时会克隆ModuleSource并检查目录冲突——moduleConfigClient : modules.ModuleConfigClient{ Generator: args.Generator.String(), Directory: args.OutputDir.String(), } for _, client : range src.ConfigClients { if filepath.Clean(client.Directory) filepath.Clean(moduleConfigClient.Directory) { return nil, fmt.Errorf(a client is already generated in the %s directory, client.Directory) } }即同一模块不允许两个客户端生成到同一目录clients列表通过filepath.Clean归一化后去重。3. 基于 ID 重建对象loadModuleConfigClientFromIDModuleConfigClientID的核心用途是对象持久化与重建。由于 Dagger 引擎中的对象是惰性的、可跨查询/跨会话存取的SDK 提供了“凭 ID 还原对象”的统一入口。Schema 中的声明见 base_schema.graphqlsLoad a ModuleConfigClient from its ID. loadModuleConfigClientFromID(id: ModuleConfigClientID!): ModuleConfigClient!对应的 Go 生成实现dagger.gen.go展示了其惰性构造方式——调用LoadModuleConfigClientFromID时并不会立即请求引擎而是把选择与参数挂到查询树上// Load a ModuleConfigClient from its ID. func (r *Query) LoadModuleConfigClientFromID(id ModuleConfigClientID) *ModuleConfigClient { q : r.query.Select(loadModuleConfigClientFromID) q q.Arg(id, id) return ModuleConfigClient{ query: q, } }TypeScript SDK 中同名的生成类 ModuleConfigClient 遵循相同的惰性模式构造函数保存上下文id()、directory()、generator()均为返回Promise的字段访问器字段被缓存过构造参数中已知的_id、_directory、_generator时直接返回否则才执行 GraphQL 选择export class ModuleConfigClient extends BaseClient { private readonly _id?: ID undefined private readonly _directory?: string undefined private readonly _generator?: string undefined id async (): PromiseID { if (this._id) { return this._id } const ctx this._ctx.select(id) const response: AwaitedID await ctx.execute() return response } // directory() / generator() 同理 }而客户端列表本身通过ModuleSource.configClients获取client.gen.ts 中的生成代码在查询configClients字段时只取id再依据返回的 ID 为每一项重建ModuleConfigClient实例configClients async (): PromiseModuleConfigClient[] { type configClients { id: ID } const ctx this._ctx.select(configClients).select(id) const response: AwaitedconfigClients[] await ctx.execute() return response.map( (r) new ModuleConfigClient( ctx.copy().selectNode(r.id, ModuleConfigClient), ), ) }这段代码清楚地说明了 ID 别名在整个 API 体系中的角色列表查询只传输 ID随后按 ID 重建对象引用字段访问再按需延迟执行——这是 Dagger 惰性求值模型在类型层面的体现。4. 实战在模块工程中使用这一 API在 Dagger 模块工程dagger init生成的 TS 模块中典型用法是先拿到当前模块源再枚举/重建其 SDK 客户端配置。以 TS 模块的 Dagger 上下文为例import { dagger } from dagger.io/dagger export async function main(ctx: dagger.Context) { // 1. 枚举当前模块配置的所有 SDK 客户端 const clients await ctx.currentModule().source().configClients() for (const client of clients) { const id await client.id() // 即 ModuleConfigClientID 的运行时值 const gen await client.generator() // 如 typescript const dir await client.directory() // 如 clients/typescript console.log({ id, gen, dir }) } // 2. 凭 ID 重建某个客户端对象loadModuleConfigClientFromID const targetId clients[0] ? await clients[0].id() : undefined if (targetId) { const restored ctx.currentModule() .source() .loadClientFromID(targetId) // 对应 loadClientFromID(id: ModuleConfigClientID!) console.log(await restored.directory()) } }要点说明client.id()返回的字符串就是ModuleConfigClientID类型的值同一对象在引擎生命周期内 ID 稳定因此可以缓存 ID、在后续查询中复用。ID 重建路径loadClientFromID/loadModuleConfigClientFromID不要求先持有原对象只依赖 ID 本身这正是ModuleConfigClientID作为独立类型别名存在的意义——它允许跨函数、跨会话传递对象引用。若新增客户端生成配置应在dagger.toml的clients数组中追加{ generator, directory }条目由withConfigClient的目录去重逻辑保证见第 2 节目录冲突会在引擎侧直接报错。5. 小结与延伸阅读ModuleConfigClientID虽然只是一个string别名但它串联起了 Dagger TypeScript SDK 的三个关键机制ID 品牌化string object的交叉类型让每个对象类型拥有互不兼容的 ID 类型编译期即可防止混用Node 接口与 ID 加载ModuleConfigClient implements Node使其可通过loadModuleConfigClientFromID按 ID 重建支撑惰性求值与对象持久化模块clients配置模型该对象的两个字段generator、directory直接映射自 core/modules/config.go 中带toml标签的结构体对应dagger.toml中的clients配置。如需进一步深入可参阅同目录下的 TypeAlias 参考索引、服务端 Schema 定义 core/schema/testdata/base_schema.graphqls以及模块源码加载逻辑 core/schema/modulesource.go。赞分享DevOpsCI/CD后端CLI云原生【免费下载链接】daggerAutomation engine to build, test and ship any codebase. Runs locally, in CI, or directly in the cloud项目地址https://gitcode.com/GitHub_Trending/da/dagger点击查看免费下载相关推荐Dagger TypeScript SDK 类型别名 ModuleConfigClientID对象标识符的强类型设计与模块客户端配置解析Dagger TypeScript SDK 类型别名 ModuleConfigClientID对象标识符的强类型设计与模块客户端配置解析 本文以 DaggerDevOpsCI/CD后端CLI云原生Dagger 中 ModuleConfigClientID 类型别名全解析从 TypeScript 标量到模块客户端生成机制Dagger 中 ModuleConfigClientID 类型别名全解析从 TypeScript 标量到模块客户端生成机制 导读 ModuleConfigCDevOpsCI/CD后端CLI云原生Dagger TypeScript SDK 中的 ModuleID 类型别名模块标识符的生成、传递与加载机制Dagger TypeScript SDK 中的 ModuleID 类型别名模块标识符的生成、传递与加载机制 导读 本文围绕 Dagger版本 0.19TDevOpsCI/CD后端CLI云原生创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考