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,342 @@
|
|
|
1
|
+
// Models: one immutable class with a builder per object, one interface per union, one exception per named error.
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
import { camel, decoder, encoder, java, javadoc, javaType, pascal, str } from "./names.ts";
|
|
6
|
+
|
|
7
|
+
const RESERVED = new Set([
|
|
8
|
+
"toJson", "toBuilder", "builder", "extraFields", "fromJson", "equals", "hashCode", "toString", "getClass", "encode",
|
|
9
|
+
"wait", "notify", "notifyAll", "clone", "finalize",
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
/** Simple names every model file imports; a model with one of these names would not compile. */
|
|
13
|
+
const IMPORTED = new Map([
|
|
14
|
+
...[
|
|
15
|
+
"java.math.BigDecimal", "java.util.ArrayList", "java.util.Arrays", "java.util.Collections", "java.util.HashSet", "java.util.LinkedHashMap", "java.util.List", "java.util.Map", "java.util.Objects",
|
|
16
|
+
"java.util.Optional", "java.util.Set", "java.util.stream.Collectors", "java.util.HashMap", "java.io.InputStream", "java.net.ProxySelector",
|
|
17
|
+
"javax.net.ssl.SSLContext", "java.util.function.Consumer", "java.util.concurrent.CopyOnWriteArrayList",
|
|
18
|
+
].map((name) => [name.slice(name.lastIndexOf(".") + 1), name]),
|
|
19
|
+
...["ApiException", "Clock", "ClientRuntime", "Decode", "EventListener", "FakeClock", "HttpResponse", "JdkTransport", "UrlConnectionTransport", "Webhooks", "WebhookSignatureException", "EventStream", "ServerSentEvent", "Links", "Waits", "MultipartForm", "Json", "JsonValue", "MiddlewareEntry", "MockTransport", "OperationMock", "OperationSpec", "RawResult", "RecordedEvent", "SdkInfo", "Transport"].map(
|
|
20
|
+
(name) => [name, `the runtime class ${name}`],
|
|
21
|
+
),
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
/** Fails generation for type names that would clash with imports or nested classes. */
|
|
25
|
+
export function checkTypeNames(ir ) {
|
|
26
|
+
for (const named of ir.types) {
|
|
27
|
+
const clash = IMPORTED.get(named.name);
|
|
28
|
+
if (clash !== undefined) throw new Error(`type ${named.name} collides with ${clash}; rename the model`);
|
|
29
|
+
if (named.name === "Builder" || named.name === "Raw") throw new Error(`type ${named.name} collides with the nested ${named.name} class; rename the model`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const MODEL_IMPORTS = [
|
|
34
|
+
"java.math.BigDecimal",
|
|
35
|
+
"java.util.ArrayList",
|
|
36
|
+
"java.util.Arrays",
|
|
37
|
+
"java.util.Collections",
|
|
38
|
+
"java.util.HashSet",
|
|
39
|
+
"java.util.LinkedHashMap",
|
|
40
|
+
"java.util.List",
|
|
41
|
+
"java.util.Map",
|
|
42
|
+
"java.util.Objects",
|
|
43
|
+
"java.util.Optional",
|
|
44
|
+
"java.util.Set",
|
|
45
|
+
"java.util.stream.Collectors",
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
/** The start of every generated Java file: header comment, package and sorted imports. */
|
|
49
|
+
export function fileStart(header , pkg , imports ) {
|
|
50
|
+
const writer = java().line(header).line(`package ${pkg};`).line();
|
|
51
|
+
[...new Set(imports)].sort().forEach((name) => writer.line(`import ${name};`));
|
|
52
|
+
return writer.line();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
function fieldInfos(owner , items ) {
|
|
64
|
+
const infos = items.map((item) => ({
|
|
65
|
+
field: item,
|
|
66
|
+
name: camel(item.name),
|
|
67
|
+
has: `has${pascal(item.name)}`,
|
|
68
|
+
type: javaType(item.type, `types.${owner}.${item.name}`),
|
|
69
|
+
path: `types.${owner}.${item.name}`,
|
|
70
|
+
}));
|
|
71
|
+
const taken = new Set([...RESERVED, ...infos.filter((info) => info.field.optional).map((info) => info.has)]);
|
|
72
|
+
for (const info of infos) {
|
|
73
|
+
if (taken.has(info.name)) throw new Error(`types.${owner}.${info.field.name}: the Java name ${info.name} is reserved for model methods; rename the field`);
|
|
74
|
+
}
|
|
75
|
+
return infos;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function copyIn(info , source ) {
|
|
79
|
+
if (info.field.type.kind === "list") return `${source} == null ? null : Collections.unmodifiableList(new ArrayList<>(${source}))`;
|
|
80
|
+
if (info.field.type.kind === "map") return `${source} == null ? null : Collections.unmodifiableMap(new LinkedHashMap<>(${source}))`;
|
|
81
|
+
return source;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function deprecatedTags(deprecated ) {
|
|
85
|
+
return deprecated === undefined ? [] : [`@deprecated ${deprecated}`];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function emitAccessors(body , fields ) {
|
|
89
|
+
for (const info of fields) {
|
|
90
|
+
const doc = [info.field.doc ?? info.field.name, ...(info.field.optional ? ["", "Empty when the field was absent or null."] : [])];
|
|
91
|
+
body.line();
|
|
92
|
+
if (info.field.optional) {
|
|
93
|
+
const tags = deprecatedTags(info.field.deprecated);
|
|
94
|
+
javadoc(body, doc, tags);
|
|
95
|
+
if (tags.length > 0) body.line("@Deprecated");
|
|
96
|
+
body.line(`public Optional<${info.type}> ${info.name}() {`).indent((fn) => fn.line(`return Optional.ofNullable(${info.name});`)).line("}");
|
|
97
|
+
body.line();
|
|
98
|
+
javadoc(body, [`True when ${info.field.name} was set, even to null.`]);
|
|
99
|
+
body.line(`public boolean ${info.has}() {`).indent((fn) => fn.line(`return ${info.has};`)).line("}");
|
|
100
|
+
} else {
|
|
101
|
+
const tags = deprecatedTags(info.field.deprecated);
|
|
102
|
+
javadoc(body, [...doc, ...(info.field.nullable ? ["", "May be null."] : [])], tags);
|
|
103
|
+
if (tags.length > 0) body.line("@Deprecated");
|
|
104
|
+
body.line(`public ${info.type} ${info.name}() {`).indent((fn) => fn.line(`return ${info.name};`)).line("}");
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function emitBuilder(body , name , fields ) {
|
|
110
|
+
javadoc(body, [`Builds ${name} values; required fields are checked by build().`]);
|
|
111
|
+
body.line("public static final class Builder {").indent((builder) => {
|
|
112
|
+
fields.forEach((info) => {
|
|
113
|
+
builder.line(`private ${info.type} ${info.name};`);
|
|
114
|
+
if (info.field.optional) builder.line(`private boolean ${info.has};`);
|
|
115
|
+
});
|
|
116
|
+
builder.line("private Map<String, Object> extraFields = new LinkedHashMap<>();").line();
|
|
117
|
+
builder.line("private Builder() {").line("}");
|
|
118
|
+
for (const info of fields) {
|
|
119
|
+
builder.line();
|
|
120
|
+
const tags = deprecatedTags(info.field.deprecated);
|
|
121
|
+
javadoc(builder, [`Sets ${info.field.name}.`], tags);
|
|
122
|
+
if (tags.length > 0) builder.line("@Deprecated");
|
|
123
|
+
builder.line(`public Builder ${info.name}(${info.type} value) {`).indent((fn) => {
|
|
124
|
+
fn.line(`this.${info.name} = value;`);
|
|
125
|
+
if (info.field.optional) fn.line(`this.${info.has} = true;`);
|
|
126
|
+
fn.line("return this;");
|
|
127
|
+
});
|
|
128
|
+
builder.line("}");
|
|
129
|
+
}
|
|
130
|
+
builder.line();
|
|
131
|
+
javadoc(builder, ["Fields this SDK version does not know; they are sent back unchanged."]);
|
|
132
|
+
builder.line("public Builder extraFields(Map<String, Object> value) {").indent((fn) => fn.line("this.extraFields = new LinkedHashMap<>(value);").line("return this;")).line("}");
|
|
133
|
+
builder.line();
|
|
134
|
+
javadoc(builder, [`Creates the ${name}; throws IllegalStateException when a required field is missing.`]);
|
|
135
|
+
builder.line(`public ${name} build() {`).indent((fn) => fn.line(`return new ${name}(this);`)).line("}");
|
|
136
|
+
});
|
|
137
|
+
body.line("}");
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function emitObject(ir , named , node , unions , header , pkg ) {
|
|
141
|
+
const name = named.name;
|
|
142
|
+
const fields = fieldInfos(name, node.fields);
|
|
143
|
+
const writer = fileStart(header, pkg, [...MODEL_IMPORTS, `${pkg}.runtime.Decode`, `${pkg}.runtime.Json`, `${pkg}.runtime.JsonValue`]);
|
|
144
|
+
const tags = deprecatedTags(named.deprecated);
|
|
145
|
+
javadoc(writer, [named.doc ?? name, "", "Immutable. Fields the server adds later are kept in extraFields() and sent back unchanged."], tags);
|
|
146
|
+
if (tags.length > 0) writer.line("@Deprecated");
|
|
147
|
+
writer.line(`public final class ${name} implements ${["JsonValue", ...unions].join(", ")} {`).indent((body) => {
|
|
148
|
+
body.line(`private static final Set<String> KEYS = Collections.unmodifiableSet(new HashSet<>(Arrays.<String>asList(${node.fields.map((item) => str(item.name)).join(", ")})));`).line();
|
|
149
|
+
fields.forEach((info) => {
|
|
150
|
+
body.line(`private final ${info.type} ${info.name};`);
|
|
151
|
+
if (info.field.optional) body.line(`private final boolean ${info.has};`);
|
|
152
|
+
});
|
|
153
|
+
body.line("private final Map<String, Object> extraFields;").line();
|
|
154
|
+
|
|
155
|
+
body.line(`private ${name}(Builder builder) {`).indent((fn) => {
|
|
156
|
+
fields
|
|
157
|
+
.filter((info) => !info.field.optional && !info.field.nullable)
|
|
158
|
+
.forEach((info) => fn.line(`if (builder.${info.name} == null) {`).indent((branch) => branch.line(`throw new IllegalStateException(${str(`${name}.${info.field.name} is required`)});`)).line("}"));
|
|
159
|
+
fields.forEach((info) => {
|
|
160
|
+
fn.line(`this.${info.name} = ${copyIn(info, `builder.${info.name}`)};`);
|
|
161
|
+
if (info.field.optional) fn.line(`this.${info.has} = builder.${info.has};`);
|
|
162
|
+
});
|
|
163
|
+
fn.line("this.extraFields = Collections.unmodifiableMap(new LinkedHashMap<>(builder.extraFields));");
|
|
164
|
+
});
|
|
165
|
+
body.line("}");
|
|
166
|
+
|
|
167
|
+
body.line();
|
|
168
|
+
javadoc(body, [`A new builder for ${name}.`]);
|
|
169
|
+
body.line("public static Builder builder() {").indent((fn) => fn.line("return new Builder();")).line("}");
|
|
170
|
+
body.line();
|
|
171
|
+
javadoc(body, ["A builder that starts from this value."]);
|
|
172
|
+
body.line("public Builder toBuilder() {").indent((fn) => {
|
|
173
|
+
fn.line("Builder builder = new Builder();");
|
|
174
|
+
fields.forEach((info) => {
|
|
175
|
+
fn.line(`builder.${info.name} = this.${info.name};`);
|
|
176
|
+
if (info.field.optional) fn.line(`builder.${info.has} = this.${info.has};`);
|
|
177
|
+
});
|
|
178
|
+
fn.line("builder.extraFields = new LinkedHashMap<>(this.extraFields);").line("return builder;");
|
|
179
|
+
});
|
|
180
|
+
body.line("}");
|
|
181
|
+
|
|
182
|
+
emitAccessors(body, fields);
|
|
183
|
+
body.line();
|
|
184
|
+
javadoc(body, ["Fields this SDK version does not know, as received."]);
|
|
185
|
+
body.line("public Map<String, Object> extraFields() {").indent((fn) => fn.line("return extraFields;")).line("}");
|
|
186
|
+
|
|
187
|
+
body.line();
|
|
188
|
+
javadoc(body, [`Reads a ${name} from parsed JSON.`, "", "throws DecodeException naming the JSON path of the first mismatch"]);
|
|
189
|
+
body.line(`public static ${name} fromJson(Object value) {`).indent((fn) => fn.line('return fromJson(value, "$");')).line("}");
|
|
190
|
+
body.line();
|
|
191
|
+
javadoc(body, [`Reads a ${name} from parsed JSON found at the given JSON path.`]);
|
|
192
|
+
body.line(`public static ${name} fromJson(Object value, String path) {`).indent((fn) => {
|
|
193
|
+
fn.line("Map<String, Object> obj = Decode.object(value, path);").line("Builder builder = new Builder();");
|
|
194
|
+
fields.forEach((info) => {
|
|
195
|
+
const call = `Decode.<${info.type}>required(obj, ${str(info.field.name)}, path, ${decoder(ir, info.field.type, info.path)}, ${info.field.nullable})`;
|
|
196
|
+
if (info.field.optional) {
|
|
197
|
+
// Many servers send null for a field they leave out: null for an optional field that cannot be null is absent.
|
|
198
|
+
fn.line(`if (${info.field.nullable ? `obj.containsKey(${str(info.field.name)})` : `obj.get(${str(info.field.name)}) != null`}) {`).indent((branch) => branch.line(`builder.${info.name} = ${call};`).line(`builder.${info.has} = true;`)).line("}");
|
|
199
|
+
} else {
|
|
200
|
+
fn.line(`builder.${info.name} = ${call};`);
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
fn.line("builder.extraFields = new LinkedHashMap<>(Decode.extra(obj, KEYS));").line("return builder.build();");
|
|
204
|
+
});
|
|
205
|
+
body.line("}");
|
|
206
|
+
|
|
207
|
+
body.line();
|
|
208
|
+
javadoc(body, ["The JSON tree of this value: unset optional fields are left out, extra fields are kept."]);
|
|
209
|
+
body.line("@Override").line("public Object toJson() {").indent((fn) => {
|
|
210
|
+
fn.line("Map<String, Object> json$ = new LinkedHashMap<>(this.extraFields);");
|
|
211
|
+
fields.forEach((info) => {
|
|
212
|
+
const field = `this.${info.name}`;
|
|
213
|
+
const value = `${field} == null ? null : ${encoder(ir, info.field.type, field)}`;
|
|
214
|
+
if (info.field.optional) fn.line(`if (this.${info.has}) {`).indent((branch) => branch.line(`json$.put(${str(info.field.name)}, ${value});`)).line("}");
|
|
215
|
+
else fn.line(`json$.put(${str(info.field.name)}, ${value});`);
|
|
216
|
+
});
|
|
217
|
+
fn.line("return json$;");
|
|
218
|
+
});
|
|
219
|
+
body.line("}");
|
|
220
|
+
|
|
221
|
+
const compared = fields.flatMap((info) => (info.field.optional ? [info.name, info.has] : [info.name]));
|
|
222
|
+
body.line();
|
|
223
|
+
javadoc(body, ["Equal when every known field is equal; extra fields are not compared."]);
|
|
224
|
+
body.line("@Override").line("public boolean equals(Object object$) {").indent((fn) => {
|
|
225
|
+
fn.line("if (this == object$) {").indent((b) => b.line("return true;")).line("}");
|
|
226
|
+
fn.line(`if (!(object$ instanceof ${name})) {`).indent((b) => b.line("return false;")).line("}");
|
|
227
|
+
if (compared.length === 0) {
|
|
228
|
+
fn.line("return true;");
|
|
229
|
+
} else {
|
|
230
|
+
const flags = new Set(fields.map((info) => info.has));
|
|
231
|
+
fn.line(`${name} that$ = (${name}) object$;`);
|
|
232
|
+
fn.line(`return ${compared.map((field) => (flags.has(field) && !fields.some((info) => info.name === field) ? `this.${field} == that$.${field}` : `Objects.equals(this.${field}, that$.${field})`)).join(" && ")};`);
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
body.line("}");
|
|
236
|
+
body.line().line("@Override").line("public int hashCode() {").indent((fn) => fn.line(`return Objects.hash(${compared.map((field) => `this.${field}`).join(", ")});`)).line("}");
|
|
237
|
+
body.line().line("@Override").line("public String toString() {").indent((fn) => fn.line(`return ${str(name)} + Json.write(this);`)).line("}");
|
|
238
|
+
body.line();
|
|
239
|
+
emitBuilder(body, name, fields);
|
|
240
|
+
});
|
|
241
|
+
return writer.line("}").toString();
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function emitUnion(named , node , header , pkg ) {
|
|
245
|
+
const name = named.name;
|
|
246
|
+
const writer = fileStart(header, pkg, ["java.util.LinkedHashMap", "java.util.Map", `${pkg}.runtime.Decode`, `${pkg}.runtime.JsonValue`]);
|
|
247
|
+
javadoc(writer, [
|
|
248
|
+
named.doc ?? name,
|
|
249
|
+
"",
|
|
250
|
+
`One of: ${node.variants.map((variant) => `${variant.type} ("${variant.tag}")`).join(", ")}, or Unknown${name} for a variant this SDK version does not know yet.`,
|
|
251
|
+
]);
|
|
252
|
+
writer.line(`public interface ${name} extends JsonValue {`).indent((body) => {
|
|
253
|
+
javadoc(body, [`Reads a ${name}, choosing the variant by the "${node.discriminator}" field.`]);
|
|
254
|
+
body.line(`static ${name} fromJson(Object value) {`).indent((fn) => fn.line('return fromJson(value, "$");')).line("}");
|
|
255
|
+
body.line();
|
|
256
|
+
javadoc(body, [`Reads a ${name} found at the given JSON path.`]);
|
|
257
|
+
body.line(`static ${name} fromJson(Object value, String path) {`).indent((fn) => {
|
|
258
|
+
fn.line("Map<String, Object> obj = Decode.object(value, path);");
|
|
259
|
+
fn.line(`String tag = Decode.string(obj.get(${str(node.discriminator)}), path + ${str(`.${node.discriminator}`)});`);
|
|
260
|
+
node.variants.forEach((variant) =>
|
|
261
|
+
fn.line(`if (tag.equals(${str(variant.tag)})) {`).indent((branch) => branch.line(`return ${variant.type}.fromJson(obj, path);`)).line("}"),
|
|
262
|
+
);
|
|
263
|
+
fn.line(`return new Unknown${name}(tag, obj);`);
|
|
264
|
+
});
|
|
265
|
+
body.line("}");
|
|
266
|
+
body.line();
|
|
267
|
+
javadoc(body, [`The JSON tree of a ${name}, including the "${node.discriminator}" field.`]);
|
|
268
|
+
body.line(`static Object encode(${name} value) {`).indent((fn) => {
|
|
269
|
+
node.variants.forEach((variant) =>
|
|
270
|
+
fn.line(`if (value instanceof ${variant.type}) {`).indent((branch) => {
|
|
271
|
+
branch.line("Map<String, Object> data = new LinkedHashMap<>(Decode.object(value.toJson(), \"$\"));");
|
|
272
|
+
branch.line(`data.put(${str(node.discriminator)}, ${str(variant.tag)});`).line("return data;");
|
|
273
|
+
}).line("}"),
|
|
274
|
+
);
|
|
275
|
+
fn.line("return value.toJson();");
|
|
276
|
+
});
|
|
277
|
+
body.line("}");
|
|
278
|
+
});
|
|
279
|
+
return writer.line("}").toString();
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function emitUnknown(named , header , pkg ) {
|
|
283
|
+
const name = `Unknown${named.name}`;
|
|
284
|
+
const writer = fileStart(header, pkg, ["java.util.Collections", "java.util.LinkedHashMap", "java.util.Map"]);
|
|
285
|
+
javadoc(writer, [`A ${named.name} variant this SDK version does not know yet; raw() keeps all its data.`]);
|
|
286
|
+
writer.line(`public final class ${name} implements ${named.name} {`).indent((body) => {
|
|
287
|
+
body.line("private final String tag;").line("private final Map<String, Object> raw;").line();
|
|
288
|
+
javadoc(body, ["Wraps an unknown variant."]);
|
|
289
|
+
body.line(`public ${name}(String tag, Map<String, Object> raw) {`).indent((fn) => fn.line("this.tag = tag;").line("this.raw = Collections.unmodifiableMap(new LinkedHashMap<>(raw));")).line("}");
|
|
290
|
+
body.line();
|
|
291
|
+
javadoc(body, ["The discriminator value the server sent."]);
|
|
292
|
+
body.line("public String tag() {").indent((fn) => fn.line("return tag;")).line("}");
|
|
293
|
+
body.line();
|
|
294
|
+
javadoc(body, ["Every field as received, including the discriminator."]);
|
|
295
|
+
body.line("public Map<String, Object> raw() {").indent((fn) => fn.line("return raw;")).line("}");
|
|
296
|
+
body.line().line("@Override").line("public Object toJson() {").indent((fn) => fn.line("return raw;")).line("}");
|
|
297
|
+
body.line().line("@Override").line("public boolean equals(Object other) {").indent((fn) =>
|
|
298
|
+
fn.line(`return other instanceof ${name} && ((${name}) other).raw.equals(raw);`),
|
|
299
|
+
).line("}");
|
|
300
|
+
body.line().line("@Override").line("public int hashCode() {").indent((fn) => fn.line("return raw.hashCode();")).line("}");
|
|
301
|
+
body.line().line("@Override").line("public String toString() {").indent((fn) => fn.line(`return ${str(name)} + raw;`)).line("}");
|
|
302
|
+
});
|
|
303
|
+
return writer.line("}").toString();
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
export function exceptionName(name ) {
|
|
307
|
+
return `${name.replace(/(Error|Exception)$/, "")}Exception`;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
export function emitException(name , statuses , header , pkg ) {
|
|
311
|
+
const className = exceptionName(name);
|
|
312
|
+
const writer = fileStart(header, pkg, [`${pkg}.runtime.ApiException`, `${pkg}.runtime.HttpResponse`]);
|
|
313
|
+
javadoc(writer, [`Thrown for HTTP ${statuses.join(", ")} (${name}).`]);
|
|
314
|
+
writer.line(`public final class ${className} extends ApiException {`).indent((body) => {
|
|
315
|
+
body.line("private static final long serialVersionUID = 1L;").line();
|
|
316
|
+
javadoc(body, ["Created by the client from the response."]);
|
|
317
|
+
body.line(`public ${className}(int status, Object body, HttpResponse response) {`).indent((fn) => fn.line(`super(status, body, response, ${str(name)});`)).line("}");
|
|
318
|
+
});
|
|
319
|
+
return writer.line("}").toString();
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** File name (without .java) → text, for every model, union and named error. */
|
|
323
|
+
export function emitModels(ir , header , pkg ) {
|
|
324
|
+
checkTypeNames(ir);
|
|
325
|
+
const files = new Map ();
|
|
326
|
+
const unionsOf = new Map ();
|
|
327
|
+
ir.types.forEach((named) => {
|
|
328
|
+
if (named.type.kind !== "union") return;
|
|
329
|
+
named.type.variants.forEach((variant) => unionsOf.set(variant.type, [...(unionsOf.get(variant.type) ?? []), named.name]));
|
|
330
|
+
});
|
|
331
|
+
for (const named of ir.types) {
|
|
332
|
+
if (named.type.kind === "object") files.set(named.name, emitObject(ir, named, named.type, unionsOf.get(named.name) ?? [], header, pkg));
|
|
333
|
+
if (named.type.kind === "union") {
|
|
334
|
+
files.set(named.name, emitUnion(named, named.type, header, pkg));
|
|
335
|
+
files.set(`Unknown${named.name}`, emitUnknown(named, header, pkg));
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
const statuses = new Map ();
|
|
339
|
+
ir.operations.forEach((operation) => operation.errors.forEach((error) => statuses.set(error.name, [...(statuses.get(error.name) ?? []), error.status])));
|
|
340
|
+
statuses.forEach((codes, name) => files.set(exceptionName(name), emitException(name, [...new Set(codes)].sort((a, b) => a - b), header, pkg)));
|
|
341
|
+
return files;
|
|
342
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// <pkg>.testing: <Name>Mock (a client answered by typed mocks) and <Name>Examples (valid example values).
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
import { clientName, configFields } from "./emit-client.ts";
|
|
5
|
+
import { fileStart, MODEL_IMPORTS } from "./emit-models.ts";
|
|
6
|
+
import { camel, decoder, example, javadoc, str, wire } from "./names.ts";
|
|
7
|
+
|
|
8
|
+
const MOCK_BASE_URL = "http://mock.sdkloom.invalid";
|
|
9
|
+
|
|
10
|
+
function placeholder(item ) {
|
|
11
|
+
const type = item.type;
|
|
12
|
+
if (type.kind === "enum") return str(type.values[0] ?? "");
|
|
13
|
+
if (type.kind === "primitive") {
|
|
14
|
+
if (type.name === "int") return "0L";
|
|
15
|
+
if (type.name === "number") return "0.0";
|
|
16
|
+
if (type.name === "bool") return "false";
|
|
17
|
+
if (type.name === "decimal") return "BigDecimal.ZERO";
|
|
18
|
+
if (type.name === "string" || type.name === "secret" || type.name === "datetime") return str("test");
|
|
19
|
+
}
|
|
20
|
+
throw new Error(`config field ${item.name}: the mock client cannot invent a value`);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Mock methods besides the operations; an operation with one of these names would not compile. */
|
|
24
|
+
const MOCK_METHODS = ["client", "transport", "clock", "events", "create", "equals", "hashCode", "toString", "getClass", "wait", "notify", "notifyAll"];
|
|
25
|
+
|
|
26
|
+
export function emitMock(ir , header , pkg ) {
|
|
27
|
+
const client = clientName(ir);
|
|
28
|
+
const name = `${ir.name}Mock`;
|
|
29
|
+
for (const operation of ir.operations) {
|
|
30
|
+
if (MOCK_METHODS.includes(camel(operation.name))) throw new Error(`operation ${camel(operation.name)} collides with ${name}.${camel(operation.name)}(); rename the operation`);
|
|
31
|
+
}
|
|
32
|
+
const writer = fileStart(header, `${pkg}.testing`, [
|
|
33
|
+
...MODEL_IMPORTS,
|
|
34
|
+
"java.util.concurrent.CopyOnWriteArrayList",
|
|
35
|
+
"java.util.function.Consumer",
|
|
36
|
+
`${pkg}.*`,
|
|
37
|
+
...["Decode", "FakeClock", "MockTransport", "OperationMock", "RecordedEvent"].map((runtime) => `${pkg}.runtime.${runtime}`),
|
|
38
|
+
]);
|
|
39
|
+
javadoc(writer, [
|
|
40
|
+
`A ${client} answered by typed mocks: middleware still runs, the clock is fake and events are recorded.`,
|
|
41
|
+
"",
|
|
42
|
+
`Use: ${name} mock = ${name}.create(); ${client} client = mock.client();`,
|
|
43
|
+
]);
|
|
44
|
+
writer.line(`public final class ${name} {`).indent((body) => {
|
|
45
|
+
body.line(`private static final String MOCK_BASE_URL = ${str(MOCK_BASE_URL)};`).line();
|
|
46
|
+
body.line("private final MockTransport transport$ = new MockTransport();").line("private final FakeClock clock$ = new FakeClock();");
|
|
47
|
+
body.line("private final List<RecordedEvent> events$ = new CopyOnWriteArrayList<>();");
|
|
48
|
+
ir.operations.forEach((operation) => body.line(`private final OperationMock ${camel(operation.name)}$;`));
|
|
49
|
+
body.line(`private final ${client} client$;`).line();
|
|
50
|
+
body.line(`private ${name}(Consumer<${client}.Builder> configure) {`).indent((fn) => {
|
|
51
|
+
ir.operations.forEach((operation) => {
|
|
52
|
+
if (operation.events !== undefined) {
|
|
53
|
+
// returns() takes the list of ServerSentEvent to send; each event's data is checked and encoded.
|
|
54
|
+
const text = operation.events.kind === "primitive" && operation.events.name === "string";
|
|
55
|
+
const check = text ? "null" : decoder(ir, operation.events, `operations.${operation.name}.events`);
|
|
56
|
+
const encode = text ? undefined : wire(ir, operation.events, "value");
|
|
57
|
+
fn.line(`${camel(operation.name)}$ = new OperationMock(${str(operation.name)}, transport$.route(${str(operation.method)}, ${str(operation.path)}), ${check}, ${encode === undefined ? "null" : `(value) -> ${encode}`}, true);`);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const decode = operation.returns !== undefined && !(operation.returns.kind === "primitive" && ["bytes", "stream"].includes(operation.returns.name))
|
|
61
|
+
? decoder(ir, operation.returns, `operations.${operation.name}.returns`)
|
|
62
|
+
: "null";
|
|
63
|
+
const encode = operation.returns === undefined ? undefined : wire(ir, operation.returns, "value");
|
|
64
|
+
fn.line(`${camel(operation.name)}$ = new OperationMock(${str(operation.name)}, transport$.route(${str(operation.method)}, ${str(operation.path)}), ${decode}${encode === undefined ? "" : `, (value) -> ${encode}`});`);
|
|
65
|
+
});
|
|
66
|
+
const settings = configFields(ir).filter((item) => !item.optional).map((item) => `.${camel(item.name)}(${placeholder(item)})`);
|
|
67
|
+
fn.line(`${client}.Builder builder = ${client}.builder()${settings.join("")}`).indent((chain) =>
|
|
68
|
+
chain.line(".baseUrl(MOCK_BASE_URL)").line(".transport(transport$)").line(".clock(clock$)").line(".onEvent((event, fields) -> events$.add(new RecordedEvent(event, fields)));"),
|
|
69
|
+
);
|
|
70
|
+
fn.line("configure.accept(builder);").line("client$ = builder.build();");
|
|
71
|
+
});
|
|
72
|
+
body.line("}");
|
|
73
|
+
body.line();
|
|
74
|
+
javadoc(body, ["A mock client with placeholder configuration."]);
|
|
75
|
+
body.line(`public static ${name} create() {`).indent((fn) => fn.line("return create((builder) -> { });")).line("}");
|
|
76
|
+
body.line();
|
|
77
|
+
javadoc(body, ["A mock client whose builder you adjust, e.g. create(b -> b.apiKey(\"sk_test\"))."]);
|
|
78
|
+
body.line(`public static ${name} create(Consumer<${client}.Builder> configure) {`).indent((fn) => fn.line(`return new ${name}(configure);`)).line("}");
|
|
79
|
+
body.line();
|
|
80
|
+
javadoc(body, ["The client under test."]);
|
|
81
|
+
body.line(`public ${client} client() {`).indent((fn) => fn.line("return client$;")).line("}");
|
|
82
|
+
ir.operations.forEach((operation) => {
|
|
83
|
+
body.line();
|
|
84
|
+
javadoc(body, [`The mock answering ${operation.name} (${operation.method} ${operation.path}).`]);
|
|
85
|
+
body.line(`public OperationMock ${camel(operation.name)}() {`).indent((fn) => fn.line(`return ${camel(operation.name)}$;`)).line("}");
|
|
86
|
+
});
|
|
87
|
+
body.line();
|
|
88
|
+
javadoc(body, ["The transport, for raw routes and every recorded request."]);
|
|
89
|
+
body.line("public MockTransport transport() {").indent((fn) => fn.line("return transport$;")).line("}");
|
|
90
|
+
body.line();
|
|
91
|
+
javadoc(body, ["The fake clock; it moves only when middleware sleeps or you advance it."]);
|
|
92
|
+
body.line("public FakeClock clock() {").indent((fn) => fn.line("return clock$;")).line("}");
|
|
93
|
+
body.line();
|
|
94
|
+
javadoc(body, ["Events emitted so far, in order."]);
|
|
95
|
+
body.line("public List<RecordedEvent> events() {").indent((fn) => fn.line("return Collections.unmodifiableList(new ArrayList<>(events$));")).line("}");
|
|
96
|
+
});
|
|
97
|
+
return writer.line("}").toString();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function emitExamples(ir , header , pkg ) {
|
|
101
|
+
const name = `${ir.name}Examples`;
|
|
102
|
+
const writer = fileStart(header, `${pkg}.testing`, [...MODEL_IMPORTS, `${pkg}.*`]);
|
|
103
|
+
javadoc(writer, ["Small, valid example values for tests; set the fields a test cares about on the returned builder."]);
|
|
104
|
+
writer.line('@SuppressWarnings("deprecation")').line(`public final class ${name} {`).indent((body) => {
|
|
105
|
+
body.line(`private ${name}() {`).line("}");
|
|
106
|
+
for (const named of ir.types) {
|
|
107
|
+
const type = named.type;
|
|
108
|
+
if (type.kind === "object") {
|
|
109
|
+
const required = type.fields.filter((item) => !item.optional && !item.nullable).map((item) => `.${camel(item.name)}(${example(ir, item.type, `types.${named.name}.${item.name}`)})`);
|
|
110
|
+
body.line();
|
|
111
|
+
javadoc(body, [`A ${named.name} builder with every required, non-nullable field set.`]);
|
|
112
|
+
body.line(`public static ${named.name}.Builder ${camel(named.name)}() {`).indent((fn) => fn.line(`return ${named.name}.builder()${required.join("")};`)).line("}");
|
|
113
|
+
} else if (type.kind === "union") {
|
|
114
|
+
const first = type.variants[0];
|
|
115
|
+
if (first === undefined) throw new Error(`types.${named.name}: a union needs a variant`);
|
|
116
|
+
body.line();
|
|
117
|
+
javadoc(body, [`A ${named.name}: the first variant, ${first.type}.`]);
|
|
118
|
+
body.line(`public static ${named.name} ${camel(named.name)}() {`).indent((fn) => fn.line(`return ${camel(first.type)}().build();`)).line("}");
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
return writer.line("}").toString();
|
|
123
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// <Name>Webhooks.java: verify<Name> per webhook, which checks the signature (kit block webhooks.verify) and decodes the
|
|
2
|
+
// body into the payload type.
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
import { MODEL_IMPORTS, fileStart } from "./emit-models.ts";
|
|
6
|
+
import { decoder, javadoc, javaType, pascal, str } from "./names.ts";
|
|
7
|
+
|
|
8
|
+
export function webhooksClass(ir ) {
|
|
9
|
+
return `${ir.name}Webhooks`;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function webhookMethod(name ) {
|
|
13
|
+
return `verify${pascal(name)}`;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function constant(hook ) {
|
|
17
|
+
return `${hook.name.replace(/[A-Z]/g, (letter) => `_${letter}`).toUpperCase()}_SIGNATURE`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function signature(hook ) {
|
|
21
|
+
const spec = hook.signature;
|
|
22
|
+
const text = (value ) => (value === undefined ? "null" : str(value));
|
|
23
|
+
return `new Webhooks.Signature(${[
|
|
24
|
+
str(spec.header), str(spec.encoding), text(spec.prefix), text(spec.separator), str(spec.signed),
|
|
25
|
+
text(spec.timestamp?.header), `${String(spec.timestamp?.toleranceSeconds ?? 0)}L`, str(spec.secret), text(spec.secretPrefix),
|
|
26
|
+
].join(", ")})`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function emitWebhooks(ir , header , pkg ) {
|
|
30
|
+
const name = webhooksClass(ir);
|
|
31
|
+
if (ir.types.some((named) => named.name === name)) throw new Error(`type ${name} collides with the generated webhooks class; rename the model`);
|
|
32
|
+
const writer = fileStart(header, pkg, [
|
|
33
|
+
...MODEL_IMPORTS,
|
|
34
|
+
"java.nio.charset.StandardCharsets",
|
|
35
|
+
...["Decode", "Decoder", "Json", "Webhooks", "WebhookSignatureException"].map((item) => `${pkg}.runtime.${item}`),
|
|
36
|
+
]);
|
|
37
|
+
javadoc(writer, [`Verifies the webhooks ${ir.name} sends and reads their bodies.`]);
|
|
38
|
+
writer.line(`public final class ${name} {`).indent((body) => {
|
|
39
|
+
for (const hook of ir.webhooks ?? []) {
|
|
40
|
+
body.line(`private static final Webhooks.Signature ${constant(hook)} = ${signature(hook)};`);
|
|
41
|
+
}
|
|
42
|
+
body.line().line(`private ${name}() {`).line("}");
|
|
43
|
+
for (const hook of ir.webhooks ?? []) {
|
|
44
|
+
const type = javaType(hook.payload, `webhooks.${hook.name}.payload`);
|
|
45
|
+
const method = webhookMethod(hook.name);
|
|
46
|
+
body.line();
|
|
47
|
+
javadoc(
|
|
48
|
+
body,
|
|
49
|
+
[
|
|
50
|
+
...(hook.doc === undefined ? [] : [hook.doc, ""]),
|
|
51
|
+
`Verifies a ${hook.name} webhook and reads its body. Pass the raw body, exactly as received, and the request headers.`,
|
|
52
|
+
],
|
|
53
|
+
["@throws WebhookSignatureException when the signature does not match the secret or the webhook is too old or too new", `@throws ${pkg}.runtime.DecodeException when the body does not match the payload type`],
|
|
54
|
+
);
|
|
55
|
+
body.line(`public static ${type} ${method}(byte[] body, Map<String, String> headers, String secret) {`).indent((fn) => {
|
|
56
|
+
fn.line(`return ${method}(body, headers, secret, System.currentTimeMillis() / 1000L);`);
|
|
57
|
+
});
|
|
58
|
+
body.line("}").line();
|
|
59
|
+
javadoc(body, [`Like ${method}(body, headers, secret), with the current time in seconds given.`]);
|
|
60
|
+
body.line(`public static ${type} ${method}(byte[] body, Map<String, String> headers, String secret, long nowSeconds) {`).indent((fn) => {
|
|
61
|
+
fn.line(`Webhooks.verify(${constant(hook)}, body, headers, secret, nowSeconds);`);
|
|
62
|
+
fn.line(`Decoder<${type}> decode = ${decoder(ir, hook.payload, `webhooks.${hook.name}.payload`)};`);
|
|
63
|
+
fn.line('return decode.decode(Json.parse(new String(body, StandardCharsets.UTF_8)), "$");');
|
|
64
|
+
});
|
|
65
|
+
body.line("}");
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
writer.line("}");
|
|
69
|
+
return writer.toString();
|
|
70
|
+
}
|