@workerstack/clients 0.0.0-stage → 1.0.0-rc.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +619 -2
- package/bin/honey-clients.js +94 -0
- package/dist/cli.d.ts +102 -0
- package/dist/cli.js +190 -0
- package/dist/emit/cli/channels.d.ts +73 -0
- package/dist/emit/cli/channels.js +197 -0
- package/dist/emit/cli/commands.d.ts +27 -0
- package/dist/emit/cli/commands.js +187 -0
- package/dist/emit/cli/discovery.d.ts +35 -0
- package/dist/emit/cli/discovery.js +202 -0
- package/dist/emit/cli/flags.d.ts +59 -0
- package/dist/emit/cli/flags.js +186 -0
- package/dist/emit/cli/index.d.ts +1 -0
- package/dist/emit/cli/index.js +1 -0
- package/dist/emit/cli/install.d.ts +37 -0
- package/dist/emit/cli/install.js +88 -0
- package/dist/emit/cli/layout.d.ts +23 -0
- package/dist/emit/cli/layout.js +118 -0
- package/dist/emit/cli/module.d.ts +30 -0
- package/dist/emit/cli/module.js +186 -0
- package/dist/emit/cli/release.d.ts +40 -0
- package/dist/emit/cli/release.js +183 -0
- package/dist/emit/cli/skills.d.ts +11 -0
- package/dist/emit/cli/skills.js +56 -0
- package/dist/emit/csharp/errors.d.ts +7 -0
- package/dist/emit/csharp/errors.js +69 -0
- package/dist/emit/csharp/index.d.ts +1 -0
- package/dist/emit/csharp/index.js +1 -0
- package/dist/emit/csharp/json.d.ts +13 -0
- package/dist/emit/csharp/json.js +113 -0
- package/dist/emit/csharp/realtime.d.ts +10 -0
- package/dist/emit/csharp/realtime.js +33 -0
- package/dist/emit/csharp/sdk-answer.d.ts +48 -0
- package/dist/emit/csharp/sdk-answer.js +85 -0
- package/dist/emit/csharp/sdk-body.d.ts +10 -0
- package/dist/emit/csharp/sdk-body.js +87 -0
- package/dist/emit/csharp/sdk-client.d.ts +32 -0
- package/dist/emit/csharp/sdk-client.js +177 -0
- package/dist/emit/csharp/sdk-csproj.d.ts +36 -0
- package/dist/emit/csharp/sdk-csproj.js +136 -0
- package/dist/emit/csharp/sdk-doc.d.ts +12 -0
- package/dist/emit/csharp/sdk-doc.js +35 -0
- package/dist/emit/csharp/sdk-method.d.ts +26 -0
- package/dist/emit/csharp/sdk-method.js +187 -0
- package/dist/emit/csharp/sdk-plan.d.ts +38 -0
- package/dist/emit/csharp/sdk-plan.js +1 -0
- package/dist/emit/csharp/sdk.d.ts +40 -0
- package/dist/emit/csharp/sdk.js +105 -0
- package/dist/emit/csharp/sse.d.ts +18 -0
- package/dist/emit/csharp/sse.js +29 -0
- package/dist/emit/csharp/transport.d.ts +11 -0
- package/dist/emit/csharp/transport.js +67 -0
- package/dist/emit/csharp/types-enum.d.ts +19 -0
- package/dist/emit/csharp/types-enum.js +63 -0
- package/dist/emit/csharp/types-names.d.ts +33 -0
- package/dist/emit/csharp/types-names.js +182 -0
- package/dist/emit/csharp/types-record.d.ts +25 -0
- package/dist/emit/csharp/types-record.js +65 -0
- package/dist/emit/csharp/types-union.d.ts +27 -0
- package/dist/emit/csharp/types-union.js +113 -0
- package/dist/emit/csharp/types.d.ts +36 -0
- package/dist/emit/csharp/types.js +126 -0
- package/dist/emit/csharp/ws.d.ts +20 -0
- package/dist/emit/csharp/ws.js +14 -0
- package/dist/emit/go/errors.d.ts +7 -0
- package/dist/emit/go/errors.js +44 -0
- package/dist/emit/go/http.d.ts +11 -0
- package/dist/emit/go/http.js +50 -0
- package/dist/emit/go/index.d.ts +1 -0
- package/dist/emit/go/index.js +1 -0
- package/dist/emit/go/module-path.d.ts +2 -0
- package/dist/emit/go/module-path.js +27 -0
- package/dist/emit/go/sdk-answer.d.ts +28 -0
- package/dist/emit/go/sdk-answer.js +41 -0
- package/dist/emit/go/sdk-body.d.ts +24 -0
- package/dist/emit/go/sdk-body.js +76 -0
- package/dist/emit/go/sdk-client.d.ts +29 -0
- package/dist/emit/go/sdk-client.js +141 -0
- package/dist/emit/go/sdk-method.d.ts +68 -0
- package/dist/emit/go/sdk-method.js +172 -0
- package/dist/emit/go/sdk-mirror.d.ts +23 -0
- package/dist/emit/go/sdk-mirror.js +97 -0
- package/dist/emit/go/sdk-opts.d.ts +55 -0
- package/dist/emit/go/sdk-opts.js +100 -0
- package/dist/emit/go/sdk.d.ts +26 -0
- package/dist/emit/go/sdk.js +111 -0
- package/dist/emit/go/sse.d.ts +16 -0
- package/dist/emit/go/sse.js +26 -0
- package/dist/emit/go/types-codec.d.ts +15 -0
- package/dist/emit/go/types-codec.js +171 -0
- package/dist/emit/go/types-names.d.ts +14 -0
- package/dist/emit/go/types-names.js +64 -0
- package/dist/emit/go/types-struct.d.ts +29 -0
- package/dist/emit/go/types-struct.js +65 -0
- package/dist/emit/go/types-union.d.ts +17 -0
- package/dist/emit/go/types-union.js +84 -0
- package/dist/emit/go/types.d.ts +32 -0
- package/dist/emit/go/types.js +177 -0
- package/dist/emit/go/ws.d.ts +12 -0
- package/dist/emit/go/ws.js +70 -0
- package/dist/emit/java/gradle.d.ts +57 -0
- package/dist/emit/java/gradle.js +221 -0
- package/dist/emit/java/index.d.ts +2 -0
- package/dist/emit/java/index.js +2 -0
- package/dist/emit/java/json.d.ts +13 -0
- package/dist/emit/java/json.js +84 -0
- package/dist/emit/java/layout-ast.d.ts +146 -0
- package/dist/emit/java/layout-ast.js +102 -0
- package/dist/emit/java/layout-decl.d.ts +20 -0
- package/dist/emit/java/layout-decl.js +155 -0
- package/dist/emit/java/layout-doc.d.ts +55 -0
- package/dist/emit/java/layout-doc.js +166 -0
- package/dist/emit/java/layout-dot.d.ts +18 -0
- package/dist/emit/java/layout-dot.js +127 -0
- package/dist/emit/java/layout-strings.d.ts +8 -0
- package/dist/emit/java/layout-strings.js +76 -0
- package/dist/emit/java/layout-types.d.ts +11 -0
- package/dist/emit/java/layout-types.js +95 -0
- package/dist/emit/java/layout-visitor.d.ts +38 -0
- package/dist/emit/java/layout-visitor.js +232 -0
- package/dist/emit/java/layout.d.ts +61 -0
- package/dist/emit/java/layout.js +121 -0
- package/dist/emit/java/models-enum.d.ts +17 -0
- package/dist/emit/java/models-enum.js +51 -0
- package/dist/emit/java/models-names.d.ts +29 -0
- package/dist/emit/java/models-names.js +97 -0
- package/dist/emit/java/models-record.d.ts +24 -0
- package/dist/emit/java/models-record.js +79 -0
- package/dist/emit/java/models-union.d.ts +27 -0
- package/dist/emit/java/models-union.js +86 -0
- package/dist/emit/java/models.d.ts +28 -0
- package/dist/emit/java/models.js +118 -0
- package/dist/emit/java/realtime.d.ts +50 -0
- package/dist/emit/java/realtime.js +69 -0
- package/dist/emit/java/sdk-client.d.ts +66 -0
- package/dist/emit/java/sdk-client.js +207 -0
- package/dist/emit/java/sdk-decl.d.ts +11 -0
- package/dist/emit/java/sdk-decl.js +63 -0
- package/dist/emit/java/sdk-errors.d.ts +13 -0
- package/dist/emit/java/sdk-errors.js +141 -0
- package/dist/emit/java/sdk-headers.d.ts +12 -0
- package/dist/emit/java/sdk-headers.js +74 -0
- package/dist/emit/java/sdk-mirror.d.ts +14 -0
- package/dist/emit/java/sdk-mirror.js +107 -0
- package/dist/emit/java/sdk-models.d.ts +16 -0
- package/dist/emit/java/sdk-models.js +68 -0
- package/dist/emit/java/sdk-op.d.ts +57 -0
- package/dist/emit/java/sdk-op.js +216 -0
- package/dist/emit/java/sdk-params.d.ts +38 -0
- package/dist/emit/java/sdk-params.js +133 -0
- package/dist/emit/java/sdk-plan.d.ts +52 -0
- package/dist/emit/java/sdk-plan.js +1 -0
- package/dist/emit/java/sdk-record.d.ts +16 -0
- package/dist/emit/java/sdk-record.js +99 -0
- package/dist/emit/java/sdk.d.ts +49 -0
- package/dist/emit/java/sdk.js +216 -0
- package/dist/emit/java/sse.d.ts +35 -0
- package/dist/emit/java/sse.js +57 -0
- package/dist/emit/java/ws.d.ts +27 -0
- package/dist/emit/java/ws.js +31 -0
- package/dist/emit/mcp/annotations.d.ts +15 -0
- package/dist/emit/mcp/annotations.js +15 -0
- package/dist/emit/mcp/files.d.ts +18 -0
- package/dist/emit/mcp/files.js +42 -0
- package/dist/emit/mcp/index.d.ts +3 -0
- package/dist/emit/mcp/index.js +3 -0
- package/dist/emit/mcp/literal.d.ts +2 -0
- package/dist/emit/mcp/literal.js +33 -0
- package/dist/emit/mcp/mounted.d.ts +21 -0
- package/dist/emit/mcp/mounted.js +89 -0
- package/dist/emit/mcp/output-schema.d.ts +18 -0
- package/dist/emit/mcp/output-schema.js +38 -0
- package/dist/emit/mcp/registry.d.ts +23 -0
- package/dist/emit/mcp/registry.js +83 -0
- package/dist/emit/mcp/runtime.d.ts +10 -0
- package/dist/emit/mcp/runtime.js +47 -0
- package/dist/emit/mcp/schema.d.ts +19 -0
- package/dist/emit/mcp/schema.js +88 -0
- package/dist/emit/mcp/server.d.ts +30 -0
- package/dist/emit/mcp/server.js +246 -0
- package/dist/emit/mcp/tools.d.ts +51 -0
- package/dist/emit/mcp/tools.js +153 -0
- package/dist/emit/python/errors.d.ts +8 -0
- package/dist/emit/python/errors.js +68 -0
- package/dist/emit/python/index.d.ts +1 -0
- package/dist/emit/python/index.js +1 -0
- package/dist/emit/python/realtime.d.ts +10 -0
- package/dist/emit/python/realtime.js +33 -0
- package/dist/emit/python/sdk-answer.d.ts +49 -0
- package/dist/emit/python/sdk-answer.js +89 -0
- package/dist/emit/python/sdk-body.d.ts +11 -0
- package/dist/emit/python/sdk-body.js +40 -0
- package/dist/emit/python/sdk-client.d.ts +36 -0
- package/dist/emit/python/sdk-client.js +163 -0
- package/dist/emit/python/sdk-doc.d.ts +9 -0
- package/dist/emit/python/sdk-doc.js +32 -0
- package/dist/emit/python/sdk-imports.d.ts +14 -0
- package/dist/emit/python/sdk-imports.js +38 -0
- package/dist/emit/python/sdk-method.d.ts +29 -0
- package/dist/emit/python/sdk-method.js +183 -0
- package/dist/emit/python/sdk-plan.d.ts +33 -0
- package/dist/emit/python/sdk-plan.js +1 -0
- package/dist/emit/python/sdk-pyproject.d.ts +27 -0
- package/dist/emit/python/sdk-pyproject.js +103 -0
- package/dist/emit/python/sdk.d.ts +22 -0
- package/dist/emit/python/sdk.js +154 -0
- package/dist/emit/python/sse.d.ts +12 -0
- package/dist/emit/python/sse.js +23 -0
- package/dist/emit/python/transport.d.ts +15 -0
- package/dist/emit/python/transport.js +60 -0
- package/dist/emit/python/types-dict.d.ts +31 -0
- package/dist/emit/python/types-dict.js +45 -0
- package/dist/emit/python/types-names.d.ts +16 -0
- package/dist/emit/python/types-names.js +52 -0
- package/dist/emit/python/types-union.d.ts +32 -0
- package/dist/emit/python/types-union.js +46 -0
- package/dist/emit/python/types.d.ts +36 -0
- package/dist/emit/python/types.js +128 -0
- package/dist/emit/python/ws.d.ts +14 -0
- package/dist/emit/python/ws.js +16 -0
- package/dist/emit/rust/errors.d.ts +7 -0
- package/dist/emit/rust/errors.js +107 -0
- package/dist/emit/rust/http.d.ts +13 -0
- package/dist/emit/rust/http.js +67 -0
- package/dist/emit/rust/index.d.ts +1 -0
- package/dist/emit/rust/index.js +1 -0
- package/dist/emit/rust/realtime.d.ts +11 -0
- package/dist/emit/rust/realtime.js +29 -0
- package/dist/emit/rust/sdk-body.d.ts +19 -0
- package/dist/emit/rust/sdk-body.js +53 -0
- package/dist/emit/rust/sdk-cargo.d.ts +43 -0
- package/dist/emit/rust/sdk-cargo.js +178 -0
- package/dist/emit/rust/sdk-client.d.ts +25 -0
- package/dist/emit/rust/sdk-client.js +108 -0
- package/dist/emit/rust/sdk-method.d.ts +15 -0
- package/dist/emit/rust/sdk-method.js +119 -0
- package/dist/emit/rust/sdk-opts.d.ts +58 -0
- package/dist/emit/rust/sdk-opts.js +117 -0
- package/dist/emit/rust/sdk-plan.d.ts +33 -0
- package/dist/emit/rust/sdk-plan.js +1 -0
- package/dist/emit/rust/sdk.d.ts +23 -0
- package/dist/emit/rust/sdk.js +111 -0
- package/dist/emit/rust/sse.d.ts +19 -0
- package/dist/emit/rust/sse.js +20 -0
- package/dist/emit/rust/types-enum.d.ts +13 -0
- package/dist/emit/rust/types-enum.js +62 -0
- package/dist/emit/rust/types-names.d.ts +19 -0
- package/dist/emit/rust/types-names.js +66 -0
- package/dist/emit/rust/types-struct.d.ts +21 -0
- package/dist/emit/rust/types-struct.js +59 -0
- package/dist/emit/rust/types-union.d.ts +24 -0
- package/dist/emit/rust/types-union.js +97 -0
- package/dist/emit/rust/types.d.ts +34 -0
- package/dist/emit/rust/types.js +105 -0
- package/dist/emit/rust/ws.d.ts +21 -0
- package/dist/emit/rust/ws.js +20 -0
- package/dist/emit/shared/escape.d.ts +55 -0
- package/dist/emit/shared/escape.js +144 -0
- package/dist/emit/shared/format.d.ts +24 -0
- package/dist/emit/shared/format.js +23 -0
- package/dist/emit/shared/gofmt.d.ts +3 -0
- package/dist/emit/shared/gofmt.js +26 -0
- package/dist/emit/shared/lang.d.ts +60 -0
- package/dist/emit/shared/lang.js +233 -0
- package/dist/emit/shared/literals.d.ts +4 -0
- package/dist/emit/shared/literals.js +31 -0
- package/dist/emit/shared/metadata.d.ts +106 -0
- package/dist/emit/shared/metadata.js +130 -0
- package/dist/emit/shared/mirror.d.ts +22 -0
- package/dist/emit/shared/mirror.js +26 -0
- package/dist/emit/shared/ruff.d.ts +3 -0
- package/dist/emit/shared/ruff.js +31 -0
- package/dist/emit/shared/rustfmt.d.ts +3 -0
- package/dist/emit/shared/rustfmt.js +32 -0
- package/dist/emit/shared/status-keys.d.ts +7 -0
- package/dist/emit/shared/status-keys.js +67 -0
- package/dist/emit/shared/type-emitter.d.ts +19 -0
- package/dist/emit/shared/type-emitter.js +100 -0
- package/dist/emit/ts/app-client.d.ts +4 -0
- package/dist/emit/ts/app-client.js +68 -0
- package/dist/emit/ts/http.d.ts +20 -0
- package/dist/emit/ts/http.js +74 -0
- package/dist/emit/ts/package.d.ts +36 -0
- package/dist/emit/ts/package.js +126 -0
- package/dist/emit/ts/route-tree.d.ts +68 -0
- package/dist/emit/ts/route-tree.js +240 -0
- package/dist/emit/ts/sdk-types-preamble.d.ts +14 -0
- package/dist/emit/ts/sdk-types-preamble.js +123 -0
- package/dist/emit/ts/sdk-types.d.ts +13 -0
- package/dist/emit/ts/sdk-types.js +213 -0
- package/dist/emit/ts/sdk.d.ts +21 -0
- package/dist/emit/ts/sdk.js +173 -0
- package/dist/emit/ts/service-map.d.ts +40 -0
- package/dist/emit/ts/service-map.js +171 -0
- package/dist/emit/ts/tanstack-query.d.ts +12 -0
- package/dist/emit/ts/tanstack-query.js +156 -0
- package/dist/emit/ts/type-gen.d.ts +26 -0
- package/dist/emit/ts/type-gen.js +255 -0
- package/dist/emit/ts/webhooks.d.ts +19 -0
- package/dist/emit/ts/webhooks.js +81 -0
- package/dist/extract/chain.d.ts +34 -0
- package/dist/extract/chain.js +84 -0
- package/dist/extract/extract.d.ts +41 -0
- package/dist/extract/extract.js +191 -0
- package/dist/extract/serialize.d.ts +34 -0
- package/dist/extract/serialize.js +295 -0
- package/dist/extract/walk.d.ts +47 -0
- package/dist/extract/walk.js +109 -0
- package/dist/gen/args.d.ts +28 -0
- package/dist/gen/args.js +133 -0
- package/dist/gen/formatters.d.ts +62 -0
- package/dist/gen/formatters.js +146 -0
- package/dist/gen/process.d.ts +13 -0
- package/dist/gen/process.js +35 -0
- package/dist/gen/shipped.d.ts +17 -0
- package/dist/gen/shipped.js +55 -0
- package/dist/gen/targets.d.ts +93 -0
- package/dist/gen/targets.js +171 -0
- package/dist/gen/write.d.ts +33 -0
- package/dist/gen/write.js +118 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +15 -0
- package/dist/ir/build.d.ts +37 -0
- package/dist/ir/build.js +152 -0
- package/dist/ir/load.d.ts +11 -0
- package/dist/ir/load.js +112 -0
- package/dist/ir/model.d.ts +206 -0
- package/dist/ir/model.js +5 -0
- package/dist/ir/resolve-refs.d.ts +11 -0
- package/dist/ir/resolve-refs.js +66 -0
- package/dist/ir/resource-tree.d.ts +9 -0
- package/dist/ir/resource-tree.js +53 -0
- package/dist/ir/route-tree-intern.d.ts +18 -0
- package/dist/ir/route-tree-intern.js +174 -0
- package/dist/ir/schema.d.ts +11 -0
- package/dist/ir/schema.js +192 -0
- package/dist/ir/sdk-model.d.ts +86 -0
- package/dist/ir/sdk-model.js +204 -0
- package/dist/ir/slots.d.ts +42 -0
- package/dist/ir/slots.js +245 -0
- package/dist/runtime/cli/body.go +176 -0
- package/dist/runtime/cli/config.go +243 -0
- package/dist/runtime/cli/discovery.go +147 -0
- package/dist/runtime/cli/format.go +174 -0
- package/dist/runtime/cli/log.go +117 -0
- package/dist/runtime/cli/output.go +247 -0
- package/dist/runtime/cli/params.go +197 -0
- package/dist/runtime/cli/run.go +226 -0
- package/dist/runtime/cli/socket.go +60 -0
- package/dist/runtime/cli/stream.go +217 -0
- package/dist/runtime/cli/target.go +117 -0
- package/dist/runtime/csharp/Adapters.cs +183 -0
- package/dist/runtime/csharp/Auth.cs +56 -0
- package/dist/runtime/csharp/Body.cs +135 -0
- package/dist/runtime/csharp/Config.cs +161 -0
- package/dist/runtime/csharp/Envelope.cs +149 -0
- package/dist/runtime/csharp/Errors.cs +194 -0
- package/dist/runtime/csharp/Events.cs +155 -0
- package/dist/runtime/csharp/Exchange.cs +247 -0
- package/dist/runtime/csharp/Handler.cs +103 -0
- package/dist/runtime/csharp/Hooks.cs +128 -0
- package/dist/runtime/csharp/Invalidation.cs +102 -0
- package/dist/runtime/csharp/Json.cs +144 -0
- package/dist/runtime/csharp/Log.cs +48 -0
- package/dist/runtime/csharp/Operation.cs +33 -0
- package/dist/runtime/csharp/Path.cs +65 -0
- package/dist/runtime/csharp/Query.cs +72 -0
- package/dist/runtime/csharp/Realtime.cs +118 -0
- package/dist/runtime/csharp/RealtimeLoop.cs +236 -0
- package/dist/runtime/csharp/Redirect.cs +53 -0
- package/dist/runtime/csharp/Result.cs +57 -0
- package/dist/runtime/csharp/Sse.cs +122 -0
- package/dist/runtime/csharp/Stale.cs +151 -0
- package/dist/runtime/csharp/Transport.cs +143 -0
- package/dist/runtime/csharp/TransportLongPoll.cs +63 -0
- package/dist/runtime/csharp/TransportSse.cs +54 -0
- package/dist/runtime/csharp/TransportWs.cs +40 -0
- package/dist/runtime/csharp/Wire.cs +109 -0
- package/dist/runtime/csharp/Ws.cs +300 -0
- package/dist/runtime/csharp/WsDial.cs +127 -0
- package/dist/runtime/go/auth.go +127 -0
- package/dist/runtime/go/body.go +129 -0
- package/dist/runtime/go/config.go +75 -0
- package/dist/runtime/go/errors.go +185 -0
- package/dist/runtime/go/export.go +37 -0
- package/dist/runtime/go/hooks.go +53 -0
- package/dist/runtime/go/invalidation.go +112 -0
- package/dist/runtime/go/log.go +92 -0
- package/dist/runtime/go/nullable.go +64 -0
- package/dist/runtime/go/options.go +118 -0
- package/dist/runtime/go/path.go +156 -0
- package/dist/runtime/go/query.go +147 -0
- package/dist/runtime/go/realtime.go +239 -0
- package/dist/runtime/go/realtime_loop.go +208 -0
- package/dist/runtime/go/redirect.go +124 -0
- package/dist/runtime/go/request.go +222 -0
- package/dist/runtime/go/result.go +86 -0
- package/dist/runtime/go/sse.go +186 -0
- package/dist/runtime/go/stale.go +229 -0
- package/dist/runtime/go/stream.go +105 -0
- package/dist/runtime/go/transport.go +242 -0
- package/dist/runtime/go/transport_longpoll.go +121 -0
- package/dist/runtime/go/transport_sse.go +175 -0
- package/dist/runtime/go/ws.go +205 -0
- package/dist/runtime/java/Answer.java +107 -0
- package/dist/runtime/java/ApiException.java +110 -0
- package/dist/runtime/java/BinaryBody.java +36 -0
- package/dist/runtime/java/Body.java +105 -0
- package/dist/runtime/java/Call.java +23 -0
- package/dist/runtime/java/CallLog.java +51 -0
- package/dist/runtime/java/ClientConfig.java +233 -0
- package/dist/runtime/java/ConnectionState.java +12 -0
- package/dist/runtime/java/Credential.java +58 -0
- package/dist/runtime/java/Deadline.java +38 -0
- package/dist/runtime/java/Decode.java +181 -0
- package/dist/runtime/java/Envelope.java +141 -0
- package/dist/runtime/java/ErrorEnvelope.java +23 -0
- package/dist/runtime/java/EventStream.java +129 -0
- package/dist/runtime/java/Exchange.java +62 -0
- package/dist/runtime/java/FilePart.java +5 -0
- package/dist/runtime/java/Flight.java +306 -0
- package/dist/runtime/java/HoneyException.java +14 -0
- package/dist/runtime/java/Hooks.java +47 -0
- package/dist/runtime/java/Hops.java +133 -0
- package/dist/runtime/java/InvalidationConfig.java +73 -0
- package/dist/runtime/java/Json.java +23 -0
- package/dist/runtime/java/Keepalive.java +60 -0
- package/dist/runtime/java/LogEntry.java +14 -0
- package/dist/runtime/java/LongPollAdapter.java +68 -0
- package/dist/runtime/java/Operation.java +21 -0
- package/dist/runtime/java/Opt.java +121 -0
- package/dist/runtime/java/Pump.java +160 -0
- package/dist/runtime/java/RawResponse.java +48 -0
- package/dist/runtime/java/Realtime.java +152 -0
- package/dist/runtime/java/RealtimeLoop.java +371 -0
- package/dist/runtime/java/RedirectPolicy.java +13 -0
- package/dist/runtime/java/Redirects.java +126 -0
- package/dist/runtime/java/RequestContext.java +78 -0
- package/dist/runtime/java/RequestHook.java +9 -0
- package/dist/runtime/java/RequestOptions.java +91 -0
- package/dist/runtime/java/ResponseContext.java +70 -0
- package/dist/runtime/java/ResponseHook.java +12 -0
- package/dist/runtime/java/Result.java +32 -0
- package/dist/runtime/java/ResumableConnection.java +165 -0
- package/dist/runtime/java/SdkException.java +11 -0
- package/dist/runtime/java/Socket.java +301 -0
- package/dist/runtime/java/Sse.java +171 -0
- package/dist/runtime/java/SseAdapter.java +61 -0
- package/dist/runtime/java/SseEvent.java +10 -0
- package/dist/runtime/java/StaleMeta.java +16 -0
- package/dist/runtime/java/StaleTracker.java +287 -0
- package/dist/runtime/java/Transport.java +16 -0
- package/dist/runtime/java/TransportAdapter.java +14 -0
- package/dist/runtime/java/TransportConnection.java +15 -0
- package/dist/runtime/java/TransportKind.java +12 -0
- package/dist/runtime/java/TransportMessage.java +9 -0
- package/dist/runtime/java/TransportOptions.java +138 -0
- package/dist/runtime/java/TypedWebSocket.java +83 -0
- package/dist/runtime/java/UrlPath.java +49 -0
- package/dist/runtime/java/UrlQuery.java +99 -0
- package/dist/runtime/java/WsAdapter.java +52 -0
- package/dist/runtime/java/WsClose.java +47 -0
- package/dist/runtime/java/WsDial.java +148 -0
- package/dist/runtime/java/WsMessage.java +18 -0
- package/dist/runtime/mcp/call.ts +167 -0
- package/dist/runtime/mcp/files.ts +183 -0
- package/dist/runtime/mcp/guard.ts +176 -0
- package/dist/runtime/mcp/http.ts +159 -0
- package/dist/runtime/mcp/jsonrpc.ts +50 -0
- package/dist/runtime/mcp/listen.ts +76 -0
- package/dist/runtime/mcp/mount.ts +123 -0
- package/dist/runtime/mcp/protocol.ts +219 -0
- package/dist/runtime/mcp/reader.ts +57 -0
- package/dist/runtime/mcp/result.ts +85 -0
- package/dist/runtime/mcp/stdio.ts +79 -0
- package/dist/runtime/mcp/stream.ts +62 -0
- package/dist/runtime/python/__init__.py +2 -0
- package/dist/runtime/python/auth.py +67 -0
- package/dist/runtime/python/body.py +83 -0
- package/dist/runtime/python/config.py +139 -0
- package/dist/runtime/python/errors.py +213 -0
- package/dist/runtime/python/events.py +152 -0
- package/dist/runtime/python/exchange.py +298 -0
- package/dist/runtime/python/hooks.py +86 -0
- package/dist/runtime/python/invalidation.py +57 -0
- package/dist/runtime/python/log.py +60 -0
- package/dist/runtime/python/operation.py +25 -0
- package/dist/runtime/python/path.py +41 -0
- package/dist/runtime/python/query.py +48 -0
- package/dist/runtime/python/realtime.py +244 -0
- package/dist/runtime/python/redirect.py +93 -0
- package/dist/runtime/python/result.py +58 -0
- package/dist/runtime/python/send_async.py +189 -0
- package/dist/runtime/python/send_sync.py +105 -0
- package/dist/runtime/python/sse.py +81 -0
- package/dist/runtime/python/stale.py +94 -0
- package/dist/runtime/python/transport.py +157 -0
- package/dist/runtime/python/transport_longpoll.py +60 -0
- package/dist/runtime/python/transport_sse.py +60 -0
- package/dist/runtime/python/transport_ws.py +57 -0
- package/dist/runtime/python/ws.py +276 -0
- package/dist/runtime/rust/Cargo.lock +1763 -0
- package/dist/runtime/rust/answer.rs +120 -0
- package/dist/runtime/rust/auth.rs +80 -0
- package/dist/runtime/rust/body.rs +150 -0
- package/dist/runtime/rust/call.rs +200 -0
- package/dist/runtime/rust/config.rs +254 -0
- package/dist/runtime/rust/envelope.rs +84 -0
- package/dist/runtime/rust/errors.rs +235 -0
- package/dist/runtime/rust/events.rs +177 -0
- package/dist/runtime/rust/exchange.rs +287 -0
- package/dist/runtime/rust/hooks.rs +63 -0
- package/dist/runtime/rust/invalidation.rs +84 -0
- package/dist/runtime/rust/log.rs +88 -0
- package/dist/runtime/rust/nullable.rs +13 -0
- package/dist/runtime/rust/path.rs +83 -0
- package/dist/runtime/rust/query.rs +84 -0
- package/dist/runtime/rust/realtime.rs +182 -0
- package/dist/runtime/rust/realtime_loop.rs +177 -0
- package/dist/runtime/rust/redirect.rs +121 -0
- package/dist/runtime/rust/request.rs +178 -0
- package/dist/runtime/rust/result.rs +101 -0
- package/dist/runtime/rust/sse.rs +104 -0
- package/dist/runtime/rust/stale.rs +209 -0
- package/dist/runtime/rust/transport.rs +221 -0
- package/dist/runtime/rust/transport_longpoll.rs +108 -0
- package/dist/runtime/rust/transport_sse.rs +91 -0
- package/dist/runtime/rust/transport_ws.rs +75 -0
- package/dist/runtime/rust/ws.rs +322 -0
- package/dist/runtime/ts/app-types.ts +200 -0
- package/dist/runtime/ts/app.ts +104 -0
- package/dist/runtime/ts/config.ts +124 -0
- package/dist/runtime/ts/defaults.ts +48 -0
- package/dist/runtime/ts/error.ts +113 -0
- package/dist/runtime/ts/hooks.ts +92 -0
- package/dist/runtime/ts/http.ts +228 -0
- package/dist/runtime/ts/media-type.ts +36 -0
- package/dist/runtime/ts/path.ts +152 -0
- package/dist/runtime/ts/realtime-connection.ts +296 -0
- package/dist/runtime/ts/realtime-transports.ts +149 -0
- package/dist/runtime/ts/realtime.ts +293 -0
- package/dist/runtime/ts/redirect-policy.ts +17 -0
- package/dist/runtime/ts/redirects.ts +74 -0
- package/dist/runtime/ts/request.ts +239 -0
- package/dist/runtime/ts/response.ts +219 -0
- package/dist/runtime/ts/sdk-realtime.ts +10 -0
- package/dist/runtime/ts/sdk-sse.ts +55 -0
- package/dist/runtime/ts/sdk-ws.ts +21 -0
- package/dist/runtime/ts/sdk.ts +242 -0
- package/dist/runtime/ts/sse.ts +175 -0
- package/dist/runtime/ts/stale.ts +137 -0
- package/dist/runtime/ts/stream.ts +87 -0
- package/dist/runtime/ts/webhook.ts +116 -0
- package/dist/runtime/ts/ws.ts +155 -0
- package/dist/runtime/ts-tanstack-query/invalidate.ts +152 -0
- package/dist/runtime/ts-tanstack-query/query.ts +261 -0
- package/package.json +84 -5
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type PackageIdentity } from "../shared/metadata.ts";
|
|
2
|
+
import { type GoCallPlan } from "./sdk-client.ts";
|
|
3
|
+
export type GoSdkOptions = {
|
|
4
|
+
/** the module path go.mod declares; `example.com/sdk` by default */
|
|
5
|
+
readonly modulePath?: string;
|
|
6
|
+
/** the module's version, semver: from v2 on, its major suffix (`/v2`) ends the module path */
|
|
7
|
+
readonly version?: string;
|
|
8
|
+
};
|
|
9
|
+
/** The module's files by name, and what the generation reports: a root member renamed for the safe mirror. */
|
|
10
|
+
export type GeneratedGoSdk = {
|
|
11
|
+
readonly files: Readonly<Record<string, string>>;
|
|
12
|
+
readonly notices: readonly string[];
|
|
13
|
+
/** the module as the Go proxy serves it, by its path and the version given, if any */
|
|
14
|
+
readonly packages: readonly PackageIdentity[];
|
|
15
|
+
};
|
|
16
|
+
export declare const WEBSOCKET = "github.com/coder/websocket v1.8.15";
|
|
17
|
+
export declare const WEBSOCKET_SUM: string[];
|
|
18
|
+
/** String and rune literals and line comments: text an import check must not read as code (the CLI's cmd files too). */
|
|
19
|
+
export declare const NOT_CODE: RegExp;
|
|
20
|
+
/** The Go SDK module for `document`, by file name, and its notices. */
|
|
21
|
+
export declare function generateGoSdk(document: Record<string, unknown>, options?: GoSdkOptions): GeneratedGoSdk;
|
|
22
|
+
/**
|
|
23
|
+
* Each operation's call plan by id, from the very emit `generateGoSdk` runs with these options: the
|
|
24
|
+
* names a test driver invokes the SDK by (G8.4), never re-derived outside the emitter.
|
|
25
|
+
*/
|
|
26
|
+
export declare function goCallPlans(document: Record<string, unknown>, options?: GoSdkOptions): Readonly<Record<string, GoCallPlan>>;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Go SDK (bit 12): a module of package `sdk` emitted from the IR alone (H2) — the vendored
|
|
3
|
+
* runtime verbatim, `types.go` (every component, then every hoisted declaration in name order, then
|
|
4
|
+
* the declared error keys), `client.go` (the client, its resources and methods, and its `Safe()`
|
|
5
|
+
* mirror answering each as a `Result`, bit 21 i3), `doc.go` and
|
|
6
|
+
* `go.mod`, plus `go.sum` when the API needs the WebSocket library. Every `.go` file carries the
|
|
7
|
+
* generated-code marker.
|
|
8
|
+
*/
|
|
9
|
+
import { toIR } from "../../ir/build.js";
|
|
10
|
+
import { sdkModel } from "../../ir/sdk-model.js";
|
|
11
|
+
import { lineComment } from "../shared/escape.js";
|
|
12
|
+
import { packageOn } from "../shared/metadata.js";
|
|
13
|
+
import { compareCodeUnits } from "../shared/lang.js";
|
|
14
|
+
import { goErrorKeys } from "./errors.js";
|
|
15
|
+
import { needsWebSocket, runtimeFiles } from "./http.js";
|
|
16
|
+
import { goModulePath } from "./module-path.js";
|
|
17
|
+
import { clientDecls } from "./sdk-client.js";
|
|
18
|
+
import { goTopLevel, goTypeContext } from "./types.js";
|
|
19
|
+
/* the marker gopls and linters read, apart from the package doc by a blank line */
|
|
20
|
+
const MARKER = "// Code generated by honey. DO NOT EDIT.";
|
|
21
|
+
/* the WebSocket library and its go.sum lines, as the Go checksum database has them; the CLI's module requires them too */
|
|
22
|
+
export const WEBSOCKET = "github.com/coder/websocket v1.8.15";
|
|
23
|
+
export const WEBSOCKET_SUM = [
|
|
24
|
+
`${WEBSOCKET} h1:6B2JPeOGlpff2Uz6vOEH1Vzpi0iUz20A+lPVhPHtNUA=`,
|
|
25
|
+
`${WEBSOCKET}/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg=`,
|
|
26
|
+
];
|
|
27
|
+
/* what client.go may import, each only when its code uses it */
|
|
28
|
+
const CLIENT_IMPORTS = [
|
|
29
|
+
["context", "context"],
|
|
30
|
+
["json", "encoding/json"],
|
|
31
|
+
["io", "io"],
|
|
32
|
+
["iter", "iter"],
|
|
33
|
+
["http", "net/http"],
|
|
34
|
+
["url", "net/url"],
|
|
35
|
+
];
|
|
36
|
+
/** String and rune literals and line comments: text an import check must not read as code (the CLI's cmd files too). */
|
|
37
|
+
export const NOT_CODE = /"(?:[^"\\\n]|\\.)*"|`[^`]*`|'(?:[^'\\\n]|\\.)*'|\/\/[^\n]*/g;
|
|
38
|
+
/** A `.go` file of package `sdk` holding `decls`, importing each of `imports` (`[name, path]`) its code uses. */
|
|
39
|
+
function goFile(decls, imports) {
|
|
40
|
+
const code = decls.replace(NOT_CODE, "");
|
|
41
|
+
const used = imports.filter(([name]) => new RegExp(`\\b${name}\\.`).test(code)).map(([, path]) => `\t"${path}"`);
|
|
42
|
+
const block = used.length === 0 ? "" : `import (\n${used.join("\n")}\n)\n\n`;
|
|
43
|
+
return `${MARKER}\n\npackage sdk\n\n${block}${decls}\n`;
|
|
44
|
+
}
|
|
45
|
+
/** go.mod at the go 1.24 floor, and go.sum, requiring the WebSocket library only when `websocket`. */
|
|
46
|
+
function moduleFiles(modulePath, websocket) {
|
|
47
|
+
const mod = [`module ${modulePath}`, "", "go 1.24", ""];
|
|
48
|
+
if (!websocket)
|
|
49
|
+
return { "go.mod": mod.join("\n") };
|
|
50
|
+
return { "go.mod": [...mod, `require ${WEBSOCKET}`, ""].join("\n"), "go.sum": `${WEBSOCKET_SUM.join("\n")}\n` };
|
|
51
|
+
}
|
|
52
|
+
/** doc.go: the package godoc, naming the API and holding its description. */
|
|
53
|
+
function docFile(info) {
|
|
54
|
+
const named = [info.title].filter((title) => title !== undefined).map((title) => ` for ${title}`);
|
|
55
|
+
const text = [`Package sdk is a generated client${named.join("")}.`, info.description].filter((part) => part !== undefined);
|
|
56
|
+
return `${MARKER}\n\n${lineComment(text.join("\n\n"), "//").join("\n")}\npackage sdk\n`;
|
|
57
|
+
}
|
|
58
|
+
/** Each component's declaration in code-unit order, its description as its godoc. */
|
|
59
|
+
function componentDecls(ir, at) {
|
|
60
|
+
return Object.keys(ir.schemas)
|
|
61
|
+
.sort(compareCodeUnits)
|
|
62
|
+
.map((name) => {
|
|
63
|
+
const described = Object.hasOwn(ir.schemaDescriptions, name);
|
|
64
|
+
const doc = described ? lineComment(ir.schemaDescriptions[name], "//") : [];
|
|
65
|
+
return [...doc, goTopLevel(at.names.ref(name), ir.schemas[name], at)].join("\n");
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
/** The module's files for `document`, and the call plan of every method in them. */
|
|
69
|
+
function emitGo(document, options) {
|
|
70
|
+
const ir = toIR(document, { deriveOperationIds: true });
|
|
71
|
+
const model = sdkModel(ir);
|
|
72
|
+
const at = goTypeContext(ir);
|
|
73
|
+
/* components first: the client's hoisted types then land beside them */
|
|
74
|
+
const components = componentDecls(ir, at);
|
|
75
|
+
const ops = model.opsById;
|
|
76
|
+
const notices = [];
|
|
77
|
+
const client = clientDecls(ir, { at, notices, ops });
|
|
78
|
+
const errorKeys = goErrorKeys(model, at);
|
|
79
|
+
const hoisted = [...at.decls].sort(([a], [b]) => compareCodeUnits(a, b)).map(([, decl]) => decl);
|
|
80
|
+
const features = {
|
|
81
|
+
realtime: model.ops.some((op) => op.stream === "realtime"),
|
|
82
|
+
ws: model.ops.some((op) => op.stream === "ws"),
|
|
83
|
+
};
|
|
84
|
+
const modulePath = goModulePath(options.modulePath ?? "example.com/sdk", options.version);
|
|
85
|
+
return {
|
|
86
|
+
notices,
|
|
87
|
+
packages: [packageOn("go", modulePath, options.version)],
|
|
88
|
+
plans: client.plans,
|
|
89
|
+
files: {
|
|
90
|
+
...runtimeFiles(features),
|
|
91
|
+
...moduleFiles(modulePath, needsWebSocket(features)),
|
|
92
|
+
"client.go": goFile(client.decls, CLIENT_IMPORTS),
|
|
93
|
+
"doc.go": docFile(ir.info),
|
|
94
|
+
"types.go": goFile([...components, ...hoisted, errorKeys].filter((decl) => decl !== "").join("\n\n"), [
|
|
95
|
+
["json", "encoding/json"],
|
|
96
|
+
]),
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/** The Go SDK module for `document`, by file name, and its notices. */
|
|
101
|
+
export function generateGoSdk(document, options = {}) {
|
|
102
|
+
const { files, notices, packages } = emitGo(document, options);
|
|
103
|
+
return { files, notices, packages };
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Each operation's call plan by id, from the very emit `generateGoSdk` runs with these options: the
|
|
107
|
+
* names a test driver invokes the SDK by (G8.4), never re-derived outside the emitter.
|
|
108
|
+
*/
|
|
109
|
+
export function goCallPlans(document, options = {}) {
|
|
110
|
+
return Object.fromEntries(emitGo(document, options).plans.map((plan) => [plan.operation, plan]));
|
|
111
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A server-sent-events operation as a Go method (G12): it returns `iter.Seq2[SSEEvent[T], error]`,
|
|
3
|
+
* `T` the type of the declared event schema (`string` when the events are untyped), and opens the
|
|
4
|
+
* stream when ranged. `<Op>Opts.LastEventID` resumes it as the Last-Event-ID header, which also
|
|
5
|
+
* carries a declared Last-Event-ID header param (Fd3). An error status at the opening is the
|
|
6
|
+
* stream's error value; its `Client.Safe()` twin opens the stream on the call and answers that
|
|
7
|
+
* status as its Result's Err (bit 21 i3).
|
|
8
|
+
*/
|
|
9
|
+
import type { SdkOp } from "../../ir/sdk-model.ts";
|
|
10
|
+
import { type GoStream } from "./sdk-answer.ts";
|
|
11
|
+
import type { OptsField } from "./sdk-opts.ts";
|
|
12
|
+
import { type GoTypeContext } from "./types.ts";
|
|
13
|
+
/** Whether a declared param is the Last-Event-ID header (any casing), the resume channel an SSE dial sends. */
|
|
14
|
+
export declare const isLastEventIdHeader: (param: string, where: OptsField["in"]) => boolean;
|
|
15
|
+
/** The method of the SSE operation `op`, its event type hoisted into `at` as `<Op>Event` when inline. */
|
|
16
|
+
export declare function sseStream(op: SdkOp, at: GoTypeContext): GoStream;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { sendArgs } from "./sdk-answer.js";
|
|
2
|
+
import { goTypeOf } from "./types.js";
|
|
3
|
+
/** Whether a declared param is the Last-Event-ID header (any casing), the resume channel an SSE dial sends. */
|
|
4
|
+
export const isLastEventIdHeader = (param, where) => where === "header" && param.toLowerCase() === "last-event-id";
|
|
5
|
+
/* LastEventID, sent only when set: an empty header would still name a position */
|
|
6
|
+
const SSE_OPTS = {
|
|
7
|
+
before: [],
|
|
8
|
+
carries: isLastEventIdHeader,
|
|
9
|
+
fields: [{ name: "LastEventID", type: "string" }],
|
|
10
|
+
sets: ['if opts.LastEventID != "" {', '\tc.headers["Last-Event-ID"] = opts.LastEventID', "}"],
|
|
11
|
+
};
|
|
12
|
+
/** The method of the SSE operation `op`, its event type hoisted into `at` as `<Op>Event` when inline. */
|
|
13
|
+
export function sseStream(op, at) {
|
|
14
|
+
const typed = op.events && goTypeOf(op.events, { ...at, fieldName: "Event", parentName: op.id }).replace(/^\*/, "");
|
|
15
|
+
const event = typed ?? "string";
|
|
16
|
+
const open = typed === undefined ? "streamText" : `streamJSON[${typed}]`;
|
|
17
|
+
return {
|
|
18
|
+
doc: "Breaking out of the loop closes the stream; ctx ends it quietly.",
|
|
19
|
+
opts: SSE_OPTS,
|
|
20
|
+
shape: {
|
|
21
|
+
fail: `sseFail[${event}](err)`,
|
|
22
|
+
returns: `iter.Seq2[SSEEvent[${event}], error]`,
|
|
23
|
+
tail: (client) => [`\treturn ${open}(${sendArgs(client)})`],
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { StructField, StructShape } from "./types-struct.ts";
|
|
2
|
+
/** One struct as its methods see it: its name, its shape, and the declaration of its `plain` copy. */
|
|
3
|
+
export type Codec = {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly shape: StructShape;
|
|
6
|
+
readonly plain: readonly string[];
|
|
7
|
+
};
|
|
8
|
+
/** Whether a Go value of type `type` can be nil already, so an optional one needs no pointer. */
|
|
9
|
+
export declare function isNilable(type: string): boolean;
|
|
10
|
+
/** MarshalJSON that only fills the required slices and maps a nil would encode as null. */
|
|
11
|
+
export declare function fillingMarshal(name: string, filled: readonly StructField[]): string[];
|
|
12
|
+
/** UnmarshalJSON: the plain copy as encoding/json reads it, the by-hand keys, each embed from the whole object. */
|
|
13
|
+
export declare function unmarshal(codec: Codec): string[];
|
|
14
|
+
/** MarshalJSON: the plain copy as encoding/json writes it, then each embed's keys, the extra keys and the untagged ones. */
|
|
15
|
+
export declare function marshal(codec: Codec, filled: readonly StructField[]): string[];
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The (un)marshal methods a Go struct gets when encoding/json alone would get it wrong. Each struct
|
|
3
|
+
* gets at most one MarshalJSON and one UnmarshalJSON: encoding/json handles a `plain` copy of the
|
|
4
|
+
* struct, then the methods add what it cannot — the untagged keys, the extra keys, and each embedded
|
|
5
|
+
* part on its own (its own methods would otherwise be promoted and drop the outer fields). On a key
|
|
6
|
+
* both an embed and the struct itself encode, the struct's own field wins; a valid allOf has none.
|
|
7
|
+
*/
|
|
8
|
+
import { goString } from "../shared/escape.js";
|
|
9
|
+
/** Whether a Go value of type `type` can be nil already, so an optional one needs no pointer. */
|
|
10
|
+
export function isNilable(type) {
|
|
11
|
+
return (type === "json.RawMessage" ||
|
|
12
|
+
type === "interface{}" ||
|
|
13
|
+
type.startsWith("*") ||
|
|
14
|
+
type.startsWith("[]") ||
|
|
15
|
+
type.startsWith("map["));
|
|
16
|
+
}
|
|
17
|
+
const ERR_RETURN = ["\tif err != nil {", "\t\treturn nil, err", "\t}"];
|
|
18
|
+
/** An embed's field name: its type without the pointer. */
|
|
19
|
+
const embedField = (embed) => embed.replace(/^\*/, "");
|
|
20
|
+
/** `if p.F == nil { p.F = T{} }` for each required slice or map. */
|
|
21
|
+
function fillNil(fields) {
|
|
22
|
+
return fields.flatMap((f) => [`\tif p.${f.goName} == nil {`, `\t\tp.${f.goName} = ${f.type}{}`, "\t}"]);
|
|
23
|
+
}
|
|
24
|
+
/** MarshalJSON that only fills the required slices and maps a nil would encode as null. */
|
|
25
|
+
export function fillingMarshal(name, filled) {
|
|
26
|
+
return [
|
|
27
|
+
`func (s ${name}) MarshalJSON() ([]byte, error) {`,
|
|
28
|
+
`\ttype plain ${name}`,
|
|
29
|
+
"\tp := plain(s)",
|
|
30
|
+
...fillNil(filled),
|
|
31
|
+
"\treturn json.Marshal(p)",
|
|
32
|
+
"}",
|
|
33
|
+
];
|
|
34
|
+
}
|
|
35
|
+
/** The loop that decodes every undeclared key into the extra map. */
|
|
36
|
+
function extraDecode({ shape }) {
|
|
37
|
+
const extra = shape.extra;
|
|
38
|
+
if (extra === undefined)
|
|
39
|
+
return [];
|
|
40
|
+
const known = shape.fields.map((f) => `${goString(f.key)}: true`).join(", ");
|
|
41
|
+
return [
|
|
42
|
+
`\tknown := map[string]bool{${known}}`,
|
|
43
|
+
"\tfor k, v := range raw {",
|
|
44
|
+
"\t\tif known[k] {",
|
|
45
|
+
"\t\t\tcontinue",
|
|
46
|
+
"\t\t}",
|
|
47
|
+
`\t\tvar item ${extra.valueType}`,
|
|
48
|
+
"\t\tif err := json.Unmarshal(v, &item); err != nil {",
|
|
49
|
+
"\t\t\treturn err",
|
|
50
|
+
"\t\t}",
|
|
51
|
+
`\t\tif base.${extra.goName} == nil {`,
|
|
52
|
+
`\t\t\tbase.${extra.goName} = map[string]${extra.valueType}{}`,
|
|
53
|
+
"\t\t}",
|
|
54
|
+
`\t\tbase.${extra.goName}[k] = item`,
|
|
55
|
+
"\t}",
|
|
56
|
+
];
|
|
57
|
+
}
|
|
58
|
+
/** The object as raw members, then each untagged key and the extra keys read from it by hand. */
|
|
59
|
+
function byHandDecode(codec) {
|
|
60
|
+
const untagged = codec.shape.fields.filter((f) => !f.tagged);
|
|
61
|
+
if (untagged.length === 0 && codec.shape.extra === undefined)
|
|
62
|
+
return [];
|
|
63
|
+
return [
|
|
64
|
+
"\tvar raw map[string]json.RawMessage",
|
|
65
|
+
"\tif err := json.Unmarshal(data, &raw); err != nil {",
|
|
66
|
+
"\t\treturn err",
|
|
67
|
+
"\t}",
|
|
68
|
+
...untagged.flatMap((f) => [
|
|
69
|
+
`\tif v, ok := raw[${goString(f.key)}]; ok {`,
|
|
70
|
+
`\t\tif err := json.Unmarshal(v, &base.${f.goName}); err != nil {`,
|
|
71
|
+
"\t\t\treturn err",
|
|
72
|
+
"\t\t}",
|
|
73
|
+
"\t}",
|
|
74
|
+
]),
|
|
75
|
+
...extraDecode(codec),
|
|
76
|
+
];
|
|
77
|
+
}
|
|
78
|
+
/** UnmarshalJSON: the plain copy as encoding/json reads it, the by-hand keys, each embed from the whole object. */
|
|
79
|
+
export function unmarshal(codec) {
|
|
80
|
+
const { name, shape } = codec;
|
|
81
|
+
const embeds = shape.embeds.map(embedField);
|
|
82
|
+
const store = embeds.length === 0 ? [`\t*s = ${name}(base)`] : shape.fields.map((f) => `\ts.${f.goName} = base.${f.goName}`);
|
|
83
|
+
return [
|
|
84
|
+
`func (s *${name}) UnmarshalJSON(data []byte) error {`,
|
|
85
|
+
...codec.plain,
|
|
86
|
+
"\tvar base plain",
|
|
87
|
+
"\tif err := json.Unmarshal(data, &base); err != nil {",
|
|
88
|
+
"\t\treturn err",
|
|
89
|
+
"\t}",
|
|
90
|
+
...byHandDecode(codec),
|
|
91
|
+
...embeds.flatMap((embed) => [
|
|
92
|
+
`\tif err := json.Unmarshal(data, &s.${embed}); err != nil {`,
|
|
93
|
+
"\t\treturn err",
|
|
94
|
+
"\t}",
|
|
95
|
+
]),
|
|
96
|
+
...store,
|
|
97
|
+
"\treturn nil",
|
|
98
|
+
"}",
|
|
99
|
+
];
|
|
100
|
+
}
|
|
101
|
+
/** The loop adding each embed's own encoding under the keys the struct did not encode itself. */
|
|
102
|
+
function embedsEncode(embeds) {
|
|
103
|
+
if (embeds.length === 0)
|
|
104
|
+
return [];
|
|
105
|
+
return [
|
|
106
|
+
`\tfor _, part := range []any{${embeds.map((embed) => `s.${embed}`).join(", ")}} {`,
|
|
107
|
+
"\t\tencoded, err := json.Marshal(part)",
|
|
108
|
+
...ERR_RETURN.map((line) => `\t${line}`),
|
|
109
|
+
"\t\tvar members map[string]json.RawMessage",
|
|
110
|
+
"\t\tif err := json.Unmarshal(encoded, &members); err != nil {",
|
|
111
|
+
"\t\t\treturn nil, err",
|
|
112
|
+
"\t\t}",
|
|
113
|
+
"\t\tfor k, v := range members {",
|
|
114
|
+
"\t\t\tif _, taken := out[k]; !taken {",
|
|
115
|
+
"\t\t\t\tout[k] = v",
|
|
116
|
+
"\t\t\t}",
|
|
117
|
+
"\t\t}",
|
|
118
|
+
"\t}",
|
|
119
|
+
];
|
|
120
|
+
}
|
|
121
|
+
/** The loop adding each extra key the declared ones did not take. */
|
|
122
|
+
function extraEncode({ shape }) {
|
|
123
|
+
if (shape.extra === undefined)
|
|
124
|
+
return [];
|
|
125
|
+
return [
|
|
126
|
+
`\tfor k, v := range p.${shape.extra.goName} {`,
|
|
127
|
+
"\t\tif _, taken := out[k]; taken {",
|
|
128
|
+
"\t\t\tcontinue",
|
|
129
|
+
"\t\t}",
|
|
130
|
+
...["\tb, err := json.Marshal(v)", ...ERR_RETURN, "\tout[k] = b"].map((line) => `\t${line}`),
|
|
131
|
+
"\t}",
|
|
132
|
+
];
|
|
133
|
+
}
|
|
134
|
+
/** The statements that encode one untagged field into `out`, skipped while it is absent. */
|
|
135
|
+
function untaggedEncode(field) {
|
|
136
|
+
const guard = field.triState
|
|
137
|
+
? `\tif !p.${field.goName}.IsZero() {`
|
|
138
|
+
: isNilable(field.type)
|
|
139
|
+
? `\tif p.${field.goName} != nil {`
|
|
140
|
+
: undefined;
|
|
141
|
+
const indent = guard ? "\t" : "";
|
|
142
|
+
const encode = [
|
|
143
|
+
`\tif out[${goString(field.key)}], err = json.Marshal(p.${field.goName}); err != nil {`,
|
|
144
|
+
"\t\treturn nil, err",
|
|
145
|
+
"\t}",
|
|
146
|
+
].map((line) => `${indent}${line}`);
|
|
147
|
+
return guard ? [guard, ...encode, "\t}"] : encode;
|
|
148
|
+
}
|
|
149
|
+
/** MarshalJSON: the plain copy as encoding/json writes it, then each embed's keys, the extra keys and the untagged ones. */
|
|
150
|
+
export function marshal(codec, filled) {
|
|
151
|
+
const { name, shape } = codec;
|
|
152
|
+
const embeds = shape.embeds.map(embedField);
|
|
153
|
+
const copy = embeds.length === 0 ? "plain(s)" : `plain{${shape.fields.map((f) => `${f.goName}: s.${f.goName}`).join(", ")}}`;
|
|
154
|
+
return [
|
|
155
|
+
`func (s ${name}) MarshalJSON() ([]byte, error) {`,
|
|
156
|
+
...codec.plain,
|
|
157
|
+
`\tp := ${copy}`,
|
|
158
|
+
...fillNil(filled),
|
|
159
|
+
"\tencoded, err := json.Marshal(p)",
|
|
160
|
+
...ERR_RETURN,
|
|
161
|
+
"\tvar out map[string]json.RawMessage",
|
|
162
|
+
"\tif err := json.Unmarshal(encoded, &out); err != nil {",
|
|
163
|
+
"\t\treturn nil, err",
|
|
164
|
+
"\t}",
|
|
165
|
+
...embedsEncode(embeds),
|
|
166
|
+
...extraEncode(codec),
|
|
167
|
+
...shape.fields.filter((f) => !f.tagged).flatMap(untaggedEncode),
|
|
168
|
+
"\treturn json.Marshal(out)",
|
|
169
|
+
"}",
|
|
170
|
+
];
|
|
171
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The package-level names Go types are declared under (v0 buildGoNames): one scope that components,
|
|
3
|
+
* hoisted types and constants all claim from, clear of every identifier the Go runtime declares.
|
|
4
|
+
*/
|
|
5
|
+
import { type IR } from "../../ir/model.ts";
|
|
6
|
+
import type { GoTypeNames } from "./types.ts";
|
|
7
|
+
/** Names for one schema on its own (v0 DEFAULT_NAMES): each as asked for, no cycles, refs left unresolved. */
|
|
8
|
+
export declare function plainNames(): GoTypeNames;
|
|
9
|
+
/**
|
|
10
|
+
* Names for the SDK of `ir`: each component claims its exported name in code-unit order, as
|
|
11
|
+
* `<Name>Model` when the runtime or the client owns it; hoisted types and constants claim after
|
|
12
|
+
* them, a key always getting the name it got first; `cyclic` is every component on a by-value cycle.
|
|
13
|
+
*/
|
|
14
|
+
export declare function irNames(ir: IR): GoTypeNames;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { irResolver } from "../../ir/schema.js";
|
|
2
|
+
import { valueCycleSchemas } from "../../ir/sdk-model.js";
|
|
3
|
+
import { compareCodeUnits, goExported, NameScope } from "../shared/lang.js";
|
|
4
|
+
import { runtimeFiles } from "./http.js";
|
|
5
|
+
/** The package-level identifiers one Go source declares: `func`, `type`, `var` and `const`, grouped or not. */
|
|
6
|
+
function declared(source) {
|
|
7
|
+
const names = [];
|
|
8
|
+
let grouped = false;
|
|
9
|
+
for (const line of source.split("\n")) {
|
|
10
|
+
const single = /^(?:func|type|var|const)\s+([A-Za-z_]\w*)/.exec(line)?.[1];
|
|
11
|
+
const member = grouped ? /^\t([A-Za-z_]\w*)/.exec(line)?.[1] : undefined;
|
|
12
|
+
if (single !== undefined)
|
|
13
|
+
names.push(single);
|
|
14
|
+
if (member !== undefined)
|
|
15
|
+
names.push(member);
|
|
16
|
+
if (/^(?:var|const|type)\s*\($/.test(line))
|
|
17
|
+
grouped = true;
|
|
18
|
+
else if (line.startsWith(")"))
|
|
19
|
+
grouped = false;
|
|
20
|
+
}
|
|
21
|
+
return names;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Every identifier the runtime's Go files declare at package level (v0 goRuntimeIdentifiers): the
|
|
25
|
+
* whole runtime, so a component keeps its Go name whichever runtime files its SDK ships.
|
|
26
|
+
*/
|
|
27
|
+
function runtimeIdentifiers() {
|
|
28
|
+
return Object.values(runtimeFiles({ realtime: true, ws: true })).flatMap(declared);
|
|
29
|
+
}
|
|
30
|
+
/** Names for one schema on its own (v0 DEFAULT_NAMES): each as asked for, no cycles, refs left unresolved. */
|
|
31
|
+
export function plainNames() {
|
|
32
|
+
return {
|
|
33
|
+
claimConst: (base) => base,
|
|
34
|
+
cyclic: new Set(),
|
|
35
|
+
hoist: (_key, base) => base,
|
|
36
|
+
ref: goExported,
|
|
37
|
+
resolve: (schema) => schema,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Names for the SDK of `ir`: each component claims its exported name in code-unit order, as
|
|
42
|
+
* `<Name>Model` when the runtime or the client owns it; hoisted types and constants claim after
|
|
43
|
+
* them, a key always getting the name it got first; `cyclic` is every component on a by-value cycle.
|
|
44
|
+
*/
|
|
45
|
+
export function irNames(ir) {
|
|
46
|
+
const scope = new NameScope([...runtimeIdentifiers(), "Client", "NewClient", "SafeClient"]);
|
|
47
|
+
const components = new Map();
|
|
48
|
+
for (const name of Object.keys(ir.schemas).sort(compareCodeUnits)) {
|
|
49
|
+
const base = goExported(name);
|
|
50
|
+
components.set(name, scope.claim(scope.has(base) ? `${base}Model` : base));
|
|
51
|
+
}
|
|
52
|
+
const hoisted = new Map();
|
|
53
|
+
return {
|
|
54
|
+
claimConst: (base) => scope.claim(base),
|
|
55
|
+
cyclic: valueCycleSchemas(ir.schemas),
|
|
56
|
+
hoist: (key, base) => {
|
|
57
|
+
const name = hoisted.get(key) ?? scope.claim(base);
|
|
58
|
+
hoisted.set(key, name);
|
|
59
|
+
return name;
|
|
60
|
+
},
|
|
61
|
+
ref: (name) => components.get(name) ?? goExported(name),
|
|
62
|
+
resolve: irResolver(ir.schemas),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export type StructField = {
|
|
2
|
+
goName: string;
|
|
3
|
+
/** the raw JSON key */
|
|
4
|
+
key: string;
|
|
5
|
+
type: string;
|
|
6
|
+
required: boolean;
|
|
7
|
+
/** whether a json tag can name the key; an untagged field is (un)marshalled by hand */
|
|
8
|
+
tagged: boolean;
|
|
9
|
+
/** optional and nullable: `Nullable[T]` under `omitzero` */
|
|
10
|
+
triState: boolean;
|
|
11
|
+
};
|
|
12
|
+
/** The field holding the keys `additionalProperties` admits beyond the declared ones. */
|
|
13
|
+
export type ExtraField = {
|
|
14
|
+
goName: string;
|
|
15
|
+
valueType: string;
|
|
16
|
+
};
|
|
17
|
+
/** What one struct declares: its embedded types, its fields in order, and its extra map. */
|
|
18
|
+
export type StructShape = {
|
|
19
|
+
readonly embeds: readonly string[];
|
|
20
|
+
readonly fields: readonly StructField[];
|
|
21
|
+
/** an object's extra map; an allOf struct, the only one with embeds, never has one */
|
|
22
|
+
readonly extra?: ExtraField | undefined;
|
|
23
|
+
/** whether an embed may (un)marshal itself: the struct then (un)marshals each embed on its own */
|
|
24
|
+
readonly embedsEncode?: boolean;
|
|
25
|
+
};
|
|
26
|
+
/** Whether encoding/json reads `key` as a tag name (its isValidTag: a letter, a decimal digit or that punctuation). */
|
|
27
|
+
export declare function isTagName(key: string): boolean;
|
|
28
|
+
/** `type <name> struct { … }` with its embeds, fields and extra map, then whatever methods its keys need. */
|
|
29
|
+
export declare function structDecl(name: string, shape: StructShape): string;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A Go struct declaration and, when encoding/json needs help, its (un)marshal methods: for a key no
|
|
3
|
+
* json tag can name, for the extra keys `additionalProperties` admits, for an embedded part that
|
|
4
|
+
* (un)marshals itself (Go would promote its methods over the outer fields), and for a required
|
|
5
|
+
* slice or map left nil (it would encode as null, which the schema does not allow).
|
|
6
|
+
*/
|
|
7
|
+
import { goString } from "../shared/escape.js";
|
|
8
|
+
import { fillingMarshal, marshal, unmarshal } from "./types-codec.js";
|
|
9
|
+
/* the punctuation encoding/json's isValidTag accepts in a tag name */
|
|
10
|
+
const TAG_PUNCTUATION = "!#$%&()*+-./:;<=>?@[]^_{|}~ ";
|
|
11
|
+
/** Whether encoding/json reads `key` as a tag name (its isValidTag: a letter, a decimal digit or that punctuation). */
|
|
12
|
+
export function isTagName(key) {
|
|
13
|
+
return key !== "" && [...key].every((ch) => TAG_PUNCTUATION.includes(ch) || /[\p{L}\p{Nd}]/u.test(ch));
|
|
14
|
+
}
|
|
15
|
+
/** A struct tag holding `value`: the raw-string form, or an interpreted string when `value` has a backtick. */
|
|
16
|
+
const tagLiteral = (value) => (value.includes("`") ? goString(value) : `\`${value}\``);
|
|
17
|
+
/** The json tag encoding/json reads: the key and its option, a bare `-` spelled `-,` (alone it skips the field). */
|
|
18
|
+
function jsonTag(field) {
|
|
19
|
+
const value = field.triState ? `${field.key},omitzero` : field.required ? field.key : `${field.key},omitempty`;
|
|
20
|
+
return tagLiteral(`json:${goString(value === "-" ? "-," : value)}`);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The field's tag on the declared struct. An untagged key's tag still names it for a tag reader
|
|
24
|
+
* (reflect's `Tag.Get`), unless no reader could recover it: empty, or split at its comma.
|
|
25
|
+
*/
|
|
26
|
+
function tagOf(field) {
|
|
27
|
+
if (field.tagged)
|
|
28
|
+
return jsonTag(field);
|
|
29
|
+
const named = field.key !== "" && !field.key.includes(",");
|
|
30
|
+
return named ? tagLiteral(`json:${goString(field.key)}`) : '`json:"-"`';
|
|
31
|
+
}
|
|
32
|
+
/** The field's tag on the copy the methods hand to encoding/json: an untagged key is skipped there. */
|
|
33
|
+
const plainTagOf = (field) => (field.tagged ? jsonTag(field) : '`json:"-"`');
|
|
34
|
+
/** The field lines of `shape`, each tagged by `tag`, the extra map last. */
|
|
35
|
+
function fieldLines(shape, tag, indent) {
|
|
36
|
+
return [
|
|
37
|
+
...shape.fields.map((f) => `${indent}${f.goName} ${f.type} ${tag(f)}`),
|
|
38
|
+
...(shape.extra ? [`${indent}${shape.extra.goName} map[string]${shape.extra.valueType} \`json:"-"\``] : []),
|
|
39
|
+
];
|
|
40
|
+
}
|
|
41
|
+
/** The copy of the struct the methods hand to encoding/json: the struct itself, or one without its embeds and untagged keys. */
|
|
42
|
+
function plainDecl(name, shape) {
|
|
43
|
+
if (shape.embeds.length === 0 && shape.fields.every((f) => f.tagged))
|
|
44
|
+
return [`\ttype plain ${name}`];
|
|
45
|
+
return ["\ttype plain struct {", ...fieldLines(shape, plainTagOf, "\t\t"), "\t}"];
|
|
46
|
+
}
|
|
47
|
+
/** `type <name> struct { … }` with its embeds, fields and extra map, then whatever methods its keys need. */
|
|
48
|
+
export function structDecl(name, shape) {
|
|
49
|
+
const lines = [
|
|
50
|
+
`type ${name} struct {`,
|
|
51
|
+
...shape.embeds.map((embed) => `\t${embed}`),
|
|
52
|
+
...fieldLines(shape, tagOf, "\t"),
|
|
53
|
+
"}",
|
|
54
|
+
];
|
|
55
|
+
const filled = shape.fields.filter((f) => f.required && (f.type.startsWith("[]") || f.type.startsWith("map[")));
|
|
56
|
+
if (shape.extra === undefined && shape.embedsEncode !== true && shape.fields.every((f) => f.tagged)) {
|
|
57
|
+
if (filled.length > 0)
|
|
58
|
+
lines.push("", ...fillingMarshal(name, filled));
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
const codec = { name, plain: plainDecl(name, shape), shape };
|
|
62
|
+
lines.push("", ...unmarshal(codec), "", ...marshal(codec, filled));
|
|
63
|
+
}
|
|
64
|
+
return lines.join("\n");
|
|
65
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A discriminated union as Go (v0): a struct of one `*Variant` per variant plus `Raw`, decoded on the
|
|
3
|
+
* discriminator's value and encoded as whichever variant is set. Go has no union type; a payload whose
|
|
4
|
+
* value matches no variant is kept whole in `Raw`.
|
|
5
|
+
*/
|
|
6
|
+
import type { IRDiscriminator, IRSchema } from "../../ir/model.ts";
|
|
7
|
+
import type { GoTypeNames } from "./types.ts";
|
|
8
|
+
type Union = Extract<IRSchema, {
|
|
9
|
+
kind: "union";
|
|
10
|
+
}> & {
|
|
11
|
+
discriminator: IRDiscriminator;
|
|
12
|
+
};
|
|
13
|
+
/** Declares an inline variant once under `names.hoist(key, base)`; its name. */
|
|
14
|
+
export type DeclareVariant = (schema: IRSchema, key: string, base: string) => string;
|
|
15
|
+
/** The union's struct and its (un)marshal methods; inline object variants are declared through `declare`. */
|
|
16
|
+
export declare function unionDecl(name: string, union: Union, names: GoTypeNames, declare: DeclareVariant): string;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { goString, lineComment } from "../shared/escape.js";
|
|
2
|
+
import { goExported, NameScope } from "../shared/lang.js";
|
|
3
|
+
/** The discriminator values that select `variant`: its mapping keys, else its property's const or enum, else its name. */
|
|
4
|
+
function selectors(variant, { mapping, propertyName }, names) {
|
|
5
|
+
if (variant.kind === "ref" && mapping) {
|
|
6
|
+
const mapped = Object.keys(mapping).filter((value) => mapping[value] === variant.name);
|
|
7
|
+
if (mapped.length > 0)
|
|
8
|
+
return mapped;
|
|
9
|
+
}
|
|
10
|
+
const resolved = names.resolve(variant);
|
|
11
|
+
const property = resolved.kind === "object" ? resolved.fields.find((field) => field.name === propertyName)?.schema : undefined;
|
|
12
|
+
if (property?.kind === "const" && typeof property.value === "string")
|
|
13
|
+
return [property.value];
|
|
14
|
+
if (property?.kind === "scalar" && property.enum)
|
|
15
|
+
return property.enum.map(String);
|
|
16
|
+
return variant.kind === "ref" ? [variant.name] : [];
|
|
17
|
+
}
|
|
18
|
+
/** One field per ref or inline-object variant, named after its first value; any other variant has none. */
|
|
19
|
+
function variantsOf(name, union, names, declare) {
|
|
20
|
+
const scope = new NameScope(["Raw", "UnmarshalJSON", "MarshalJSON"]);
|
|
21
|
+
return union.variants.flatMap((schema, i) => {
|
|
22
|
+
if (schema.kind !== "ref" && schema.kind !== "object")
|
|
23
|
+
return [];
|
|
24
|
+
const values = selectors(schema, union.discriminator, names);
|
|
25
|
+
const type = schema.kind === "ref"
|
|
26
|
+
? names.ref(schema.name)
|
|
27
|
+
: declare(schema, `${name}.variant${i}`, `${name}${goExported(values[0] ?? `Variant${i}`)}`);
|
|
28
|
+
return [{ field: scope.claim(goExported(values[0] ?? type)), type, values }];
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
function unmarshal(name, property, variants) {
|
|
32
|
+
return [
|
|
33
|
+
`func (u *${name}) UnmarshalJSON(data []byte) error {`,
|
|
34
|
+
"\tvar probe map[string]json.RawMessage",
|
|
35
|
+
"\tif err := json.Unmarshal(data, &probe); err != nil {",
|
|
36
|
+
"\t\treturn err",
|
|
37
|
+
"\t}",
|
|
38
|
+
"\tvar tag string",
|
|
39
|
+
`\tif v, ok := probe[${goString(property)}]; ok {`,
|
|
40
|
+
"\t\t_ = json.Unmarshal(v, &tag)",
|
|
41
|
+
"\t}",
|
|
42
|
+
`\t*u = ${name}{}`,
|
|
43
|
+
"\tswitch tag {",
|
|
44
|
+
...variants
|
|
45
|
+
.filter((v) => v.values.length > 0)
|
|
46
|
+
.flatMap((v) => [
|
|
47
|
+
`\tcase ${v.values.map(goString).join(", ")}:`,
|
|
48
|
+
`\t\tu.${v.field} = new(${v.type})`,
|
|
49
|
+
`\t\treturn json.Unmarshal(data, u.${v.field})`,
|
|
50
|
+
]),
|
|
51
|
+
"\t}",
|
|
52
|
+
"\tu.Raw = append(json.RawMessage(nil), data...)",
|
|
53
|
+
"\treturn nil",
|
|
54
|
+
"}",
|
|
55
|
+
];
|
|
56
|
+
}
|
|
57
|
+
function marshal(name, variants) {
|
|
58
|
+
return [
|
|
59
|
+
`func (u ${name}) MarshalJSON() ([]byte, error) {`,
|
|
60
|
+
...variants.flatMap((v) => [`\tif u.${v.field} != nil {`, `\t\treturn json.Marshal(u.${v.field})`, "\t}"]),
|
|
61
|
+
"\tif u.Raw != nil {",
|
|
62
|
+
"\t\treturn u.Raw, nil",
|
|
63
|
+
"\t}",
|
|
64
|
+
'\treturn []byte("null"), nil',
|
|
65
|
+
"}",
|
|
66
|
+
];
|
|
67
|
+
}
|
|
68
|
+
/** The union's struct and its (un)marshal methods; inline object variants are declared through `declare`. */
|
|
69
|
+
export function unionDecl(name, union, names, declare) {
|
|
70
|
+
const property = union.discriminator.propertyName;
|
|
71
|
+
const variants = variantsOf(name, union, names, declare);
|
|
72
|
+
return [
|
|
73
|
+
...lineComment(`${name} holds exactly one variant, selected by the "${property}" property.`, "//"),
|
|
74
|
+
`type ${name} struct {`,
|
|
75
|
+
...variants.map((v) => `\t${v.field} *${v.type}`),
|
|
76
|
+
"\t// Raw keeps a payload whose discriminator matches no known variant.",
|
|
77
|
+
"\tRaw json.RawMessage",
|
|
78
|
+
"}",
|
|
79
|
+
"",
|
|
80
|
+
...unmarshal(name, property, variants),
|
|
81
|
+
"",
|
|
82
|
+
...marshal(name, variants),
|
|
83
|
+
].join("\n");
|
|
84
|
+
}
|