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,360 @@
|
|
|
1
|
+
// Checked subset AST → a static Java method. Integers are long, other numbers double; JSON values are Object.
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
import { inputType, logicArgs, STRING_PARAMS, } from "./check.ts";
|
|
5
|
+
|
|
6
|
+
const KEYWORDS = new Set([
|
|
7
|
+
"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum",
|
|
8
|
+
"extends", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new",
|
|
9
|
+
"package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw",
|
|
10
|
+
"throws", "transient", "try", "void", "volatile", "while", "true", "false", "null", "var", "record", "yield", "Js", "Math", "String",
|
|
11
|
+
]);
|
|
12
|
+
|
|
13
|
+
export function javaName(name ) {
|
|
14
|
+
return KEYWORDS.has(name) ? `${name}_` : name;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The Java type of a checked type. Nullable numbers and booleans are boxed. */
|
|
18
|
+
export function javaType(type ) {
|
|
19
|
+
switch (type.base) {
|
|
20
|
+
case "int":
|
|
21
|
+
return type.nullable ? "Long" : "long";
|
|
22
|
+
case "float":
|
|
23
|
+
return type.nullable ? "Double" : "double";
|
|
24
|
+
case "boolean":
|
|
25
|
+
return type.nullable ? "Boolean" : "boolean";
|
|
26
|
+
case "string":
|
|
27
|
+
return "String";
|
|
28
|
+
case "request":
|
|
29
|
+
return "HttpRequest";
|
|
30
|
+
case "response":
|
|
31
|
+
return "HttpResponse";
|
|
32
|
+
case "map":
|
|
33
|
+
return "Map<String, Object>";
|
|
34
|
+
case "list":
|
|
35
|
+
return "List<Object>";
|
|
36
|
+
case "error":
|
|
37
|
+
return "RuntimeException";
|
|
38
|
+
default:
|
|
39
|
+
return "Object";
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const CAST = { int: "Long", float: "Double", boolean: "Boolean", string: "String" };
|
|
44
|
+
|
|
45
|
+
class JavaEmitter {
|
|
46
|
+
lines = [];
|
|
47
|
+
checked ;
|
|
48
|
+
param ;
|
|
49
|
+
|
|
50
|
+
logicName ;
|
|
51
|
+
|
|
52
|
+
constructor(checked , logicName ) {
|
|
53
|
+
this.checked = checked;
|
|
54
|
+
this.logicName = logicName;
|
|
55
|
+
this.param = (index) => javaName(checked.fn.params[index]);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
type(expr ) {
|
|
59
|
+
const type = this.checked.types.get(expr);
|
|
60
|
+
if (type === undefined) throw new Error(`internal: no type for ${expr.kind} at line ${expr.line}`);
|
|
61
|
+
return type;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
line(depth , text ) {
|
|
65
|
+
this.lines.push(`${" ".repeat(depth)}${text}`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
run() {
|
|
69
|
+
const logic = this.checked.logic;
|
|
70
|
+
if (logic === undefined) {
|
|
71
|
+
this.line(0, `static HttpResponse ${javaName(this.checked.fn.name)}(HttpRequest ${this.param(0)}, Next ${this.param(1)}, Context ${this.param(2)}) {`);
|
|
72
|
+
} else {
|
|
73
|
+
const params = logic.input.map((field) => `${javaType(inputType(field))} ${javaName(field.name)}`).join(", ");
|
|
74
|
+
this.line(0, `static ${javaType(logic.returns)} ${javaName(this.checked.fn.name)}(${params}) {`);
|
|
75
|
+
}
|
|
76
|
+
this.block(this.checked.fn.body, 1);
|
|
77
|
+
this.line(0, "}");
|
|
78
|
+
return `${this.lines.join("\n")}\n`;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
block(statements , depth ) {
|
|
82
|
+
statements.forEach((statement) => this.statement(statement, depth));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** A statement as it appears in a for header: no trailing semicolon. */
|
|
86
|
+
simple(statement ) {
|
|
87
|
+
if (statement.kind === "let") {
|
|
88
|
+
const type = this.checked.declarations.get(statement);
|
|
89
|
+
if (type === undefined) throw new Error("internal: undeclared variable");
|
|
90
|
+
return `${statement.constant ? "final " : ""}${javaType(type)} ${javaName(statement.name)} = ${this.coerce(statement.init, type)}`;
|
|
91
|
+
}
|
|
92
|
+
if (statement.kind === "assign") return `${javaName(statement.name)} ${statement.op} ${this.expr(statement.value)}`;
|
|
93
|
+
if (statement.kind === "expression") return this.expr(statement.value);
|
|
94
|
+
throw new Error(`internal: ${statement.kind} in a for header`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
statement(statement , depth ) {
|
|
98
|
+
switch (statement.kind) {
|
|
99
|
+
case "let":
|
|
100
|
+
case "assign":
|
|
101
|
+
case "expression":
|
|
102
|
+
return this.line(depth, `${this.simple(statement)};`);
|
|
103
|
+
case "if": {
|
|
104
|
+
this.line(depth, `if (${this.expr(statement.test)}) {`);
|
|
105
|
+
this.block(statement.then, depth + 1);
|
|
106
|
+
let otherwise = statement.otherwise;
|
|
107
|
+
while (otherwise !== undefined && otherwise.length === 1 && otherwise[0]?.kind === "if") {
|
|
108
|
+
const nested = otherwise[0];
|
|
109
|
+
this.line(depth, `} else if (${this.expr(nested.test)}) {`);
|
|
110
|
+
this.block(nested.then, depth + 1);
|
|
111
|
+
otherwise = nested.otherwise;
|
|
112
|
+
}
|
|
113
|
+
if (otherwise !== undefined) {
|
|
114
|
+
this.line(depth, "} else {");
|
|
115
|
+
this.block(otherwise, depth + 1);
|
|
116
|
+
}
|
|
117
|
+
return this.line(depth, "}");
|
|
118
|
+
}
|
|
119
|
+
case "for": {
|
|
120
|
+
const init = statement.init === undefined ? "" : this.simple(statement.init);
|
|
121
|
+
const update = statement.update === undefined ? "" : this.simple(statement.update);
|
|
122
|
+
this.line(depth, `for (${init}; ${this.expr(statement.test)}; ${update}) {`);
|
|
123
|
+
this.block(statement.body, depth + 1);
|
|
124
|
+
return this.line(depth, "}");
|
|
125
|
+
}
|
|
126
|
+
case "forOf":
|
|
127
|
+
this.line(depth, `for (Object ${javaName(statement.name)} : Js.iterate(${this.expr(statement.iterable)})) {`);
|
|
128
|
+
this.block(statement.body, depth + 1);
|
|
129
|
+
return this.line(depth, "}");
|
|
130
|
+
case "while":
|
|
131
|
+
this.line(depth, `while (${this.expr(statement.test)}) {`);
|
|
132
|
+
this.block(statement.body, depth + 1);
|
|
133
|
+
return this.line(depth, "}");
|
|
134
|
+
case "break":
|
|
135
|
+
return this.line(depth, "break;");
|
|
136
|
+
case "return":
|
|
137
|
+
return this.line(depth, `return ${this.checked.logic === undefined ? this.expr(statement.value) : this.coerce(statement.value, this.checked.logic.returns)};`);
|
|
138
|
+
case "throw":
|
|
139
|
+
return this.line(depth, `throw new IllegalStateException(${this.expr(statement.message)});`);
|
|
140
|
+
case "try":
|
|
141
|
+
this.line(depth, "try {");
|
|
142
|
+
this.block(statement.body, depth + 1);
|
|
143
|
+
this.line(depth, `} catch (RuntimeException ${javaName(statement.param)}) {`);
|
|
144
|
+
this.block(statement.handler, depth + 1);
|
|
145
|
+
return this.line(depth, "}");
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** An expression for a slot of the given type: JSON values are cast, integers widen to double. */
|
|
150
|
+
coerce(expr , target ) {
|
|
151
|
+
const actual = this.type(expr);
|
|
152
|
+
const value = this.expr(expr);
|
|
153
|
+
if (actual.base === "json" && target.base !== "json") return `((${CAST[target.base] ?? javaType(target)}) ${value})`;
|
|
154
|
+
if (actual.base === "int" && target.base === "float") return `((double) ${value})`;
|
|
155
|
+
return value;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
text(expr ) {
|
|
159
|
+
const type = this.type(expr);
|
|
160
|
+
return type.base === "string" ? this.expr(expr) : `Js.str(${this.expr(expr)})`;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
long(expr ) {
|
|
164
|
+
return this.type(expr).base === "int" ? this.expr(expr) : `((long) ${this.expr(expr)})`;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
args(expr , builtin ) {
|
|
168
|
+
const strings = STRING_PARAMS[builtin] ?? [];
|
|
169
|
+
return expr.args.map((arg, index) => (strings.includes(index) && this.type(arg).base !== "string" ? this.text(arg) : this.expr(arg)));
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
expr(expr ) {
|
|
173
|
+
switch (expr.kind) {
|
|
174
|
+
case "number":
|
|
175
|
+
if (expr.integer) return `${expr.value}L`;
|
|
176
|
+
return /[.eE]/.test(String(expr.value)) ? String(expr.value) : `${expr.value}.0`;
|
|
177
|
+
case "string":
|
|
178
|
+
return JSON.stringify(expr.value).replaceAll("\u2028", "\\u2028").replaceAll("\u2029", "\\u2029");
|
|
179
|
+
case "template": {
|
|
180
|
+
const parts = expr.parts.map((part) => (typeof part === "string" ? this.expr({ kind: "string", line: expr.line, value: part }) : this.text(part)));
|
|
181
|
+
return parts.length === 0 ? '""' : `(${parts.join(" + ")})`;
|
|
182
|
+
}
|
|
183
|
+
case "boolean":
|
|
184
|
+
return String(expr.value);
|
|
185
|
+
case "null":
|
|
186
|
+
return "null";
|
|
187
|
+
case "name":
|
|
188
|
+
return javaName(expr.name);
|
|
189
|
+
case "member":
|
|
190
|
+
return this.member(expr);
|
|
191
|
+
case "call":
|
|
192
|
+
return this.call(expr);
|
|
193
|
+
case "await":
|
|
194
|
+
return this.expr(expr.value);
|
|
195
|
+
case "unary":
|
|
196
|
+
return `(${expr.op}${this.expr(expr.value)})`;
|
|
197
|
+
case "binary":
|
|
198
|
+
return this.binary(expr);
|
|
199
|
+
case "logical": {
|
|
200
|
+
if (expr.op !== "??") return `(${this.expr(expr.left)} ${expr.op} ${this.expr(expr.right)})`;
|
|
201
|
+
const type = this.type(expr);
|
|
202
|
+
return `(${this.expr(expr.left)} != null ? ${this.coerce(expr.left, type)} : ${this.coerce(expr.right, type)})`;
|
|
203
|
+
}
|
|
204
|
+
case "conditional": {
|
|
205
|
+
const type = this.type(expr);
|
|
206
|
+
return `(${this.expr(expr.test)} ? ${this.coerce(expr.then, type)} : ${this.coerce(expr.otherwise, type)})`;
|
|
207
|
+
}
|
|
208
|
+
case "object":
|
|
209
|
+
return `Js.map(${expr.entries.flatMap((entry) => [this.expr({ kind: "string", line: expr.line, value: entry.key }), this.expr(entry.value)]).join(", ")})`;
|
|
210
|
+
case "array":
|
|
211
|
+
return `Js.list(${expr.items.map((item) => this.expr(item)).join(", ")})`;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
binary(expr ) {
|
|
216
|
+
const left = this.type(expr.left);
|
|
217
|
+
const right = this.type(expr.right);
|
|
218
|
+
const a = this.expr(expr.left);
|
|
219
|
+
const b = this.expr(expr.right);
|
|
220
|
+
const primitive = (type ) => !type.nullable && (type.base === "int" || type.base === "float" || type.base === "boolean");
|
|
221
|
+
switch (expr.op) {
|
|
222
|
+
case "===":
|
|
223
|
+
case "!==": {
|
|
224
|
+
const negate = expr.op === "!==" ? "!" : "";
|
|
225
|
+
if (expr.right.kind === "null") return `(${a} ${negate === "" ? "==" : "!="} null)`;
|
|
226
|
+
if (expr.left.kind === "null") return `(${b} ${negate === "" ? "==" : "!="} null)`;
|
|
227
|
+
if (primitive(left) && primitive(right)) return `(${a} ${negate === "" ? "==" : "!="} ${b})`;
|
|
228
|
+
return `(${negate}Js.same(${a}, ${b}))`;
|
|
229
|
+
}
|
|
230
|
+
case "+":
|
|
231
|
+
if (left.base === "string" || right.base === "string") return `(${this.text(expr.left)} + ${this.text(expr.right)})`;
|
|
232
|
+
return `(${a} + ${b})`;
|
|
233
|
+
case "/":
|
|
234
|
+
return left.base === "int" && right.base === "int" ? `((double) ${a} / ${b})` : `(${a} / ${b})`;
|
|
235
|
+
case "**":
|
|
236
|
+
return `Math.pow(${a}, ${b})`;
|
|
237
|
+
default:
|
|
238
|
+
return `(${a} ${expr.op} ${b})`;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
member(expr ) {
|
|
243
|
+
const builtin = this.checked.builtins.get(expr);
|
|
244
|
+
const ctx = this.param(2);
|
|
245
|
+
switch (builtin) {
|
|
246
|
+
case "input":
|
|
247
|
+
return javaName(this.checked.configFields.get(expr)?.name ?? expr.name);
|
|
248
|
+
case "ctx.config": {
|
|
249
|
+
const field = this.checked.configFields.get(expr);
|
|
250
|
+
const cast = CAST[this.type(expr).base];
|
|
251
|
+
const read = `${ctx}.config().get(${JSON.stringify(field?.name ?? expr.name)})`;
|
|
252
|
+
return cast === undefined ? read : `((${cast}) ${read})`;
|
|
253
|
+
}
|
|
254
|
+
case "ctx.operation.name":
|
|
255
|
+
case "ctx.operation.method":
|
|
256
|
+
case "ctx.operation.path":
|
|
257
|
+
return `${ctx}.operation().${expr.name}()`;
|
|
258
|
+
case "ctx.sdk.name":
|
|
259
|
+
case "ctx.sdk.version":
|
|
260
|
+
case "ctx.sdk.language":
|
|
261
|
+
return `${ctx}.sdk().${expr.name}()`;
|
|
262
|
+
case "req.method":
|
|
263
|
+
case "req.url":
|
|
264
|
+
return `${this.expr(expr.object)}.${expr.name}()`;
|
|
265
|
+
case "res.status":
|
|
266
|
+
return `((long) ${this.expr(expr.object)}.status())`;
|
|
267
|
+
case "json.get":
|
|
268
|
+
return `Js.get(${this.expr(expr.object)}, ${JSON.stringify(expr.name)})`;
|
|
269
|
+
case "error.message":
|
|
270
|
+
return `String.valueOf(${this.expr(expr.object)}.getMessage())`;
|
|
271
|
+
default:
|
|
272
|
+
throw new Error(`internal: unresolved member ${expr.name} at line ${expr.line}`);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
call(expr ) {
|
|
277
|
+
const builtin = this.checked.builtins.get(expr);
|
|
278
|
+
if (builtin === undefined) throw new Error(`internal: unresolved call at line ${expr.line}`);
|
|
279
|
+
if (builtin === "logic") return `${this.logicName(this.checked.logicCalls.get(expr)?.name ?? "")}(${logicArgs(this.checked, expr).map(({ value, type }) => this.coerce(value, type)).join(", ")})`;
|
|
280
|
+
const args = this.args(expr, builtin);
|
|
281
|
+
const ctx = this.param(2);
|
|
282
|
+
const callee = expr.callee;
|
|
283
|
+
const target = () => (callee.kind === "member" ? this.expr(callee.object) : "");
|
|
284
|
+
const method = expr.callee.kind === "member" ? expr.callee.name : "";
|
|
285
|
+
switch (builtin) {
|
|
286
|
+
case "next":
|
|
287
|
+
return `${this.param(1)}.call(${args.join(", ")})`;
|
|
288
|
+
case "req.header":
|
|
289
|
+
case "req.withHeader":
|
|
290
|
+
case "req.withoutHeader":
|
|
291
|
+
case "req.withUrl":
|
|
292
|
+
case "res.header":
|
|
293
|
+
case "res.text":
|
|
294
|
+
case "res.json":
|
|
295
|
+
return `${target()}.${method}(${args.join(", ")})`;
|
|
296
|
+
case "req.withBody":
|
|
297
|
+
return `${target()}.withBody(Js.utf8(${args[0] ?? '""'}))`;
|
|
298
|
+
case "req.text":
|
|
299
|
+
return `${target()}.text()`;
|
|
300
|
+
case "ctx.clock.now":
|
|
301
|
+
return `${ctx}.clock().now()`;
|
|
302
|
+
case "ctx.clock.sleep":
|
|
303
|
+
return `${ctx}.clock().sleep(${this.long(expr.args[0] ?? expr)})`;
|
|
304
|
+
case "ctx.state.get":
|
|
305
|
+
return `${ctx}.state().get(${args.join(", ")})`;
|
|
306
|
+
case "ctx.state.set": {
|
|
307
|
+
const options = expr.args[2];
|
|
308
|
+
const ttl = options?.kind === "object" ? options.entries[0]?.value : undefined;
|
|
309
|
+
return `${ctx}.state().set(${args.slice(0, 2).join(", ")}${ttl === undefined ? "" : `, ${this.long(ttl)}`})`;
|
|
310
|
+
}
|
|
311
|
+
case "ctx.state.delete":
|
|
312
|
+
return `${ctx}.state().delete(${args.join(", ")})`;
|
|
313
|
+
case "ctx.emit": {
|
|
314
|
+
const fields = expr.args[1];
|
|
315
|
+
const value = fields !== undefined && this.type(fields).base === "json" ? `Js.asMap(${args[1] ?? ""})` : (args[1] ?? "");
|
|
316
|
+
return `${ctx}.emit(${args[0] ?? ""}, ${value})`;
|
|
317
|
+
}
|
|
318
|
+
case "ctx.request":
|
|
319
|
+
return `${ctx}.request(${args.join(", ")})`;
|
|
320
|
+
case "ctx.send":
|
|
321
|
+
return `${ctx}.send(${args.join(", ")})`;
|
|
322
|
+
case "Math.min":
|
|
323
|
+
case "Math.max": {
|
|
324
|
+
const name = builtin === "Math.min" ? "min" : "max";
|
|
325
|
+
return args.slice(1).reduce((acc, arg) => `Math.${name}(${acc}, ${arg})`, args[0] ?? "");
|
|
326
|
+
}
|
|
327
|
+
case "Math.floor":
|
|
328
|
+
return `((long) Math.floor(${args.join(", ")}))`;
|
|
329
|
+
case "Math.ceil":
|
|
330
|
+
return `((long) Math.ceil(${args.join(", ")}))`;
|
|
331
|
+
case "Math.abs":
|
|
332
|
+
return `Math.abs(${args.join(", ")})`;
|
|
333
|
+
case "Math.random":
|
|
334
|
+
return "Math.random()";
|
|
335
|
+
case "String":
|
|
336
|
+
return `Js.str(${args.join(", ")})`;
|
|
337
|
+
case "Number":
|
|
338
|
+
return `Js.number(${args.join(", ")})`;
|
|
339
|
+
case "btoa":
|
|
340
|
+
return `Js.btoa(${args.join(", ")})`;
|
|
341
|
+
case "encodeURIComponent":
|
|
342
|
+
return `Js.encodeURIComponent(${args.join(", ")})`;
|
|
343
|
+
case "JSON.parse":
|
|
344
|
+
return `Json.parse(${args.join(", ")})`;
|
|
345
|
+
case "randomUUID":
|
|
346
|
+
return "Js.randomUUID()";
|
|
347
|
+
case "hmacSha256Hex":
|
|
348
|
+
return `Js.hmacSha256Hex(${args.join(", ")})`;
|
|
349
|
+
case "hmacSha256Base64":
|
|
350
|
+
return `Js.hmacSha256Base64(${args.join(", ")})`;
|
|
351
|
+
default:
|
|
352
|
+
throw new Error(`internal: ${builtin} is not a call`);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/** The Java source of one middleware: `static HttpResponse name(HttpRequest req, Next next, Context ctx) { … }`. */
|
|
358
|
+
export function emitJava(checked , logicName = (name) => name) {
|
|
359
|
+
return new JavaEmitter(checked, logicName).run();
|
|
360
|
+
}
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
// Checked subset AST → a Python function. Every subexpression is parenthesized, so precedence never differs.
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
import { logicArgs, STRING_PARAMS, } from "./check.ts";
|
|
5
|
+
import { TranspileError } from "./errors.ts";
|
|
6
|
+
|
|
7
|
+
const KEYWORDS = new Set([
|
|
8
|
+
"False", "None", "True", "and", "as", "assert", "async", "await", "break", "class", "continue", "def", "del", "elif", "else", "except",
|
|
9
|
+
"finally", "for", "from", "global", "if", "import", "in", "is", "lambda", "nonlocal", "not", "or", "pass", "raise", "return", "try",
|
|
10
|
+
"while", "with", "yield", "math", "random",
|
|
11
|
+
// builtins and helpers the emitted code calls
|
|
12
|
+
"abs", "bool", "dict", "float", "int", "len", "list", "max", "min", "pow", "print", "round", "str",
|
|
13
|
+
"js_btoa", "js_div", "js_get", "js_message", "js_mod", "js_number", "js_pow", "js_same", "js_str",
|
|
14
|
+
"json_loads", "js_encode_uri_component", "js_hmac_sha256_hex", "js_hmac_sha256_base64", "js_random_uuid", "sys",
|
|
15
|
+
]);
|
|
16
|
+
|
|
17
|
+
/** getOrder → get_order; keywords and the module names the output imports get a trailing underscore. */
|
|
18
|
+
export function pythonName(name ) {
|
|
19
|
+
const snake = name.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
|
|
20
|
+
return KEYWORDS.has(snake) ? `${snake}_` : snake;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Names the emitted code needs from the runtime's lang module and the standard library. */
|
|
24
|
+
export const PYTHON_HELPERS = ["js_btoa", "js_div", "js_encode_uri_component", "js_get", "js_hmac_sha256_base64", "js_hmac_sha256_hex", "js_mod", "js_number", "js_pow", "js_random_uuid", "js_same", "js_str"] ;
|
|
25
|
+
|
|
26
|
+
class PythonEmitter {
|
|
27
|
+
lines = [];
|
|
28
|
+
checked ;
|
|
29
|
+
configKey ;
|
|
30
|
+
logicName ;
|
|
31
|
+
|
|
32
|
+
constructor(checked , configKey , logicName ) {
|
|
33
|
+
this.checked = checked;
|
|
34
|
+
this.configKey = configKey;
|
|
35
|
+
this.logicName = logicName;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
type(expr ) {
|
|
39
|
+
const type = this.checked.types.get(expr);
|
|
40
|
+
if (type === undefined) throw new Error(`internal: no type for ${expr.kind} at line ${expr.line}`);
|
|
41
|
+
return type;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
line(depth , text ) {
|
|
45
|
+
this.lines.push(`${" ".repeat(depth)}${text}`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Two TypeScript names that become one Python name would silently share a variable. */
|
|
49
|
+
checkNames() {
|
|
50
|
+
const seen = new Map ();
|
|
51
|
+
const visit = (name , line ) => {
|
|
52
|
+
const python = pythonName(name);
|
|
53
|
+
const other = seen.get(python);
|
|
54
|
+
if (other !== undefined && other !== name) throw new TranspileError(this.checked.fn.name, line, `${other} and ${name} are both ${python} in Python; rename one`, "SL-T004");
|
|
55
|
+
seen.set(python, name);
|
|
56
|
+
};
|
|
57
|
+
this.checked.fn.params.forEach((name) => visit(name, 1));
|
|
58
|
+
const walk = (statements ) =>
|
|
59
|
+
statements.forEach((statement) => {
|
|
60
|
+
if (statement.kind === "let" || statement.kind === "forOf") visit(statement.name, statement.line);
|
|
61
|
+
if (statement.kind === "try") visit(statement.param, statement.line);
|
|
62
|
+
if (statement.kind === "if") {
|
|
63
|
+
walk(statement.then);
|
|
64
|
+
walk(statement.otherwise ?? []);
|
|
65
|
+
}
|
|
66
|
+
if (statement.kind === "for") walk([...(statement.init === undefined ? [] : [statement.init]), ...statement.body]);
|
|
67
|
+
if (statement.kind === "forOf" || statement.kind === "while") walk(statement.body);
|
|
68
|
+
if (statement.kind === "try") {
|
|
69
|
+
walk(statement.body);
|
|
70
|
+
walk(statement.handler);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
walk(this.checked.fn.body);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
run() {
|
|
77
|
+
this.checkNames();
|
|
78
|
+
const [req, next, ctx] = this.checked.fn.params.map(pythonName);
|
|
79
|
+
const logic = this.checked.logic;
|
|
80
|
+
const params = logic === undefined ? `${req}, ${next}, ${ctx}` : logic.input.map((field) => pythonName(field.name)).join(", ");
|
|
81
|
+
this.line(0, `def ${pythonName(this.checked.fn.name)}(${params}):`);
|
|
82
|
+
this.block(this.checked.fn.body, 1);
|
|
83
|
+
return `${this.lines.join("\n")}\n`;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
block(statements , depth ) {
|
|
87
|
+
if (statements.length === 0) this.line(depth, "pass");
|
|
88
|
+
statements.forEach((statement) => this.statement(statement, depth));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
statement(statement , depth ) {
|
|
92
|
+
switch (statement.kind) {
|
|
93
|
+
case "let":
|
|
94
|
+
return this.line(depth, `${pythonName(statement.name)} = ${this.expr(statement.init)}`);
|
|
95
|
+
case "assign":
|
|
96
|
+
return this.line(depth, `${pythonName(statement.name)} ${statement.op} ${this.expr(statement.value)}`);
|
|
97
|
+
case "if": {
|
|
98
|
+
this.line(depth, `if ${this.expr(statement.test)}:`);
|
|
99
|
+
this.block(statement.then, depth + 1);
|
|
100
|
+
let otherwise = statement.otherwise;
|
|
101
|
+
while (otherwise !== undefined && otherwise.length === 1 && otherwise[0]?.kind === "if") {
|
|
102
|
+
const nested = otherwise[0];
|
|
103
|
+
this.line(depth, `elif ${this.expr(nested.test)}:`);
|
|
104
|
+
this.block(nested.then, depth + 1);
|
|
105
|
+
otherwise = nested.otherwise;
|
|
106
|
+
}
|
|
107
|
+
if (otherwise !== undefined) {
|
|
108
|
+
this.line(depth, "else:");
|
|
109
|
+
this.block(otherwise, depth + 1);
|
|
110
|
+
}
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
case "for":
|
|
114
|
+
if (statement.init !== undefined) this.statement(statement.init, depth);
|
|
115
|
+
this.line(depth, `while ${this.expr(statement.test)}:`);
|
|
116
|
+
this.block(statement.body, depth + 1);
|
|
117
|
+
if (statement.update !== undefined) this.statement(statement.update, depth + 1);
|
|
118
|
+
return;
|
|
119
|
+
case "forOf":
|
|
120
|
+
this.line(depth, `for ${pythonName(statement.name)} in ${this.expr(statement.iterable)}:`);
|
|
121
|
+
return this.block(statement.body, depth + 1);
|
|
122
|
+
case "while":
|
|
123
|
+
this.line(depth, `while ${this.expr(statement.test)}:`);
|
|
124
|
+
return this.block(statement.body, depth + 1);
|
|
125
|
+
case "break":
|
|
126
|
+
return this.line(depth, "break");
|
|
127
|
+
case "return":
|
|
128
|
+
return this.line(depth, `return ${this.expr(statement.value)}`);
|
|
129
|
+
case "throw":
|
|
130
|
+
return this.line(depth, `raise RuntimeError(${this.expr(statement.message)})`);
|
|
131
|
+
case "try":
|
|
132
|
+
this.line(depth, "try:");
|
|
133
|
+
this.block(statement.body, depth + 1);
|
|
134
|
+
this.line(depth, `except Exception as ${pythonName(statement.param)}:`);
|
|
135
|
+
return this.block(statement.handler, depth + 1);
|
|
136
|
+
case "expression":
|
|
137
|
+
return this.line(depth, this.expr(statement.value));
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** A value converted like String(value) unless it already is a string. */
|
|
142
|
+
text(expr ) {
|
|
143
|
+
const type = this.type(expr);
|
|
144
|
+
return type.base === "string" && !type.nullable ? this.expr(expr) : `js_str(${this.expr(expr)})`;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
args(expr , builtin ) {
|
|
148
|
+
const strings = STRING_PARAMS[builtin] ?? [];
|
|
149
|
+
return expr.args.map((arg, index) => (strings.includes(index) && this.type(arg).base === "json" ? this.text(arg) : this.expr(arg)));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
expr(expr ) {
|
|
153
|
+
switch (expr.kind) {
|
|
154
|
+
case "number":
|
|
155
|
+
return expr.integer ? String(expr.value) : /[.eE]/.test(String(expr.value)) ? String(expr.value) : `${expr.value}.0`;
|
|
156
|
+
case "string":
|
|
157
|
+
return JSON.stringify(expr.value);
|
|
158
|
+
case "template": {
|
|
159
|
+
const parts = expr.parts.map((part) => (typeof part === "string" ? JSON.stringify(part) : this.text(part)));
|
|
160
|
+
return parts.length === 0 ? '""' : `(${parts.join(" + ")})`;
|
|
161
|
+
}
|
|
162
|
+
case "boolean":
|
|
163
|
+
return expr.value ? "True" : "False";
|
|
164
|
+
case "null":
|
|
165
|
+
return "None";
|
|
166
|
+
case "name":
|
|
167
|
+
return pythonName(expr.name);
|
|
168
|
+
case "member":
|
|
169
|
+
return this.member(expr);
|
|
170
|
+
case "call":
|
|
171
|
+
return this.call(expr);
|
|
172
|
+
case "await":
|
|
173
|
+
return this.expr(expr.value);
|
|
174
|
+
case "unary":
|
|
175
|
+
return expr.op === "!" ? `(not ${this.expr(expr.value)})` : `(-${this.expr(expr.value)})`;
|
|
176
|
+
case "binary":
|
|
177
|
+
return this.binary(expr);
|
|
178
|
+
case "logical": {
|
|
179
|
+
const left = this.expr(expr.left);
|
|
180
|
+
const right = this.expr(expr.right);
|
|
181
|
+
if (expr.op === "??") return `(${right} if ${left} is None else ${left})`;
|
|
182
|
+
return `(${left} ${expr.op === "&&" ? "and" : "or"} ${right})`;
|
|
183
|
+
}
|
|
184
|
+
case "conditional":
|
|
185
|
+
return `(${this.expr(expr.then)} if ${this.expr(expr.test)} else ${this.expr(expr.otherwise)})`;
|
|
186
|
+
case "object":
|
|
187
|
+
return `{${expr.entries.map((entry) => `${JSON.stringify(entry.key)}: ${this.expr(entry.value)}`).join(", ")}}`;
|
|
188
|
+
case "array":
|
|
189
|
+
return `[${expr.items.map((item) => this.expr(item)).join(", ")}]`;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
binary(expr ) {
|
|
194
|
+
const left = this.expr(expr.left);
|
|
195
|
+
const right = this.expr(expr.right);
|
|
196
|
+
if (expr.op === "===" || expr.op === "!==") {
|
|
197
|
+
const negate = expr.op === "!==";
|
|
198
|
+
if (expr.right.kind === "null") return `(${left} is ${negate ? "not " : ""}None)`;
|
|
199
|
+
if (expr.left.kind === "null") return `(${right} is ${negate ? "not " : ""}None)`;
|
|
200
|
+
const loose = this.type(expr.left).base === "json" || this.type(expr.right).base === "json";
|
|
201
|
+
if (loose) return `(${negate ? "not " : ""}js_same(${left}, ${right}))`;
|
|
202
|
+
return `(${left} ${negate ? "!=" : "=="} ${right})`;
|
|
203
|
+
}
|
|
204
|
+
if (expr.op === "+" && (this.type(expr.left).base === "string" || this.type(expr.right).base === "string")) return `(${this.text(expr.left)} + ${this.text(expr.right)})`;
|
|
205
|
+
if (expr.op === "%") return `js_mod(${left}, ${right})`;
|
|
206
|
+
if (expr.op === "/") return `js_div(${left}, ${right})`;
|
|
207
|
+
if (expr.op === "**") return `js_pow(${left}, ${right})`;
|
|
208
|
+
return `(${left} ${expr.op} ${right})`;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
member(expr ) {
|
|
212
|
+
const builtin = this.checked.builtins.get(expr);
|
|
213
|
+
const object = () => this.expr(expr.object);
|
|
214
|
+
switch (builtin) {
|
|
215
|
+
case "input":
|
|
216
|
+
return pythonName(this.checked.configFields.get(expr)?.name ?? expr.name);
|
|
217
|
+
case "ctx.config": {
|
|
218
|
+
const field = this.checked.configFields.get(expr);
|
|
219
|
+
return `${pythonName(this.checked.fn.params[2])}.config[${JSON.stringify(this.configKey(field?.name ?? expr.name))}]`;
|
|
220
|
+
}
|
|
221
|
+
case "ctx.operation.name":
|
|
222
|
+
case "ctx.operation.method":
|
|
223
|
+
case "ctx.operation.path":
|
|
224
|
+
return `${pythonName(this.checked.fn.params[2])}.operation.${expr.name}`;
|
|
225
|
+
case "ctx.sdk.name":
|
|
226
|
+
case "ctx.sdk.version":
|
|
227
|
+
case "ctx.sdk.language":
|
|
228
|
+
return `${pythonName(this.checked.fn.params[2])}.sdk.${expr.name}`;
|
|
229
|
+
case "json.get":
|
|
230
|
+
return `js_get(${object()}, ${JSON.stringify(expr.name)})`;
|
|
231
|
+
case "error.message":
|
|
232
|
+
return `js_message(${object()})`;
|
|
233
|
+
default:
|
|
234
|
+
return `${object()}.${expr.name}`;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
call(expr ) {
|
|
239
|
+
const builtin = this.checked.builtins.get(expr);
|
|
240
|
+
if (builtin === undefined) throw new Error(`internal: unresolved call at line ${expr.line}`);
|
|
241
|
+
if (builtin === "logic") return `${this.logicName(this.checked.logicCalls.get(expr)?.name ?? "")}(${logicArgs(this.checked, expr).map(({ value }) => this.expr(value)).join(", ")})`;
|
|
242
|
+
const args = this.args(expr, builtin);
|
|
243
|
+
const ctx = pythonName(this.checked.fn.params[2]);
|
|
244
|
+
const callee = expr.callee;
|
|
245
|
+
const target = () => (callee.kind === "member" ? this.expr(callee.object) : "");
|
|
246
|
+
switch (builtin) {
|
|
247
|
+
case "next":
|
|
248
|
+
return `${pythonName(this.checked.fn.params[1])}(${args.join(", ")})`;
|
|
249
|
+
case "req.header":
|
|
250
|
+
case "res.header":
|
|
251
|
+
case "res.text":
|
|
252
|
+
case "res.json":
|
|
253
|
+
return `${target()}.${expr.callee.kind === "member" ? expr.callee.name : ""}(${args.join(", ")})`;
|
|
254
|
+
case "req.withHeader":
|
|
255
|
+
return `${target()}.with_header(${args.join(", ")})`;
|
|
256
|
+
case "req.withoutHeader":
|
|
257
|
+
return `${target()}.without_header(${args.join(", ")})`;
|
|
258
|
+
case "req.withUrl":
|
|
259
|
+
return `${target()}.with_url(${args.join(", ")})`;
|
|
260
|
+
case "req.withBody":
|
|
261
|
+
return `${target()}.with_body((${args[0] ?? '""'}).encode("utf-8"))`;
|
|
262
|
+
case "req.text":
|
|
263
|
+
return `${target()}.text()`;
|
|
264
|
+
case "ctx.clock.now":
|
|
265
|
+
return `${ctx}.clock.now()`;
|
|
266
|
+
case "ctx.clock.sleep":
|
|
267
|
+
return `${ctx}.clock.sleep(${args.join(", ")})`;
|
|
268
|
+
case "ctx.state.get":
|
|
269
|
+
return `${ctx}.state.get(${args.join(", ")})`;
|
|
270
|
+
case "ctx.state.set": {
|
|
271
|
+
const options = expr.args[2];
|
|
272
|
+
const ttl = options?.kind === "object" ? options.entries[0]?.value : undefined;
|
|
273
|
+
return `${ctx}.state.set(${args.slice(0, 2).join(", ")}${ttl === undefined ? "" : `, ttl_ms=${this.expr(ttl)}`})`;
|
|
274
|
+
}
|
|
275
|
+
case "ctx.state.delete":
|
|
276
|
+
return `${ctx}.state.delete(${args.join(", ")})`;
|
|
277
|
+
case "ctx.emit":
|
|
278
|
+
return `${ctx}.emit(${args.join(", ")})`;
|
|
279
|
+
case "ctx.request":
|
|
280
|
+
return `${ctx}.request(${args.join(", ")})`;
|
|
281
|
+
case "ctx.send":
|
|
282
|
+
return `${ctx}.send(${args.join(", ")})`;
|
|
283
|
+
case "Math.min":
|
|
284
|
+
case "Math.max":
|
|
285
|
+
return `${builtin === "Math.min" ? "min" : "max"}(${args.join(", ")})`;
|
|
286
|
+
case "Math.floor":
|
|
287
|
+
// int(): math.floor returns a float on Python 2.7.
|
|
288
|
+
return `int(math.floor(${args.join(", ")}))`;
|
|
289
|
+
case "Math.ceil":
|
|
290
|
+
return `int(math.ceil(${args.join(", ")}))`;
|
|
291
|
+
case "Math.abs":
|
|
292
|
+
return `abs(${args.join(", ")})`;
|
|
293
|
+
case "Math.random":
|
|
294
|
+
return "random.random()";
|
|
295
|
+
case "String":
|
|
296
|
+
return `js_str(${args.join(", ")})`;
|
|
297
|
+
case "Number":
|
|
298
|
+
return `js_number(${args.join(", ")})`;
|
|
299
|
+
case "btoa":
|
|
300
|
+
return `js_btoa(${args.join(", ")})`;
|
|
301
|
+
case "encodeURIComponent":
|
|
302
|
+
return `js_encode_uri_component(${args.join(", ")})`;
|
|
303
|
+
case "JSON.parse":
|
|
304
|
+
return `json_loads(${args.join(", ")})`;
|
|
305
|
+
case "randomUUID":
|
|
306
|
+
return "js_random_uuid()";
|
|
307
|
+
case "hmacSha256Hex":
|
|
308
|
+
return `js_hmac_sha256_hex(${args.join(", ")})`;
|
|
309
|
+
case "hmacSha256Base64":
|
|
310
|
+
return `js_hmac_sha256_base64(${args.join(", ")})`;
|
|
311
|
+
default:
|
|
312
|
+
throw new Error(`internal: ${builtin} is not a call`);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** The Python source of one middleware: `def name(req, next, ctx): …`. configKey maps a config field to its key in ctx.config. */
|
|
318
|
+
export function emitPython(checked , configKey , logicName = pythonName) {
|
|
319
|
+
return new PythonEmitter(checked, configKey, logicName).run();
|
|
320
|
+
}
|