sdkloom 0.0.0-stage → 0.1.0
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 +202 -0
- package/README.md +239 -2
- package/kits/allowed-sources.json +24 -0
- package/kits/csharp/kit.json +6 -0
- package/kits/csharp/src/Runtime/Client.cs +295 -0
- package/kits/csharp/src/Runtime/Context.cs +216 -0
- package/kits/csharp/src/Runtime/Errors.cs +121 -0
- package/kits/csharp/src/Runtime/Http.cs +218 -0
- package/kits/csharp/src/Runtime/Json.cs +650 -0
- package/kits/csharp/src/Runtime/Lang.cs +212 -0
- package/kits/csharp/src/Runtime/Models.cs +206 -0
- package/kits/csharp/src/Runtime/Multipart.cs +108 -0
- package/kits/csharp/src/Runtime/Pagination.cs +122 -0
- package/kits/csharp/src/Runtime/Sse.cs +197 -0
- package/kits/csharp/src/Runtime/Testing.cs +375 -0
- package/kits/csharp/src/Runtime/Transport.cs +212 -0
- package/kits/csharp/src/Runtime/Wait.cs +57 -0
- package/kits/csharp/src/Runtime/Webhooks.cs +148 -0
- package/kits/csharp/tests/ClientTests.cs +128 -0
- package/kits/csharp/tests/HttpTests.cs +44 -0
- package/kits/csharp/tests/JsonTests.cs +132 -0
- package/kits/csharp/tests/LangTests.cs +57 -0
- package/kits/csharp/tests/MultipartTests.cs +31 -0
- package/kits/csharp/tests/PaginationTests.cs +93 -0
- package/kits/csharp/tests/SseTests.cs +122 -0
- package/kits/csharp/tests/TestRunner.cs +93 -0
- package/kits/csharp/tests/TestingTests.cs +80 -0
- package/kits/csharp/tests/TransportTests.cs +145 -0
- package/kits/csharp/tests/WaitTests.cs +39 -0
- package/kits/csharp/tests/WebhooksTests.cs +46 -0
- package/kits/csharp/versions.json +12 -0
- package/kits/go/kit.json +6 -0
- package/kits/go/src/sdkruntime/client.go +322 -0
- package/kits/go/src/sdkruntime/context.go +207 -0
- package/kits/go/src/sdkruntime/errors.go +74 -0
- package/kits/go/src/sdkruntime/http.go +261 -0
- package/kits/go/src/sdkruntime/json.go +527 -0
- package/kits/go/src/sdkruntime/lang.go +462 -0
- package/kits/go/src/sdkruntime/multipart.go +75 -0
- package/kits/go/src/sdkruntime/pagination.go +150 -0
- package/kits/go/src/sdkruntime/sse.go +267 -0
- package/kits/go/src/sdkruntime/testing.go +343 -0
- package/kits/go/src/sdkruntime/transport.go +168 -0
- package/kits/go/src/sdkruntime/wait.go +49 -0
- package/kits/go/src/sdkruntime/webhooks.go +129 -0
- package/kits/go/tests/client_test.go +185 -0
- package/kits/go/tests/http_test.go +78 -0
- package/kits/go/tests/json_test.go +127 -0
- package/kits/go/tests/lang_test.go +98 -0
- package/kits/go/tests/multipart_test.go +29 -0
- package/kits/go/tests/pagination_test.go +69 -0
- package/kits/go/tests/sse_test.go +129 -0
- package/kits/go/tests/testing_test.go +121 -0
- package/kits/go/tests/transport_test.go +133 -0
- package/kits/go/tests/wait_test.go +46 -0
- package/kits/go/tests/webhooks_test.go +62 -0
- package/kits/go/versions.json +12 -0
- package/kits/java/kit.json +6 -0
- package/kits/java/src/dev/sdkloom/runtime/ApiException.java +63 -0
- package/kits/java/src/dev/sdkloom/runtime/ArgumentException.java +24 -0
- package/kits/java/src/dev/sdkloom/runtime/Assert.java +72 -0
- package/kits/java/src/dev/sdkloom/runtime/ClientRuntime.java +220 -0
- package/kits/java/src/dev/sdkloom/runtime/Clock.java +39 -0
- package/kits/java/src/dev/sdkloom/runtime/Context.java +79 -0
- package/kits/java/src/dev/sdkloom/runtime/Decode.java +200 -0
- package/kits/java/src/dev/sdkloom/runtime/DecodeException.java +26 -0
- package/kits/java/src/dev/sdkloom/runtime/Decoder.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/Emitter.java +56 -0
- package/kits/java/src/dev/sdkloom/runtime/ErrorFactory.java +18 -0
- package/kits/java/src/dev/sdkloom/runtime/EventListener.java +20 -0
- package/kits/java/src/dev/sdkloom/runtime/EventStream.java +232 -0
- package/kits/java/src/dev/sdkloom/runtime/FakeClock.java +37 -0
- package/kits/java/src/dev/sdkloom/runtime/Headers.java +42 -0
- package/kits/java/src/dev/sdkloom/runtime/HttpRequest.java +88 -0
- package/kits/java/src/dev/sdkloom/runtime/HttpResponse.java +69 -0
- package/kits/java/src/dev/sdkloom/runtime/JdkTransport.java +97 -0
- package/kits/java/src/dev/sdkloom/runtime/Js.java +230 -0
- package/kits/java/src/dev/sdkloom/runtime/Json.java +281 -0
- package/kits/java/src/dev/sdkloom/runtime/JsonValue.java +15 -0
- package/kits/java/src/dev/sdkloom/runtime/Links.java +62 -0
- package/kits/java/src/dev/sdkloom/runtime/Middleware.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/MiddlewareEntry.java +43 -0
- package/kits/java/src/dev/sdkloom/runtime/MockRoute.java +111 -0
- package/kits/java/src/dev/sdkloom/runtime/MockTransport.java +62 -0
- package/kits/java/src/dev/sdkloom/runtime/MultipartForm.java +111 -0
- package/kits/java/src/dev/sdkloom/runtime/Next.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationInfo.java +35 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationMock.java +105 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationSpec.java +125 -0
- package/kits/java/src/dev/sdkloom/runtime/Pages.java +55 -0
- package/kits/java/src/dev/sdkloom/runtime/Pipeline.java +29 -0
- package/kits/java/src/dev/sdkloom/runtime/RawResult.java +31 -0
- package/kits/java/src/dev/sdkloom/runtime/RecordedEvent.java +36 -0
- package/kits/java/src/dev/sdkloom/runtime/SdkInfo.java +38 -0
- package/kits/java/src/dev/sdkloom/runtime/SendOptions.java +34 -0
- package/kits/java/src/dev/sdkloom/runtime/ServerSentEvent.java +58 -0
- package/kits/java/src/dev/sdkloom/runtime/State.java +58 -0
- package/kits/java/src/dev/sdkloom/runtime/Test.java +21 -0
- package/kits/java/src/dev/sdkloom/runtime/TestRunner.java +63 -0
- package/kits/java/src/dev/sdkloom/runtime/Transport.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/TransportException.java +30 -0
- package/kits/java/src/dev/sdkloom/runtime/UrlConnectionTransport.java +195 -0
- package/kits/java/src/dev/sdkloom/runtime/Urls.java +116 -0
- package/kits/java/src/dev/sdkloom/runtime/Waits.java +53 -0
- package/kits/java/src/dev/sdkloom/runtime/WebhookSignatureException.java +24 -0
- package/kits/java/src/dev/sdkloom/runtime/Webhooks.java +146 -0
- package/kits/java/tests/dev/sdkloom/runtime/ClientTest.java +266 -0
- package/kits/java/tests/dev/sdkloom/runtime/EventStreamTest.java +131 -0
- package/kits/java/tests/dev/sdkloom/runtime/HttpTest.java +95 -0
- package/kits/java/tests/dev/sdkloom/runtime/JdkTransportTest.java +12 -0
- package/kits/java/tests/dev/sdkloom/runtime/JsTest.java +102 -0
- package/kits/java/tests/dev/sdkloom/runtime/JsonTest.java +87 -0
- package/kits/java/tests/dev/sdkloom/runtime/LinksTest.java +22 -0
- package/kits/java/tests/dev/sdkloom/runtime/MultipartFormTest.java +30 -0
- package/kits/java/tests/dev/sdkloom/runtime/PagesTest.java +75 -0
- package/kits/java/tests/dev/sdkloom/runtime/TestData.java +53 -0
- package/kits/java/tests/dev/sdkloom/runtime/TestingTest.java +156 -0
- package/kits/java/tests/dev/sdkloom/runtime/TransportContract.java +201 -0
- package/kits/java/tests/dev/sdkloom/runtime/UrlConnectionTransportTest.java +49 -0
- package/kits/java/tests/dev/sdkloom/runtime/WaitsTest.java +43 -0
- package/kits/java/tests/dev/sdkloom/runtime/WebhooksTest.java +45 -0
- package/kits/java/versions.json +41 -0
- package/kits/python/kit.json +6 -0
- package/kits/python/src/sdkloom_runtime/__init__.py +1 -0
- package/kits/python/src/sdkloom_runtime/client.py +232 -0
- package/kits/python/src/sdkloom_runtime/compat.py +195 -0
- package/kits/python/src/sdkloom_runtime/context.py +134 -0
- package/kits/python/src/sdkloom_runtime/errors.py +65 -0
- package/kits/python/src/sdkloom_runtime/http.py +192 -0
- package/kits/python/src/sdkloom_runtime/lang.py +207 -0
- package/kits/python/src/sdkloom_runtime/models.py +221 -0
- package/kits/python/src/sdkloom_runtime/multipart.py +69 -0
- package/kits/python/src/sdkloom_runtime/pagination.py +61 -0
- package/kits/python/src/sdkloom_runtime/pipeline.py +39 -0
- package/kits/python/src/sdkloom_runtime/sse.py +121 -0
- package/kits/python/src/sdkloom_runtime/testing.py +224 -0
- package/kits/python/src/sdkloom_runtime/transport.py +120 -0
- package/kits/python/src/sdkloom_runtime/wait.py +44 -0
- package/kits/python/src/sdkloom_runtime/webhooks.py +84 -0
- package/kits/python/tests/support.py +28 -0
- package/kits/python/tests/test_client.py +275 -0
- package/kits/python/tests/test_compat.py +87 -0
- package/kits/python/tests/test_http.py +94 -0
- package/kits/python/tests/test_lang.py +104 -0
- package/kits/python/tests/test_models.py +110 -0
- package/kits/python/tests/test_multipart.py +37 -0
- package/kits/python/tests/test_pagination.py +70 -0
- package/kits/python/tests/test_sse.py +61 -0
- package/kits/python/tests/test_testing.py +130 -0
- package/kits/python/tests/test_transport.py +124 -0
- package/kits/python/tests/test_wait.py +46 -0
- package/kits/python/tests/test_webhooks.py +57 -0
- package/kits/python/versions.json +33 -0
- package/kits/rust/kit.json +6 -0
- package/kits/rust/src/runtime/client.rs +212 -0
- package/kits/rust/src/runtime/context.rs +182 -0
- package/kits/rust/src/runtime/crypto.rs +84 -0
- package/kits/rust/src/runtime/errors.rs +147 -0
- package/kits/rust/src/runtime/http.rs +248 -0
- package/kits/rust/src/runtime/json.rs +424 -0
- package/kits/rust/src/runtime/lang.rs +296 -0
- package/kits/rust/src/runtime/mod.rs +30 -0
- package/kits/rust/src/runtime/models.rs +266 -0
- package/kits/rust/src/runtime/multipart.rs +81 -0
- package/kits/rust/src/runtime/pagination.rs +117 -0
- package/kits/rust/src/runtime/sse.rs +195 -0
- package/kits/rust/src/runtime/testing.rs +308 -0
- package/kits/rust/src/runtime/transport.rs +324 -0
- package/kits/rust/src/runtime/wait.rs +29 -0
- package/kits/rust/src/runtime/webhooks.rs +142 -0
- package/kits/rust/tests/client.rs +152 -0
- package/kits/rust/tests/http.rs +44 -0
- package/kits/rust/tests/json.rs +82 -0
- package/kits/rust/tests/lang.rs +74 -0
- package/kits/rust/tests/multipart.rs +21 -0
- package/kits/rust/tests/pagination.rs +61 -0
- package/kits/rust/tests/sse.rs +77 -0
- package/kits/rust/tests/testing.rs +90 -0
- package/kits/rust/tests/transport.rs +218 -0
- package/kits/rust/tests/wait.rs +32 -0
- package/kits/rust/tests/webhooks.rs +73 -0
- package/kits/rust/versions.json +6 -0
- package/kits/typescript/kit.json +6 -0
- package/kits/typescript/src/client.ts +223 -0
- package/kits/typescript/src/context.ts +108 -0
- package/kits/typescript/src/errors.ts +63 -0
- package/kits/typescript/src/helpers.ts +49 -0
- package/kits/typescript/src/http.ts +192 -0
- package/kits/typescript/src/models.ts +139 -0
- package/kits/typescript/src/multipart.ts +66 -0
- package/kits/typescript/src/pagination.ts +54 -0
- package/kits/typescript/src/pipeline.ts +25 -0
- package/kits/typescript/src/sse.ts +102 -0
- package/kits/typescript/src/testing.ts +217 -0
- package/kits/typescript/src/transport.ts +101 -0
- package/kits/typescript/src/wait.ts +36 -0
- package/kits/typescript/src/webhooks.ts +168 -0
- package/kits/typescript/tests/client.test.ts +269 -0
- package/kits/typescript/tests/helpers.test.ts +27 -0
- package/kits/typescript/tests/http.test.ts +80 -0
- package/kits/typescript/tests/models.test.ts +71 -0
- package/kits/typescript/tests/multipart.test.ts +28 -0
- package/kits/typescript/tests/pagination.test.ts +76 -0
- package/kits/typescript/tests/sse.test.ts +82 -0
- package/kits/typescript/tests/testing.test.ts +103 -0
- package/kits/typescript/tests/transport.test.ts +127 -0
- package/kits/typescript/tests/wait.test.ts +31 -0
- package/kits/typescript/tests/webhooks.test.ts +53 -0
- package/kits/typescript/versions.json +15 -0
- package/package.json +37 -4
- package/packages/cli/recipes/apiKeyHeader.ts +16 -0
- package/packages/cli/recipes/apiVersionHeader.ts +16 -0
- package/packages/cli/recipes/basic.ts +13 -0
- package/packages/cli/recipes/bearer.ts +10 -0
- package/packages/cli/recipes/hmacSign.ts +23 -0
- package/packages/cli/recipes/idempotencyKey.ts +19 -0
- package/packages/cli/recipes/oauthClientCredentials.ts +50 -0
- package/packages/cli/recipes/retry.ts +42 -0
- package/packages/cli/recipes/timing.ts +12 -0
- package/packages/cli/recipes/userAgent.ts +8 -0
- package/packages/cli/src/build.js +93 -0
- package/packages/cli/src/commands/build.js +31 -0
- package/packages/cli/src/commands/conformance-check.js +50 -0
- package/packages/cli/src/commands/contract.js +181 -0
- package/packages/cli/src/commands/diff.js +113 -0
- package/packages/cli/src/commands/doctor.js +43 -0
- package/packages/cli/src/commands/import-openapi.js +42 -0
- package/packages/cli/src/commands/init.js +27 -0
- package/packages/cli/src/commands/kit-check.js +145 -0
- package/packages/cli/src/commands/publish.js +81 -0
- package/packages/cli/src/commands/recipe.js +49 -0
- package/packages/cli/src/commands/record.js +181 -0
- package/packages/cli/src/commands/serve.js +156 -0
- package/packages/cli/src/commands/spec-check.js +57 -0
- package/packages/cli/src/commands/test.js +177 -0
- package/packages/cli/src/commands/variants.js +32 -0
- package/packages/cli/src/commands/verify.js +57 -0
- package/packages/cli/src/commands/version.js +63 -0
- package/packages/cli/src/containers.js +82 -0
- package/packages/cli/src/coverage.js +72 -0
- package/packages/cli/src/git.js +25 -0
- package/packages/cli/src/io.js +50 -0
- package/packages/cli/src/java/ScenarioHarness.java +297 -0
- package/packages/cli/src/java/SchemaRoundTrip.java +63 -0
- package/packages/cli/src/java.js +70 -0
- package/packages/cli/src/js/harness.mjs +120 -0
- package/packages/cli/src/kits.js +103 -0
- package/packages/cli/src/languages/csharp.js +274 -0
- package/packages/cli/src/languages/go.js +185 -0
- package/packages/cli/src/languages/index.js +18 -0
- package/packages/cli/src/languages/java.js +187 -0
- package/packages/cli/src/languages/python.js +120 -0
- package/packages/cli/src/languages/rust.js +189 -0
- package/packages/cli/src/languages/types.js +93 -0
- package/packages/cli/src/languages/typescript.js +112 -0
- package/packages/cli/src/main.js +13 -0
- package/packages/cli/src/packaging.js +39 -0
- package/packages/cli/src/portability.js +45 -0
- package/packages/cli/src/profiles.js +55 -0
- package/packages/cli/src/project.js +56 -0
- package/packages/cli/src/python/scenario_harness.py +162 -0
- package/packages/cli/src/python/schema_roundtrip.py +42 -0
- package/packages/cli/src/registries.js +214 -0
- package/packages/cli/src/resolve-core.js +39 -0
- package/packages/cli/src/run.js +133 -0
- package/packages/cli/src/runtimes.js +61 -0
- package/packages/cli/src/scenarios-java.js +82 -0
- package/packages/cli/src/scenarios-javascript.js +54 -0
- package/packages/cli/src/scenarios-python.js +90 -0
- package/packages/cli/src/scenarios.js +183 -0
- package/packages/cli/src/schema.js +84 -0
- package/packages/cli/src/templates.js +85 -0
- package/packages/cli/src/toolchains.js +68 -0
- package/packages/cli/src/transpiled-tests.js +92 -0
- package/packages/codegen/src/canonical-json.js +31 -0
- package/packages/codegen/src/capabilities.js +50 -0
- package/packages/codegen/src/code-writer.js +54 -0
- package/packages/codegen/src/file-tree.js +25 -0
- package/packages/codegen/src/hash.js +6 -0
- package/packages/codegen/src/index.js +16 -0
- package/packages/codegen/src/lock.js +106 -0
- package/packages/conformance/src/case.js +143 -0
- package/packages/conformance/src/checks.js +40 -0
- package/packages/conformance/src/index.js +3 -0
- package/packages/conformance/src/matrix.js +48 -0
- package/packages/conformance/src/suite.js +48 -0
- package/packages/differ/src/diff.js +500 -0
- package/packages/differ/src/index.js +3 -0
- package/packages/differ/src/notes.js +112 -0
- package/packages/differ/src/support.js +34 -0
- package/packages/gen-csharp/src/emit-client.js +397 -0
- package/packages/gen-csharp/src/emit-harness.js +333 -0
- package/packages/gen-csharp/src/emit-mock.js +111 -0
- package/packages/gen-csharp/src/emit-models.js +119 -0
- package/packages/gen-csharp/src/emit-webhooks.js +57 -0
- package/packages/gen-csharp/src/generate.js +245 -0
- package/packages/gen-csharp/src/index.js +5 -0
- package/packages/gen-csharp/src/names.js +284 -0
- package/packages/gen-go/src/emit-client.js +405 -0
- package/packages/gen-go/src/emit-harness.js +294 -0
- package/packages/gen-go/src/emit-mock.js +112 -0
- package/packages/gen-go/src/emit-models.js +146 -0
- package/packages/gen-go/src/emit-webhooks.js +58 -0
- package/packages/gen-go/src/generate.js +205 -0
- package/packages/gen-go/src/index.js +5 -0
- package/packages/gen-go/src/names.js +197 -0
- package/packages/gen-java/src/emit-client.js +441 -0
- package/packages/gen-java/src/emit-models.js +342 -0
- package/packages/gen-java/src/emit-testing.js +123 -0
- package/packages/gen-java/src/emit-webhooks.js +70 -0
- package/packages/gen-java/src/generate.js +244 -0
- package/packages/gen-java/src/index.js +4 -0
- package/packages/gen-java/src/names.js +197 -0
- package/packages/gen-python/src/emit-client.js +272 -0
- package/packages/gen-python/src/emit-models.js +139 -0
- package/packages/gen-python/src/emit-testing.js +93 -0
- package/packages/gen-python/src/emit-webhooks.js +48 -0
- package/packages/gen-python/src/generate.js +237 -0
- package/packages/gen-python/src/index.js +2 -0
- package/packages/gen-python/src/names.js +161 -0
- package/packages/gen-python/src/style.js +67 -0
- package/packages/gen-rust/src/emit-client.js +379 -0
- package/packages/gen-rust/src/emit-harness.js +278 -0
- package/packages/gen-rust/src/emit-mock.js +121 -0
- package/packages/gen-rust/src/emit-models.js +117 -0
- package/packages/gen-rust/src/emit-webhooks.js +58 -0
- package/packages/gen-rust/src/generate.js +243 -0
- package/packages/gen-rust/src/index.js +5 -0
- package/packages/gen-rust/src/names.js +223 -0
- package/packages/gen-typescript/src/emit-client.js +434 -0
- package/packages/gen-typescript/src/emit-models.js +93 -0
- package/packages/gen-typescript/src/emit-package.js +90 -0
- package/packages/gen-typescript/src/emit-testing.js +138 -0
- package/packages/gen-typescript/src/emit-webhooks.js +52 -0
- package/packages/gen-typescript/src/generate.js +70 -0
- package/packages/gen-typescript/src/index.js +3 -0
- package/packages/gen-typescript/src/naming.js +64 -0
- package/packages/gen-typescript/src/runtime.js +81 -0
- package/packages/gen-typescript/src/types.js +136 -0
- package/packages/ir/src/api-surface.js +22 -0
- package/packages/ir/src/checks.js +52 -0
- package/packages/ir/src/index.js +32 -0
- package/packages/ir/src/ir.js +214 -0
- package/packages/ir/src/pages.js +18 -0
- package/packages/ir/src/validate-ir.js +462 -0
- package/packages/ir/src/validate-types.js +140 -0
- package/packages/kit/src/block-parser.js +196 -0
- package/packages/kit/src/index.js +15 -0
- package/packages/kit/src/kit-check.js +203 -0
- package/packages/kit/src/sources.js +53 -0
- package/packages/kit/src/version-policy.js +133 -0
- package/packages/kit/src/version-range.js +132 -0
- package/packages/openapi/src/import.js +292 -0
- package/packages/openapi/src/index.js +1 -0
- package/packages/openapi/src/names.js +50 -0
- package/packages/openapi/src/schemas.js +264 -0
- package/packages/packager/src/archive.js +106 -0
- package/packages/packager/src/crate.js +39 -0
- package/packages/packager/src/gomodule.js +11 -0
- package/packages/packager/src/index.js +9 -0
- package/packages/packager/src/maven.js +85 -0
- package/packages/packager/src/metadata.js +34 -0
- package/packages/packager/src/npm.js +71 -0
- package/packages/packager/src/nuget.js +78 -0
- package/packages/packager/src/sbom.js +56 -0
- package/packages/packager/src/wheel.js +62 -0
- package/packages/spec/src/index.js +11 -0
- package/packages/spec/src/requirements.js +139 -0
- package/packages/transpile/src/ast.js +51 -0
- package/packages/transpile/src/check.js +669 -0
- package/packages/transpile/src/emit-csharp.js +469 -0
- package/packages/transpile/src/emit-go.js +529 -0
- package/packages/transpile/src/emit-java.js +360 -0
- package/packages/transpile/src/emit-python.js +320 -0
- package/packages/transpile/src/emit-rust.js +499 -0
- package/packages/transpile/src/errors.js +15 -0
- package/packages/transpile/src/index.js +13 -0
- package/packages/transpile/src/lexer.js +161 -0
- package/packages/transpile/src/parser.js +543 -0
- package/packages/transpile/src/tests/ast.js +46 -0
- package/packages/transpile/src/tests/parse.js +376 -0
- package/packages/transpile/src/tests/translate.js +849 -0
- package/packages/transpile/src/transpile.js +57 -0
|
@@ -0,0 +1,529 @@
|
|
|
1
|
+
// Checked subset AST → a Go function. Integers are int64, other numbers float64; values that may be absent and JSON
|
|
2
|
+
// values are `any`. Go has no exceptions: fallible calls go through sdkruntime.Must, which panics with the error, and
|
|
3
|
+
// one recover per middleware (and per try) turns the panic back into an error, so `throw` and try/catch keep their
|
|
4
|
+
// JavaScript meaning.
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
import { inputType, logicArgs, STRING_PARAMS, } from "./check.ts";
|
|
8
|
+
import { TranspileError } from "./errors.ts";
|
|
9
|
+
|
|
10
|
+
const KEYWORDS = new Set([
|
|
11
|
+
"break", "case", "chan", "const", "continue", "default", "defer", "else", "fallthrough", "for", "func", "go", "goto", "if", "import",
|
|
12
|
+
"interface", "map", "package", "range", "return", "select", "struct", "switch", "type", "var",
|
|
13
|
+
// predeclared names the emitted code relies on, and the packages it imports
|
|
14
|
+
"any", "append", "bool", "error", "false", "float64", "int64", "len", "make", "new", "nil", "panic", "recover", "string", "true",
|
|
15
|
+
"math", "rand", "sdkruntime", "result", "err", "done", "value", "recovered",
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
export function goName(name ) {
|
|
19
|
+
return KEYWORDS.has(name) ? `${name}_` : name;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The Go type of a checked type: anything that may be absent, and JSON, is `any`. */
|
|
23
|
+
export function goType(type ) {
|
|
24
|
+
if (type.nullable) return "any";
|
|
25
|
+
switch (type.base) {
|
|
26
|
+
case "int":
|
|
27
|
+
return "int64";
|
|
28
|
+
case "float":
|
|
29
|
+
return "float64";
|
|
30
|
+
case "boolean":
|
|
31
|
+
return "bool";
|
|
32
|
+
case "string":
|
|
33
|
+
return "string";
|
|
34
|
+
case "request":
|
|
35
|
+
return "sdkruntime.Request";
|
|
36
|
+
case "response":
|
|
37
|
+
return "sdkruntime.Response";
|
|
38
|
+
case "map":
|
|
39
|
+
return "map[string]any";
|
|
40
|
+
case "list":
|
|
41
|
+
return "[]any";
|
|
42
|
+
case "error":
|
|
43
|
+
return "error";
|
|
44
|
+
default:
|
|
45
|
+
return "any";
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** sdkruntime helper that turns an `any` into the concrete type, failing like a JavaScript TypeError. */
|
|
50
|
+
const FROM_ANY = {
|
|
51
|
+
int: "sdkruntime.ToInt64",
|
|
52
|
+
float: "sdkruntime.ToFloat64",
|
|
53
|
+
string: "sdkruntime.ToStr",
|
|
54
|
+
boolean: "sdkruntime.ToBool",
|
|
55
|
+
map: "sdkruntime.ToMap",
|
|
56
|
+
list: "sdkruntime.ToList",
|
|
57
|
+
request: "sdkruntime.ToRequest",
|
|
58
|
+
response: "sdkruntime.ToResponse",
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const ZERO_RESPONSE = "sdkruntime.Response{}";
|
|
62
|
+
|
|
63
|
+
class GoEmitter {
|
|
64
|
+
lines = [];
|
|
65
|
+
checked ;
|
|
66
|
+
param ;
|
|
67
|
+
/** How deep in try closures the current statement is; a return there leaves the closure, not the function. */
|
|
68
|
+
tries = 0;
|
|
69
|
+
/** How deep in loops; a break inside a try closure cannot reach its loop. */
|
|
70
|
+
loopsOutsideTry = 0;
|
|
71
|
+
|
|
72
|
+
logicName ;
|
|
73
|
+
|
|
74
|
+
constructor(checked , logicName ) {
|
|
75
|
+
this.checked = checked;
|
|
76
|
+
this.logicName = logicName;
|
|
77
|
+
this.param = (index) => goName(checked.fn.params[index]);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
type(expr ) {
|
|
81
|
+
const type = this.checked.types.get(expr);
|
|
82
|
+
if (type === undefined) throw new Error(`internal: no type for ${expr.kind} at line ${expr.line}`);
|
|
83
|
+
return type;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
line(depth , text ) {
|
|
87
|
+
this.lines.push(`${"\t".repeat(depth)}${text}`);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
run() {
|
|
91
|
+
const fn = this.checked.fn;
|
|
92
|
+
const logic = this.checked.logic;
|
|
93
|
+
if (logic === undefined) {
|
|
94
|
+
this.line(0, `func ${goName(fn.name)}(${this.param(0)} sdkruntime.Request, ${this.param(1)} sdkruntime.Next, ${this.param(2)} sdkruntime.Context) (result sdkruntime.Response, err error) {`);
|
|
95
|
+
this.line(1, "defer sdkruntime.Recover(&err)");
|
|
96
|
+
this.line(1, `_, _, _ = ${this.param(0)}, ${this.param(1)}, ${this.param(2)}`);
|
|
97
|
+
} else {
|
|
98
|
+
const params = logic.input.map((field) => `${goName(field.name)} ${goType(inputType(field))}`).join(", ");
|
|
99
|
+
this.line(0, `func ${goName(fn.name)}(${params}) (result ${goType(logic.returns)}, err error) {`);
|
|
100
|
+
this.line(1, "defer sdkruntime.Recover(&err)");
|
|
101
|
+
}
|
|
102
|
+
this.block(fn.body, 1);
|
|
103
|
+
const last = fn.body.at(-1);
|
|
104
|
+
if (last?.kind !== "return" && last?.kind !== "throw") this.line(1, 'panic("sdkloom: the middleware ended without a return")');
|
|
105
|
+
this.line(0, "}");
|
|
106
|
+
return `${this.lines.join("\n")}\n`;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
block(statements , depth ) {
|
|
110
|
+
statements.forEach((statement) => this.statement(statement, depth));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
declareVariable(statement , depth ) {
|
|
114
|
+
const type = this.checked.declarations.get(statement);
|
|
115
|
+
if (type === undefined) throw new Error("internal: undeclared variable");
|
|
116
|
+
const name = goName(statement.name);
|
|
117
|
+
this.line(depth, `var ${name} ${goType(type)} = ${this.coerce(statement.init, type)}`);
|
|
118
|
+
// JavaScript allows a variable nobody reads; Go does not.
|
|
119
|
+
this.line(depth, `_ = ${name}`);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** A statement as it appears in a for header (Go only allows simple statements there). */
|
|
123
|
+
simple(statement ) {
|
|
124
|
+
if (statement.kind === "assign") {
|
|
125
|
+
const target = this.variableType(statement.name, statement.line);
|
|
126
|
+
const op = statement.op;
|
|
127
|
+
if (op === "=") return `${goName(statement.name)} = ${this.coerce(statement.value, target)}`;
|
|
128
|
+
if (target.base === "string") return `${goName(statement.name)} += ${this.text(statement.value)}`;
|
|
129
|
+
return `${goName(statement.name)} ${op} ${this.coerce(statement.value, target)}`;
|
|
130
|
+
}
|
|
131
|
+
if (statement.kind === "expression") return this.expr(statement.value);
|
|
132
|
+
throw new Error(`internal: ${statement.kind} in a for header`);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
variableType(name , line ) {
|
|
136
|
+
for (const [statement, type] of this.checked.declarations) {
|
|
137
|
+
if ((statement.kind === "let" || statement.kind === "forOf") && statement.name === name) return type;
|
|
138
|
+
}
|
|
139
|
+
throw new Error(`internal: unknown variable ${name} at line ${line}`);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
returnValue(value ) {
|
|
143
|
+
return this.tries > 0 ? `return true, ${value}` : `return ${value}, nil`;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
statement(statement , depth ) {
|
|
147
|
+
switch (statement.kind) {
|
|
148
|
+
case "let":
|
|
149
|
+
return this.declareVariable(statement, depth);
|
|
150
|
+
case "assign":
|
|
151
|
+
case "expression":
|
|
152
|
+
return this.line(depth, statement.kind === "expression" && this.isValue(statement.value) ? `_ = ${this.simple(statement)}` : this.simple(statement));
|
|
153
|
+
case "if":
|
|
154
|
+
return this.ifStatement(statement, depth);
|
|
155
|
+
case "for": {
|
|
156
|
+
this.line(depth, "{");
|
|
157
|
+
if (statement.init?.kind === "let") this.declareVariable(statement.init, depth + 1);
|
|
158
|
+
else if (statement.init !== undefined) this.line(depth + 1, this.simple(statement.init));
|
|
159
|
+
const update = statement.update === undefined ? "" : this.simple(statement.update);
|
|
160
|
+
this.line(depth + 1, `for ; ${this.expr(statement.test)}; ${update} {`);
|
|
161
|
+
this.loop(statement.body, depth + 2);
|
|
162
|
+
this.line(depth + 1, "}");
|
|
163
|
+
return this.line(depth, "}");
|
|
164
|
+
}
|
|
165
|
+
case "forOf":
|
|
166
|
+
this.line(depth, `for _, ${goName(statement.name)} := range sdkruntime.Iterate(${this.expr(statement.iterable)}) {`);
|
|
167
|
+
this.line(depth + 1, `_ = ${goName(statement.name)}`);
|
|
168
|
+
this.loop(statement.body, depth + 1);
|
|
169
|
+
return this.line(depth, "}");
|
|
170
|
+
case "while":
|
|
171
|
+
this.line(depth, `for ${this.expr(statement.test)} {`);
|
|
172
|
+
this.loop(statement.body, depth + 1);
|
|
173
|
+
return this.line(depth, "}");
|
|
174
|
+
case "break":
|
|
175
|
+
if (this.loopsOutsideTry === 0) throw new TranspileError(this.checked.fn.name, statement.line, "Go cannot break out of a loop from inside try; move the loop into the try", "SL-T002");
|
|
176
|
+
return this.line(depth, "break");
|
|
177
|
+
case "return":
|
|
178
|
+
return this.line(depth, this.returnValue(this.coerce(statement.value, this.checked.logic?.returns ?? { base: "response", nullable: false })));
|
|
179
|
+
case "throw":
|
|
180
|
+
return this.line(depth, `panic(sdkruntime.Throw(${this.text(statement.message)}))`);
|
|
181
|
+
case "try":
|
|
182
|
+
return this.tryStatement(statement, depth);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
loop(body , depth ) {
|
|
187
|
+
this.loopsOutsideTry += 1;
|
|
188
|
+
this.block(body, depth);
|
|
189
|
+
this.loopsOutsideTry -= 1;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Calls whose result Go would refuse to drop silently are assigned to _. */
|
|
193
|
+
isValue(expr ) {
|
|
194
|
+
const inner = expr.kind === "await" ? expr.value : expr;
|
|
195
|
+
if (inner.kind !== "call") return true;
|
|
196
|
+
const builtin = this.checked.builtins.get(inner);
|
|
197
|
+
return !["ctx.clock.sleep", "ctx.state.set", "ctx.state.delete", "ctx.emit"].includes(builtin ?? "");
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
ifStatement(statement , depth ) {
|
|
201
|
+
this.line(depth, `if ${this.expr(statement.test)} {`);
|
|
202
|
+
this.block(statement.then, depth + 1);
|
|
203
|
+
let otherwise = statement.otherwise;
|
|
204
|
+
while (otherwise !== undefined && otherwise.length === 1 && otherwise[0]?.kind === "if") {
|
|
205
|
+
const nested = otherwise[0];
|
|
206
|
+
this.line(depth, `} else if ${this.expr(nested.test)} {`);
|
|
207
|
+
this.block(nested.then, depth + 1);
|
|
208
|
+
otherwise = nested.otherwise;
|
|
209
|
+
}
|
|
210
|
+
if (otherwise !== undefined) {
|
|
211
|
+
this.line(depth, "} else {");
|
|
212
|
+
this.block(otherwise, depth + 1);
|
|
213
|
+
}
|
|
214
|
+
this.line(depth, "}");
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** The zero value of the function's result, for paths of a try closure that do not return. */
|
|
218
|
+
zeroResult() {
|
|
219
|
+
const base = this.checked.logic?.returns.base;
|
|
220
|
+
if (base === undefined) return ZERO_RESPONSE;
|
|
221
|
+
return base === "string" ? '""' : base === "boolean" ? "false" : "0";
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** try { … } catch (e) { … } as a closure whose recover runs the handler; returns inside either leave the function. */
|
|
225
|
+
tryStatement(statement , depth ) {
|
|
226
|
+
const saved = this.loopsOutsideTry;
|
|
227
|
+
this.loopsOutsideTry = 0;
|
|
228
|
+
const outer = this.tries;
|
|
229
|
+
this.tries = outer + 1;
|
|
230
|
+
const result = this.checked.logic === undefined ? "sdkruntime.Response" : goType(this.checked.logic.returns);
|
|
231
|
+
const zero = this.zeroResult();
|
|
232
|
+
this.line(depth, `if done, value := func() (done bool, value ${result}) {`);
|
|
233
|
+
this.line(depth + 1, "defer func() {");
|
|
234
|
+
this.line(depth + 2, "if recovered := recover(); recovered != nil {");
|
|
235
|
+
this.line(depth + 3, `${goName(statement.param)} := sdkruntime.Caught(recovered)`);
|
|
236
|
+
this.line(depth + 3, `_ = ${goName(statement.param)}`);
|
|
237
|
+
this.line(depth + 3, `done, value = func() (bool, ${result}) {`);
|
|
238
|
+
this.block(statement.handler, depth + 4);
|
|
239
|
+
this.line(depth + 4, `return false, ${zero}`);
|
|
240
|
+
this.line(depth + 3, "}()");
|
|
241
|
+
this.line(depth + 2, "}");
|
|
242
|
+
this.line(depth + 1, "}()");
|
|
243
|
+
this.block(statement.body, depth + 1);
|
|
244
|
+
this.line(depth + 1, `return false, ${zero}`);
|
|
245
|
+
this.tries = outer;
|
|
246
|
+
this.loopsOutsideTry = saved;
|
|
247
|
+
this.line(depth, "}(); done {");
|
|
248
|
+
this.line(depth + 1, this.returnValue("value"));
|
|
249
|
+
this.line(depth, "}");
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** An expression for a slot of the given type. */
|
|
253
|
+
coerce(expr , target ) {
|
|
254
|
+
const actual = this.type(expr);
|
|
255
|
+
const value = this.expr(expr);
|
|
256
|
+
if (expr.kind === "null") return target.nullable || goType(target) === "any" ? "nil" : value;
|
|
257
|
+
const from = goType(actual);
|
|
258
|
+
const to = goType(target);
|
|
259
|
+
if (from === to) return value;
|
|
260
|
+
if (to === "any") return value;
|
|
261
|
+
if (from === "any") return `${FROM_ANY[target.base] ?? "sdkruntime.ToAny"}(${value})`;
|
|
262
|
+
if (from === "int64" && to === "float64") return `float64(${value})`;
|
|
263
|
+
return value;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** A number as float64, whatever its type. */
|
|
267
|
+
float(expr ) {
|
|
268
|
+
return this.coerce(expr, { base: "float", nullable: false });
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
text(expr ) {
|
|
272
|
+
const type = this.type(expr);
|
|
273
|
+
return type.base === "string" && !type.nullable ? this.expr(expr) : `sdkruntime.JsStr(${this.expr(expr)})`;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
args(expr , builtin ) {
|
|
277
|
+
const strings = STRING_PARAMS[builtin] ?? [];
|
|
278
|
+
return expr.args.map((arg, index) => (strings.includes(index) ? this.text(arg) : this.expr(arg)));
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** A typed immediately-called function: Go has no conditional expression. */
|
|
282
|
+
choose(test , type , then , otherwise ) {
|
|
283
|
+
return `func() ${goType(type)} {\n\t\tif ${test} {\n\t\t\treturn ${then}\n\t\t}\n\t\treturn ${otherwise}\n\t}()`;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
expr(expr ) {
|
|
287
|
+
switch (expr.kind) {
|
|
288
|
+
case "number":
|
|
289
|
+
if (expr.integer) return `int64(${expr.value})`;
|
|
290
|
+
return /[.eE]/.test(String(expr.value)) ? `float64(${expr.value})` : `float64(${expr.value}.0)`;
|
|
291
|
+
case "string":
|
|
292
|
+
return JSON.stringify(expr.value).replaceAll("\u2028", "\\u2028").replaceAll("\u2029", "\\u2029");
|
|
293
|
+
case "template": {
|
|
294
|
+
const parts = expr.parts.map((part) => (typeof part === "string" ? this.expr({ kind: "string", line: expr.line, value: part }) : this.text(part)));
|
|
295
|
+
return parts.length === 0 ? '""' : `(${parts.join(" + ")})`;
|
|
296
|
+
}
|
|
297
|
+
case "boolean":
|
|
298
|
+
return String(expr.value);
|
|
299
|
+
case "null":
|
|
300
|
+
return "nil";
|
|
301
|
+
case "name":
|
|
302
|
+
return goName(expr.name);
|
|
303
|
+
case "member":
|
|
304
|
+
return this.member(expr);
|
|
305
|
+
case "call":
|
|
306
|
+
return this.call(expr);
|
|
307
|
+
case "await":
|
|
308
|
+
return this.expr(expr.value);
|
|
309
|
+
case "unary":
|
|
310
|
+
return expr.op === "!" ? `(!${this.expr(expr.value)})` : `(-${this.expr(expr.value)})`;
|
|
311
|
+
case "binary":
|
|
312
|
+
return this.binary(expr);
|
|
313
|
+
case "logical": {
|
|
314
|
+
if (expr.op !== "??") return `(${this.expr(expr.left)} ${expr.op} ${this.expr(expr.right)})`;
|
|
315
|
+
const type = this.type(expr);
|
|
316
|
+
const left = this.expr(expr.left);
|
|
317
|
+
return this.choose(`${left} != nil`, type, this.coerceText(left, this.type(expr.left), type), this.coerce(expr.right, type));
|
|
318
|
+
}
|
|
319
|
+
case "conditional": {
|
|
320
|
+
const type = this.type(expr);
|
|
321
|
+
return this.choose(this.expr(expr.test), type, this.coerce(expr.then, type), this.coerce(expr.otherwise, type));
|
|
322
|
+
}
|
|
323
|
+
case "object":
|
|
324
|
+
return `map[string]any{${expr.entries.map((entry) => `${JSON.stringify(entry.key)}: ${this.expr(entry.value)}`).join(", ")}}`;
|
|
325
|
+
case "array":
|
|
326
|
+
return `[]any{${expr.items.map((item) => this.expr(item)).join(", ")}}`;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** Like coerce, for an already emitted expression (the left side of ??, read twice). */
|
|
331
|
+
coerceText(value , actual , target ) {
|
|
332
|
+
const from = goType(actual);
|
|
333
|
+
const to = goType(target);
|
|
334
|
+
if (from === to || to === "any") return value;
|
|
335
|
+
if (from === "any") return `${FROM_ANY[target.base] ?? "sdkruntime.ToAny"}(${value})`;
|
|
336
|
+
if (from === "int64" && to === "float64") return `float64(${value})`;
|
|
337
|
+
return value;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
numeric(type ) {
|
|
341
|
+
return !type.nullable && (type.base === "int" || type.base === "float");
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
binary(expr ) {
|
|
345
|
+
const left = this.type(expr.left);
|
|
346
|
+
const right = this.type(expr.right);
|
|
347
|
+
const a = this.expr(expr.left);
|
|
348
|
+
const b = this.expr(expr.right);
|
|
349
|
+
const floats = left.base === "float" || right.base === "float";
|
|
350
|
+
const number = (side ) => (floats ? this.float(side) : this.expr(side));
|
|
351
|
+
switch (expr.op) {
|
|
352
|
+
case "===":
|
|
353
|
+
case "!==":
|
|
354
|
+
return this.equality(expr, left, right, a, b);
|
|
355
|
+
case "+":
|
|
356
|
+
if (left.base === "string" || right.base === "string") return `(${this.text(expr.left)} + ${this.text(expr.right)})`;
|
|
357
|
+
return `(${number(expr.left)} + ${number(expr.right)})`;
|
|
358
|
+
case "/":
|
|
359
|
+
return `sdkruntime.JsDiv(${this.float(expr.left)}, ${this.float(expr.right)})`;
|
|
360
|
+
case "**":
|
|
361
|
+
return `sdkruntime.JsPow(${this.float(expr.left)}, ${this.float(expr.right)})`;
|
|
362
|
+
case "%":
|
|
363
|
+
return floats ? `sdkruntime.JsModFloat(${this.float(expr.left)}, ${this.float(expr.right)})` : `(${a} % ${b})`;
|
|
364
|
+
case "<":
|
|
365
|
+
case "<=":
|
|
366
|
+
case ">":
|
|
367
|
+
case ">=":
|
|
368
|
+
if (left.base === "string" && right.base === "string") return `(${a} ${expr.op} ${b})`;
|
|
369
|
+
return `(${number(expr.left)} ${expr.op} ${number(expr.right)})`;
|
|
370
|
+
default:
|
|
371
|
+
return `(${number(expr.left)} ${expr.op} ${number(expr.right)})`;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
equality(expr , left , right , a , b ) {
|
|
376
|
+
const equal = expr.op === "===";
|
|
377
|
+
const other = expr.right.kind === "null" ? { type: left, code: a } : expr.left.kind === "null" ? { type: right, code: b } : undefined;
|
|
378
|
+
if (other !== undefined) {
|
|
379
|
+
// A value Go knows is present is never null.
|
|
380
|
+
if (goType(other.type) !== "any") return equal ? "false" : "true";
|
|
381
|
+
return `(${other.code} ${equal ? "==" : "!="} nil)`;
|
|
382
|
+
}
|
|
383
|
+
const plain = (type ) => this.numeric(type) || (!type.nullable && (type.base === "boolean" || type.base === "string"));
|
|
384
|
+
if (plain(left) && plain(right) && (left.base === right.base || (this.numeric(left) && this.numeric(right)))) {
|
|
385
|
+
const floats = left.base === "float" || right.base === "float";
|
|
386
|
+
const side = (code ) => (floats ? this.float(code) : this.expr(code));
|
|
387
|
+
return `(${side(expr.left)} ${equal ? "==" : "!="} ${side(expr.right)})`;
|
|
388
|
+
}
|
|
389
|
+
return `(${equal ? "" : "!"}sdkruntime.JsSame(${a}, ${b}))`;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
member(expr ) {
|
|
393
|
+
const builtin = this.checked.builtins.get(expr);
|
|
394
|
+
const ctx = this.param(2);
|
|
395
|
+
switch (builtin) {
|
|
396
|
+
case "input":
|
|
397
|
+
return goName(this.checked.configFields.get(expr)?.name ?? expr.name);
|
|
398
|
+
case "ctx.config": {
|
|
399
|
+
const field = this.checked.configFields.get(expr);
|
|
400
|
+
const read = `${ctx}.Config[${JSON.stringify(field?.name ?? expr.name)}]`;
|
|
401
|
+
const type = this.type(expr);
|
|
402
|
+
return goType(type) === "any" ? read : `${FROM_ANY[type.base] ?? "sdkruntime.ToAny"}(${read})`;
|
|
403
|
+
}
|
|
404
|
+
case "ctx.operation.name":
|
|
405
|
+
return `${ctx}.Operation.Name`;
|
|
406
|
+
case "ctx.operation.method":
|
|
407
|
+
return `${ctx}.Operation.Method`;
|
|
408
|
+
case "ctx.operation.path":
|
|
409
|
+
return `${ctx}.Operation.Path`;
|
|
410
|
+
case "ctx.sdk.name":
|
|
411
|
+
return `${ctx}.SDK.Name`;
|
|
412
|
+
case "ctx.sdk.version":
|
|
413
|
+
return `${ctx}.SDK.Version`;
|
|
414
|
+
case "ctx.sdk.language":
|
|
415
|
+
return `${ctx}.SDK.Language`;
|
|
416
|
+
case "req.method":
|
|
417
|
+
return `${this.expr(expr.object)}.Method`;
|
|
418
|
+
case "req.url":
|
|
419
|
+
return `${this.expr(expr.object)}.URL`;
|
|
420
|
+
case "res.status":
|
|
421
|
+
return `int64(${this.expr(expr.object)}.Status)`;
|
|
422
|
+
case "json.get":
|
|
423
|
+
return `sdkruntime.Must(sdkruntime.JsGet(${this.expr(expr.object)}, ${JSON.stringify(expr.name)}))`;
|
|
424
|
+
case "error.message":
|
|
425
|
+
return `sdkruntime.JsMessage(${this.expr(expr.object)})`;
|
|
426
|
+
default:
|
|
427
|
+
throw new Error(`internal: unresolved member ${expr.name} at line ${expr.line}`);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
call(expr ) {
|
|
432
|
+
const builtin = this.checked.builtins.get(expr);
|
|
433
|
+
if (builtin === undefined) throw new Error(`internal: unresolved call at line ${expr.line}`);
|
|
434
|
+
if (builtin === "logic") return `sdkruntime.Must(${this.logicName(this.checked.logicCalls.get(expr)?.name ?? "")}(${logicArgs(this.checked, expr).map(({ value, type }) => this.coerce(value, type)).join(", ")}))`;
|
|
435
|
+
const args = this.args(expr, builtin);
|
|
436
|
+
const ctx = this.param(2);
|
|
437
|
+
const callee = expr.callee;
|
|
438
|
+
const target = () => (callee.kind === "member" ? this.expr(callee.object) : "");
|
|
439
|
+
// Milliseconds and TTLs are whole numbers in every target; a fraction is dropped (Java does the same).
|
|
440
|
+
const int = (arg ) => {
|
|
441
|
+
if (arg === undefined) return "0";
|
|
442
|
+
const type = this.type(arg);
|
|
443
|
+
return type.base === "float" && !type.nullable ? `int64(${this.expr(arg)})` : this.coerce(arg, { base: "int", nullable: false });
|
|
444
|
+
};
|
|
445
|
+
switch (builtin) {
|
|
446
|
+
case "next":
|
|
447
|
+
return `sdkruntime.Must(${this.param(1)}(${this.coerce(expr.args[0] ?? expr, { base: "request", nullable: false })}))`;
|
|
448
|
+
case "req.header":
|
|
449
|
+
case "res.header":
|
|
450
|
+
return `sdkruntime.FromPtr(${target()}.Header(${args[0] ?? ""}))`;
|
|
451
|
+
case "req.withHeader":
|
|
452
|
+
return `${target()}.WithHeader(${args.join(", ")})`;
|
|
453
|
+
case "req.withoutHeader":
|
|
454
|
+
return `${target()}.WithoutHeader(${args.join(", ")})`;
|
|
455
|
+
case "req.withUrl":
|
|
456
|
+
return `${target()}.WithURL(${args.join(", ")})`;
|
|
457
|
+
case "req.withBody":
|
|
458
|
+
return `${target()}.WithBody([]byte(${args[0] ?? '""'}))`;
|
|
459
|
+
case "req.text":
|
|
460
|
+
return `${target()}.Text()`;
|
|
461
|
+
case "res.text":
|
|
462
|
+
return `${target()}.Text()`;
|
|
463
|
+
case "res.json":
|
|
464
|
+
return `sdkruntime.Must(${target()}.JSON())`;
|
|
465
|
+
case "ctx.clock.now":
|
|
466
|
+
return `${ctx}.Clock.Now()`;
|
|
467
|
+
case "ctx.clock.sleep":
|
|
468
|
+
return `${ctx}.Clock.Sleep(${int(expr.args[0])})`;
|
|
469
|
+
case "ctx.state.get":
|
|
470
|
+
return `${ctx}.State.Get(${args.join(", ")})`;
|
|
471
|
+
case "ctx.state.set": {
|
|
472
|
+
const options = expr.args[2];
|
|
473
|
+
const ttl = options?.kind === "object" ? options.entries[0]?.value : undefined;
|
|
474
|
+
return `${ctx}.State.Set(${args.slice(0, 2).join(", ")}${ttl === undefined ? "" : `, ${int(ttl)}`})`;
|
|
475
|
+
}
|
|
476
|
+
case "ctx.state.delete":
|
|
477
|
+
return `${ctx}.State.Delete(${args.join(", ")})`;
|
|
478
|
+
case "ctx.emit": {
|
|
479
|
+
const fields = expr.args[1];
|
|
480
|
+
const value = fields !== undefined && this.type(fields).base === "json" ? `sdkruntime.ToMap(${args[1] ?? ""})` : (args[1] ?? "nil");
|
|
481
|
+
return `${ctx}.Emit(${args[0] ?? ""}, ${value})`;
|
|
482
|
+
}
|
|
483
|
+
case "ctx.request":
|
|
484
|
+
return `${ctx}.Request(${args.join(", ")})`;
|
|
485
|
+
case "ctx.send":
|
|
486
|
+
return `sdkruntime.Must(${ctx}.Send(${this.coerce(expr.args[0] ?? expr, { base: "request", nullable: false })}))`;
|
|
487
|
+
case "Math.min":
|
|
488
|
+
case "Math.max":
|
|
489
|
+
return this.minMax(expr, builtin === "Math.min" ? "Min" : "Max");
|
|
490
|
+
case "Math.floor":
|
|
491
|
+
return `int64(math.Floor(${this.float(expr.args[0] ?? expr)}))`;
|
|
492
|
+
case "Math.ceil":
|
|
493
|
+
return `int64(math.Ceil(${this.float(expr.args[0] ?? expr)}))`;
|
|
494
|
+
case "Math.abs":
|
|
495
|
+
return this.type(expr).base === "int" ? `sdkruntime.AbsInt(${args[0] ?? ""})` : `math.Abs(${this.float(expr.args[0] ?? expr)})`;
|
|
496
|
+
case "Math.random":
|
|
497
|
+
return "rand.Float64()";
|
|
498
|
+
case "String":
|
|
499
|
+
return `sdkruntime.JsStr(${args.join(", ")})`;
|
|
500
|
+
case "Number":
|
|
501
|
+
return `sdkruntime.JsNumber(${args.join(", ")})`;
|
|
502
|
+
case "btoa":
|
|
503
|
+
return `sdkruntime.Must(sdkruntime.JsBtoa(${args.join(", ")}))`;
|
|
504
|
+
case "encodeURIComponent":
|
|
505
|
+
return `sdkruntime.PathSegment(${args.join(", ")})`;
|
|
506
|
+
case "JSON.parse":
|
|
507
|
+
return `sdkruntime.Must(sdkruntime.ParseJSON([]byte(${args.join(", ")})))`;
|
|
508
|
+
case "randomUUID":
|
|
509
|
+
return "sdkruntime.RandomUUID()";
|
|
510
|
+
case "hmacSha256Hex":
|
|
511
|
+
return `sdkruntime.HmacSha256Hex(${args.join(", ")})`;
|
|
512
|
+
case "hmacSha256Base64":
|
|
513
|
+
return `sdkruntime.HmacSha256Base64(${args.join(", ")})`;
|
|
514
|
+
default:
|
|
515
|
+
throw new Error(`internal: ${builtin} is not a call`);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
minMax(expr , name ) {
|
|
520
|
+
const floats = this.type(expr).base === "float";
|
|
521
|
+
const items = expr.args.map((arg) => (floats ? this.float(arg) : this.coerce(arg, { base: "int", nullable: false })));
|
|
522
|
+
return items.slice(1).reduce((acc, item) => `sdkruntime.${name}(${acc}, ${item})`, items[0] ?? "0");
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/** The Go source of one middleware: `func name(req sdkruntime.Request, next sdkruntime.Next, ctx sdkruntime.Context) (sdkruntime.Response, error)`. */
|
|
527
|
+
export function emitGo(checked , logicName = (name) => name) {
|
|
528
|
+
return new GoEmitter(checked, logicName).run();
|
|
529
|
+
}
|