@workerstack/clients 1.0.0-rc.28 → 1.0.0-rc.30
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/README.md +403 -21
- package/assets/installers/examples/github.config.ts +17 -19
- package/assets/installers/examples/goreleaser.config.ts +16 -18
- package/assets/installers/examples/raw.config.ts +13 -15
- package/bin/clients.js +87 -66
- package/bin/installgen.js +19 -10
- package/dist/cli.d.ts +4 -4
- package/dist/cli.js +18 -12
- package/dist/emit/cli/channels.d.ts +2 -2
- package/dist/emit/cli/channels.js +18 -13
- package/dist/emit/cli/commands.d.ts +1 -1
- package/dist/emit/cli/commands.js +39 -13
- package/dist/emit/cli/discovery.d.ts +0 -6
- package/dist/emit/cli/discovery.js +9 -7
- package/dist/emit/cli/flags.d.ts +1 -3
- package/dist/emit/cli/flags.js +18 -8
- package/dist/emit/cli/install.d.ts +0 -8
- package/dist/emit/cli/install.js +3 -3
- package/dist/emit/cli/layout.d.ts +1 -1
- package/dist/emit/cli/layout.js +13 -10
- package/dist/emit/cli/module.d.ts +6 -6
- package/dist/emit/cli/module.js +10 -10
- package/dist/emit/cli/release.d.ts +0 -11
- package/dist/emit/cli/release.js +4 -4
- package/dist/emit/cli/skills.js +8 -4
- package/dist/emit/csharp/errors.js +4 -4
- package/dist/emit/csharp/json.js +8 -8
- package/dist/emit/csharp/realtime.d.ts +1 -8
- package/dist/emit/csharp/realtime.js +26 -26
- package/dist/emit/csharp/sdk-answer.d.ts +10 -2
- package/dist/emit/csharp/sdk-answer.js +4 -4
- package/dist/emit/csharp/sdk-body.js +8 -6
- package/dist/emit/csharp/sdk-client.d.ts +3 -3
- package/dist/emit/csharp/sdk-client.js +2 -2
- package/dist/emit/csharp/sdk-csproj.d.ts +7 -9
- package/dist/emit/csharp/sdk-csproj.js +10 -10
- package/dist/emit/csharp/sdk-doc.js +1 -1
- package/dist/emit/csharp/sdk-method.d.ts +0 -1
- package/dist/emit/csharp/sdk-method.js +19 -8
- package/dist/emit/csharp/sdk-plan.js +0 -1
- package/dist/emit/csharp/sdk.js +3 -3
- package/dist/emit/csharp/sse.d.ts +0 -13
- package/dist/emit/csharp/sse.js +92 -11
- package/dist/emit/csharp/transport.js +9 -9
- package/dist/emit/csharp/types-enum.js +3 -1
- package/dist/emit/csharp/types-messages.d.ts +47 -0
- package/dist/emit/csharp/types-messages.js +172 -0
- package/dist/emit/csharp/types-names.js +13 -14
- package/dist/emit/csharp/types-record.js +3 -3
- package/dist/emit/csharp/types-union.d.ts +1 -1
- package/dist/emit/csharp/types.d.ts +0 -1
- package/dist/emit/csharp/types.js +3 -3
- package/dist/emit/csharp/ws.d.ts +0 -15
- package/dist/emit/csharp/ws.js +26 -5
- package/dist/emit/go/errors.js +1 -1
- package/dist/emit/go/http.js +2 -2
- package/dist/emit/go/module-path.js +2 -2
- package/dist/emit/go/sdk-answer.d.ts +24 -1
- package/dist/emit/go/sdk-answer.js +1 -1
- package/dist/emit/go/sdk-body.js +11 -7
- package/dist/emit/go/sdk-client.js +1 -1
- package/dist/emit/go/sdk-method.d.ts +3 -0
- package/dist/emit/go/sdk-method.js +18 -12
- package/dist/emit/go/sdk-mirror.js +2 -2
- package/dist/emit/go/sdk-opts.d.ts +0 -2
- package/dist/emit/go/sdk-opts.js +3 -3
- package/dist/emit/go/sdk.d.ts +1 -1
- package/dist/emit/go/sdk.js +9 -9
- package/dist/emit/go/sse.d.ts +2 -10
- package/dist/emit/go/sse.js +88 -13
- package/dist/emit/go/types-names.js +5 -0
- package/dist/emit/go/types-struct.d.ts +2 -1
- package/dist/emit/go/types-struct.js +1 -1
- package/dist/emit/go/types-union.d.ts +3 -2
- package/dist/emit/go/types-union.js +2 -0
- package/dist/emit/go/types.d.ts +6 -0
- package/dist/emit/go/types.js +20 -9
- package/dist/emit/go/ws.d.ts +4 -11
- package/dist/emit/go/ws.js +215 -59
- package/dist/emit/java/gradle.d.ts +7 -9
- package/dist/emit/java/gradle.js +8 -8
- package/dist/emit/java/json.d.ts +3 -0
- package/dist/emit/java/json.js +1 -1
- package/dist/emit/java/layout-ast.d.ts +4 -1
- package/dist/emit/java/layout-ast.js +1 -1
- package/dist/emit/java/layout-decl.js +5 -4
- package/dist/emit/java/layout-doc.js +13 -7
- package/dist/emit/java/layout-dot.js +1 -1
- package/dist/emit/java/layout-strings.js +4 -5
- package/dist/emit/java/layout-types.js +7 -5
- package/dist/emit/java/layout-visitor.js +4 -3
- package/dist/emit/java/layout.js +5 -4
- package/dist/emit/java/models-names.d.ts +2 -0
- package/dist/emit/java/models-names.js +6 -4
- package/dist/emit/java/models-record.d.ts +0 -1
- package/dist/emit/java/models-record.js +3 -3
- package/dist/emit/java/models-union.d.ts +13 -2
- package/dist/emit/java/models-union.js +12 -6
- package/dist/emit/java/models.d.ts +5 -1
- package/dist/emit/java/models.js +44 -20
- package/dist/emit/java/realtime.d.ts +34 -26
- package/dist/emit/java/realtime.js +45 -10
- package/dist/emit/java/sdk-client.d.ts +6 -6
- package/dist/emit/java/sdk-client.js +1 -1
- package/dist/emit/java/sdk-decl.js +2 -2
- package/dist/emit/java/sdk-errors.js +1 -1
- package/dist/emit/java/sdk-headers.js +2 -2
- package/dist/emit/java/sdk-mirror.js +7 -6
- package/dist/emit/java/sdk-models.d.ts +1 -1
- package/dist/emit/java/sdk-models.js +7 -7
- package/dist/emit/java/sdk-op.d.ts +1 -10
- package/dist/emit/java/sdk-op.js +9 -9
- package/dist/emit/java/sdk-params.d.ts +5 -5
- package/dist/emit/java/sdk-params.js +3 -2
- package/dist/emit/java/sdk-plan.js +0 -1
- package/dist/emit/java/sdk.d.ts +1 -1
- package/dist/emit/java/sdk.js +14 -13
- package/dist/emit/java/sse.d.ts +12 -13
- package/dist/emit/java/sse.js +105 -6
- package/dist/emit/java/ws.d.ts +48 -9
- package/dist/emit/java/ws.js +88 -4
- package/dist/emit/mcp/annotations.js +8 -4
- package/dist/emit/mcp/files.js +9 -9
- package/dist/emit/mcp/mounted.js +2 -2
- package/dist/emit/mcp/output-schema.d.ts +4 -2
- package/dist/emit/mcp/output-schema.js +58 -14
- package/dist/emit/mcp/registry.d.ts +0 -2
- package/dist/emit/mcp/registry.js +5 -4
- package/dist/emit/mcp/runtime.js +2 -2
- package/dist/emit/mcp/schema.js +22 -11
- package/dist/emit/mcp/server.d.ts +7 -7
- package/dist/emit/mcp/server.js +7 -6
- package/dist/emit/mcp/tools.js +7 -6
- package/dist/emit/python/errors.js +3 -3
- package/dist/emit/python/realtime.d.ts +9 -6
- package/dist/emit/python/realtime.js +22 -20
- package/dist/emit/python/sdk-answer.d.ts +8 -8
- package/dist/emit/python/sdk-answer.js +2 -2
- package/dist/emit/python/sdk-body.js +4 -5
- package/dist/emit/python/sdk-client.js +5 -5
- package/dist/emit/python/sdk-doc.js +2 -2
- package/dist/emit/python/sdk-method.d.ts +1 -1
- package/dist/emit/python/sdk-method.js +38 -11
- package/dist/emit/python/sdk-plan.js +0 -1
- package/dist/emit/python/sdk-pyproject.d.ts +0 -2
- package/dist/emit/python/sdk-pyproject.js +8 -8
- package/dist/emit/python/sdk.d.ts +1 -1
- package/dist/emit/python/sdk.js +28 -8
- package/dist/emit/python/sse.d.ts +0 -7
- package/dist/emit/python/sse.js +36 -6
- package/dist/emit/python/transport.js +4 -4
- package/dist/emit/python/types-dict.d.ts +2 -2
- package/dist/emit/python/types-dict.js +1 -1
- package/dist/emit/python/types-names.js +2 -2
- package/dist/emit/python/types-union.js +1 -1
- package/dist/emit/python/types.d.ts +7 -0
- package/dist/emit/python/types.js +23 -6
- package/dist/emit/python/ws.d.ts +21 -10
- package/dist/emit/python/ws.js +69 -3
- package/dist/emit/rust/errors.js +3 -3
- package/dist/emit/rust/http.js +4 -4
- package/dist/emit/rust/realtime.d.ts +7 -5
- package/dist/emit/rust/realtime.js +15 -18
- package/dist/emit/rust/sdk-body.js +8 -5
- package/dist/emit/rust/sdk-cargo.d.ts +16 -18
- package/dist/emit/rust/sdk-cargo.js +12 -12
- package/dist/emit/rust/sdk-client.d.ts +1 -1
- package/dist/emit/rust/sdk-client.js +3 -3
- package/dist/emit/rust/sdk-method.js +3 -3
- package/dist/emit/rust/sdk-opts.d.ts +2 -1
- package/dist/emit/rust/sdk-opts.js +2 -2
- package/dist/emit/rust/sdk-plan.js +0 -1
- package/dist/emit/rust/sdk.d.ts +1 -1
- package/dist/emit/rust/sdk.js +5 -5
- package/dist/emit/rust/sse.d.ts +11 -12
- package/dist/emit/rust/sse.js +54 -8
- package/dist/emit/rust/types-names.js +3 -3
- package/dist/emit/rust/types-struct.js +1 -1
- package/dist/emit/rust/types-union.d.ts +10 -2
- package/dist/emit/rust/types-union.js +4 -0
- package/dist/emit/rust/types.d.ts +9 -0
- package/dist/emit/rust/types.js +20 -12
- package/dist/emit/rust/ws.d.ts +22 -15
- package/dist/emit/rust/ws.js +60 -10
- package/dist/emit/shared/escape.js +17 -17
- package/dist/emit/shared/format.js +1 -1
- package/dist/emit/shared/gofmt.js +1 -1
- package/dist/emit/shared/lang.js +10 -10
- package/dist/emit/shared/literals.d.ts +5 -0
- package/dist/emit/shared/literals.js +8 -0
- package/dist/emit/shared/metadata.d.ts +0 -6
- package/dist/emit/shared/metadata.js +8 -7
- package/dist/emit/shared/ruff.js +1 -1
- package/dist/emit/shared/rustfmt.js +1 -1
- package/dist/emit/shared/type-emitter.js +37 -20
- package/dist/emit/shared/unknown-type.constants.d.ts +2 -0
- package/dist/emit/shared/unknown-type.constants.js +2 -0
- package/dist/emit/ts/http.d.ts +0 -2
- package/dist/emit/ts/http.js +4 -4
- package/dist/emit/ts/package.d.ts +14 -12
- package/dist/emit/ts/package.js +9 -8
- package/dist/emit/ts/route-tree.js +2 -2
- package/dist/emit/ts/sdk-types-preamble.d.ts +1 -1
- package/dist/emit/ts/sdk-types-preamble.js +6 -2
- package/dist/emit/ts/sdk-types.js +59 -20
- package/dist/emit/ts/sdk.js +6 -6
- package/dist/emit/ts/service-map.d.ts +10 -3
- package/dist/emit/ts/service-map.js +30 -2
- package/dist/emit/ts/tanstack-query.js +6 -6
- package/dist/emit/ts/type-gen.js +36 -21
- package/dist/emit/ts/webhooks.d.ts +0 -2
- package/dist/emit/ts/webhooks.js +3 -3
- package/dist/extract/chain.js +5 -4
- package/dist/extract/extract.js +5 -5
- package/dist/extract/serialize.d.ts +0 -2
- package/dist/extract/serialize.js +14 -15
- package/dist/extract/walk.js +15 -12
- package/dist/gen/args.js +9 -5
- package/dist/gen/formatters.js +5 -3
- package/dist/gen/shipped.js +2 -2
- package/dist/gen/targets.d.ts +2 -4
- package/dist/gen/targets.js +8 -4
- package/dist/gen/write.js +4 -2
- package/dist/index.js +2 -2
- package/dist/installers/cli.d.ts +4 -4
- package/dist/installers/cli.js +24 -24
- package/dist/installers/commands/build.d.ts +4 -4
- package/dist/installers/commands/build.js +2 -3
- package/dist/installers/commands/check.d.ts +2 -2
- package/dist/installers/commands/check.js +11 -10
- package/dist/installers/commands/init.d.ts +2 -2
- package/dist/installers/commands/init.js +1 -2
- package/dist/installers/config/define-config.d.ts +16 -16
- package/dist/installers/config/define-config.js +1 -1
- package/dist/installers/config/hooks/bun.d.ts +6 -6
- package/dist/installers/config/hooks/node.js +1 -1
- package/dist/installers/config/load.d.ts +6 -8
- package/dist/installers/config/load.js +7 -8
- package/dist/installers/config/resolve.d.ts +1 -2
- package/dist/installers/config/resolve.js +28 -33
- package/dist/installers/config/types.d.ts +6 -6
- package/dist/installers/config/types.js +0 -1
- package/dist/installers/render/quote.js +3 -5
- package/dist/installers/render/render.d.ts +6 -6
- package/dist/installers/render/render.js +26 -26
- package/dist/installers/ui.d.ts +4 -4
- package/dist/installers/ui.js +3 -3
- package/dist/ir/build.d.ts +3 -3
- package/dist/ir/build.js +12 -5
- package/dist/ir/load.js +9 -10
- package/dist/ir/model.d.ts +16 -1
- package/dist/ir/model.js +0 -1
- package/dist/ir/resolve-refs.js +2 -2
- package/dist/ir/resource-tree.js +2 -1
- package/dist/ir/route-tree-intern.js +1 -1
- package/dist/ir/schema.js +3 -3
- package/dist/ir/sdk-model.d.ts +25 -1
- package/dist/ir/sdk-model.js +38 -8
- package/dist/ir/slots.d.ts +1 -1
- package/dist/ir/slots.js +22 -11
- package/dist/ir/streams.d.ts +16 -0
- package/dist/ir/streams.js +40 -0
- package/dist/release/bootstrap.d.ts +3 -3
- package/dist/release/bootstrap.js +7 -7
- package/dist/release/bump-args.js +4 -4
- package/dist/release/bump-command.d.ts +1 -1
- package/dist/release/bump-command.js +9 -10
- package/dist/release/bump.d.ts +5 -4
- package/dist/release/bump.js +2 -2
- package/dist/release/cli.d.ts +4 -4
- package/dist/release/cli.js +4 -4
- package/dist/release/codegen.js +1 -1
- package/dist/release/digest.js +5 -7
- package/dist/release/git.js +5 -8
- package/dist/release/load.js +3 -3
- package/dist/release/manifest.js +9 -10
- package/dist/release/oasdiff.d.ts +10 -15
- package/dist/release/oasdiff.js +5 -6
- package/dist/release/registries.d.ts +0 -11
- package/dist/release/registries.js +17 -17
- package/dist/release/release-state.d.ts +16 -13
- package/dist/release/release-workflow.js +5 -7
- package/dist/release/repo-settings.d.ts +2 -1
- package/dist/release/spec-gate.d.ts +0 -2
- package/dist/release/spec-gate.js +6 -10
- package/dist/release/sync-command.d.ts +2 -2
- package/dist/release/sync-command.js +1 -1
- package/dist/release/sync-tree.js +9 -13
- package/dist/release/sync.d.ts +6 -6
- package/dist/release/sync.js +12 -13
- package/dist/release/types.d.ts +22 -22
- package/dist/release/types.js +0 -1
- package/dist/release/version-files.d.ts +3 -4
- package/dist/release/version-files.js +4 -5
- package/dist/runtime/.oxfmtrc.jsonc +8 -0
- package/dist/runtime/cli/socket.go +107 -13
- package/dist/runtime/cli/stream.go +18 -11
- package/dist/runtime/csharp/Adapters.cs +12 -11
- package/dist/runtime/csharp/Events.cs +22 -17
- package/dist/runtime/csharp/Realtime.cs +50 -23
- package/dist/runtime/csharp/RealtimeLoop.cs +1 -1
- package/dist/runtime/csharp/TransportSse.cs +3 -3
- package/dist/runtime/csharp/Ws.cs +1 -1
- package/dist/runtime/csharp/WsDial.cs +19 -1
- package/dist/runtime/csharp/WsTyped.cs +109 -0
- package/dist/runtime/go/realtime.go +125 -41
- package/dist/runtime/go/realtime_loop.go +21 -18
- package/dist/runtime/go/result.go +2 -1
- package/dist/runtime/go/sse.go +11 -0
- package/dist/runtime/go/stream.go +29 -13
- package/dist/runtime/go/transport.go +15 -21
- package/dist/runtime/go/transport_longpoll.go +3 -4
- package/dist/runtime/go/transport_sse.go +6 -6
- package/dist/runtime/go/ws.go +157 -61
- package/dist/runtime/java/EventStream.java +47 -29
- package/dist/runtime/java/Exchange.java +5 -5
- package/dist/runtime/java/Flight.java +3 -3
- package/dist/runtime/java/Realtime.java +45 -11
- package/dist/runtime/java/ResumableConnection.java +77 -32
- package/dist/runtime/java/Socket.java +8 -4
- package/dist/runtime/java/SseAdapter.java +1 -1
- package/dist/runtime/java/TypedWebSocket.java +81 -24
- package/dist/runtime/java/WsAdapter.java +4 -2
- package/dist/runtime/java/WsDial.java +12 -5
- package/dist/runtime/mcp/files.ts +1 -1
- package/dist/runtime/mcp/http.ts +6 -2
- package/dist/runtime/mcp/listen.ts +1 -1
- package/dist/runtime/mcp/mount.ts +2 -2
- package/dist/runtime/mcp/protocol.ts +1 -1
- package/dist/runtime/python/events.py +25 -11
- package/dist/runtime/python/operation.py +6 -1
- package/dist/runtime/python/realtime.py +47 -33
- package/dist/runtime/python/send_async.py +1 -0
- package/dist/runtime/python/transport_sse.py +2 -2
- package/dist/runtime/python/transport_ws.py +1 -1
- package/dist/runtime/python/ws.py +100 -39
- package/dist/runtime/rust/events.rs +64 -41
- package/dist/runtime/rust/realtime.rs +80 -34
- package/dist/runtime/rust/realtime_loop.rs +37 -12
- package/dist/runtime/rust/sse.rs +3 -2
- package/dist/runtime/rust/transport_sse.rs +1 -1
- package/dist/runtime/rust/transport_ws.rs +3 -2
- package/dist/runtime/rust/ws.rs +122 -50
- package/dist/runtime/ts/realtime-connection.ts +22 -0
- package/dist/runtime/ts/realtime-transports.ts +13 -4
- package/dist/runtime/ts/realtime.ts +27 -3
- package/dist/runtime/ts/sdk-realtime.ts +1 -1
- package/dist/runtime/ts/sdk-sse.ts +31 -14
- package/dist/runtime/ts/sdk-ws.ts +2 -1
- package/dist/runtime/ts/sdk.ts +9 -2
- package/dist/runtime/ts/sse.ts +8 -0
- package/dist/runtime/ts/ws.ts +88 -50
- package/dist/runtime/ts-tanstack-query/query.ts +0 -2
- package/package.json +12 -40
- package/dist/emit/cli/index.d.ts +0 -1
- package/dist/emit/cli/index.js +0 -1
- package/dist/emit/csharp/index.d.ts +0 -1
- package/dist/emit/csharp/index.js +0 -1
- package/dist/emit/go/index.d.ts +0 -1
- package/dist/emit/go/index.js +0 -1
- package/dist/emit/java/index.d.ts +0 -2
- package/dist/emit/java/index.js +0 -2
- package/dist/emit/mcp/index.d.ts +0 -3
- package/dist/emit/mcp/index.js +0 -3
- package/dist/emit/python/index.d.ts +0 -1
- package/dist/emit/python/index.js +0 -1
- package/dist/emit/rust/index.d.ts +0 -1
- package/dist/emit/rust/index.js +0 -1
package/dist/emit/java/gradle.js
CHANGED
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
import { escapeText } from "../shared/escape.js";
|
|
11
11
|
import { AUTHOR, authorOf, checked, HTTPS_URL, SPDX_ID, TEXT } from "../shared/metadata.js";
|
|
12
12
|
/** The one runtime dependency, its coordinates pinned with the test classpath's (java-pins.json). */
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
const JACKSON = "tools.jackson.core:jackson-databind:3.2.4";
|
|
14
|
+
// Java's keywords and literals (JLS 3.9, 3.10.3, 3.10.7): no package segment may be one, and `_` is one since 9
|
|
15
15
|
const RESERVED = [
|
|
16
16
|
"abstract",
|
|
17
17
|
"assert",
|
|
@@ -68,15 +68,15 @@ const RESERVED = [
|
|
|
68
68
|
"null",
|
|
69
69
|
"_",
|
|
70
70
|
];
|
|
71
|
-
|
|
71
|
+
// the type names the client file spells or imports, and its nested Builder: a client of one of these clashes
|
|
72
72
|
const SPELLED = ["Builder", "IllegalStateException", "String", "Transport", "URI"];
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
// a Maven group: lowercase dotted words; an artifact: lowercase words joined by `-`; a package segment: a lowercase
|
|
74
|
+
// identifier; a client: a PascalCase type
|
|
75
75
|
const GROUP_ID = /^[a-z][a-z0-9_]*(?:\.[a-z][a-z0-9_]*)*$/;
|
|
76
76
|
const ARTIFACT_ID = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
|
|
77
77
|
const SEGMENT = /^[a-z_][a-z0-9_]*$/;
|
|
78
78
|
const CLIENT_NAME = /^[A-Z][A-Za-z0-9_]*$/;
|
|
79
|
-
|
|
79
|
+
// SemVer 2 (semver.org's recommended pattern), as the C# and Rust packages take it
|
|
80
80
|
const SEMVER = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
|
|
81
81
|
const refuse = (option, value) => {
|
|
82
82
|
throw new Error(`Invalid ${option}: ${JSON.stringify(value)}`);
|
|
@@ -95,7 +95,7 @@ export function validatePackageName(value) {
|
|
|
95
95
|
export const validateClientName = (value) => CLIENT_NAME.test(value) && !SPELLED.includes(value) ? value : refuse("clientName", value);
|
|
96
96
|
/** A SemVer 2 version. */
|
|
97
97
|
export const validateVersion = (value) => (SEMVER.test(value) ? value : refuse("version", value));
|
|
98
|
-
|
|
98
|
+
// a repository is a path on its host: `https://host/owner/name`
|
|
99
99
|
const REPOSITORY_PATH = /^https:\/\/[^/]+\/[^/]/;
|
|
100
100
|
/** The developers option as one author each, or a refusal of the whole value when any is not a name or `Name <email>`. */
|
|
101
101
|
function developersOf(value) {
|
|
@@ -128,7 +128,7 @@ export function pomOf(options, info) {
|
|
|
128
128
|
* text neither interpolates (no `$` in `'…'`) nor ends the string.
|
|
129
129
|
*/
|
|
130
130
|
const groovyString = (text) => `'${escapeText(text, "java").replaceAll("'", "\\'")}'`;
|
|
131
|
-
|
|
131
|
+
// the indent of the `pom { … }` block's members, and of one level in
|
|
132
132
|
const POM = " ";
|
|
133
133
|
const IN = " ";
|
|
134
134
|
/** `key = '…'` lines at `indent`, each value a Groovy string. */
|
package/dist/emit/java/json.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
/** One dispatch: the values that select a wrapper, the wrapper, and the payload type it reads. */
|
|
2
|
+
/** A keyed union's arm: the values that select it, its wrapper, its payload, and what its first value spells. */
|
|
2
3
|
export type Dispatch = {
|
|
3
4
|
readonly values: readonly string[];
|
|
4
5
|
readonly wrapper: string;
|
|
5
6
|
readonly payload: string;
|
|
7
|
+
/** its first value as a Java type name, `Variant<index>` when none selects it */
|
|
8
|
+
readonly spelled: string;
|
|
6
9
|
};
|
|
7
10
|
/** A union's nested `Deserializer`, two spaces in: from its `final class` line to its closing brace. */
|
|
8
11
|
export declare function deserializerLines(name: string, property: string, dispatch: readonly Dispatch[], unknown: string): string[];
|
package/dist/emit/java/json.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { javaString } from "../shared/escape.js";
|
|
7
7
|
import { bin, call, classOf, forOpen, ifOpen, lit, local, methodHeader, newOf, param, returnStmt, rule, statement, throwStmt, typeHeader, } from "./layout.js";
|
|
8
|
-
|
|
8
|
+
// a payload's erasure: `Class` takes no type arguments
|
|
9
9
|
const erased = (type) => type.replace(/<.*$/, "");
|
|
10
10
|
/** One switch arm, `case label -> value;` (`default` for no label), in the switch's body. */
|
|
11
11
|
const arm = (label, value) => statement(8, rule(label === null ? null : lit(label), value));
|
|
@@ -129,7 +129,10 @@ type Header = {
|
|
|
129
129
|
readonly name: string;
|
|
130
130
|
readonly end: "{" | "{}";
|
|
131
131
|
};
|
|
132
|
-
/**
|
|
132
|
+
/**
|
|
133
|
+
* A type declaration's header: a record's components and interface, a class's superclass, an interface's permits (none
|
|
134
|
+
* when its subtypes are nested in it).
|
|
135
|
+
*/
|
|
133
136
|
export type TypeHeader = Header & ({
|
|
134
137
|
readonly kind: "enum";
|
|
135
138
|
} | {
|
|
@@ -52,7 +52,7 @@ export const id = (path) => path
|
|
|
52
52
|
/** A literal or any token kept whole: `"GET"`, `null`, `true`, `16L`. */
|
|
53
53
|
export const lit = (text) => ({ k: "lit", text });
|
|
54
54
|
/** A string as an expression: an identifier path, else a token kept whole. */
|
|
55
|
-
export const expr = (e) => typeof e
|
|
55
|
+
export const expr = (e) => typeof e === "string" ? (/^[A-Za-z_$][\w$]*(\.[A-Za-z_$][\w$]*)*$/.test(e) ? id(e) : lit(e)) : e;
|
|
56
56
|
/** `of.name(args)`, or `name(args)` with no receiver; a string receiver is an identifier path. */
|
|
57
57
|
export const call = (of, name, args = []) => ({
|
|
58
58
|
args: args.map(expr),
|
|
@@ -125,15 +125,16 @@ export function method(v, m) {
|
|
|
125
125
|
/** The return type and the name with its `(`, the break before the name tagged. */
|
|
126
126
|
function nameScope(v, m, beforeName) {
|
|
127
127
|
v.b.open(0);
|
|
128
|
-
if (m.returns
|
|
128
|
+
if (m.returns === undefined) {
|
|
129
|
+
v.b.open(0);
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
129
132
|
v.b.open(0);
|
|
130
133
|
v.b.open(0);
|
|
131
134
|
v.type(type(m.returns));
|
|
132
135
|
v.b.close();
|
|
133
136
|
v.b.brk("independent", " ", 0, beforeName);
|
|
134
|
-
}
|
|
135
|
-
else
|
|
136
|
-
v.b.open(0); /* a constructor's name scope opens with its name */
|
|
137
|
+
} /* a constructor's name scope opens with its name */
|
|
137
138
|
v.b.token(m.name);
|
|
138
139
|
v.b.token("(");
|
|
139
140
|
v.b.close();
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
/** google-java-format's line width. */
|
|
10
10
|
export const WIDTH = 100;
|
|
11
|
-
|
|
11
|
+
// a forced break is "wider" than any line (Doc.MAX_LINE_WIDTH)
|
|
12
12
|
const MAX = 1000;
|
|
13
13
|
const evalIndent = (indent) => typeof indent === "number" ? indent : indent.tag.taken ? indent.onBreak : indent.onFlat;
|
|
14
14
|
/** Indent.If: `onBreak` when `tag`'s break was taken, else `onFlat`. */
|
|
@@ -50,8 +50,9 @@ function computeBroken(doc, start) {
|
|
|
50
50
|
doc.breaks.push(child);
|
|
51
51
|
doc.splits.push([]);
|
|
52
52
|
}
|
|
53
|
-
else
|
|
53
|
+
else {
|
|
54
54
|
doc.splits.at(-1).push(child);
|
|
55
|
+
}
|
|
55
56
|
}
|
|
56
57
|
return doc.splits.reduce((state, split, i) => breakAndSplit(state, i === 0 ? undefined : doc.breaks[i - 1], split), start);
|
|
57
58
|
}
|
|
@@ -68,8 +69,9 @@ function breakAndSplit(state, brk, split) {
|
|
|
68
69
|
brk.newIndent = Math.max(state.lastIndent + evalIndent(brk.indent), 0);
|
|
69
70
|
next = { ...state, column: brk.newIndent };
|
|
70
71
|
}
|
|
71
|
-
else
|
|
72
|
+
else {
|
|
72
73
|
next = { ...state, column: state.column + brk.flat.length };
|
|
74
|
+
}
|
|
73
75
|
}
|
|
74
76
|
const room = next.column + splitWidth <= WIDTH;
|
|
75
77
|
next = { ...next, mustBreak: false };
|
|
@@ -78,20 +80,24 @@ function breakAndSplit(state, brk, split) {
|
|
|
78
80
|
return room ? next : { ...next, mustBreak: true };
|
|
79
81
|
}
|
|
80
82
|
function write(doc, out) {
|
|
81
|
-
if (doc.k === "text")
|
|
83
|
+
if (doc.k === "text") {
|
|
82
84
|
put(doc, out);
|
|
83
|
-
|
|
85
|
+
}
|
|
86
|
+
else if (doc.k === "break") {
|
|
84
87
|
out.text += doc.broken ? `\n${" ".repeat(doc.newIndent)}` : doc.flat;
|
|
85
|
-
|
|
88
|
+
}
|
|
89
|
+
else if (doc.oneLine) {
|
|
86
90
|
for (const child of doc.docs)
|
|
87
91
|
writeFlat(child, out);
|
|
88
|
-
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
89
94
|
doc.splits.forEach((split, i) => {
|
|
90
95
|
if (i > 0)
|
|
91
96
|
write(doc.breaks[i - 1], out);
|
|
92
97
|
for (const child of split)
|
|
93
98
|
write(child, out);
|
|
94
99
|
});
|
|
100
|
+
}
|
|
95
101
|
}
|
|
96
102
|
function writeFlat(doc, out) {
|
|
97
103
|
if (doc.k === "level")
|
|
@@ -59,7 +59,7 @@ function regularDot(v, items) {
|
|
|
59
59
|
}
|
|
60
60
|
v.b.token(e.name);
|
|
61
61
|
argsAndParen(v, e, 0);
|
|
62
|
-
|
|
62
|
+
// the last item is the chain itself, and no dot follows it
|
|
63
63
|
if (i < items.length - 1)
|
|
64
64
|
length += v.flat(e).length;
|
|
65
65
|
});
|
|
@@ -4,10 +4,8 @@
|
|
|
4
4
|
* `+` chain (or stands alone), else under its own quote.
|
|
5
5
|
*/
|
|
6
6
|
import { WIDTH } from "./layout-doc.js";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
* `\n`/`\r` escapes ending the piece it closes
|
|
10
|
-
*/
|
|
7
|
+
// StringWrapper.stringComponents: a literal's body cut before each whitespace character (and a `\t`), a run of
|
|
8
|
+
// `\n`/`\r` escapes ending the piece it closes
|
|
11
9
|
function piecesOf(body) {
|
|
12
10
|
const out = [];
|
|
13
11
|
let start = 0;
|
|
@@ -19,8 +17,9 @@ function piecesOf(body) {
|
|
|
19
17
|
while (body.startsWith("\\r", i) || body.startsWith("\\n", i))
|
|
20
18
|
i += 2;
|
|
21
19
|
}
|
|
22
|
-
else
|
|
20
|
+
else {
|
|
23
21
|
continue;
|
|
22
|
+
}
|
|
24
23
|
out.push(body.slice(start, i));
|
|
25
24
|
start = i;
|
|
26
25
|
}
|
|
@@ -31,8 +31,9 @@ function typeList(v, word, types) {
|
|
|
31
31
|
v.b.open(4);
|
|
32
32
|
v.b.token(word);
|
|
33
33
|
types.forEach((t, i) => {
|
|
34
|
-
if (i === 0)
|
|
34
|
+
if (i === 0) {
|
|
35
35
|
v.b.space();
|
|
36
|
+
}
|
|
36
37
|
else {
|
|
37
38
|
v.b.token(",");
|
|
38
39
|
v.b.breakOp(" ");
|
|
@@ -44,7 +45,7 @@ function typeList(v, word, types) {
|
|
|
44
45
|
/** A type declaration's header, through its `{` (or `{}`). */
|
|
45
46
|
export function typeDeclaration(v, h) {
|
|
46
47
|
if (h.kind === "enum") {
|
|
47
|
-
|
|
48
|
+
// visitEnumDeclaration's header: a level of its own, `enum` and a unified break before the name at +4
|
|
48
49
|
v.b.open(0);
|
|
49
50
|
typeModifiers(v, h);
|
|
50
51
|
v.b.open(4);
|
|
@@ -61,7 +62,7 @@ export function typeDeclaration(v, h) {
|
|
|
61
62
|
v.b.token(h.name);
|
|
62
63
|
v.b.open(4);
|
|
63
64
|
if (h.kind === "record") {
|
|
64
|
-
|
|
65
|
+
// visitRecordDeclaration's components as formals, then its interface
|
|
65
66
|
v.b.token("(");
|
|
66
67
|
if (h.components.length > 0) {
|
|
67
68
|
v.b.breakToFill("");
|
|
@@ -79,15 +80,16 @@ export function typeDeclaration(v, h) {
|
|
|
79
80
|
}
|
|
80
81
|
else if (h.kind === "class") {
|
|
81
82
|
if (h.extends !== undefined) {
|
|
82
|
-
|
|
83
|
+
// classDeclarationTypeList of one superclass, in no level of its own
|
|
83
84
|
v.b.breakToFill(" ");
|
|
84
85
|
v.b.token("extends");
|
|
85
86
|
v.b.space();
|
|
86
87
|
v.type(type(h.extends));
|
|
87
88
|
}
|
|
88
89
|
}
|
|
89
|
-
else
|
|
90
|
+
else if (h.permits.length > 0) {
|
|
90
91
|
typeList(v, "permits", h.permits);
|
|
92
|
+
}
|
|
91
93
|
v.b.close();
|
|
92
94
|
}
|
|
93
95
|
v.b.space();
|
|
@@ -59,7 +59,7 @@ export class Visitor {
|
|
|
59
59
|
new Visitor(b).expr(e);
|
|
60
60
|
return b.flat();
|
|
61
61
|
}
|
|
62
|
-
|
|
62
|
+
// hasOnlyShortItems: every item under MAX_ITEM_LENGTH_FOR_FILLING (10) characters
|
|
63
63
|
shortItems(items) {
|
|
64
64
|
return items.every((item) => this.flat(item).length < 10);
|
|
65
65
|
}
|
|
@@ -78,8 +78,9 @@ export class Visitor {
|
|
|
78
78
|
const dims = t.dims > 0;
|
|
79
79
|
if (dims)
|
|
80
80
|
this.b.open(4);
|
|
81
|
-
if (t.args.length === 0)
|
|
81
|
+
if (t.args.length === 0) {
|
|
82
82
|
this.typeName(t.name);
|
|
83
|
+
}
|
|
83
84
|
else {
|
|
84
85
|
this.b.open(4);
|
|
85
86
|
this.typeName(t.name);
|
|
@@ -220,7 +221,7 @@ export class Visitor {
|
|
|
220
221
|
this.b.brk(fill, " ");
|
|
221
222
|
this.b.token(e.op);
|
|
222
223
|
this.b.space();
|
|
223
|
-
|
|
224
|
+
// a literal in a `+` after a non-literal operand reflows under its own quote
|
|
224
225
|
const later = e.op === "+" && isString(operand) && !e.operands.slice(0, i + 1).every(isString);
|
|
225
226
|
if (later)
|
|
226
227
|
this.b.literal(this.flat(operand), true);
|
package/dist/emit/java/layout.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* (GJF's Doc and DocBuilder), the visitor layout-visitor.ts, layout-dot.ts and layout-decl.ts (JavaInputAstVisitor),
|
|
6
6
|
* and a long string literal is reflowed as StringWrapper reflows it (layout-strings.ts).
|
|
7
7
|
*/
|
|
8
|
-
import { expr } from "./layout-ast.js";
|
|
8
|
+
import { expr, } from "./layout-ast.js";
|
|
9
9
|
import { annotation, declareOne, method } from "./layout-decl.js";
|
|
10
10
|
import { Builder } from "./layout-doc.js";
|
|
11
11
|
import { wrapStrings } from "./layout-strings.js";
|
|
@@ -26,7 +26,7 @@ const STATEMENTS = {
|
|
|
26
26
|
v.b.token(";");
|
|
27
27
|
},
|
|
28
28
|
for: (v, s) => {
|
|
29
|
-
|
|
29
|
+
// visitEnhancedForLoop: the variable declared with `:` before its iterable
|
|
30
30
|
v.b.open(0);
|
|
31
31
|
v.b.token("for");
|
|
32
32
|
v.b.space();
|
|
@@ -53,10 +53,11 @@ const STATEMENTS = {
|
|
|
53
53
|
local: (v, s) => declareOne(v, s.decl, { kind: "variable", trailing: ";" }),
|
|
54
54
|
return: (v, s) => keyword(v, "return", s.expr),
|
|
55
55
|
rule: (v, s) => {
|
|
56
|
-
|
|
56
|
+
// visitCase, a rule: `case label ->` or `default ->`, a unified break, the value at +4
|
|
57
57
|
v.b.open(4);
|
|
58
|
-
if (s.label === null)
|
|
58
|
+
if (s.label === null) {
|
|
59
59
|
v.b.token("default");
|
|
60
|
+
}
|
|
60
61
|
else {
|
|
61
62
|
v.b.token("case");
|
|
62
63
|
v.b.open(0);
|
|
@@ -11,6 +11,8 @@ export type JavaTypeNames = {
|
|
|
11
11
|
/** the schema of the component `name` when Java declares nothing for it, so a ref to it is its use type */
|
|
12
12
|
alias(name: string): IRSchema | undefined;
|
|
13
13
|
resolve(schema: IRSchema): IRSchema;
|
|
14
|
+
/** whether `name` is held: by the runtime, a type the SDK spells, `taken`, or a declaration claimed so far */
|
|
15
|
+
has(name: string): boolean;
|
|
14
16
|
/** each component emitted under another name than its own (`<Name>Model`), for the generation's notices */
|
|
15
17
|
readonly renamed: readonly Rename[];
|
|
16
18
|
};
|
|
@@ -9,7 +9,7 @@ import { irResolver } from "../../ir/schema.js";
|
|
|
9
9
|
import { compareCodeUnits, javaTypeName, NameScope } from "../shared/lang.js";
|
|
10
10
|
/** The type a value without a schema is, and the one a nullable open value is. */
|
|
11
11
|
export const JSON_NODE = "JsonNode";
|
|
12
|
-
|
|
12
|
+
// the type names the emitted models and the client spell, which no runtime file holds: each is reserved
|
|
13
13
|
const SPELLED = [
|
|
14
14
|
"Boolean",
|
|
15
15
|
"Builder",
|
|
@@ -39,7 +39,7 @@ const SPELLED = [
|
|
|
39
39
|
* The code text a scan reads: comments dropped and string and char literals emptied, in one left-to-right pass, so a
|
|
40
40
|
* `//` inside a string opens no comment and a quote inside a comment opens no string.
|
|
41
41
|
*/
|
|
42
|
-
export const codeOf = (code) => code.
|
|
42
|
+
export const codeOf = (code) => code.replaceAll(/\/\*[\s\S]*?\*\/|\/\/.*$|"(?:[^"\\\n]|\\.)*"|'(?:[^'\\\n]|\\.)'/gm, (token) => token.startsWith("/") ? "" : token[0] === '"' ? '""' : "''");
|
|
43
43
|
/** The type names `code` spells: each imported type by its simple name, and each capitalized identifier that is not a member access. */
|
|
44
44
|
export function spelledIn(code) {
|
|
45
45
|
const text = codeOf(code);
|
|
@@ -70,13 +70,14 @@ export function javaNames(schemas, taken, declares) {
|
|
|
70
70
|
const base = javaTypeName(name);
|
|
71
71
|
const taken = scope.has(base);
|
|
72
72
|
components.set(name, scope.claim(taken ? `${base}Model` : base));
|
|
73
|
-
if (taken)
|
|
73
|
+
if (taken) {
|
|
74
74
|
renamed.push({
|
|
75
75
|
because: `"${base}" is a name the SDK's runtime, the JDK or another declaration holds`,
|
|
76
76
|
name,
|
|
77
77
|
renamed: components.get(name),
|
|
78
78
|
what: "component",
|
|
79
79
|
});
|
|
80
|
+
}
|
|
80
81
|
}
|
|
81
82
|
const hoisted = new Map();
|
|
82
83
|
return {
|
|
@@ -84,12 +85,13 @@ export function javaNames(schemas, taken, declares) {
|
|
|
84
85
|
const schema = schemas[name] ?? UNKNOWN;
|
|
85
86
|
return declares(schema) ? undefined : schema;
|
|
86
87
|
},
|
|
88
|
+
has: (name) => scope.has(name),
|
|
87
89
|
hoist: (key, base) => {
|
|
88
90
|
const name = hoisted.get(key) ?? scope.claim(base);
|
|
89
91
|
hoisted.set(key, name);
|
|
90
92
|
return name;
|
|
91
93
|
},
|
|
92
|
-
|
|
94
|
+
// a ref reaches here only for a declared component: `alias` answers for every other one
|
|
93
95
|
ref: (name) => components.get(name),
|
|
94
96
|
renamed,
|
|
95
97
|
resolve: irResolver(schemas),
|
|
@@ -10,7 +10,6 @@
|
|
|
10
10
|
*/
|
|
11
11
|
import type { IRField, IRSchema } from "../../ir/model.ts";
|
|
12
12
|
import type { JavaTypeContext, Member } from "./models.ts";
|
|
13
|
-
export declare const RECORD_MEMBERS: readonly string[];
|
|
14
13
|
/** A record's members: its fields, and whether it keeps every other member (it declares `additionalProperties`). */
|
|
15
14
|
export type RecordShape = {
|
|
16
15
|
readonly fields: readonly IRField[];
|
|
@@ -2,8 +2,8 @@ import { javaString } from "../shared/escape.js";
|
|
|
2
2
|
import { camelWords, compareCodeUnits, isJavaReserved, NameScope } from "../shared/lang.js";
|
|
3
3
|
import { typeHeader } from "./layout.js";
|
|
4
4
|
import { JSON_NODE } from "./models-names.js";
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
// every member name a record component collides with: java.lang.Record's and Object's synthesized and final methods
|
|
6
|
+
const RECORD_MEMBERS = [
|
|
7
7
|
"clone",
|
|
8
8
|
"equals",
|
|
9
9
|
"finalize",
|
|
@@ -16,7 +16,7 @@ export const RECORD_MEMBERS = [
|
|
|
16
16
|
];
|
|
17
17
|
const NON_NULL = { args: ["JsonInclude.Include.NON_NULL"], name: "JsonInclude" };
|
|
18
18
|
const NON_EMPTY = { args: ["JsonInclude.Include.NON_EMPTY"], name: "JsonInclude" };
|
|
19
|
-
|
|
19
|
+
// a required, non-nullable use of these is written primitive
|
|
20
20
|
const PRIMITIVE = { Boolean: "boolean", Double: "double", Long: "long" };
|
|
21
21
|
/** Whether an object, or any object part of an allOf (refs followed), declares `additionalProperties`. */
|
|
22
22
|
export function isOpen(schema, resolve) {
|
|
@@ -5,13 +5,17 @@
|
|
|
5
5
|
* `<Union><Value>Body`. The nested `Deserializer` (json.ts) dispatches on the discriminator's value. Every permit and
|
|
6
6
|
* wrapper is `@JsonDeserialize` with no argument; no `@JsonTypeInfo` is ever written.
|
|
7
7
|
*/
|
|
8
|
-
import type { IRSchema } from "../../ir/model.ts";
|
|
8
|
+
import type { IRDiscriminator, IRSchema } from "../../ir/model.ts";
|
|
9
|
+
import { type Dispatch } from "./json.ts";
|
|
9
10
|
import type { JavaTypeContext, Member } from "./models.ts";
|
|
10
11
|
type Of<K extends IRSchema["kind"]> = Extract<IRSchema, {
|
|
11
12
|
kind: K;
|
|
12
13
|
}>;
|
|
14
|
+
type Discriminated = Of<"union"> & {
|
|
15
|
+
discriminator: IRDiscriminator;
|
|
16
|
+
};
|
|
13
17
|
/** Declares `schema` once under `key` and `base`; its name. */
|
|
14
|
-
|
|
18
|
+
type Declare = (schema: IRSchema, key: string, base: string) => string;
|
|
15
19
|
/** What a union's declaration reaches: the context, its members' types, and the hoist. */
|
|
16
20
|
export type UnionScope = {
|
|
17
21
|
at: JavaTypeContext;
|
|
@@ -22,6 +26,13 @@ export type UnionScope = {
|
|
|
22
26
|
export declare const isLiteral: (variant: IRSchema) => variant is Of<"const"> & {
|
|
23
27
|
value: string;
|
|
24
28
|
};
|
|
29
|
+
/** Whether `union` is keyed on one field, so Java declares it as a sealed interface of wrappers. */
|
|
30
|
+
export declare const isKeyed: (union: Of<"union">) => union is Discriminated;
|
|
31
|
+
/**
|
|
32
|
+
* Each selectable variant's payload, wrapper and selecting values, in the Deserializer's order; the wrappers are
|
|
33
|
+
* declared here, under the same names however often it runs.
|
|
34
|
+
*/
|
|
35
|
+
export declare function dispatchOf(name: string, union: Discriminated, scope: UnionScope): Dispatch[];
|
|
25
36
|
/** The declaration of a union `declares`: a string enum of its const strings, or a discriminated interface. */
|
|
26
37
|
export declare function unionDecl(name: string, union: Of<"union">, scope: UnionScope): string;
|
|
27
38
|
export {};
|
|
@@ -2,8 +2,8 @@ import { compareCodeUnits, javaTypeName } from "../shared/lang.js";
|
|
|
2
2
|
import { deserializerLines } from "./json.js";
|
|
3
3
|
import { classOf, typeHeader } from "./layout.js";
|
|
4
4
|
import { enumValues, stringEnumDecl } from "./models-enum.js";
|
|
5
|
-
import { fieldsOf } from "./models-record.js";
|
|
6
5
|
import { JSON_NODE } from "./models-names.js";
|
|
6
|
+
import { fieldsOf } from "./models-record.js";
|
|
7
7
|
/** Whether a variant is a const string: a union of these alone is a string enum. */
|
|
8
8
|
export const isLiteral = (variant) => variant.kind === "const" && typeof variant.value === "string";
|
|
9
9
|
/** The values that select `variant`: its mapping keys, else its property's const or enum, else its ref's name. */
|
|
@@ -45,16 +45,22 @@ const wrapperText = (wrapper, union, payload) => typeHeader(0, {
|
|
|
45
45
|
modifiers: ["public"],
|
|
46
46
|
name: wrapper,
|
|
47
47
|
}).join("\n");
|
|
48
|
-
/**
|
|
49
|
-
|
|
48
|
+
/** Whether `union` is keyed on one field, so Java declares it as a sealed interface of wrappers. */
|
|
49
|
+
export const isKeyed = (union) => union.discriminator !== undefined && union.variants.length > 0 && !union.variants.every(isLiteral);
|
|
50
|
+
/**
|
|
51
|
+
* Each selectable variant's payload, wrapper and selecting values, in the Deserializer's order; the wrappers are
|
|
52
|
+
* declared here, under the same names however often it runs.
|
|
53
|
+
*/
|
|
54
|
+
export function dispatchOf(name, union, scope) {
|
|
50
55
|
return selectedBy(union, scope.at).map(({ index, schema, values }) => {
|
|
51
56
|
const first = values[0] ?? `Variant${index}`;
|
|
57
|
+
const spelled = javaTypeName(first);
|
|
52
58
|
const payload = schema.kind === "ref"
|
|
53
59
|
? scope.member(schema, first)
|
|
54
|
-
: scope.declare(schema, `${name}.variant${index}`, `${name}${
|
|
55
|
-
const wrapper = scope.at.names.hoist(`${name}.wrap${index}`, `${name}${
|
|
60
|
+
: scope.declare(schema, `${name}.variant${index}`, `${name}${spelled}Body`);
|
|
61
|
+
const wrapper = scope.at.names.hoist(`${name}.wrap${index}`, `${name}${spelled}`);
|
|
56
62
|
scope.at.decls.set(wrapper, wrapperText(wrapper, name, payload));
|
|
57
|
-
return { payload, values, wrapper };
|
|
63
|
+
return { payload, spelled, values, wrapper };
|
|
58
64
|
});
|
|
59
65
|
}
|
|
60
66
|
/** The sealed interface of `union`'s wrappers, its nested Deserializer, then the `<Union>Unknown` wrapper. */
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* type name is reserved where it collides (models-names.ts).
|
|
6
6
|
*/
|
|
7
7
|
import type { IR, IRSchema } from "../../ir/model.ts";
|
|
8
|
+
import type { Dispatch } from "./json.ts";
|
|
8
9
|
import { type JavaTypeNames } from "./models-names.ts";
|
|
9
|
-
export type { JavaTypeNames } from "./models-names.ts";
|
|
10
10
|
/** Where a type is being written, the declarations it hoists, and the names. */
|
|
11
11
|
export type JavaTypeContext = {
|
|
12
12
|
parentName: string;
|
|
@@ -24,5 +24,9 @@ export declare function javaTypeContext(ir: IR, taken?: readonly string[]): Java
|
|
|
24
24
|
export declare function declares(ir: IRSchema): boolean;
|
|
25
25
|
/** The Java type of `ir` at a use position of `at`. */
|
|
26
26
|
export declare function javaTypeOf(ir: IRSchema, at: JavaTypeContext): string;
|
|
27
|
+
/** The Java type of `ir` at `at`, a schema that declares hoisted as `<Parent><Field>`: an inline keyed union too. */
|
|
28
|
+
export declare function declaredTypeOf(ir: IRSchema, at: JavaTypeContext): string;
|
|
29
|
+
/** The wrappers of `name`, the keyed union `ir` resolves to, in its Deserializer's order; none when `ir` is not one. */
|
|
30
|
+
export declare function keyedArms(name: string, ir: IRSchema, ctx: JavaTypeContext): readonly Dispatch[];
|
|
27
31
|
/** The top-level declaration of the type `name` (a Java name already) for `ir`, which `declares`. */
|
|
28
32
|
export declare function javaTopLevel(name: string, ir: IRSchema, ctx: JavaTypeContext): string;
|
package/dist/emit/java/models.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { pascalWords, javaTypeName } from "../shared/lang.js";
|
|
2
2
|
import { enumDecl, enumValues } from "./models-enum.js";
|
|
3
|
-
import { fieldsOf, isOpen, recordDecl } from "./models-record.js";
|
|
4
3
|
import { javaNames, JSON_NODE } from "./models-names.js";
|
|
5
|
-
import {
|
|
6
|
-
|
|
4
|
+
import { fieldsOf, isOpen, recordDecl } from "./models-record.js";
|
|
5
|
+
import { dispatchOf, isKeyed, isLiteral, unionDecl } from "./models-union.js";
|
|
6
|
+
// deeper than this, a schema is any JSON value
|
|
7
7
|
const MAX_DEPTH = 12;
|
|
8
8
|
const SCALARS = {
|
|
9
9
|
boolean: "Boolean",
|
|
@@ -32,7 +32,7 @@ export function declares(ir) {
|
|
|
32
32
|
function declare(ir, at, key, base) {
|
|
33
33
|
const name = at.names.hoist(key, base);
|
|
34
34
|
if (!at.decls.has(name)) {
|
|
35
|
-
|
|
35
|
+
// claimed before its body: what the body hoists in turn comes after it
|
|
36
36
|
at.decls.set(name, "");
|
|
37
37
|
at.decls.set(name, javaTopLevel(name, ir, at));
|
|
38
38
|
}
|
|
@@ -75,44 +75,68 @@ function use(ir, at) {
|
|
|
75
75
|
if (at.depth > MAX_DEPTH)
|
|
76
76
|
return JSON_NODE;
|
|
77
77
|
switch (ir.kind) {
|
|
78
|
-
case "allOf":
|
|
78
|
+
case "allOf": {
|
|
79
79
|
return allOfUse(ir, at);
|
|
80
|
-
|
|
80
|
+
}
|
|
81
|
+
case "array": {
|
|
81
82
|
return `List<${use(ir.items, deeper(at, `${at.fieldName}Item`, "[]"))}>`;
|
|
82
|
-
|
|
83
|
+
}
|
|
84
|
+
case "binary": {
|
|
83
85
|
return "String";
|
|
84
|
-
|
|
86
|
+
}
|
|
87
|
+
case "const": {
|
|
85
88
|
return constType(ir);
|
|
86
|
-
|
|
89
|
+
}
|
|
90
|
+
case "nullable": {
|
|
87
91
|
return use(ir.inner, deeper(at));
|
|
88
|
-
|
|
92
|
+
}
|
|
93
|
+
case "object": {
|
|
89
94
|
return objectUse(ir, at);
|
|
90
|
-
|
|
95
|
+
}
|
|
96
|
+
case "ref": {
|
|
91
97
|
return refUse(ir, at);
|
|
92
|
-
|
|
98
|
+
}
|
|
99
|
+
case "scalar": {
|
|
93
100
|
return enumValues(ir).length > 0 ? hoist(ir, at) : SCALARS[ir.type];
|
|
94
|
-
|
|
101
|
+
}
|
|
102
|
+
case "tuple": {
|
|
95
103
|
return `List<${JSON_NODE}>`;
|
|
96
|
-
|
|
104
|
+
}
|
|
105
|
+
// a union in use position is any JSON value; a component union is declared
|
|
97
106
|
case "union":
|
|
98
|
-
case "unknown":
|
|
107
|
+
case "unknown": {
|
|
99
108
|
return JSON_NODE;
|
|
109
|
+
}
|
|
100
110
|
}
|
|
101
111
|
}
|
|
102
112
|
/** The Java type of `ir` at a use position of `at`. */
|
|
103
113
|
export function javaTypeOf(ir, at) {
|
|
104
114
|
return use(ir, { ...at, depth: 0, path: "" });
|
|
105
115
|
}
|
|
116
|
+
/** The Java type of `ir` at `at`, a schema that declares hoisted as `<Parent><Field>`: an inline keyed union too. */
|
|
117
|
+
export function declaredTypeOf(ir, at) {
|
|
118
|
+
return ir.kind !== "ref" && declares(ir) ? hoist(ir, { ...at, depth: 0, path: "" }) : javaTypeOf(ir, at);
|
|
119
|
+
}
|
|
120
|
+
/** The members of the declaration `name` (a Java name already) and what its variants hoist, in `ctx`. */
|
|
121
|
+
const scopeOf = (name, ctx) => ({
|
|
122
|
+
at: ctx,
|
|
123
|
+
declare: (schema, key, base) => declare(schema, ctx, key, base),
|
|
124
|
+
member: (schema, fieldName) => javaTypeOf(schema, { ...ctx, fieldName, parentName: name }),
|
|
125
|
+
});
|
|
126
|
+
/** The wrappers of `name`, the keyed union `ir` resolves to, in its Deserializer's order; none when `ir` is not one. */
|
|
127
|
+
export function keyedArms(name, ir, ctx) {
|
|
128
|
+
const union = ctx.names.resolve(ir);
|
|
129
|
+
return union.kind === "union" && isKeyed(union) ? dispatchOf(name, union, scopeOf(name, ctx)) : [];
|
|
130
|
+
}
|
|
106
131
|
/** The top-level declaration of the type `name` (a Java name already) for `ir`, which `declares`. */
|
|
107
132
|
export function javaTopLevel(name, ir, ctx) {
|
|
108
|
-
const
|
|
133
|
+
const scope = scopeOf(name, ctx);
|
|
109
134
|
if (ir.kind === "scalar")
|
|
110
135
|
return enumDecl(name, ir);
|
|
111
136
|
const open = isOpen(ir, ctx.names.resolve);
|
|
112
137
|
if (ir.kind === "object")
|
|
113
|
-
return recordDecl(name, { fields: ir.fields, open }, member, ctx);
|
|
138
|
+
return recordDecl(name, { fields: ir.fields, open }, scope.member, ctx);
|
|
114
139
|
if (ir.kind === "allOf")
|
|
115
|
-
return recordDecl(name, { fields: fieldsOf(ir, ctx.names.resolve), open }, member, ctx);
|
|
116
|
-
|
|
117
|
-
return unionDecl(name, ir, { at: ctx, declare: declareVariant, member });
|
|
140
|
+
return recordDecl(name, { fields: fieldsOf(ir, ctx.names.resolve), open }, scope.member, ctx);
|
|
141
|
+
return unionDecl(name, ir, scope);
|
|
118
142
|
}
|