ajsc 7.1.0 → 7.2.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/CHANGELOG.md +16 -0
- package/README.md +43 -0
- package/dist/converter/index.d.ts +6 -3
- package/dist/converter/index.js +5 -3
- package/dist/converter/index.js.map +1 -1
- package/dist/converter/inlineEmission.d.ts +42 -0
- package/dist/converter/inlineEmission.js +49 -0
- package/dist/converter/inlineEmission.js.map +1 -0
- package/dist/kotlin/KotlinConverter.d.ts +22 -0
- package/dist/kotlin/KotlinConverter.js +1 -0
- package/dist/kotlin/KotlinConverter.js.map +1 -1
- package/dist/kotlin/objectEmitter.d.ts +6 -0
- package/dist/kotlin/objectEmitter.js +31 -3
- package/dist/kotlin/objectEmitter.js.map +1 -1
- package/dist/swift/SwiftConverter.d.ts +23 -0
- package/dist/swift/SwiftConverter.js +1 -0
- package/dist/swift/SwiftConverter.js.map +1 -1
- package/dist/swift/structEmitter.d.ts +5 -0
- package/dist/swift/structEmitter.js +29 -5
- package/dist/swift/structEmitter.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to ajsc are documented here. The format is based on [Keep a Changelog](https://keepachangelog.com/), and this project follows [Semantic Versioning](https://semver.org/).
|
|
4
4
|
|
|
5
|
+
## [7.1.0] — 2026-04-25
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
|
|
9
|
+
- **`inlineTypes` opt for Kotlin and Swift converters.** When `inlineTypes: true`, nested object types are emitted as nested `data class` (Kotlin) or `struct` (Swift) declarations inside their parent's body, rather than as top-level siblings. For codegen pipelines that emit multiple schemas into one namespace, this eliminates the cross-call name-collision class structurally — each parent's `Address` is name-scoped (`Body.Address` vs `Response.Address`), so no shared `nameRegistry` plumbing is required. Default: `false` (extract to top-level — the v7.0 behavior).
|
|
10
|
+
- Top-level enums and discriminated unions remain extracted regardless of `inlineTypes` — they have their own dedup story and don't benefit from nesting.
|
|
11
|
+
- The TypeScript converter has had `inlineTypes` since v6 (anonymous inline literals — a different language mechanism); the Kotlin/Swift addition uses the same opt name for the same consumer-facing intent.
|
|
12
|
+
- New `nested-emission` cross-language fixture covering the inline pattern across TypeScript, Kotlin, and Swift (19th fixture, brings the golden corpus to 57 files).
|
|
13
|
+
- **`generateInlineNestedDecl` and `InlineNestedCollector`** exported from `ajsc/converter` for downstream language authors implementing similar nested-decl semantics.
|
|
14
|
+
|
|
15
|
+
### Internal
|
|
16
|
+
|
|
17
|
+
- New shared module `src/converter/inlineEmission.ts` consolidates the per-parent collector type, the `getReferencedType` replacement, and the multi-line indent helper that previously existed (in identical form) inside both `kotlin/objectEmitter.ts` and `swift/structEmitter.ts`. Each language emitter now passes a small language-specific `formatNestedDecl(c, name, body, ir)` callback — the only part that genuinely differs.
|
|
18
|
+
- Test count: 473 → 500 (per-language inline-types coverage + sibling-parent dedup pinning + the cross-language nested-emission fixture).
|
|
19
|
+
- Existing TS / Kotlin / Swift goldens are byte-identical between v7.0.0 and v7.1.0 (the new opt defaults to `false`).
|
|
20
|
+
|
|
5
21
|
## [7.0.0] — 2026-04-24
|
|
6
22
|
|
|
7
23
|
This release adds first-class **Kotlin** and **Swift** language targets, restores the root subpath import that v6 dropped, and ships several behavioral fixes to the TypeScript converter that consumers depending on the older buggy output may need to migrate.
|
package/README.md
CHANGED
|
@@ -173,6 +173,7 @@ All three converters accept `BaseConverterOpts` plus language-specific options.
|
|
|
173
173
|
|--------|------|---------|-------------|
|
|
174
174
|
| `serializer` | `"kotlinx" \| "none"` | `"kotlinx"` | Emit `@Serializable`/`@SerialName`/`@Contextual` annotations and matching imports. `"none"` emits plain types. |
|
|
175
175
|
| `packageName` | `string` | `undefined` | If set, emit `package <name>` at the top of the output. |
|
|
176
|
+
| `inlineTypes` | `boolean` | `false` | If true, nested object types emit as nested `data class` declarations inside their parent's body block instead of extracting to top-level siblings. Top-level enums and discriminated unions remain extracted. See "Emitting multiple schemas into one namespace" for the motivation. |
|
|
176
177
|
|
|
177
178
|
### Swift (`SwiftConverterOpts`)
|
|
178
179
|
|
|
@@ -180,6 +181,7 @@ All three converters accept `BaseConverterOpts` plus language-specific options.
|
|
|
180
181
|
|--------|------|---------|-------------|
|
|
181
182
|
| `serializer` | `"codable" \| "none"` | `"codable"` | Emit `: Codable` conformance, `CodingKeys` enums, and discriminated-union `init(from:)`/`encode(to:)` plumbing. `"none"` emits plain types. |
|
|
182
183
|
| `accessLevel` | `"public" \| "internal"` | `"public"` | Access modifier on emitted types and members. |
|
|
184
|
+
| `inlineTypes` | `boolean` | `false` | If true, nested object types emit as nested `struct` declarations inside their parent's body block instead of extracting to top-level siblings. Top-level enums and discriminated `enum`s remain extracted. See "Emitting multiple schemas into one namespace" for the motivation. |
|
|
183
185
|
|
|
184
186
|
---
|
|
185
187
|
|
|
@@ -235,6 +237,39 @@ Schema-level `default` values are emitted inline for primitive types in Kotlin/S
|
|
|
235
237
|
|
|
236
238
|
Codegen pipelines that emit several sibling schemas into a shared output (e.g., wrapping an endpoint's `pathParams`, `query`, `body`, `response`, and error types under a single Kotlin `object` or Swift `enum`) need to avoid duplicate-name collisions across the emit calls. Each emit call has its own private name-tracking state by default, so two slots with a nested `address: { type: "object" }` would each emit a `data class Address(...)` — a duplicate-class compile error in the merged output.
|
|
237
239
|
|
|
240
|
+
There are two ways to fix this: nest extracted types inside the parent (recommended for Kotlin/Swift), or share a name registry across calls. Pick whichever output shape your pipeline wants.
|
|
241
|
+
|
|
242
|
+
### Recommended: `inlineTypes: true` (Kotlin / Swift)
|
|
243
|
+
|
|
244
|
+
Set `inlineTypes: true` per emit call. Each parent's nested object types are emitted as nested `data class` (Kotlin) or `struct` (Swift) declarations inside the parent's body block, scoped to its namespace. Cross-call collisions disappear structurally — no shared registry plumbing required.
|
|
245
|
+
|
|
246
|
+
```ts
|
|
247
|
+
import { emitKotlin } from "ajsc";
|
|
248
|
+
|
|
249
|
+
const body = emitKotlin(bodySchema, { inlineTypes: true, rootTypeName: "Body" });
|
|
250
|
+
const response = emitKotlin(responseSchema, { inlineTypes: true, rootTypeName: "Response" });
|
|
251
|
+
|
|
252
|
+
// body.code:
|
|
253
|
+
// data class Body(val address: Address, ...) {
|
|
254
|
+
// data class Address(val street: String, ...)
|
|
255
|
+
// }
|
|
256
|
+
// response.code:
|
|
257
|
+
// data class Response(val address: Address, ...) {
|
|
258
|
+
// data class Address(val street: String, ...)
|
|
259
|
+
// }
|
|
260
|
+
//
|
|
261
|
+
// Concatenated under one `object Endpoint { ... }` — no name collisions:
|
|
262
|
+
// Body.Address and Response.Address are different scoped names.
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
`inlineTypes: true` only nests plain object types. Top-level enums and discriminated unions (sealed interface in Kotlin, enum w/ associated values in Swift) remain at the same level as the parent — they have their own dedup story (canonical-key for enums; self-contained namespace for discriminated unions) and don't benefit from nesting.
|
|
266
|
+
|
|
267
|
+
The TypeScript converter has had `inlineTypes` since v6 — it produces anonymous inline type literals (a different language mechanism, same `inlineTypes` option name).
|
|
268
|
+
|
|
269
|
+
### Alternative: shared `nameRegistry` + per-slot `namePrefix`
|
|
270
|
+
|
|
271
|
+
Use this when you specifically want flat top-level output (cleaner imports, simpler navigation in IDE outline panels).
|
|
272
|
+
|
|
238
273
|
Pass a shared `nameRegistry: Set<string>` across calls. The converter uses it as its declaration registry and mutates it as new types are emitted. Pair with `namePrefix` for clean per-slot names:
|
|
239
274
|
|
|
240
275
|
```ts
|
|
@@ -280,6 +315,14 @@ emitKotlin(responseSchema, { nameRegistry: registry, rootTypeName: "Response" })
|
|
|
280
315
|
|
|
281
316
|
Both `nameRegistry` and `namePrefix` work for all three languages. They're most useful for Kotlin and Swift, which require named declarations for non-primitive types. TypeScript supports them too but rarely needs them — most codegen pipelines use `inlineTypes: true` to flatten nested types instead.
|
|
282
317
|
|
|
318
|
+
### Choosing between `inlineTypes` and `nameRegistry`
|
|
319
|
+
|
|
320
|
+
| If you want… | Use |
|
|
321
|
+
|--------------------------------------------------|----------------------------------------------|
|
|
322
|
+
| Flat top-level decls (`Body`, `BodyAddress`, …) | `nameRegistry` + `namePrefix` |
|
|
323
|
+
| Nested decls inside each parent (idiomatic K/S) | `inlineTypes: true` |
|
|
324
|
+
| TypeScript anonymous inline literals | `inlineTypes: true` (TS-specific mechanism) |
|
|
325
|
+
|
|
283
326
|
---
|
|
284
327
|
|
|
285
328
|
## Working with the IR directly
|
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
*
|
|
4
4
|
* This subpath exposes the abstract {@link BaseConverter} class, the
|
|
5
5
|
* {@link LanguageProfile} pattern that consolidates per-language behavior,
|
|
6
|
-
* the {@link RefTypeEntry} registry shape, the indent-aware
|
|
7
|
-
*
|
|
8
|
-
* {@link
|
|
6
|
+
* the {@link RefTypeEntry} registry shape, the indent-aware Emitter helper
|
|
7
|
+
* (importable from `./Emitter.js`), the {@link walkIR} tree-walking helper,
|
|
8
|
+
* and the {@link generateInlineNestedDecl} helper used by language emitters
|
|
9
|
+
* to implement `inlineTypes`-style nested-class output.
|
|
9
10
|
*
|
|
10
11
|
* Most consumers do NOT need this subpath — use the function-style emitters
|
|
11
12
|
* (`emitTypescript` / `emitKotlin` / `emitSwift` from `ajsc`) or the
|
|
@@ -22,3 +23,5 @@
|
|
|
22
23
|
*/
|
|
23
24
|
export { BaseConverter, walkIR } from "./BaseConverter.js";
|
|
24
25
|
export type { BaseConverterOpts, RefTypeNamingConfig, RefTypeEntry, GenerateTypeUtils, LanguageProfile, } from "./BaseConverter.js";
|
|
26
|
+
export { generateInlineNestedDecl, indentLines } from "./inlineEmission.js";
|
|
27
|
+
export type { InlineNestedCollector } from "./inlineEmission.js";
|
package/dist/converter/index.js
CHANGED
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
*
|
|
4
4
|
* This subpath exposes the abstract {@link BaseConverter} class, the
|
|
5
5
|
* {@link LanguageProfile} pattern that consolidates per-language behavior,
|
|
6
|
-
* the {@link RefTypeEntry} registry shape, the indent-aware
|
|
7
|
-
*
|
|
8
|
-
* {@link
|
|
6
|
+
* the {@link RefTypeEntry} registry shape, the indent-aware Emitter helper
|
|
7
|
+
* (importable from `./Emitter.js`), the {@link walkIR} tree-walking helper,
|
|
8
|
+
* and the {@link generateInlineNestedDecl} helper used by language emitters
|
|
9
|
+
* to implement `inlineTypes`-style nested-class output.
|
|
9
10
|
*
|
|
10
11
|
* Most consumers do NOT need this subpath — use the function-style emitters
|
|
11
12
|
* (`emitTypescript` / `emitKotlin` / `emitSwift` from `ajsc`) or the
|
|
@@ -21,4 +22,5 @@
|
|
|
21
22
|
* Treat `@internal`-tagged symbols as protected.
|
|
22
23
|
*/
|
|
23
24
|
export { BaseConverter, walkIR } from "./BaseConverter.js";
|
|
25
|
+
export { generateInlineNestedDecl, indentLines } from "./inlineEmission.js";
|
|
24
26
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/converter/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/converter/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,OAAO,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAQ3D,OAAO,EAAE,wBAAwB,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { BaseConverterContext, GenerateTypeUtils } from "./BaseConverter.js";
|
|
2
|
+
import type { IRNode } from "../types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Per-parent collector used by language emitters under `inlineTypes: true` to
|
|
5
|
+
* accumulate nested type declarations destined for the parent's body block.
|
|
6
|
+
*
|
|
7
|
+
* Each invocation of `generate<Lang>ObjectType` allocates its own collector,
|
|
8
|
+
* so grandchildren nest into the child's body — preserving the schema's
|
|
9
|
+
* containment structure rather than flattening to the outermost parent.
|
|
10
|
+
*
|
|
11
|
+
* - `decls`: fully-formatted nested type declaration strings, in emission order.
|
|
12
|
+
* - `bySignature`: within-parent dedup map (signature → already-assigned name).
|
|
13
|
+
* Two properties of the same parent referencing the same shape share one
|
|
14
|
+
* nested decl; two properties of *different* parents do not (each parent's
|
|
15
|
+
* nested decls are scoped to its own namespace).
|
|
16
|
+
*/
|
|
17
|
+
export interface InlineNestedCollector {
|
|
18
|
+
decls: string[];
|
|
19
|
+
bySignature: Map<string, string>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Inline-mode replacement for `getReferencedType`. Returns the name to use in
|
|
23
|
+
* place of an extracted top-level reference, side-effecting `collector` with a
|
|
24
|
+
* fully-formatted nested type declaration when this signature is first seen.
|
|
25
|
+
*
|
|
26
|
+
* Language-specific decl assembly is delegated to `formatDecl` (e.g. Kotlin's
|
|
27
|
+
* `[@Serializable\n]data class ${name}${body}` vs Swift's `${access} struct
|
|
28
|
+
* ${name}: Codable ${body}`). The recursive emit step is delegated to
|
|
29
|
+
* `recurseObjectType` — typically the language's `generate<Lang>ObjectType`
|
|
30
|
+
* function. We pass it our parent's collector-aware utils as a defensive
|
|
31
|
+
* default; the recursive call sees `c.inlineTypes === true` and will still
|
|
32
|
+
* allocate its own collector internally for *its* nested types.
|
|
33
|
+
*
|
|
34
|
+
* Side effect to be aware of: `defaultResolveRefTypeName` reserves the chosen
|
|
35
|
+
* name in `c.usedDeclarationNames`. That reservation is what prevents nested
|
|
36
|
+
* names from colliding with top-level enum / discriminated-union names emitted
|
|
37
|
+
* elsewhere in the same converter run, and it's why a shared `nameRegistry`
|
|
38
|
+
* across emit calls flows through to nested-decl naming.
|
|
39
|
+
*/
|
|
40
|
+
export declare function generateInlineNestedDecl<C extends BaseConverterContext>(c: C, ir: IRNode, collector: InlineNestedCollector, formatDecl: (c: C, name: string, body: string, ir: IRNode) => string, recurseObjectType: (c: C, ir: IRNode, utils: GenerateTypeUtils) => string): string | undefined;
|
|
41
|
+
/** Indents each non-empty line of `text` by `n` spaces. */
|
|
42
|
+
export declare function indentLines(text: string, n: number): string;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { defaultResolveRefTypeName } from "./naming.js";
|
|
2
|
+
/**
|
|
3
|
+
* Inline-mode replacement for `getReferencedType`. Returns the name to use in
|
|
4
|
+
* place of an extracted top-level reference, side-effecting `collector` with a
|
|
5
|
+
* fully-formatted nested type declaration when this signature is first seen.
|
|
6
|
+
*
|
|
7
|
+
* Language-specific decl assembly is delegated to `formatDecl` (e.g. Kotlin's
|
|
8
|
+
* `[@Serializable\n]data class ${name}${body}` vs Swift's `${access} struct
|
|
9
|
+
* ${name}: Codable ${body}`). The recursive emit step is delegated to
|
|
10
|
+
* `recurseObjectType` — typically the language's `generate<Lang>ObjectType`
|
|
11
|
+
* function. We pass it our parent's collector-aware utils as a defensive
|
|
12
|
+
* default; the recursive call sees `c.inlineTypes === true` and will still
|
|
13
|
+
* allocate its own collector internally for *its* nested types.
|
|
14
|
+
*
|
|
15
|
+
* Side effect to be aware of: `defaultResolveRefTypeName` reserves the chosen
|
|
16
|
+
* name in `c.usedDeclarationNames`. That reservation is what prevents nested
|
|
17
|
+
* names from colliding with top-level enum / discriminated-union names emitted
|
|
18
|
+
* elsewhere in the same converter run, and it's why a shared `nameRegistry`
|
|
19
|
+
* across emit calls flows through to nested-decl naming.
|
|
20
|
+
*/
|
|
21
|
+
export function generateInlineNestedDecl(c, ir, collector, formatDecl, recurseObjectType) {
|
|
22
|
+
if (!ir.signature)
|
|
23
|
+
return undefined;
|
|
24
|
+
if ((c.languageProfile.detectSelfReferenceToRoot ?? true) &&
|
|
25
|
+
ir.name &&
|
|
26
|
+
ir.name === c.rootName) {
|
|
27
|
+
return c.rootName;
|
|
28
|
+
}
|
|
29
|
+
const cached = collector.bySignature.get(ir.signature);
|
|
30
|
+
if (cached)
|
|
31
|
+
return cached;
|
|
32
|
+
const name = defaultResolveRefTypeName(c, ir, ir.signature);
|
|
33
|
+
collector.bySignature.set(ir.signature, name);
|
|
34
|
+
const childUtils = {
|
|
35
|
+
getReferencedType: (node) => generateInlineNestedDecl(c, node, collector, formatDecl, recurseObjectType),
|
|
36
|
+
};
|
|
37
|
+
const body = recurseObjectType(c, ir, childUtils);
|
|
38
|
+
collector.decls.push(formatDecl(c, name, body, ir));
|
|
39
|
+
return name;
|
|
40
|
+
}
|
|
41
|
+
/** Indents each non-empty line of `text` by `n` spaces. */
|
|
42
|
+
export function indentLines(text, n) {
|
|
43
|
+
const pad = " ".repeat(n);
|
|
44
|
+
return text
|
|
45
|
+
.split("\n")
|
|
46
|
+
.map((line) => (line.length ? pad + line : line))
|
|
47
|
+
.join("\n");
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=inlineEmission.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inlineEmission.js","sourceRoot":"","sources":["../../src/converter/inlineEmission.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAC;AAuBxD;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,wBAAwB,CACtC,CAAI,EACJ,EAAU,EACV,SAAgC,EAChC,UAAoE,EACpE,iBAAyE;IAEzE,IAAI,CAAC,EAAE,CAAC,SAAS;QAAE,OAAO,SAAS,CAAC;IAEpC,IACE,CAAC,CAAC,CAAC,eAAe,CAAC,yBAAyB,IAAI,IAAI,CAAC;QACrD,EAAE,CAAC,IAAI;QACP,EAAE,CAAC,IAAI,KAAK,CAAC,CAAC,QAAQ,EACtB,CAAC;QACD,OAAO,CAAC,CAAC,QAAQ,CAAC;IACpB,CAAC;IAED,MAAM,MAAM,GAAG,SAAS,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,CAAC,SAAS,CAAC,CAAC;IACvD,IAAI,MAAM;QAAE,OAAO,MAAM,CAAC;IAE1B,MAAM,IAAI,GAAG,yBAAyB,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,CAAC,SAAS,CAAC,CAAC;IAC5D,SAAS,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;IAE9C,MAAM,UAAU,GAAsB;QACpC,iBAAiB,EAAE,CAAC,IAAI,EAAE,EAAE,CAC1B,wBAAwB,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,UAAU,EAAE,iBAAiB,CAAC;KAC9E,CAAC;IACF,MAAM,IAAI,GAAG,iBAAiB,CAAC,CAAC,EAAE,EAAE,EAAE,UAAU,CAAC,CAAC;IAElD,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC;IACpD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,2DAA2D;AAC3D,MAAM,UAAU,WAAW,CAAC,IAAY,EAAE,CAAS;IACjD,MAAM,GAAG,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IAC1B,OAAO,IAAI;SACR,KAAK,CAAC,IAAI,CAAC;SACX,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;SAChD,IAAI,CAAC,IAAI,CAAC,CAAC;AAChB,CAAC"}
|
|
@@ -5,6 +5,25 @@ import { KotlinBaseConverter } from "./KotlinBaseConverter.js";
|
|
|
5
5
|
export interface KotlinConverterOpts extends BaseConverterOpts {
|
|
6
6
|
serializer?: "kotlinx" | "none";
|
|
7
7
|
packageName?: string;
|
|
8
|
+
/**
|
|
9
|
+
* When true, nested object types are emitted as nested `data class`
|
|
10
|
+
* declarations inside their parent's body block, scoped to the parent's
|
|
11
|
+
* namespace:
|
|
12
|
+
*
|
|
13
|
+
* data class Body(val address: Address, ...) {
|
|
14
|
+
* data class Address(val street: String, ...)
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* rather than as top-level sibling declarations. Useful when emitting
|
|
18
|
+
* multiple sibling schemas into one outer Kotlin `object` namespace —
|
|
19
|
+
* each parent's `Address` is name-scoped (`Body.Address` vs
|
|
20
|
+
* `Response.Address`), eliminating cross-call collisions structurally.
|
|
21
|
+
*
|
|
22
|
+
* Default: `false` (extract to top-level siblings — the historical
|
|
23
|
+
* behavior). Top-level enums and discriminated unions remain extracted
|
|
24
|
+
* even when `inlineTypes` is `true`.
|
|
25
|
+
*/
|
|
26
|
+
inlineTypes?: boolean;
|
|
8
27
|
}
|
|
9
28
|
/**
|
|
10
29
|
* The state-and-method surface that helper modules in `src/kotlin/` need to
|
|
@@ -15,6 +34,7 @@ export interface KotlinConverterOpts extends BaseConverterOpts {
|
|
|
15
34
|
*/
|
|
16
35
|
export interface KotlinConverterContext extends BaseConverterContext {
|
|
17
36
|
readonly isKotlinx: boolean;
|
|
37
|
+
readonly inlineTypes: boolean;
|
|
18
38
|
readonly importsSet: Set<string>;
|
|
19
39
|
readonly contextualNodes: WeakSet<IRNode>;
|
|
20
40
|
readonly docFormatNodes: WeakMap<IRNode, string>;
|
|
@@ -37,6 +57,8 @@ export declare class KotlinConverter extends KotlinBaseConverter implements ILan
|
|
|
37
57
|
/** @internal Mutated by KotlinConverter helper modules; not part of the public API. */
|
|
38
58
|
isKotlinx: boolean;
|
|
39
59
|
/** @internal */
|
|
60
|
+
inlineTypes: boolean;
|
|
61
|
+
/** @internal */
|
|
40
62
|
enumDecls: Map<string, {
|
|
41
63
|
name: string;
|
|
42
64
|
values: string[];
|
|
@@ -24,6 +24,7 @@ export class KotlinConverter extends KotlinBaseConverter {
|
|
|
24
24
|
if (opts?.nameRegistry)
|
|
25
25
|
this.usedDeclarationNames = opts.nameRegistry;
|
|
26
26
|
this.isKotlinx = (opts?.serializer ?? "kotlinx") === "kotlinx";
|
|
27
|
+
this.inlineTypes = opts?.inlineTypes ?? false;
|
|
27
28
|
this.languageProfile = {
|
|
28
29
|
language: "kotlin",
|
|
29
30
|
processOneOfAsDiscriminatedUnion: true,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"KotlinConverter.js","sourceRoot":"","sources":["../../src/kotlin/KotlinConverter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AAQnE,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAClD,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EAAE,wBAAwB,EAAE,MAAM,kBAAkB,CAAC;AAC5D,OAAO,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,EAAE,mBAAmB,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAChF,OAAO,EAAE,sBAAsB,EAAE,yBAAyB,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"KotlinConverter.js","sourceRoot":"","sources":["../../src/kotlin/KotlinConverter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AAQnE,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAClD,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EAAE,wBAAwB,EAAE,MAAM,kBAAkB,CAAC;AAC5D,OAAO,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,EAAE,mBAAmB,EAAE,uBAAuB,EAAE,MAAM,kBAA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mBAAmB,CAAC,EAAU,EAAE,KAAwB;QAC7D,OAAO,mBAAmB,CAAC,IAAI,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC;IAC9C,CAAC;IAED;;;;;;OAMG;IACK,YAAY,CAAC,WAA+B,EAAE,KAAe;QACnE,MAAM,QAAQ,GAAG,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QACvC,MAAM,CAAC,GAAG,IAAI,OAAO,CAAC,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC;QAC5C,IAAI,WAAW,EAAE,CAAC;YAChB,CAAC,CAAC,IAAI,CAAC,WAAW,WAAW,EAAE,CAAC,CAAC;YACjC,CAAC,CAAC,KAAK,EAAE,CAAC;QACZ,CAAC;QACD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACzC,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;YACnB,IAAI,CAAC,GAAG,QAAQ,CAAC,MAAM,GAAG,CAAC;gBAAE,CAAC,CAAC,KAAK,EAAE,CAAC;QACzC,CAAC;QACD,OAAO,CAAC,CAAC,QAAQ,EAAE,GAAG,IAAI,CAAC;IAC7B,CAAC;CACF"}
|
|
@@ -8,5 +8,11 @@ export declare function isPropertyNullable(prop: IRNode): boolean;
|
|
|
8
8
|
* KDoc comments for unmodelled `additionalProperties`, per-property
|
|
9
9
|
* `@SerialName`/`@Contextual` annotations, and nullable/default-value suffixes
|
|
10
10
|
* driven by `required` and union-includes-null analysis.
|
|
11
|
+
*
|
|
12
|
+
* When `c.inlineTypes` is true, nested object types extracted from this node's
|
|
13
|
+
* properties are emitted as nested `data class` declarations inside this
|
|
14
|
+
* node's body block — `(\n ...,\n) {\n data class Inner(...)\n}` — instead
|
|
15
|
+
* of being registered as top-level siblings. The shared
|
|
16
|
+
* {@link generateInlineNestedDecl} helper drives the per-parent collector.
|
|
11
17
|
*/
|
|
12
18
|
export declare function generateKotlinObjectType(c: KotlinConverterContext, ir: IRNode, utils: GenerateTypeUtils): string;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { formatPrimitiveDefault } from "../converter/formatDefault.js";
|
|
2
|
+
import { generateInlineNestedDecl, indentLines, } from "../converter/inlineEmission.js";
|
|
2
3
|
import { sanitizeKotlinIdentifier } from "./KotlinBaseConverter.js";
|
|
3
4
|
import { generateKotlinType } from "./typeMapper.js";
|
|
4
|
-
import { serialNameAnnotation, contextualAnnotation, KOTLIN_ANNOTATION_IMPORTS } from "./annotations.js";
|
|
5
|
+
import { serialNameAnnotation, contextualAnnotation, serializableAnnotation, KOTLIN_ANNOTATION_IMPORTS, } from "./annotations.js";
|
|
5
6
|
/** Returns true if the property is optional or its type union includes null. */
|
|
6
7
|
export function isPropertyNullable(prop) {
|
|
7
8
|
if (!prop.required)
|
|
@@ -11,11 +12,25 @@ export function isPropertyNullable(prop) {
|
|
|
11
12
|
}
|
|
12
13
|
return false;
|
|
13
14
|
}
|
|
15
|
+
/** Language-specific decl assembly for Kotlin nested data classes. */
|
|
16
|
+
function formatKotlinNestedDecl(c, name, body, ir) {
|
|
17
|
+
const annPrefix = c.isKotlinx ? serializableAnnotation() + "\n" : "";
|
|
18
|
+
if (c.isKotlinx)
|
|
19
|
+
c.importsSet.add(KOTLIN_ANNOTATION_IMPORTS.serializable);
|
|
20
|
+
const docPrefix = ir.title || ir.description ? `/** ${ir.title || ir.description} */\n` : "";
|
|
21
|
+
return `${docPrefix}${annPrefix}data class ${name}${body}`;
|
|
22
|
+
}
|
|
14
23
|
/**
|
|
15
24
|
* Emits a Kotlin data-class constructor body for an object IR node, including
|
|
16
25
|
* KDoc comments for unmodelled `additionalProperties`, per-property
|
|
17
26
|
* `@SerialName`/`@Contextual` annotations, and nullable/default-value suffixes
|
|
18
27
|
* driven by `required` and union-includes-null analysis.
|
|
28
|
+
*
|
|
29
|
+
* When `c.inlineTypes` is true, nested object types extracted from this node's
|
|
30
|
+
* properties are emitted as nested `data class` declarations inside this
|
|
31
|
+
* node's body block — `(\n ...,\n) {\n data class Inner(...)\n}` — instead
|
|
32
|
+
* of being registered as top-level siblings. The shared
|
|
33
|
+
* {@link generateInlineNestedDecl} helper drives the per-parent collector.
|
|
19
34
|
*/
|
|
20
35
|
export function generateKotlinObjectType(c, ir, utils) {
|
|
21
36
|
// Record additionalProperties doc note for the declaration emission step
|
|
@@ -36,13 +51,21 @@ export function generateKotlinObjectType(c, ir, utils) {
|
|
|
36
51
|
if (!ir.properties || Object.keys(ir.properties).length === 0) {
|
|
37
52
|
return "()";
|
|
38
53
|
}
|
|
54
|
+
const collector = c.inlineTypes
|
|
55
|
+
? { decls: [], bySignature: new Map() }
|
|
56
|
+
: null;
|
|
57
|
+
const effectiveUtils = collector
|
|
58
|
+
? {
|
|
59
|
+
getReferencedType: (node) => generateInlineNestedDecl(c, node, collector, formatKotlinNestedDecl, generateKotlinObjectType),
|
|
60
|
+
}
|
|
61
|
+
: utils;
|
|
39
62
|
const lines = Object.entries(ir.properties).map(([key, prop]) => {
|
|
40
63
|
const name = sanitizeKotlinIdentifier(key);
|
|
41
64
|
if (name !== key && c.isKotlinx) {
|
|
42
65
|
c.propertySerialNames.set(prop, key);
|
|
43
66
|
c.importsSet.add(KOTLIN_ANNOTATION_IMPORTS.serialName);
|
|
44
67
|
}
|
|
45
|
-
const type = generateKotlinType(c, prop,
|
|
68
|
+
const type = generateKotlinType(c, prop, effectiveUtils);
|
|
46
69
|
const nullable = isPropertyNullable(prop);
|
|
47
70
|
const def = formatPrimitiveDefault(prop.defaultValue);
|
|
48
71
|
// Optional (not required) → ? = <default|null>. Required-but-nullable → ? (no default unless schema-provided).
|
|
@@ -69,6 +92,11 @@ export function generateKotlinObjectType(c, ir, utils) {
|
|
|
69
92
|
const annPrefix = annotations.length > 0 ? annotations.join("\n") + "\n" : "";
|
|
70
93
|
return `${annPrefix} val ${name}: ${type}${suffix}`;
|
|
71
94
|
});
|
|
72
|
-
|
|
95
|
+
const paramBody = `(\n${lines.join(",\n")},\n)`;
|
|
96
|
+
if (!collector || collector.decls.length === 0) {
|
|
97
|
+
return paramBody;
|
|
98
|
+
}
|
|
99
|
+
const indented = collector.decls.map((d) => indentLines(d, 2)).join("\n\n");
|
|
100
|
+
return `${paramBody} {\n${indented}\n}`;
|
|
73
101
|
}
|
|
74
102
|
//# sourceMappingURL=objectEmitter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"objectEmitter.js","sourceRoot":"","sources":["../../src/kotlin/objectEmitter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;
|
|
1
|
+
{"version":3,"file":"objectEmitter.js","sourceRoot":"","sources":["../../src/kotlin/objectEmitter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AACvE,OAAO,EACL,wBAAwB,EACxB,WAAW,GAEZ,MAAM,gCAAgC,CAAC;AAExC,OAAO,EAAE,wBAAwB,EAAE,MAAM,0BAA0B,CAAC;AACpE,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAErD,OAAO,EACL,oBAAoB,EACpB,oBAAoB,EACpB,sBAAsB,EACtB,yBAAyB,GAC1B,MAAM,kBAAkB,CAAC;AAE1B,gFAAgF;AAChF,MAAM,UAAU,kBAAkB,CAAC,IAAY;IAC7C,IAAI,CAAC,IAAI,CAAC,QAAQ;QAAE,OAAO,IAAI,CAAC;IAChC,IAAI,IAAI,CAAC,IAAI,KAAK,OAAO,IAAI,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,MAAM,CAAC,EAAE,CAAC;QAC1E,OAAO,IAAI,CAAC;IACd,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED,sEAAsE;AACtE,SAAS,sBAAsB,CAC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AAK,IAAI,GAAG,MAAM,EAAE,CAAC;IACvD,CAAC,CAAC,CAAC;IAEH,MAAM,SAAS,GAAG,MAAM,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC;IAEhD,IAAI,CAAC,SAAS,IAAI,SAAS,CAAC,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC/C,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,MAAM,QAAQ,GAAG,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAC5E,OAAO,GAAG,SAAS,OAAO,QAAQ,KAAK,CAAC;AAC1C,CAAC"}
|
|
@@ -5,6 +5,26 @@ import { SwiftBaseConverter } from "./SwiftBaseConverter.js";
|
|
|
5
5
|
export interface SwiftConverterOpts extends BaseConverterOpts {
|
|
6
6
|
serializer?: "codable" | "none";
|
|
7
7
|
accessLevel?: "public" | "internal";
|
|
8
|
+
/**
|
|
9
|
+
* When true, nested object types are emitted as nested `struct`
|
|
10
|
+
* declarations inside their parent's body, scoped to the parent's
|
|
11
|
+
* namespace:
|
|
12
|
+
*
|
|
13
|
+
* public struct Body: Codable {
|
|
14
|
+
* public let address: Address
|
|
15
|
+
* public struct Address: Codable { public let street: String }
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* rather than as top-level sibling declarations. Useful when emitting
|
|
19
|
+
* multiple sibling schemas into one outer Swift `enum` namespace —
|
|
20
|
+
* each parent's `Address` is name-scoped (`Body.Address` vs
|
|
21
|
+
* `Response.Address`), eliminating cross-call collisions structurally.
|
|
22
|
+
*
|
|
23
|
+
* Default: `false` (extract to top-level siblings — the historical
|
|
24
|
+
* behavior). Top-level enums and discriminated enums remain extracted
|
|
25
|
+
* even when `inlineTypes` is `true`.
|
|
26
|
+
*/
|
|
27
|
+
inlineTypes?: boolean;
|
|
8
28
|
}
|
|
9
29
|
/**
|
|
10
30
|
* The state-and-method surface that helper modules in `src/swift/` need to
|
|
@@ -16,6 +36,7 @@ export interface SwiftConverterOpts extends BaseConverterOpts {
|
|
|
16
36
|
export interface SwiftConverterContext extends BaseConverterContext {
|
|
17
37
|
readonly isCodable: boolean;
|
|
18
38
|
readonly accessLevel: "public" | "internal";
|
|
39
|
+
readonly inlineTypes: boolean;
|
|
19
40
|
needsAnyCodable: boolean;
|
|
20
41
|
readonly importsSet: Set<string>;
|
|
21
42
|
readonly docFormatNodes: WeakMap<IRNode, string>;
|
|
@@ -39,6 +60,8 @@ export declare class SwiftConverter extends SwiftBaseConverter implements ILangu
|
|
|
39
60
|
/** @internal */
|
|
40
61
|
accessLevel: "public" | "internal";
|
|
41
62
|
/** @internal */
|
|
63
|
+
inlineTypes: boolean;
|
|
64
|
+
/** @internal */
|
|
42
65
|
enumDecls: Map<string, {
|
|
43
66
|
name: string;
|
|
44
67
|
values: string[];
|
|
@@ -21,6 +21,7 @@ export class SwiftConverter extends SwiftBaseConverter {
|
|
|
21
21
|
this.usedDeclarationNames = opts.nameRegistry;
|
|
22
22
|
this.isCodable = (opts?.serializer ?? "codable") === "codable";
|
|
23
23
|
this.accessLevel = opts?.accessLevel ?? "public";
|
|
24
|
+
this.inlineTypes = opts?.inlineTypes ?? false;
|
|
24
25
|
this.languageProfile = {
|
|
25
26
|
language: "swift",
|
|
26
27
|
processOneOfAsDiscriminatedUnion: true,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SwiftConverter.js","sourceRoot":"","sources":["../../src/swift/SwiftConverter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AAQnE,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,wBAAwB,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAC9E,OAAO,EAAE,qBAAqB,EAAE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EAAU,EAAE,KAAwB;QACpE,OAAO,0BAA0B,CAAC,IAAI,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC;IACrD,CAAC;CACF"}
|
|
@@ -8,5 +8,10 @@ export declare function isPropertyNullable(prop: IRNode): boolean;
|
|
|
8
8
|
* declarations, doc comments for formatted strings, and a `CodingKeys` block
|
|
9
9
|
* when codable conformance requires JSON-key remapping (kebab/snake renames
|
|
10
10
|
* or backticked reserved words).
|
|
11
|
+
*
|
|
12
|
+
* When `c.inlineTypes` is true, nested object types extracted from this node's
|
|
13
|
+
* properties are emitted as nested `struct` declarations inside this node's
|
|
14
|
+
* body block instead of being registered as top-level siblings. The shared
|
|
15
|
+
* {@link generateInlineNestedDecl} helper drives the per-parent collector.
|
|
11
16
|
*/
|
|
12
17
|
export declare function generateSwiftObjectType(c: SwiftConverterContext, ir: IRNode, utils: GenerateTypeUtils): string;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { formatPrimitiveDefault } from "../converter/formatDefault.js";
|
|
2
|
+
import { generateInlineNestedDecl, indentLines, } from "../converter/inlineEmission.js";
|
|
2
3
|
import { sanitizeSwiftIdentifier } from "./SwiftBaseConverter.js";
|
|
3
4
|
import { generateSwiftType } from "./typeMapper.js";
|
|
4
5
|
/** Returns true if the property is optional or its type union includes null. */
|
|
@@ -9,11 +10,22 @@ export function isPropertyNullable(prop) {
|
|
|
9
10
|
return true;
|
|
10
11
|
return false;
|
|
11
12
|
}
|
|
13
|
+
/** Language-specific decl assembly for Swift nested structs. */
|
|
14
|
+
function formatSwiftNestedDecl(c, name, body, ir) {
|
|
15
|
+
const conformance = c.isCodable ? ": Codable" : "";
|
|
16
|
+
const docPrefix = ir.title || ir.description ? `/// ${ir.title || ir.description}\n` : "";
|
|
17
|
+
return `${docPrefix}${c.accessLevel} struct ${name}${conformance} ${body}`;
|
|
18
|
+
}
|
|
12
19
|
/**
|
|
13
20
|
* Emits a Swift struct body for an object IR node, including the property
|
|
14
21
|
* declarations, doc comments for formatted strings, and a `CodingKeys` block
|
|
15
22
|
* when codable conformance requires JSON-key remapping (kebab/snake renames
|
|
16
23
|
* or backticked reserved words).
|
|
24
|
+
*
|
|
25
|
+
* When `c.inlineTypes` is true, nested object types extracted from this node's
|
|
26
|
+
* properties are emitted as nested `struct` declarations inside this node's
|
|
27
|
+
* body block instead of being registered as top-level siblings. The shared
|
|
28
|
+
* {@link generateInlineNestedDecl} helper drives the per-parent collector.
|
|
17
29
|
*/
|
|
18
30
|
export function generateSwiftObjectType(c, ir, utils) {
|
|
19
31
|
if (ir.additionalProperties && typeof ir.additionalProperties !== "boolean") {
|
|
@@ -31,11 +43,19 @@ export function generateSwiftObjectType(c, ir, utils) {
|
|
|
31
43
|
if (!ir.properties || Object.keys(ir.properties).length === 0) {
|
|
32
44
|
return "{}";
|
|
33
45
|
}
|
|
46
|
+
const collector = c.inlineTypes
|
|
47
|
+
? { decls: [], bySignature: new Map() }
|
|
48
|
+
: null;
|
|
49
|
+
const effectiveUtils = collector
|
|
50
|
+
? {
|
|
51
|
+
getReferencedType: (node) => generateInlineNestedDecl(c, node, collector, formatSwiftNestedDecl, generateSwiftObjectType),
|
|
52
|
+
}
|
|
53
|
+
: utils;
|
|
34
54
|
const propInfos = [];
|
|
35
55
|
const lines = [];
|
|
36
56
|
for (const [key, prop] of Object.entries(ir.properties)) {
|
|
37
57
|
const swiftName = sanitizeSwiftIdentifier(key);
|
|
38
|
-
const type = generateSwiftType(c, prop,
|
|
58
|
+
const type = generateSwiftType(c, prop, effectiveUtils);
|
|
39
59
|
const nullable = isPropertyNullable(prop);
|
|
40
60
|
const def = formatPrimitiveDefault(prop.defaultValue);
|
|
41
61
|
let suffix;
|
|
@@ -49,13 +69,9 @@ export function generateSwiftObjectType(c, ir, utils) {
|
|
|
49
69
|
lines.push(` /// ${c.docFormatNodes.get(prop)}`);
|
|
50
70
|
}
|
|
51
71
|
lines.push(` ${c.accessLevel} let ${swiftName}: ${type}${suffix}`);
|
|
52
|
-
// Need a CodingKey if the swift name differs from the JSON key (kebab/snake renames)
|
|
53
|
-
// OR if the swift name uses backticks (reserved word)
|
|
54
72
|
const needsCodingKey = swiftName !== key || swiftName.startsWith("`");
|
|
55
73
|
propInfos.push({ jsonKey: key, swiftName, needsCodingKey });
|
|
56
74
|
}
|
|
57
|
-
// CodingKeys block under codable when any prop needs mapping.
|
|
58
|
-
// Swift requires ALL properties be listed when CodingKeys is present.
|
|
59
75
|
if (c.isCodable && propInfos.some((p) => p.needsCodingKey)) {
|
|
60
76
|
const cases = propInfos.map(({ jsonKey, swiftName }) => {
|
|
61
77
|
return ` case ${swiftName} = "${jsonKey}"`;
|
|
@@ -65,6 +81,14 @@ export function generateSwiftObjectType(c, ir, utils) {
|
|
|
65
81
|
lines.push(...cases);
|
|
66
82
|
lines.push(" }");
|
|
67
83
|
}
|
|
84
|
+
if (collector && collector.decls.length > 0) {
|
|
85
|
+
lines.push("");
|
|
86
|
+
for (let i = 0; i < collector.decls.length; i++) {
|
|
87
|
+
lines.push(indentLines(collector.decls[i], 2));
|
|
88
|
+
if (i < collector.decls.length - 1)
|
|
89
|
+
lines.push("");
|
|
90
|
+
}
|
|
91
|
+
}
|
|
68
92
|
return `{\n${lines.join("\n")}\n}`;
|
|
69
93
|
}
|
|
70
94
|
//# sourceMappingURL=structEmitter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"structEmitter.js","sourceRoot":"","sources":["../../src/swift/structEmitter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;
|
|
1
|
+
{"version":3,"file":"structEmitter.js","sourceRoot":"","sources":["../../src/swift/structEmitter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AACvE,OAAO,EACL,wBAAwB,EACxB,WAAW,GAEZ,MAAM,gCAAgC,CAAC;AAExC,OAAO,EAAE,uBAAuB,EAAE,MAAM,yBAAyB,CAAC;AA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