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,244 @@
|
|
|
1
|
+
// Generates the Java SDK as an in-memory file tree (Maven layout, no dependencies).
|
|
2
|
+
|
|
3
|
+
import { createFileTree, sha256, } from "@sdkloom/codegen";
|
|
4
|
+
|
|
5
|
+
import { parseBlocks, } from "@sdkloom/kit";
|
|
6
|
+
import { javaName, transpileLogic, transpileMiddleware, TranspileError } from "@sdkloom/transpile";
|
|
7
|
+
import { clientName, emitClient, emitParams, paramsClass } from "./emit-client.ts";
|
|
8
|
+
import { emitModels } from "./emit-models.ts";
|
|
9
|
+
import { emitExamples, emitMock } from "./emit-testing.ts";
|
|
10
|
+
import { emitWebhooks, webhooksClass } from "./emit-webhooks.ts";
|
|
11
|
+
import { java, javadoc } from "./names.ts";
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
/** The package the kit's sources are written in; vendoring moves them into <javaPackage>.runtime. */
|
|
50
|
+
export const KIT_PACKAGE = "dev.sdkloom.runtime";
|
|
51
|
+
const TAG_LINE = /^\s*\/\/\s*@([a-z][a-z-]*)\b/;
|
|
52
|
+
const JAVA_PACKAGE = /^[a-z_][a-z0-9_]*(\.[a-z_][a-z0-9_]*)*$/;
|
|
53
|
+
|
|
54
|
+
function blockDoc(block , kit ) {
|
|
55
|
+
return [
|
|
56
|
+
block.summary ?? "",
|
|
57
|
+
"",
|
|
58
|
+
`Supported versions: Java ${block.versions ?? kit.supports}`,
|
|
59
|
+
"Sources:",
|
|
60
|
+
...block.sources.flatMap((source) => [` [${source.index}] ${source.url}`, ...block.quotes.filter((quote) => quote.index === source.index).map((quote) => ` "${quote.text}"`)]),
|
|
61
|
+
` Retrieved: ${block.retrieved ?? "unknown"}`,
|
|
62
|
+
`Provenance: block ${block.id} · kit ${kit.name} ${kit.version} · sha256:${sha256(block.code).slice(0, 12)}`,
|
|
63
|
+
];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function vendor(file , kit , header , pkg ) {
|
|
67
|
+
const parsed = parseBlocks(file.text, { commentPrefix: "//", file: file.path });
|
|
68
|
+
if (parsed.errors.length > 0) throw new Error(`kit file ${file.path} is invalid: ${parsed.errors.map((error) => error.message).join("; ")}`);
|
|
69
|
+
const writer = java().line(header);
|
|
70
|
+
let index = 0;
|
|
71
|
+
let inMetadata = false;
|
|
72
|
+
for (const line of file.text.split("\n")) {
|
|
73
|
+
const tag = TAG_LINE.exec(line)?.[1];
|
|
74
|
+
if (tag === "block") {
|
|
75
|
+
const block = parsed.blocks[index];
|
|
76
|
+
index += 1;
|
|
77
|
+
inMetadata = true;
|
|
78
|
+
if (block !== undefined) blockDoc(block, kit).forEach((text) => writer.line(text === "" ? "//" : `// ${text}`));
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (tag === "end" || (inMetadata && tag !== undefined)) continue;
|
|
82
|
+
inMetadata = false;
|
|
83
|
+
writer.line(line.replaceAll(KIT_PACKAGE, `${pkg}.runtime`));
|
|
84
|
+
}
|
|
85
|
+
return { text: writer.toString(), blocks: parsed.blocks };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const CUSTOM_FILE = "custom/java/Middlewares.java";
|
|
89
|
+
|
|
90
|
+
/** Middleware the author wrote natively in custom/java/Middlewares.java. */
|
|
91
|
+
export function nativeMiddleware(ir , custom ) {
|
|
92
|
+
return new Set(ir.middlewares.map((entry) => entry.name).filter((name) => custom !== undefined && new RegExp(`\\bstatic\\s+HttpResponse\\s+${javaName(name)}\\s*\\(`).test(custom)));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function transpile(ir , name , sources ) {
|
|
96
|
+
const middleware = ir.middlewares.find((candidate) => candidate.name === name);
|
|
97
|
+
const source = sources.get(name);
|
|
98
|
+
if (middleware === undefined || source === undefined) throw new Error(`middleware ${name} has no source to transpile`);
|
|
99
|
+
try {
|
|
100
|
+
return transpileMiddleware({ name, source, config: middleware.config }, "java", logicOptions(ir));
|
|
101
|
+
} catch (error ) {
|
|
102
|
+
if (!(error instanceof TranspileError)) throw error;
|
|
103
|
+
throw new Error(
|
|
104
|
+
`${error.message}\nTo keep this middleware as written, write static HttpResponse ${javaName(name)}(HttpRequest request, Next next, Context ctx) in ${CUSTOM_FILE}; it replaces the transpiled one.`,
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const MIDDLEWARE_IMPORTS = ["Context", "HttpRequest", "HttpResponse", "Js", "Json", "Next"];
|
|
110
|
+
|
|
111
|
+
function emitTranspiled(ir , sources , native , header , pkg ) {
|
|
112
|
+
const names = ir.middlewares.map((entry) => entry.name).filter((name) => !native.has(name));
|
|
113
|
+
if (names.length === 0) return undefined;
|
|
114
|
+
const writer = java().line(header).line(`package ${pkg};`).line();
|
|
115
|
+
["java.util.List", "java.util.Map", ...MIDDLEWARE_IMPORTS.map((name) => `${pkg}.runtime.${name}`)].sort().forEach((name) => writer.line(`import ${name};`));
|
|
116
|
+
writer.line().line("/** Middleware transpiled from the TypeScript definition (spec/subset.md); methods in custom/java/Middlewares.java replace these. */").line("final class TranspiledMiddlewares {").indent((body) => {
|
|
117
|
+
body.line("private TranspiledMiddlewares() {").line("}");
|
|
118
|
+
names.forEach((name) => {
|
|
119
|
+
body.line();
|
|
120
|
+
transpile(ir, name, sources).trimEnd().split("\n").forEach((line) => body.line(line));
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
return writer.line("}").toString();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Middleware and logic call the project's logic functions as <Name>Logic.<name>. */
|
|
127
|
+
function logicOptions(ir ) {
|
|
128
|
+
return { functions: ir.logic ?? [], logicName: (name) => `${logicClass(ir)}.${javaName(name)}` };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The class of the SDK's logic functions, e.g. PaymentsLogic. */
|
|
132
|
+
export function logicClass(ir ) {
|
|
133
|
+
return `${ir.name}Logic`;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** <Name>Logic.java: the definition's logic functions as public static methods, transpiled. */
|
|
137
|
+
function emitLogic(ir , sources , header , pkg ) {
|
|
138
|
+
const writer = java().line(header).line(`package ${pkg};`).line();
|
|
139
|
+
writer.line(`import ${pkg}.runtime.Js;`).line(`import ${pkg}.runtime.Json;`).line();
|
|
140
|
+
javadoc(writer, [`Functions of the ${ir.name} API, transpiled from its definition (spec/subset.md).`]);
|
|
141
|
+
writer.line(`public final class ${logicClass(ir)} {`).indent((body) => {
|
|
142
|
+
body.line(`private ${logicClass(ir)}() {`).line("}");
|
|
143
|
+
for (const logic of ir.logic ?? []) {
|
|
144
|
+
const source = sources.get(logic.name);
|
|
145
|
+
if (source === undefined) throw new Error(`logic function ${logic.name} has no source to transpile`);
|
|
146
|
+
body.line();
|
|
147
|
+
if (logic.doc !== undefined) javadoc(body, [logic.doc]);
|
|
148
|
+
transpileLogic(logic, source, "java", logicOptions(ir)).trimEnd().split("\n").forEach((line, index) => body.line(index === 0 ? `public ${line}` : line));
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
return writer.line("}").toString();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function emitCustom(custom , header , pkg ) {
|
|
155
|
+
const writer = java().line(header).line(`// Copied from ${CUSTOM_FILE}.`).line(`package ${pkg};`).line();
|
|
156
|
+
MIDDLEWARE_IMPORTS.forEach((name) => writer.line(`import ${pkg}.runtime.${name};`));
|
|
157
|
+
return `${writer.line().toString()}${custom}`;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function xml(text ) {
|
|
161
|
+
return text.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function emitPom(input ) {
|
|
165
|
+
return java()
|
|
166
|
+
.line('<?xml version="1.0" encoding="UTF-8"?>')
|
|
167
|
+
.line(`<!-- Generated by sdkloom ${input.sdkloomVersion}. No dependencies: the SDK uses only the JDK. -->`)
|
|
168
|
+
.line('<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">')
|
|
169
|
+
.indent((project) =>
|
|
170
|
+
project
|
|
171
|
+
.line("<modelVersion>4.0.0</modelVersion>")
|
|
172
|
+
.line(`<groupId>${xml(input.groupId)}</groupId>`)
|
|
173
|
+
.line(`<artifactId>${xml(input.artifactId)}</artifactId>`)
|
|
174
|
+
.line(`<version>${xml(input.version)}</version>`)
|
|
175
|
+
.line("<packaging>jar</packaging>")
|
|
176
|
+
.line(`<description>${xml(input.ir.doc ?? input.ir.name)}</description>`)
|
|
177
|
+
.line("<properties>")
|
|
178
|
+
.indent((properties) =>
|
|
179
|
+
properties.line(`<maven.compiler.release>${input.kit.release}</maven.compiler.release>`).line("<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>"),
|
|
180
|
+
)
|
|
181
|
+
.line("</properties>"),
|
|
182
|
+
)
|
|
183
|
+
.line("</project>")
|
|
184
|
+
.toString();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function emitReadme(input ) {
|
|
188
|
+
const client = clientName(input.ir);
|
|
189
|
+
return java()
|
|
190
|
+
.line(`# ${input.artifactId}`)
|
|
191
|
+
.line()
|
|
192
|
+
.line(input.ir.doc ?? "")
|
|
193
|
+
.line()
|
|
194
|
+
.line(`> Generated by sdkloom ${input.sdkloomVersion} from \`${input.source}\`. Do not edit by hand: run \`sdkloom build\`.`)
|
|
195
|
+
.line()
|
|
196
|
+
.line("```java")
|
|
197
|
+
.line(`import ${input.javaPackage}.${client};`)
|
|
198
|
+
.line()
|
|
199
|
+
.line(`${client} client = ${client}.builder()...build();`)
|
|
200
|
+
.line("```")
|
|
201
|
+
.line()
|
|
202
|
+
.line(`JDK only, compiled for Java ${input.kit.release} and runs on Java ${input.kit.release} or later. Test helpers: \`${input.javaPackage}.testing.${input.ir.name}Mock\` and \`${input.ir.name}Examples\`.`)
|
|
203
|
+
.toString();
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export function generateJava(input ) {
|
|
207
|
+
const { ir } = input;
|
|
208
|
+
const pkg = input.javaPackage;
|
|
209
|
+
if (!JAVA_PACKAGE.test(pkg)) throw new Error(`"${pkg}" is not a valid Java package name; use lower case segments such as com.acme.payments`);
|
|
210
|
+
// Java reads \uXXXX even in comments, so a Windows path must not reach the header with its backslashes.
|
|
211
|
+
const header = `// Generated by sdkloom ${input.sdkloomVersion} from ${input.source.replaceAll("\\", "/")}. Do not edit: run \`sdkloom build\` to regenerate.`;
|
|
212
|
+
const src = `src/main/java/${pkg.split(".").join("/")}`;
|
|
213
|
+
const files = [];
|
|
214
|
+
const trace = new Map ();
|
|
215
|
+
const add = (path , text , from ) => {
|
|
216
|
+
files.push([path, text]);
|
|
217
|
+
trace.set(path, from);
|
|
218
|
+
};
|
|
219
|
+
const operations = ir.operations.map((operation) => `ir:operations.${operation.name}`);
|
|
220
|
+
const types = ir.types.map((named) => `ir:types.${named.name}`);
|
|
221
|
+
|
|
222
|
+
const blocks = [];
|
|
223
|
+
for (const file of [...input.kit.files].sort((a, b) => (a.path < b.path ? -1 : 1))) {
|
|
224
|
+
const vendored = vendor(file, input.kit, header, pkg);
|
|
225
|
+
add(`${src}/runtime/${file.path}`, vendored.text, vendored.blocks.map((block) => `block:${block.id}`));
|
|
226
|
+
vendored.blocks.forEach((block) => blocks.push({ id: block.id, versions: block.versions ?? "*", code: block.code }));
|
|
227
|
+
}
|
|
228
|
+
emitModels(ir, header, pkg).forEach((text, name) => add(`${src}/${name}.java`, text, [...types, ...operations]));
|
|
229
|
+
ir.operations
|
|
230
|
+
.filter((operation) => operation.params.some((param) => param.in !== "path"))
|
|
231
|
+
.forEach((operation) => add(`${src}/${paramsClass(operation)}.java`, emitParams(operation, header, pkg), [`ir:operations.${operation.name}`]));
|
|
232
|
+
const native = nativeMiddleware(ir, input.middleware);
|
|
233
|
+
const transpiled = emitTranspiled(ir, input.middlewareSources, native, header, pkg);
|
|
234
|
+
if (transpiled !== undefined) add(`${src}/TranspiledMiddlewares.java`, transpiled, ir.middlewares.filter((entry) => !native.has(entry.name)).map((entry) => `middleware:${entry.name}`));
|
|
235
|
+
if (input.middleware !== undefined) add(`${src}/Middlewares.java`, emitCustom(input.middleware, header, pkg), [...native].map((name) => `middleware:${name}`));
|
|
236
|
+
add(`${src}/${clientName(ir)}.java`, emitClient(ir, header, pkg, native, { name: input.artifactId, version: input.version }, input.kit.transport), ["ir:servers", ...operations]);
|
|
237
|
+
if ((ir.logic ?? []).length > 0) add(`${src}/${logicClass(ir)}.java`, emitLogic(ir, input.logicSources ?? new Map(), header, pkg), (ir.logic ?? []).map((entry) => `ir:logic.${entry.name}`));
|
|
238
|
+
if ((ir.webhooks ?? []).length > 0) add(`${src}/${webhooksClass(ir)}.java`, emitWebhooks(ir, header, pkg), (ir.webhooks ?? []).map((hook) => `ir:webhooks.${hook.name}`));
|
|
239
|
+
add(`${src}/testing/${ir.name}Mock.java`, emitMock(ir, header, pkg), operations);
|
|
240
|
+
add(`${src}/testing/${ir.name}Examples.java`, emitExamples(ir, header, pkg), types);
|
|
241
|
+
add("pom.xml", emitPom(input), ["config"]);
|
|
242
|
+
add("README.md", emitReadme(input), ["config", "ir"]);
|
|
243
|
+
return { files: createFileTree(files), trace, blocks };
|
|
244
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
// Java names, literals, Javadoc and the IR → Java type, decoder and encoder mapping.
|
|
2
|
+
|
|
3
|
+
import { createCodeWriter, } from "@sdkloom/codegen";
|
|
4
|
+
|
|
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",
|
|
11
|
+
]);
|
|
12
|
+
|
|
13
|
+
export const java = () => createCodeWriter({ indent: " " });
|
|
14
|
+
|
|
15
|
+
/** x-request-id → xRequestId, created-at → createdAt, class → class_ */
|
|
16
|
+
export function camel(name ) {
|
|
17
|
+
const parts = name.split(/[^A-Za-z0-9]+/).filter((part) => part !== "");
|
|
18
|
+
const joined = parts.map((part, index) => (index === 0 ? part.charAt(0).toLowerCase() + part.slice(1) : part.charAt(0).toUpperCase() + part.slice(1))).join("");
|
|
19
|
+
const safe = /^[0-9]/.test(joined) ? `_${joined}` : joined;
|
|
20
|
+
return KEYWORDS.has(safe) ? `${safe}_` : safe;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** getOrder → GetOrder */
|
|
24
|
+
export function pascal(name ) {
|
|
25
|
+
const value = camel(name).replace(/_$/, "");
|
|
26
|
+
return value.charAt(0).toUpperCase() + value.slice(1);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A Java string literal. JSON string syntax is valid Java except for U+2028/9, which JSON.stringify leaves raw. */
|
|
30
|
+
export function str(text ) {
|
|
31
|
+
return JSON.stringify(text).replaceAll("\u2028", "\\u2028").replaceAll("\u2029", "\\u2029");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Writes a Javadoc comment; `tags` are block tags such as "@deprecated Use x." and are not escaped. */
|
|
35
|
+
export function javadoc(writer , lines , tags = []) {
|
|
36
|
+
const escape = (line ) =>
|
|
37
|
+
line.replaceAll("&", "&").replaceAll("\\", "\").replaceAll("*/", "*/").replaceAll("@", "{@literal @}").replaceAll("<", "<").replaceAll(">", ">");
|
|
38
|
+
const text = [
|
|
39
|
+
...lines.filter((line, index, all) => line !== "" || (index > 0 && index < all.length - 1)).map(escape),
|
|
40
|
+
...tags.map((tag) => tag.replaceAll("\\", "\").replaceAll("*/", "*/")),
|
|
41
|
+
];
|
|
42
|
+
if (text.length === 0) return;
|
|
43
|
+
if (text.length === 1) {
|
|
44
|
+
writer.line(`/** ${text[0]} */`);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
writer.line("/**");
|
|
48
|
+
text.forEach((line) => writer.line(line === "" ? " *" : ` * ${line}`));
|
|
49
|
+
writer.line(" */");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const JAVA_TYPE = {
|
|
53
|
+
string: "String",
|
|
54
|
+
secret: "String",
|
|
55
|
+
datetime: "String",
|
|
56
|
+
decimal: "BigDecimal",
|
|
57
|
+
int: "Long",
|
|
58
|
+
number: "Double",
|
|
59
|
+
bool: "Boolean",
|
|
60
|
+
bytes: "byte[]",
|
|
61
|
+
stream: "InputStream",
|
|
62
|
+
multipart: "MultipartForm",
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const DECODER = {
|
|
66
|
+
string: "Decode.STRING",
|
|
67
|
+
secret: "Decode.STRING",
|
|
68
|
+
datetime: "Decode.STRING",
|
|
69
|
+
decimal: "Decode.DECIMAL",
|
|
70
|
+
int: "Decode.INTEGER",
|
|
71
|
+
number: "Decode.NUMBER",
|
|
72
|
+
bool: "Decode.BOOLEAN",
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const EXAMPLE = {
|
|
76
|
+
string: str("string"),
|
|
77
|
+
secret: str("secret"),
|
|
78
|
+
datetime: str("2026-01-01T00:00:00Z"),
|
|
79
|
+
decimal: 'new BigDecimal("1.00")',
|
|
80
|
+
int: "1L",
|
|
81
|
+
number: "1.5",
|
|
82
|
+
bool: "true",
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
function only(name , path ) {
|
|
86
|
+
throw new Error(`${path}: ${name} values are only supported as an operation body or result`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function inline(path ) {
|
|
90
|
+
throw new Error(`${path}: inline object and union types are not supported; use model() or union()`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function kindOf(ir , name ) {
|
|
94
|
+
const found = ir.types.find((named) => named.name === name);
|
|
95
|
+
if (found === undefined) throw new Error(`unknown type ${name}`);
|
|
96
|
+
return found.type.kind === "object" || found.type.kind === "union" ? found.type.kind : "other";
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function javaType(node , path ) {
|
|
100
|
+
switch (node.kind) {
|
|
101
|
+
case "primitive":
|
|
102
|
+
return JAVA_TYPE[node.name];
|
|
103
|
+
case "enum":
|
|
104
|
+
return "String";
|
|
105
|
+
case "list":
|
|
106
|
+
return `List<${javaType(node.item, path)}>`;
|
|
107
|
+
case "map":
|
|
108
|
+
return `Map<String, ${javaType(node.value, path)}>`;
|
|
109
|
+
case "ref":
|
|
110
|
+
return node.name;
|
|
111
|
+
case "object":
|
|
112
|
+
case "union":
|
|
113
|
+
return inline(path);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function decoder(ir , node , path ) {
|
|
118
|
+
switch (node.kind) {
|
|
119
|
+
case "primitive":
|
|
120
|
+
return DECODER[node.name] ?? only(node.name, path);
|
|
121
|
+
case "enum":
|
|
122
|
+
return `Decode.enumOf(${node.values.map(str).join(", ")})`;
|
|
123
|
+
case "list":
|
|
124
|
+
return `Decode.listOf(${decoder(ir, node.item, path)})`;
|
|
125
|
+
case "map":
|
|
126
|
+
return `Decode.mapOf(${decoder(ir, node.value, path)})`;
|
|
127
|
+
case "ref":
|
|
128
|
+
kindOf(ir, node.name);
|
|
129
|
+
return `${node.name}::fromJson`;
|
|
130
|
+
case "object":
|
|
131
|
+
case "union":
|
|
132
|
+
return inline(path);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Expression turning a non-null Java value into its JSON tree; decimals become strings, unions add their tag. */
|
|
137
|
+
export function encoder(ir , node , expr , depth = 0) {
|
|
138
|
+
const item = `item${depth}`;
|
|
139
|
+
switch (node.kind) {
|
|
140
|
+
case "primitive":
|
|
141
|
+
return node.name === "decimal" ? `${expr}.toPlainString()` : expr;
|
|
142
|
+
case "list": {
|
|
143
|
+
const inner = encoder(ir, node.item, item, depth + 1);
|
|
144
|
+
return inner === item ? expr : `${expr}.stream().map((${item}) -> ${item} == null ? null : ${inner}).collect(Collectors.toList())`;
|
|
145
|
+
}
|
|
146
|
+
case "map": {
|
|
147
|
+
const inner = encoder(ir, node.value, item, depth + 1);
|
|
148
|
+
if (inner === item) return expr;
|
|
149
|
+
const map = `map${depth}`;
|
|
150
|
+
const value = `${item}.getValue()`;
|
|
151
|
+
return `${expr}.entrySet().stream().collect(LinkedHashMap<String, Object>::new, (${map}, ${item}) -> ${map}.put(${item}.getKey(), ${value} == null ? null : ${encoder(ir, node.value, value, depth + 1)}), Map::putAll)`;
|
|
152
|
+
}
|
|
153
|
+
case "ref":
|
|
154
|
+
return kindOf(ir, node.name) === "union" ? `${node.name}.encode(${expr})` : expr;
|
|
155
|
+
default:
|
|
156
|
+
return expr;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* A Java expression that turns a value given to a mock (a model, a list or map of them, or raw JSON) into its JSON tree,
|
|
162
|
+
* adding union tags; returns undefined when the JSON writer needs no help.
|
|
163
|
+
*/
|
|
164
|
+
export function wire(ir , node , expr , depth = 0) {
|
|
165
|
+
const item = `item${depth}`;
|
|
166
|
+
if (node.kind === "ref" && kindOf(ir, node.name) === "union") return `(${expr} instanceof ${node.name} ? ${node.name}.encode((${node.name}) ${expr}) : ${expr})`;
|
|
167
|
+
if (node.kind === "list") {
|
|
168
|
+
const inner = wire(ir, node.item, item, depth + 1);
|
|
169
|
+
return inner === undefined ? undefined : `(${expr} instanceof List ? ((List<?>) ${expr}).stream().map((${item}) -> ${inner}).collect(Collectors.toList()) : ${expr})`;
|
|
170
|
+
}
|
|
171
|
+
if (node.kind === "map") {
|
|
172
|
+
const inner = wire(ir, node.value, `${item}.getValue()`, depth + 1);
|
|
173
|
+
const map = `map${depth}`;
|
|
174
|
+
return inner === undefined
|
|
175
|
+
? undefined
|
|
176
|
+
: `(${expr} instanceof Map ? ((Map<?, ?>) ${expr}).entrySet().stream().collect(LinkedHashMap<Object, Object>::new, (${map}, ${item}) -> ${map}.put(${item}.getKey(), ${inner}), Map::putAll) : ${expr})`;
|
|
177
|
+
}
|
|
178
|
+
return undefined;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function example(ir , node , path ) {
|
|
182
|
+
switch (node.kind) {
|
|
183
|
+
case "primitive":
|
|
184
|
+
return EXAMPLE[node.name] ?? only(node.name, path);
|
|
185
|
+
case "enum":
|
|
186
|
+
return str(node.values[0] ?? "");
|
|
187
|
+
case "list":
|
|
188
|
+
return "Collections.emptyList()";
|
|
189
|
+
case "map":
|
|
190
|
+
return "Collections.emptyMap()";
|
|
191
|
+
case "ref":
|
|
192
|
+
return kindOf(ir, node.name) === "union" ? `${camel(node.name)}()` : `${camel(node.name)}().build()`;
|
|
193
|
+
case "object":
|
|
194
|
+
case "union":
|
|
195
|
+
return inline(path);
|
|
196
|
+
}
|
|
197
|
+
}
|