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,245 @@
|
|
|
1
|
+
// Generates the C# SDK as an in-memory file tree: one netstandard2.1 project with the vendored kit under <Ns>.Runtime.
|
|
2
|
+
|
|
3
|
+
import { createFileTree, sha256, } from "@sdkloom/codegen";
|
|
4
|
+
|
|
5
|
+
import { parseBlocks, } from "@sdkloom/kit";
|
|
6
|
+
import { csharpName, transpileLogic, transpileMiddleware, TranspileError } from "@sdkloom/transpile";
|
|
7
|
+
import { emitWebhooks, webhooksClass } from "./emit-webhooks.ts";
|
|
8
|
+
import { emitClientFile, emitErrors } from "./emit-client.ts";
|
|
9
|
+
import { emitMock } from "./emit-mock.ts";
|
|
10
|
+
import { emitModels } from "./emit-models.ts";
|
|
11
|
+
import { csharp, doc, pascal } 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
|
+
const TAG_LINE = /^\s*\/\/\s*@([a-z][a-z-]*)\b/;
|
|
47
|
+
const NAMESPACE = /^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)*$/;
|
|
48
|
+
const PACKAGE_ID = /^[A-Za-z0-9_][A-Za-z0-9_.-]{0,99}$/;
|
|
49
|
+
const KIT_NAMESPACE = "Sdkloom.Runtime";
|
|
50
|
+
export const CUSTOM_FILE = "custom/csharp/Middleware.cs";
|
|
51
|
+
|
|
52
|
+
/** "acme-payments" → "Acme.Payments": the namespace when the config names none. */
|
|
53
|
+
export function defaultNamespace(packageId ) {
|
|
54
|
+
return packageId
|
|
55
|
+
.split(/[.\-_]+/)
|
|
56
|
+
.filter((part) => part !== "")
|
|
57
|
+
.map((part) => (/^[0-9]/.test(part) ? `N${part}` : part.charAt(0).toUpperCase() + part.slice(1)))
|
|
58
|
+
.join(".");
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function blockDoc(block , kit ) {
|
|
62
|
+
return [
|
|
63
|
+
block.summary ?? "",
|
|
64
|
+
"",
|
|
65
|
+
`Supported versions: .NET ${block.versions ?? kit.supports}`,
|
|
66
|
+
"Sources:",
|
|
67
|
+
...block.sources.flatMap((source) => [` [${source.index}] ${source.url}`, ...block.quotes.filter((quote) => quote.index === source.index).map((quote) => ` "${quote.text}"`)]),
|
|
68
|
+
` Retrieved: ${block.retrieved ?? "unknown"}`,
|
|
69
|
+
`Provenance: block ${block.id} · kit ${kit.name} ${kit.version} · sha256:${sha256(block.code).slice(0, 12)}`,
|
|
70
|
+
];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function vendor(file , kit , header , namespace ) {
|
|
74
|
+
const parsed = parseBlocks(file.text, { commentPrefix: "//", file: file.path });
|
|
75
|
+
if (parsed.errors.length > 0) throw new Error(`kit file ${file.path} is invalid: ${parsed.errors.map((error) => error.message).join("; ")}`);
|
|
76
|
+
const writer = csharp().line(header);
|
|
77
|
+
let index = 0;
|
|
78
|
+
let inMetadata = false;
|
|
79
|
+
for (const line of file.text.split("\n")) {
|
|
80
|
+
const tag = TAG_LINE.exec(line)?.[1];
|
|
81
|
+
const indent = /^\s*/.exec(line)?.[0] ?? "";
|
|
82
|
+
if (tag === "block") {
|
|
83
|
+
const block = parsed.blocks[index];
|
|
84
|
+
index += 1;
|
|
85
|
+
inMetadata = true;
|
|
86
|
+
if (block !== undefined) blockDoc(block, kit).forEach((text) => writer.line(text === "" ? `${indent}//` : `${indent}// ${text}`));
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (tag === "end" || (inMetadata && tag !== undefined)) continue;
|
|
90
|
+
inMetadata = false;
|
|
91
|
+
writer.line(line.replace(`namespace ${KIT_NAMESPACE}`, `namespace ${namespace}.Runtime`));
|
|
92
|
+
}
|
|
93
|
+
return { text: writer.toString(), blocks: parsed.blocks };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Middleware the author wrote natively in custom/csharp/Middleware.cs. */
|
|
97
|
+
export function nativeMiddleware(ir , custom ) {
|
|
98
|
+
return new Set(ir.middlewares.map((entry) => entry.name).filter((name) => custom !== undefined && new RegExp(`Task<HttpResponse>\\s+${csharpName(name)}\\s*\\(`).test(custom)));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function transpile(ir , name , sources ) {
|
|
102
|
+
const middleware = ir.middlewares.find((candidate) => candidate.name === name);
|
|
103
|
+
const source = sources.get(name);
|
|
104
|
+
if (middleware === undefined || source === undefined) throw new Error(`middleware ${name} has no source to transpile`);
|
|
105
|
+
try {
|
|
106
|
+
return transpileMiddleware({ name, source, config: middleware.config }, "csharp", logicOptions(ir));
|
|
107
|
+
} catch (error ) {
|
|
108
|
+
if (!(error instanceof TranspileError)) throw error;
|
|
109
|
+
throw new Error(
|
|
110
|
+
`${error.message}\nTo keep this middleware as written, write public static async Task<HttpResponse> ${csharpName(name)}(HttpRequest req, Next next, Context ctx) in ${CUSTOM_FILE}; it replaces the transpiled one.`,
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Middleware and logic call the project's logic functions as <Name>Logic.<Name>. */
|
|
116
|
+
function logicOptions(ir ) {
|
|
117
|
+
return { functions: ir.logic ?? [], logicName: (name) => `${logicClass(ir)}.${pascal(name)}` };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The class of the SDK's logic functions, e.g. PaymentsLogic. */
|
|
121
|
+
export function logicClass(ir ) {
|
|
122
|
+
return `${ir.name}Logic`;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** <Name>Logic.cs: the definition's logic functions as public static methods, transpiled. */
|
|
126
|
+
function emitLogic(input , header ) {
|
|
127
|
+
const { ir } = input;
|
|
128
|
+
const writer = csharp().line(header).line("// Logic functions transpiled from the TypeScript definition (spec/subset.md).");
|
|
129
|
+
writer.line("#pragma warning disable CS0162, CS0168, CS0219 // transpiled code keeps the shape of its TypeScript source").line();
|
|
130
|
+
["System", `${input.namespace}.Runtime`].forEach((name) => writer.line(`using ${name};`));
|
|
131
|
+
writer.line().line(`namespace ${input.namespace}`).line("{").indent((ns) => {
|
|
132
|
+
doc(ns, [`Functions of the ${ir.name} API, transpiled from its definition.`]);
|
|
133
|
+
ns.line(`public static class ${logicClass(ir)}`).line("{").indent((body) => {
|
|
134
|
+
(ir.logic ?? []).forEach((logic, index) => {
|
|
135
|
+
const source = input.logicSources?.get(logic.name);
|
|
136
|
+
if (source === undefined) throw new Error(`logic function ${logic.name} has no source to transpile`);
|
|
137
|
+
if (index > 0) body.line();
|
|
138
|
+
if (logic.doc !== undefined) doc(body, [logic.doc]);
|
|
139
|
+
transpileLogic(logic, source, "csharp", logicOptions(ir))
|
|
140
|
+
.trimEnd()
|
|
141
|
+
.split("\n")
|
|
142
|
+
.forEach((line, lineIndex) => body.line(lineIndex === 0 ? line.replace(` ${csharpName(logic.name)}(`, ` ${pascal(logic.name)}(`) : line));
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
ns.line("}");
|
|
146
|
+
});
|
|
147
|
+
return writer.line("}").toString();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function emitMiddleware(input , header ) {
|
|
151
|
+
const { ir } = input;
|
|
152
|
+
const native = nativeMiddleware(ir, input.middleware);
|
|
153
|
+
const writer = csharp().line(header).line("// Middleware transpiled from the TypeScript definition (spec/subset.md).").line(`// Methods in ${CUSTOM_FILE} replace transpiled ones of the same name.`);
|
|
154
|
+
writer.line("#pragma warning disable CS0162, CS0168, CS0219, CS1998 // transpiled code keeps the shape of its TypeScript source").line();
|
|
155
|
+
["System", "System.Threading.Tasks", `${input.namespace}.Runtime`].forEach((name) => writer.line(`using ${name};`));
|
|
156
|
+
writer.line().line(`namespace ${input.namespace}`).line("{").indent((ns) => {
|
|
157
|
+
ns.line("internal static class Middlewares").line("{").indent((body) => {
|
|
158
|
+
const methods = ir.middlewares.filter((entry) => !native.has(entry.name)).map((entry) => transpile(ir, entry.name, input.middlewareSources).trimEnd());
|
|
159
|
+
if (input.middleware !== undefined) methods.push(`// Copied from ${CUSTOM_FILE}.\n${input.middleware.trimEnd()}`);
|
|
160
|
+
methods.forEach((method, index) => {
|
|
161
|
+
if (index > 0) body.line();
|
|
162
|
+
method.split("\n").forEach((line) => body.line(line));
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
ns.line("}");
|
|
166
|
+
});
|
|
167
|
+
writer.line("}");
|
|
168
|
+
return writer.toString();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function xml(text ) {
|
|
172
|
+
return text.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function emitProject(input ) {
|
|
176
|
+
const properties = [
|
|
177
|
+
["TargetFramework", "netstandard2.1"],
|
|
178
|
+
["LangVersion", "8.0"],
|
|
179
|
+
["Nullable", "enable"],
|
|
180
|
+
["TreatWarningsAsErrors", "true"],
|
|
181
|
+
["Deterministic", "true"],
|
|
182
|
+
["RootNamespace", input.namespace],
|
|
183
|
+
["AssemblyName", input.packageId],
|
|
184
|
+
["PackageId", input.packageId],
|
|
185
|
+
["Version", input.version],
|
|
186
|
+
["Description", input.ir.doc ?? input.ir.name],
|
|
187
|
+
...(input.license === undefined ? [] : ([["PackageLicenseExpression", input.license]] )),
|
|
188
|
+
];
|
|
189
|
+
return csharp()
|
|
190
|
+
.line('<Project Sdk="Microsoft.NET.Sdk">')
|
|
191
|
+
.indent((project) => {
|
|
192
|
+
project.line("<PropertyGroup>").indent((group) => properties.forEach(([name, value]) => group.line(`<${name}>${xml(value)}</${name}>`)));
|
|
193
|
+
project.line("</PropertyGroup>");
|
|
194
|
+
})
|
|
195
|
+
.line("</Project>")
|
|
196
|
+
.toString();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function emitReadme(input ) {
|
|
200
|
+
return csharp()
|
|
201
|
+
.line(`# ${input.packageId}`)
|
|
202
|
+
.line()
|
|
203
|
+
.line(input.ir.doc ?? "")
|
|
204
|
+
.line()
|
|
205
|
+
.line(`> Generated by sdkloom ${input.sdkloomVersion} from \`${input.source}\`. Do not edit by hand: run \`sdkloom build\`.`)
|
|
206
|
+
.line()
|
|
207
|
+
.line("```csharp")
|
|
208
|
+
.line(`var client = new ${input.namespace}.Client(new ${input.namespace}.Options());`)
|
|
209
|
+
.line("```")
|
|
210
|
+
.line()
|
|
211
|
+
.line(`netstandard2.1 (.NET Core 3.0+, .NET 5+, Mono, Unity); no package dependencies. Every operation is async and takes a CancellationToken. Test helpers: \`${input.namespace}.Testing.MockClient.Create()\` and \`${input.namespace}.Testing.Examples\`.`)
|
|
212
|
+
.toString();
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export function generateCSharp(input ) {
|
|
216
|
+
const { ir } = input;
|
|
217
|
+
if (!PACKAGE_ID.test(input.packageId)) throw new Error(`invalid NuGet package id ${input.packageId}: use letters, digits, ., - and _`);
|
|
218
|
+
if (!NAMESPACE.test(input.namespace)) throw new Error(`invalid C# namespace ${input.namespace}`);
|
|
219
|
+
const header = `// Generated by sdkloom ${input.sdkloomVersion} from ${input.source}. Do not edit: run \`sdkloom build\` to regenerate.`;
|
|
220
|
+
const files = [];
|
|
221
|
+
const trace = new Map ();
|
|
222
|
+
const add = (path , text , from ) => {
|
|
223
|
+
files.push([path, text]);
|
|
224
|
+
trace.set(path, from);
|
|
225
|
+
};
|
|
226
|
+
const operations = ir.operations.map((operation) => `ir:operations.${operation.name}`);
|
|
227
|
+
const types = ir.types.map((named) => `ir:types.${named.name}`);
|
|
228
|
+
|
|
229
|
+
const blocks = [];
|
|
230
|
+
for (const file of [...input.kit.files].sort((a, b) => (a.path < b.path ? -1 : 1))) {
|
|
231
|
+
const vendored = vendor(file, input.kit, header, input.namespace);
|
|
232
|
+
add(`Runtime/${file.path}`, vendored.text, vendored.blocks.map((block) => `block:${block.id}`));
|
|
233
|
+
vendored.blocks.forEach((block) => blocks.push({ id: block.id, versions: block.versions ?? "*", code: block.code }));
|
|
234
|
+
}
|
|
235
|
+
add("Models.cs", emitModels(ir, header, input.namespace), types);
|
|
236
|
+
add("Errors.cs", emitErrors(ir, header, input.namespace), operations);
|
|
237
|
+
add("Middleware.cs", emitMiddleware(input, header), ir.middlewares.map((entry) => `middleware:${entry.name}`));
|
|
238
|
+
add("Client.cs", emitClientFile(ir, header, input.namespace, { name: input.packageId, version: input.version }), ["ir:servers", ...operations]);
|
|
239
|
+
if ((ir.logic ?? []).length > 0) add(`${logicClass(ir)}.cs`, emitLogic(input, header), (ir.logic ?? []).map((entry) => `ir:logic.${entry.name}`));
|
|
240
|
+
if ((ir.webhooks ?? []).length > 0) add(`${webhooksClass(ir)}.cs`, emitWebhooks(ir, header, input.namespace), (ir.webhooks ?? []).map((hook) => `ir:webhooks.${hook.name}`));
|
|
241
|
+
add("Testing.cs", emitMock(ir, header, input.namespace), [...types, ...operations]);
|
|
242
|
+
add(`${input.packageId}.csproj`, emitProject(input), ["config"]);
|
|
243
|
+
add("README.md", emitReadme(input), ["config", "ir"]);
|
|
244
|
+
return { files: createFileTree(files), trace, blocks };
|
|
245
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { CUSTOM_FILE, defaultNamespace, generateCSharp, } from "./generate.ts";
|
|
2
|
+
export { configFields, exceptionName, kindOf, methodName, paramsType, resultType } from "./emit-client.ts";
|
|
3
|
+
export { emitHarness } from "./emit-harness.ts";
|
|
4
|
+
export { camel, csType, pascal, typeName } from "./names.ts";
|
|
5
|
+
export { webhookMethod, webhooksClass } from "./emit-webhooks.ts";
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
// C# names, literals, doc comments and the IR → C# type mapping, decoders, encoders and examples.
|
|
2
|
+
|
|
3
|
+
import { createCodeWriter, } from "@sdkloom/codegen";
|
|
4
|
+
|
|
5
|
+
import { csharpString } from "@sdkloom/transpile";
|
|
6
|
+
|
|
7
|
+
export const csharp = () => createCodeWriter({ indent: " " });
|
|
8
|
+
export const str = csharpString;
|
|
9
|
+
|
|
10
|
+
const KEYWORDS = new Set([
|
|
11
|
+
"abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked", "class", "const", "continue", "decimal", "default",
|
|
12
|
+
"delegate", "do", "double", "else", "enum", "event", "explicit", "extern", "false", "finally", "fixed", "float", "for", "foreach", "goto",
|
|
13
|
+
"if", "implicit", "in", "int", "interface", "internal", "is", "lock", "long", "namespace", "new", "null", "object", "operator", "out",
|
|
14
|
+
"override", "params", "private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed", "short", "sizeof", "stackalloc",
|
|
15
|
+
"static", "string", "struct", "switch", "this", "throw", "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe", "ushort", "using",
|
|
16
|
+
"virtual", "void", "volatile", "while",
|
|
17
|
+
]);
|
|
18
|
+
|
|
19
|
+
function words(name ) {
|
|
20
|
+
return name
|
|
21
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
22
|
+
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2")
|
|
23
|
+
.split(/[^A-Za-z0-9]+/)
|
|
24
|
+
.filter((word) => word !== "");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** payment-method → PaymentMethod, x-request-id → XRequestId. */
|
|
28
|
+
export function pascal(name ) {
|
|
29
|
+
const result = words(name).map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join("");
|
|
30
|
+
return /^[0-9]/.test(result) ? `N${result}` : result;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Names generated methods use for their own parameters and locals; a path parameter with one of them gets "Value". */
|
|
34
|
+
const LOCALS = new Set(["body", "parameters", "options", "cancellationToken", "named", "args", "response", "client", "op", "result", "plan", "value"]);
|
|
35
|
+
|
|
36
|
+
/** A path parameter's C# name: camelCase, never one of the generated method's own names. */
|
|
37
|
+
export function pathParamName(name ) {
|
|
38
|
+
const result = camel(name);
|
|
39
|
+
return LOCALS.has(result) ? `${result}Value` : result;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** getPayment → getPayment, x-request-id → xRequestId; keywords get an @. */
|
|
43
|
+
export function camel(name ) {
|
|
44
|
+
const result = pascal(name);
|
|
45
|
+
const lower = result.charAt(0).toLowerCase() + result.slice(1);
|
|
46
|
+
return KEYWORDS.has(lower) ? `@${lower}` : lower;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Names a model may not take: they would hide runtime, system or generated types the SDK uses. */
|
|
50
|
+
const TAKEN_TYPES = new Set([
|
|
51
|
+
"Client", "Options", "RawClient", "Json", "JsonKind", "Js", "Decode", "Encode", "Maybe", "HttpRequest", "HttpResponse", "Context", "Next",
|
|
52
|
+
"Middleware", "Task", "Exception", "Math", "String", "Object", "Array", "Func", "Action", "Url", "Call", "CallArgs", "CallOptions",
|
|
53
|
+
"RawResult", "SdkException", "ApiException", "Examples", "Mock", "MockClient", "Middlewares", "Version", "State", "Events", "BodyKind",
|
|
54
|
+
"ITransport", "IClock", "IToJson", "Dictionary", "KeyValuePair", "IReadOnlyList", "IReadOnlyDictionary", "Runtime", "Testing", "FakeClock",
|
|
55
|
+
"SystemClock", "MockTransport", "MockRoute", "OperationMock", "RecordedEvent", "HttpClientTransport", "ClientRuntime", "OperationSpec",
|
|
56
|
+
"ErrorFactory", "MiddlewareEntry", "EventListener", "SendOptions", "DecodeException", "TransportException", "OperationInfo", "SdkInfo",
|
|
57
|
+
"ArgumentException", "Program",
|
|
58
|
+
]);
|
|
59
|
+
|
|
60
|
+
/** The C# name of a model or union: PascalCase, with "Model" appended when it would hide another type. */
|
|
61
|
+
export function typeName(name ) {
|
|
62
|
+
const result = pascal(name);
|
|
63
|
+
return TAKEN_TYPES.has(result) ? `${result}Model` : result;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The interface every variant of a union implements, e.g. IPaymentMethod. */
|
|
67
|
+
export function unionInterface(name ) {
|
|
68
|
+
return `I${typeName(name)}`;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** /// <summary> doc comment lines, XML-escaped. */
|
|
72
|
+
export function doc(writer , lines ) {
|
|
73
|
+
const text = lines.filter((line, index, all) => line !== "" || index < all.length - 1).map((line) => line.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">"));
|
|
74
|
+
if (text.length === 0) return;
|
|
75
|
+
if (text.length === 1) {
|
|
76
|
+
writer.line(`/// <summary>${text[0] ?? ""}</summary>`);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
writer.line("/// <summary>");
|
|
80
|
+
text.forEach((line) => writer.line(line === "" ? "///" : `/// ${line}`));
|
|
81
|
+
writer.line("/// </summary>");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function namedType(ir , name ) {
|
|
85
|
+
const found = ir.types.find((named) => named.name === name);
|
|
86
|
+
if (found === undefined) throw new Error(`unknown type ${name}`);
|
|
87
|
+
return found;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const isUnion = (ir , name ) => namedType(ir, name).type.kind === "union";
|
|
91
|
+
|
|
92
|
+
const CS_TYPE = {
|
|
93
|
+
string: "string",
|
|
94
|
+
secret: "string",
|
|
95
|
+
datetime: "string",
|
|
96
|
+
decimal: "decimal",
|
|
97
|
+
int: "long",
|
|
98
|
+
number: "double",
|
|
99
|
+
bool: "bool",
|
|
100
|
+
bytes: "byte[]",
|
|
101
|
+
stream: "byte[]",
|
|
102
|
+
multipart: "MultipartForm",
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
function inline(path ) {
|
|
106
|
+
throw new Error(`${path}: inline object and union types are not supported; use model() or union()`);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function csType(ir , node ) {
|
|
110
|
+
switch (node.kind) {
|
|
111
|
+
case "primitive":
|
|
112
|
+
return CS_TYPE[node.name];
|
|
113
|
+
case "enum":
|
|
114
|
+
return "string";
|
|
115
|
+
case "list":
|
|
116
|
+
return `IReadOnlyList<${csType(ir, node.item)}>`;
|
|
117
|
+
case "map":
|
|
118
|
+
return `IReadOnlyDictionary<string, ${csType(ir, node.value)}>`;
|
|
119
|
+
case "ref":
|
|
120
|
+
return isUnion(ir, node.name) ? unionInterface(node.name) : typeName(node.name);
|
|
121
|
+
case "object":
|
|
122
|
+
case "union":
|
|
123
|
+
return inline("type");
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** long, double, bool and decimal: nullable as T?, read with .Value. */
|
|
128
|
+
export function isValueType(node ) {
|
|
129
|
+
return node.kind === "primitive" && ["int", "number", "bool", "decimal"].includes(node.name);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
/** A field's type: optional or nullable is T?; both is Maybe<T?>, which tells absent from null. */
|
|
135
|
+
export function fieldType(ir , item ) {
|
|
136
|
+
const base = csType(ir, item.type);
|
|
137
|
+
if (item.optional && item.nullable) return `Maybe<${base}?>`;
|
|
138
|
+
return item.optional || item.nullable ? `${base}?` : base;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** What a property starts as, so a non-nullable reference is never null. */
|
|
142
|
+
export function initializer(ir , item ) {
|
|
143
|
+
if (item.optional || item.nullable || isValueType(item.type)) return "";
|
|
144
|
+
const node = item.type;
|
|
145
|
+
if (node.kind === "list") return ` = Array.Empty<${csType(ir, node.item)}>();`;
|
|
146
|
+
if (node.kind === "map") return ` = new Dictionary<string, ${csType(ir, node.value)}>();`;
|
|
147
|
+
if (node.kind === "primitive" && (node.name === "bytes" || node.name === "stream")) return " = Array.Empty<byte>();";
|
|
148
|
+
if (node.kind === "primitive" || node.kind === "enum") return ' = "";';
|
|
149
|
+
return " = null!;";
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const DECODER = {
|
|
153
|
+
string: "Decode.String",
|
|
154
|
+
secret: "Decode.String",
|
|
155
|
+
datetime: "Decode.String",
|
|
156
|
+
decimal: "Decode.Decimal",
|
|
157
|
+
int: "Decode.Integer",
|
|
158
|
+
number: "Decode.Number",
|
|
159
|
+
bool: "Decode.Boolean",
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
function only(name , path ) {
|
|
163
|
+
throw new Error(`${path}: ${name} values are only supported as an operation body or result`);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** A decoder: a method group or lambda usable as Func<Json, string, T>. */
|
|
167
|
+
export function decoder(ir , node , path ) {
|
|
168
|
+
switch (node.kind) {
|
|
169
|
+
case "primitive":
|
|
170
|
+
return DECODER[node.name] ?? only(node.name, path);
|
|
171
|
+
case "enum":
|
|
172
|
+
return `(v, p) => Decode.Enum(v, p, ${node.values.map(str).join(", ")})`;
|
|
173
|
+
case "list":
|
|
174
|
+
return `(v, p) => Decode.List(v, p, ${decoder(ir, node.item, path)})`;
|
|
175
|
+
case "map":
|
|
176
|
+
return `(v, p) => Decode.Map(v, p, ${decoder(ir, node.value, path)})`;
|
|
177
|
+
case "ref":
|
|
178
|
+
return `${typeName(node.name)}.FromJson`;
|
|
179
|
+
case "object":
|
|
180
|
+
case "union":
|
|
181
|
+
return inline(path);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** The decoder with null handled for a nullable value. */
|
|
186
|
+
export function nullableDecoder(ir , node , path ) {
|
|
187
|
+
return `Decode.${isValueType(node) ? "NullableValue" : "NullableRef"}<${csType(ir, node)}>(${decoder(ir, node, path)})`;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Calls a decoder on a value: method groups directly, lambdas through a Func. */
|
|
191
|
+
export function decode(ir , node , path , value , at ) {
|
|
192
|
+
const fn = decoder(ir, node, path);
|
|
193
|
+
return fn.includes("=>") ? `((Func<Json, string, ${csType(ir, node)}>)(${fn}))(${value}, ${at})` : `${fn}(${value}, ${at})`;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Reads one model field from the object `json` at `path`. */
|
|
197
|
+
export function fieldDecoder(ir , item , path ) {
|
|
198
|
+
const at = `types.${path}`;
|
|
199
|
+
const name = str(item.name);
|
|
200
|
+
const value = isValueType(item.type);
|
|
201
|
+
if (item.optional && item.nullable) return `Decode.Optional(json, ${name}, path, ${nullableDecoder(ir, item.type, at)})`;
|
|
202
|
+
if (item.optional) return `Decode.${value ? "OptionalValue" : "OptionalRef"}(json, ${name}, path, ${decoder(ir, item.type, at)})`;
|
|
203
|
+
if (item.nullable) return `Decode.Required(json, ${name}, path, ${nullableDecoder(ir, item.type, at)})`;
|
|
204
|
+
return `Decode.Required(json, ${name}, path, ${decoder(ir, item.type, at)})`;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** The JSON form of a present (non-null) value. */
|
|
208
|
+
export function encoder(ir , node , value , depth = 0) {
|
|
209
|
+
const item = `item${depth}`;
|
|
210
|
+
switch (node.kind) {
|
|
211
|
+
case "primitive":
|
|
212
|
+
if (node.name === "int") return `Json.Int(${value})`;
|
|
213
|
+
if (node.name === "number") return `Json.Float(${value})`;
|
|
214
|
+
if (node.name === "bool") return `Json.Bool(${value})`;
|
|
215
|
+
if (node.name === "decimal") return `Encode.Decimal(${value})`;
|
|
216
|
+
return `Json.Str(${value})`;
|
|
217
|
+
case "enum":
|
|
218
|
+
return `Json.Str(${value})`;
|
|
219
|
+
case "list":
|
|
220
|
+
return `Encode.List(${value}, ${item} => ${encoder(ir, node.item, item, depth + 1)})`;
|
|
221
|
+
case "map":
|
|
222
|
+
return `Encode.Map(${value}, ${item} => ${encoder(ir, node.value, item, depth + 1)})`;
|
|
223
|
+
case "ref":
|
|
224
|
+
return isUnion(ir, node.name) ? `${typeName(node.name)}.ToJson(${value})` : `${value}.ToJson()`;
|
|
225
|
+
case "object":
|
|
226
|
+
case "union":
|
|
227
|
+
return inline("type");
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** The JSON form of a value that may be null: null is JSON null. */
|
|
232
|
+
export function nullableEncoder(ir , node , value ) {
|
|
233
|
+
return isValueType(node) ? `(${value}.HasValue ? ${encoder(ir, node, `${value}.Value`)} : Json.Null)` : `(${value} == null ? Json.Null : ${encoder(ir, node, value)})`;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** A model field for Encode.Object: null leaves an absent optional field out. */
|
|
237
|
+
export function fieldEncoder(ir , item , value ) {
|
|
238
|
+
if (item.optional && item.nullable) return `(${value}.IsSet ? ${nullableEncoder(ir, item.type, `${value}.Value`)} : null)`;
|
|
239
|
+
if (item.optional) return `(${value} == null ? null : ${encoder(ir, item.type, isValueType(item.type) ? `${value}.Value` : value)})`;
|
|
240
|
+
if (item.nullable) return nullableEncoder(ir, item.type, value);
|
|
241
|
+
return encoder(ir, item.type, value);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** A parameter or config value as Json; an absent optional one is null. */
|
|
245
|
+
export function paramEncoder(ir , item , value ) {
|
|
246
|
+
return item.optional ? nullableEncoder(ir, item.type, value) : encoder(ir, item.type, value);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const EXAMPLE = {
|
|
250
|
+
string: str("string"),
|
|
251
|
+
secret: str("secret"),
|
|
252
|
+
datetime: str("2026-01-01T00:00:00Z"),
|
|
253
|
+
decimal: "1.00m",
|
|
254
|
+
int: "1L",
|
|
255
|
+
number: "1.5",
|
|
256
|
+
bool: "true",
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
/** A small valid example value; `qualify` names generated types from outside their namespace. */
|
|
260
|
+
export function example(ir , node , path , qualify ) {
|
|
261
|
+
switch (node.kind) {
|
|
262
|
+
case "primitive":
|
|
263
|
+
return EXAMPLE[node.name] ?? only(node.name, path);
|
|
264
|
+
case "enum":
|
|
265
|
+
return str(node.values[0] ?? "");
|
|
266
|
+
case "list":
|
|
267
|
+
return `Array.Empty<${qualify(csType(ir, node.item))}>()`;
|
|
268
|
+
case "map":
|
|
269
|
+
return `new Dictionary<string, ${qualify(csType(ir, node.value))}>()`;
|
|
270
|
+
case "ref":
|
|
271
|
+
return `${typeName(node.name)}()`;
|
|
272
|
+
case "object":
|
|
273
|
+
case "union":
|
|
274
|
+
return inline(path);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** Prefixes every generated type name in a piece of code with global::<namespace>. */
|
|
279
|
+
export function qualifier(ir , namespace ) {
|
|
280
|
+
const names = [...new Set(ir.types.flatMap((named) => (named.type.kind === "union" ? [typeName(named.name), unionInterface(named.name), `Unknown${typeName(named.name)}`] : [typeName(named.name)])))];
|
|
281
|
+
const extra = ["Client", "Options"];
|
|
282
|
+
const pattern = new RegExp(`(?<![.\\w"])(${[...names, ...extra].sort((a, b) => b.length - a.length).join("|")})\\b`, "g");
|
|
283
|
+
return names.length === 0 ? (code) => code.replace(/(?<![.\w"])(Client|Options)\b/g, `global::${namespace}.$1`) : (code) => code.replace(pattern, `global::${namespace}.$1`);
|
|
284
|
+
}
|