@syncular/typegen 0.58.0 → 0.60.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/README.md +96 -43
- package/dist/cli.js +8 -3
- package/dist/emit-dart.d.ts +4 -3
- package/dist/emit-dart.js +40 -100
- package/dist/emit-kotlin.d.ts +4 -4
- package/dist/emit-kotlin.js +41 -99
- package/dist/emit-queries-dart.d.ts +1 -1
- package/dist/emit-queries-dart.js +17 -26
- package/dist/emit-queries-kotlin.d.ts +1 -1
- package/dist/emit-queries-kotlin.js +15 -24
- package/dist/emit-queries-rust.d.ts +1 -1
- package/dist/emit-queries-rust.js +18 -19
- package/dist/emit-queries-swift.d.ts +1 -1
- package/dist/emit-queries-swift.js +22 -26
- package/dist/emit-queries.d.ts +1 -1
- package/dist/emit-queries.js +18 -27
- package/dist/emit-swift.d.ts +4 -4
- package/dist/emit-swift.js +43 -102
- package/dist/emit.d.ts +3 -3
- package/dist/emit.js +37 -113
- package/dist/errors.d.ts +6 -2
- package/dist/errors.js +10 -2
- package/dist/fmt.js +11 -2
- package/dist/generate.d.ts +4 -3
- package/dist/generate.js +61 -73
- package/dist/init.js +5 -6
- package/dist/ir.d.ts +14 -2
- package/dist/ir.js +67 -49
- package/dist/lower.d.ts +0 -7
- package/dist/lower.js +0 -62
- package/dist/lsp.js +6 -1
- package/dist/manifest.d.ts +1 -2
- package/dist/manifest.js +13 -12
- package/dist/migration-lock.d.ts +32 -23
- package/dist/migration-lock.js +115 -92
- package/dist/naming.d.ts +25 -0
- package/dist/naming.js +92 -0
- package/dist/query.d.ts +94 -14
- package/dist/query.js +642 -461
- package/dist/sql.js +18 -3
- package/dist/syql-ast.d.ts +2 -0
- package/dist/syql-ast.js +7 -1
- package/dist/syql-browser.js +7 -6
- package/dist/syql-diagnostics.d.ts +1 -0
- package/dist/syql-diagnostics.js +1 -0
- package/dist/syql-lowering.js +11 -1
- package/dist/syql-parser.d.ts +4 -1
- package/dist/syql-parser.js +42 -5
- package/dist/syql-validator.js +13 -220
- package/package.json +3 -3
- package/src/cli.ts +9 -3
- package/src/emit-dart.ts +44 -116
- package/src/emit-kotlin.ts +41 -118
- package/src/emit-queries-dart.ts +23 -32
- package/src/emit-queries-kotlin.ts +20 -30
- package/src/emit-queries-rust.ts +29 -31
- package/src/emit-queries-swift.ts +26 -32
- package/src/emit-queries.ts +26 -47
- package/src/emit-swift.ts +49 -120
- package/src/emit.ts +45 -135
- package/src/errors.ts +13 -2
- package/src/fmt.ts +10 -2
- package/src/generate.ts +113 -107
- package/src/init.ts +8 -6
- package/src/ir.ts +91 -54
- package/src/lower.ts +0 -53
- package/src/lsp.ts +11 -4
- package/src/manifest.ts +22 -18
- package/src/migration-lock.ts +192 -137
- package/src/naming.ts +154 -0
- package/src/query.ts +864 -502
- package/src/sql.ts +30 -3
- package/src/syql-ast.ts +9 -0
- package/src/syql-browser.ts +8 -7
- package/src/syql-diagnostics.ts +2 -0
- package/src/syql-lowering.ts +16 -1
- package/src/syql-parser.ts +73 -8
- package/src/syql-validator.ts +21 -279
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { snakeToCamel } from './naming.js';
|
|
2
|
+
import { syqlInput, } from './query.js';
|
|
2
3
|
const SWIFT_TYPE = {
|
|
3
4
|
string: 'String',
|
|
4
5
|
integer: 'Int',
|
|
@@ -13,10 +14,6 @@ const SYQL_SWIFT_TYPE = {
|
|
|
13
14
|
...SWIFT_TYPE,
|
|
14
15
|
integer: 'Int64',
|
|
15
16
|
};
|
|
16
|
-
/** Language-facing field name — the pinned §12 naming map. */
|
|
17
|
-
function camelCase(name) {
|
|
18
|
-
return snakeToCamel(name);
|
|
19
|
-
}
|
|
20
17
|
/** The query function/type name: already camelCase; PascalCase for the type. */
|
|
21
18
|
function typeName(name) {
|
|
22
19
|
return name.charAt(0).toUpperCase() + name.slice(1);
|
|
@@ -73,22 +70,16 @@ function optionalParamValue(type, name) {
|
|
|
73
70
|
return `${name}.map { JSONValue.string($0) } ?? .null`;
|
|
74
71
|
}
|
|
75
72
|
}
|
|
76
|
-
function syqlInput(query, name) {
|
|
77
|
-
const input = query.syql?.inputs.find((candidate) => candidate.name === name);
|
|
78
|
-
if (input === undefined)
|
|
79
|
-
throw new Error(`unknown SYQL input ${name}`);
|
|
80
|
-
return input;
|
|
81
|
-
}
|
|
82
73
|
function syqlSwiftType(type, nullable) {
|
|
83
74
|
return `${SYQL_SWIFT_TYPE[type]}${nullable ? '?' : ''}`;
|
|
84
75
|
}
|
|
85
76
|
function syqlControlActive(query, name) {
|
|
86
77
|
const input = syqlInput(query, name);
|
|
87
|
-
const access =
|
|
78
|
+
const access = snakeToCamel(input.langName);
|
|
88
79
|
if (input.kind === 'value' && input.default === false)
|
|
89
80
|
return access;
|
|
90
81
|
if (input.kind === 'value' && input.nullable) {
|
|
91
|
-
return
|
|
82
|
+
return `${access}.isPresent`;
|
|
92
83
|
}
|
|
93
84
|
if (input.kind === 'value' || input.kind === 'group') {
|
|
94
85
|
return `${access} != nil`;
|
|
@@ -106,7 +97,7 @@ function syqlBindExpr(query, bind) {
|
|
|
106
97
|
return `.bool(${active})`;
|
|
107
98
|
}
|
|
108
99
|
const input = syqlInput(query, bind.input);
|
|
109
|
-
const access =
|
|
100
|
+
const access = snakeToCamel(input.langName);
|
|
110
101
|
if (bind.kind === 'limit')
|
|
111
102
|
return `.number(Double(effective${typeName(access)}))`;
|
|
112
103
|
if (bind.kind === 'group-member') {
|
|
@@ -115,7 +106,7 @@ function syqlBindExpr(query, bind) {
|
|
|
115
106
|
const member = input.members.find((candidate) => candidate.name === bind.member);
|
|
116
107
|
if (member === undefined)
|
|
117
108
|
throw new Error(`unknown group member ${bind.member}`);
|
|
118
|
-
const memberName =
|
|
109
|
+
const memberName = snakeToCamel(member.langName);
|
|
119
110
|
const present = member.nullable
|
|
120
111
|
? optionalParamValue(member.type, `value.${memberName}`)
|
|
121
112
|
: paramValue(member.type, `value.${memberName}`);
|
|
@@ -141,14 +132,14 @@ function emitSyqlSwiftTypes(query) {
|
|
|
141
132
|
const name = `${typeName(query.name)}${typeName(input.langName)}`;
|
|
142
133
|
lines.push(`public struct ${name}: Sendable {`);
|
|
143
134
|
for (const member of input.members) {
|
|
144
|
-
lines.push(` public let ${
|
|
135
|
+
lines.push(` public let ${snakeToCamel(member.langName)}: ${syqlSwiftType(member.type, member.nullable)}`);
|
|
145
136
|
}
|
|
146
137
|
const args = input.members
|
|
147
|
-
.map((member) => `${
|
|
138
|
+
.map((member) => `${snakeToCamel(member.langName)}: ${syqlSwiftType(member.type, member.nullable)}`)
|
|
148
139
|
.join(', ');
|
|
149
140
|
lines.push(` public init(${args}) {`);
|
|
150
141
|
for (const member of input.members) {
|
|
151
|
-
const name =
|
|
142
|
+
const name = snakeToCamel(member.langName);
|
|
152
143
|
lines.push(` self.${name} = ${name}`);
|
|
153
144
|
}
|
|
154
145
|
lines.push(' }', '}', '');
|
|
@@ -157,7 +148,7 @@ function emitSyqlSwiftTypes(query) {
|
|
|
157
148
|
const name = `${typeName(query.name)}${typeName(input.langName)}`;
|
|
158
149
|
lines.push(`public enum ${name}: Int, Sendable {`);
|
|
159
150
|
input.profiles.forEach((profile, index) => {
|
|
160
|
-
lines.push(` case ${
|
|
151
|
+
lines.push(` case ${snakeToCamel(profile.langName)} = ${index}`);
|
|
161
152
|
});
|
|
162
153
|
lines.push('}', '');
|
|
163
154
|
}
|
|
@@ -175,7 +166,7 @@ function emitSyqlSwiftRunner(query) {
|
|
|
175
166
|
lines.push(` public static let ${query.name}Tables = [${query.tables.map(quote).join(', ')}]`, '', ` /// Run the ${query.name} revision-1 SYQL query.`);
|
|
176
167
|
const args = ['client: SyncularClient'];
|
|
177
168
|
for (const input of metadata.inputs) {
|
|
178
|
-
const name =
|
|
169
|
+
const name = snakeToCamel(input.langName);
|
|
179
170
|
if (input.kind === 'value') {
|
|
180
171
|
const type = syqlSwiftType(input.type, input.nullable);
|
|
181
172
|
if (input.required)
|
|
@@ -193,7 +184,7 @@ function emitSyqlSwiftRunner(query) {
|
|
|
193
184
|
else if (input.kind === 'sort') {
|
|
194
185
|
const defaultCase = input.profiles.find((profile) => profile.name === input.defaultProfile)
|
|
195
186
|
?.langName ?? input.defaultProfile;
|
|
196
|
-
args.push(`${name}: ${typeName(query.name)}${typeName(input.langName)} = .${
|
|
187
|
+
args.push(`${name}: ${typeName(query.name)}${typeName(input.langName)} = .${snakeToCamel(defaultCase)}`);
|
|
197
188
|
}
|
|
198
189
|
else {
|
|
199
190
|
args.push(`${name}: Int? = nil`);
|
|
@@ -202,7 +193,7 @@ function emitSyqlSwiftRunner(query) {
|
|
|
202
193
|
lines.push(` public static func ${query.name}(${args.join(', ')}) throws -> [${Row}] {`);
|
|
203
194
|
const page = metadata.inputs.find((input) => input.kind === 'limit');
|
|
204
195
|
if (page?.kind === 'limit') {
|
|
205
|
-
const name =
|
|
196
|
+
const name = snakeToCamel(page.langName);
|
|
206
197
|
lines.push(` let effective${typeName(name)} = ${name} ?? ${page.defaultSize}`, ` guard effective${typeName(name)} >= 1 && effective${typeName(name)} <= ${page.maxSize} else {`, ` throw SyncularQueryInputError(code: "SYQL_RUNTIME_INVALID_LIMIT", message: ${quote(`${query.name}: invalid limit`)})`, ' }');
|
|
207
198
|
}
|
|
208
199
|
if (metadata.plan.backend === 'variants') {
|
|
@@ -213,7 +204,7 @@ function emitSyqlSwiftRunner(query) {
|
|
|
213
204
|
}
|
|
214
205
|
const sort = metadata.inputs.find((input) => input.kind === 'sort');
|
|
215
206
|
const profileCount = sort?.kind === 'sort' ? sort.profiles.length : 1;
|
|
216
|
-
const sortIndex = sort?.kind === 'sort' ? `${
|
|
207
|
+
const sortIndex = sort?.kind === 'sort' ? `${snakeToCamel(sort.langName)}.rawValue` : '0';
|
|
217
208
|
const index = metadata.plan.backend === 'variants'
|
|
218
209
|
? `activationMask * ${profileCount} + ${sortIndex}`
|
|
219
210
|
: sortIndex;
|
|
@@ -234,12 +225,12 @@ function emitRowStruct(query) {
|
|
|
234
225
|
lines.push(`public struct ${Row}: Sendable, Equatable {`);
|
|
235
226
|
for (const column of query.columns) {
|
|
236
227
|
const opt = column.nullable ? '?' : '';
|
|
237
|
-
lines.push(` public let ${
|
|
228
|
+
lines.push(` public let ${snakeToCamel(column.langName)}: ${SWIFT_TYPE[column.type]}${opt}`);
|
|
238
229
|
}
|
|
239
230
|
lines.push('');
|
|
240
231
|
lines.push(' public init?(row: [String: JSONValue]) {');
|
|
241
232
|
for (const column of query.columns) {
|
|
242
|
-
const name =
|
|
233
|
+
const name = snakeToCamel(column.langName);
|
|
243
234
|
const accessor = rowAccessor(column);
|
|
244
235
|
if (column.nullable) {
|
|
245
236
|
lines.push(` self.${name} = ${accessor}`);
|
|
@@ -264,7 +255,7 @@ function emitRunner(query) {
|
|
|
264
255
|
lines.push(` /// Run the ${query.name} named query (SELECT-only).`);
|
|
265
256
|
const args = [];
|
|
266
257
|
for (const p of query.params) {
|
|
267
|
-
const name =
|
|
258
|
+
const name = snakeToCamel(p.langName);
|
|
268
259
|
args.push(`${name}: ${SWIFT_TYPE[p.type]}`);
|
|
269
260
|
}
|
|
270
261
|
const signature = args.length > 0
|
|
@@ -275,7 +266,7 @@ function emitRunner(query) {
|
|
|
275
266
|
if (query.params.length > 0) {
|
|
276
267
|
const binds = query.params
|
|
277
268
|
.map((p) => {
|
|
278
|
-
const name =
|
|
269
|
+
const name = snakeToCamel(p.langName);
|
|
279
270
|
return paramValue(p.type, name);
|
|
280
271
|
})
|
|
281
272
|
.join(', ');
|
|
@@ -306,6 +297,11 @@ export function emitQueriesSwiftModule(queries, hash, irVersion, enumName) {
|
|
|
306
297
|
'public enum SyncularQueryPresence<Value: Sendable>: Sendable {',
|
|
307
298
|
' case absent',
|
|
308
299
|
' case present(Value)',
|
|
300
|
+
'',
|
|
301
|
+
' public var isPresent: Bool {',
|
|
302
|
+
' if case .present = self { return true }',
|
|
303
|
+
' return false',
|
|
304
|
+
' }',
|
|
309
305
|
'}',
|
|
310
306
|
'',
|
|
311
307
|
'public struct SyncularQueryInputError: Error, Sendable {',
|
package/dist/emit-queries.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type AnalyzedQuery } from './query.js';
|
|
2
2
|
export declare function emitQueriesModule(queries: readonly AnalyzedQuery[], hash: string, irVersion: number): string;
|
package/dist/emit-queries.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { syqlInput, windowBases, } from './query.js';
|
|
1
2
|
const TS_TYPE = {
|
|
2
3
|
string: 'string',
|
|
3
4
|
integer: 'number',
|
|
@@ -61,11 +62,19 @@ function reactiveParam(query, name) {
|
|
|
61
62
|
throw new Error(`unknown reactive param ${name}`);
|
|
62
63
|
return `String(params.${propertyKey(param.langName)})`;
|
|
63
64
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
/** The `coverage` member's opening line and one line per window base. */
|
|
66
|
+
function emitCoverage(query) {
|
|
67
|
+
const coverage = windowBases(query.reactive.coverage);
|
|
68
|
+
return [
|
|
69
|
+
` coverage: (${coverage.length > 0 ? 'params' : ''}) => [`,
|
|
70
|
+
...coverage.map((entry) => {
|
|
71
|
+
const fixed = entry.fixedScopes
|
|
72
|
+
.map((scope) => `${propertyKey(scope.variable)}: ${reactiveParam(query, scope.param)}`)
|
|
73
|
+
.join(', ');
|
|
74
|
+
const fixedPart = fixed.length > 0 ? `, fixedScopes: { ${fixed} }` : '';
|
|
75
|
+
return ` { base: { variable: ${quote(entry.variable)}${fixedPart} }, units: [${entry.units.map((param) => reactiveParam(query, param)).join(', ')}] },`;
|
|
76
|
+
}),
|
|
77
|
+
];
|
|
69
78
|
}
|
|
70
79
|
function syqlTsValueType(type, nullable) {
|
|
71
80
|
return `${SYQL_PARAM_TS_TYPE[type]}${nullable ? ' | null' : ''}`;
|
|
@@ -281,15 +290,7 @@ function emitSyqlQuery(query, hash) {
|
|
|
281
290
|
lines.push(` { table: ${quote(dependency.table)}${keys.length > 0 ? `, scopeKeys: [${keys.join(', ')}]` : ''} },`);
|
|
282
291
|
}
|
|
283
292
|
lines.push(' ],');
|
|
284
|
-
|
|
285
|
-
coverage.fixedScopes.some((scope) => scope.params.length > 0));
|
|
286
|
-
lines.push(` coverage: (${coverageUsesParams ? 'params' : ''}) => [`);
|
|
287
|
-
for (const coverage of query.reactive.coverage) {
|
|
288
|
-
const fixed = coverage.fixedScopes
|
|
289
|
-
.map((scope) => `${propertyKey(scope.variable)}: [${scope.params.map((param) => reactiveParam(query, param)).join(', ')}]`)
|
|
290
|
-
.join(', ');
|
|
291
|
-
lines.push(` { base: { table: ${quote(coverage.table)}, variable: ${quote(coverage.variable)}${fixed.length > 0 ? `, fixedScopes: { ${fixed} }` : ''} }, units: [${coverage.units.map((param) => reactiveParam(query, param)).join(', ')}] },`);
|
|
292
|
-
}
|
|
293
|
+
lines.push(...emitCoverage(query));
|
|
293
294
|
lines.push(' ],', hasParams
|
|
294
295
|
? ` bind: (params: ${Params}) => ${query.name}Select(params).bind,`
|
|
295
296
|
: ` bind: () => ${query.name}Select().bind,`);
|
|
@@ -370,16 +371,7 @@ function emitQuery(query, hash) {
|
|
|
370
371
|
lines.push(` { table: ${quote(dependency.table)}${keys.length > 0 ? `, scopeKeys: [${keys.join(', ')}]` : ''} },`);
|
|
371
372
|
}
|
|
372
373
|
lines.push(' ],');
|
|
373
|
-
|
|
374
|
-
coverage.fixedScopes.some((scope) => scope.params.length > 0));
|
|
375
|
-
lines.push(` coverage: (${coverageUsesParams ? 'params' : ''}) => [`);
|
|
376
|
-
for (const coverage of query.reactive.coverage) {
|
|
377
|
-
const fixed = coverage.fixedScopes
|
|
378
|
-
.map((scope) => `${propertyKey(scope.variable)}: [${scope.params.map((param) => reactiveParam(query, param)).join(', ')}]`)
|
|
379
|
-
.join(', ');
|
|
380
|
-
const fixedPart = fixed.length > 0 ? `, fixedScopes: { ${fixed} }` : '';
|
|
381
|
-
lines.push(` { base: { table: ${quote(coverage.table)}, variable: ${quote(coverage.variable)}${fixedPart} }, units: [${coverage.units.map((param) => reactiveParam(query, param)).join(', ')}] },`);
|
|
382
|
-
}
|
|
374
|
+
lines.push(...emitCoverage(query));
|
|
383
375
|
lines.push(' ],');
|
|
384
376
|
if (hasParams) {
|
|
385
377
|
lines.push(` bind: (params: ${Params}) => ${positional},`);
|
|
@@ -503,12 +495,11 @@ export function emitQueriesModule(queries, hash, irVersion) {
|
|
|
503
495
|
' readonly nullable: boolean;',
|
|
504
496
|
'}',
|
|
505
497
|
'',
|
|
498
|
+
'/** A window base (SPEC §4.8) and the unit values a query reads. */',
|
|
506
499
|
'export interface WindowCoverage {',
|
|
507
500
|
' readonly base: {',
|
|
508
|
-
' readonly table: string;',
|
|
509
501
|
' readonly variable: string;',
|
|
510
|
-
' readonly fixedScopes?: Readonly<Record<string,
|
|
511
|
-
' readonly params?: string;',
|
|
502
|
+
' readonly fixedScopes?: Readonly<Record<string, string>>;',
|
|
512
503
|
' };',
|
|
513
504
|
' readonly units: readonly string[];',
|
|
514
505
|
'}',
|
package/dist/emit-swift.d.ts
CHANGED
|
@@ -2,17 +2,17 @@
|
|
|
2
2
|
* Swift emitter: neutral IR → one standalone `.swift` file exporting
|
|
3
3
|
*
|
|
4
4
|
* - `enum <EnumName> { static let schema: JSONValue }` — the ServerSchema
|
|
5
|
-
* object
|
|
6
|
-
* `SyncularClient(schema:)`),
|
|
5
|
+
* object: the exact `serializeSchema` JSON the TS module embeds, parsed
|
|
6
|
+
* from a string literal (fed straight into `SyncularClient(schema:)`),
|
|
7
7
|
* - one `struct` per table with typed properties (per the six §2.4 column
|
|
8
8
|
* types, plus blob_ref/crdt in their honest Swift shapes) and a
|
|
9
9
|
* `init?(row:)` from a `[String: JSONValue]` (the shape `query`/`readRows`
|
|
10
10
|
* return),
|
|
11
|
-
* - a `subscriptions` namespace of typed
|
|
11
|
+
* - a `subscriptions` namespace of typed unit-template builders (SPEC §4.1).
|
|
12
12
|
*
|
|
13
13
|
* The file imports only `Syncular` (for `JSONValue`). The header carries the
|
|
14
14
|
* IR hash so `--check` verifies freshness byte-exactly, exactly like the TS
|
|
15
15
|
* emitter.
|
|
16
16
|
*/
|
|
17
|
-
import type
|
|
17
|
+
import { type IrDocument } from './ir.js';
|
|
18
18
|
export declare function emitSwiftModule(ir: IrDocument, hash: string, enumName: string): string;
|
package/dist/emit-swift.js
CHANGED
|
@@ -1,4 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Swift emitter: neutral IR → one standalone `.swift` file exporting
|
|
3
|
+
*
|
|
4
|
+
* - `enum <EnumName> { static let schema: JSONValue }` — the ServerSchema
|
|
5
|
+
* object: the exact `serializeSchema` JSON the TS module embeds, parsed
|
|
6
|
+
* from a string literal (fed straight into `SyncularClient(schema:)`),
|
|
7
|
+
* - one `struct` per table with typed properties (per the six §2.4 column
|
|
8
|
+
* types, plus blob_ref/crdt in their honest Swift shapes) and a
|
|
9
|
+
* `init?(row:)` from a `[String: JSONValue]` (the shape `query`/`readRows`
|
|
10
|
+
* return),
|
|
11
|
+
* - a `subscriptions` namespace of typed unit-template builders (SPEC §4.1).
|
|
12
|
+
*
|
|
13
|
+
* The file imports only `Syncular` (for `JSONValue`). The header carries the
|
|
14
|
+
* IR hash so `--check` verifies freshness byte-exactly, exactly like the TS
|
|
15
|
+
* emitter.
|
|
16
|
+
*/
|
|
17
|
+
import { appColumnType, serializeSchema, } from './ir.js';
|
|
18
|
+
import { pascalCase, snakeToCamel, stringLiteral } from './naming.js';
|
|
2
19
|
/**
|
|
3
20
|
* §2.4 column type → honest Swift type. `json`/`blob_ref` are the raw
|
|
4
21
|
* canonical JSON string (the client's blob API parses blob_ref); `bytes`/
|
|
@@ -14,81 +31,9 @@ const SWIFT_TYPE = {
|
|
|
14
31
|
blob_ref: 'String',
|
|
15
32
|
crdt: '[UInt8]',
|
|
16
33
|
};
|
|
17
|
-
function appColumnType(column) {
|
|
18
|
-
return column.encrypted === true && column.declaredType !== undefined
|
|
19
|
-
? column.declaredType
|
|
20
|
-
: column.type;
|
|
21
|
-
}
|
|
22
|
-
/** §5.11: app-side Swift type — declaredType for an encrypted column. */
|
|
23
|
-
function appSwiftType(column) {
|
|
24
|
-
return SWIFT_TYPE[appColumnType(column)];
|
|
25
|
-
}
|
|
26
|
-
function pascalCase(name) {
|
|
27
|
-
return name
|
|
28
|
-
.split(/[_-]+/)
|
|
29
|
-
.filter((part) => part.length > 0)
|
|
30
|
-
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
31
|
-
.join('');
|
|
32
|
-
}
|
|
33
|
-
/** Language-facing field name — the pinned §12 naming map. */
|
|
34
|
-
function camelCase(name) {
|
|
35
|
-
return snakeToCamel(name);
|
|
36
|
-
}
|
|
37
|
-
function quote(value) {
|
|
38
|
-
return `"${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
|
|
39
|
-
}
|
|
40
|
-
/** Emit a `JSONValue` literal for the schema object (built from the IR). */
|
|
41
|
-
function emitSchemaValue(ir, indent) {
|
|
42
|
-
const lines = [];
|
|
43
|
-
const i = indent;
|
|
44
|
-
lines.push(`${i}.object([`);
|
|
45
|
-
lines.push(`${i} "version": .number(${ir.schemaVersion}),`);
|
|
46
|
-
lines.push(`${i} "tables": .array([`);
|
|
47
|
-
for (const table of ir.tables) {
|
|
48
|
-
lines.push(`${i} .object([`);
|
|
49
|
-
lines.push(`${i} "name": .string(${quote(table.name)}),`);
|
|
50
|
-
lines.push(`${i} "primaryKey": .string(${quote(table.primaryKey)}),`);
|
|
51
|
-
lines.push(`${i} "columns": .array([`);
|
|
52
|
-
for (const column of table.columns) {
|
|
53
|
-
const parts = [
|
|
54
|
-
`"name": .string(${quote(column.name)})`,
|
|
55
|
-
`"type": .string(${quote(column.type)})`,
|
|
56
|
-
`"nullable": .bool(${column.nullable})`,
|
|
57
|
-
];
|
|
58
|
-
if (column.crdtType !== undefined) {
|
|
59
|
-
parts.push(`"crdtType": .string(${quote(column.crdtType)})`);
|
|
60
|
-
}
|
|
61
|
-
if (column.encrypted === true) {
|
|
62
|
-
parts.push('"encrypted": .bool(true)');
|
|
63
|
-
parts.push(`"declaredType": .string(${quote(column.declaredType ?? column.type)})`);
|
|
64
|
-
}
|
|
65
|
-
lines.push(`${i} .object([${parts.join(', ')}]),`);
|
|
66
|
-
}
|
|
67
|
-
lines.push(`${i} ]),`);
|
|
68
|
-
lines.push(`${i} "scopes": .array([`);
|
|
69
|
-
for (const scope of table.scopes) {
|
|
70
|
-
lines.push(`${i} .object(["pattern": .string(${quote(scope.pattern)}), "column": .string(${quote(scope.column)})]),`);
|
|
71
|
-
}
|
|
72
|
-
lines.push(`${i} ]),`);
|
|
73
|
-
if (table.ftsIndexes.length > 0) {
|
|
74
|
-
lines.push(`${i} "ftsIndexes": .array([`);
|
|
75
|
-
for (const index of table.ftsIndexes) {
|
|
76
|
-
const columns = index.columns
|
|
77
|
-
.map((column) => `.string(${quote(column)})`)
|
|
78
|
-
.join(', ');
|
|
79
|
-
lines.push(`${i} .object(["name": .string(${quote(index.name)}), "columns": .array([${columns}]), "tokenize": .string(${quote(index.tokenize)})]),`);
|
|
80
|
-
}
|
|
81
|
-
lines.push(`${i} ]),`);
|
|
82
|
-
}
|
|
83
|
-
lines.push(`${i} ]),`);
|
|
84
|
-
}
|
|
85
|
-
lines.push(`${i} ]),`);
|
|
86
|
-
lines.push(`${i}])`);
|
|
87
|
-
return lines;
|
|
88
|
-
}
|
|
89
34
|
/** The accessor that lifts a §2.4 value out of a `[String: JSONValue]` row. */
|
|
90
35
|
function rowAccessor(column) {
|
|
91
|
-
const key = `row[${
|
|
36
|
+
const key = `row[${stringLiteral(column.name, 'swift')}]`;
|
|
92
37
|
switch (appColumnType(column)) {
|
|
93
38
|
case 'string':
|
|
94
39
|
case 'json':
|
|
@@ -115,22 +60,22 @@ function emitStruct(table) {
|
|
|
115
60
|
lines.push(`/// One ${table.name} row (§2.4 column order).`);
|
|
116
61
|
lines.push(`public struct ${type}: Sendable, Equatable {`);
|
|
117
62
|
for (const column of table.columns) {
|
|
118
|
-
const swift =
|
|
63
|
+
const swift = SWIFT_TYPE[appColumnType(column)];
|
|
119
64
|
const opt = column.nullable ? '?' : '';
|
|
120
|
-
lines.push(` public let ${
|
|
65
|
+
lines.push(` public let ${snakeToCamel(column.name)}: ${swift}${opt}`);
|
|
121
66
|
}
|
|
122
67
|
lines.push('');
|
|
123
68
|
// Memberwise init (public, so app code can build rows too).
|
|
124
69
|
const params = table.columns
|
|
125
70
|
.map((c) => {
|
|
126
|
-
const swift =
|
|
71
|
+
const swift = SWIFT_TYPE[appColumnType(c)];
|
|
127
72
|
const opt = c.nullable ? '? = nil' : '';
|
|
128
|
-
return `${
|
|
73
|
+
return `${snakeToCamel(c.name)}: ${swift}${opt}`;
|
|
129
74
|
})
|
|
130
75
|
.join(', ');
|
|
131
76
|
lines.push(` public init(${params}) {`);
|
|
132
77
|
for (const column of table.columns) {
|
|
133
|
-
const name =
|
|
78
|
+
const name = snakeToCamel(column.name);
|
|
134
79
|
lines.push(` self.${name} = ${name}`);
|
|
135
80
|
}
|
|
136
81
|
lines.push(' }');
|
|
@@ -139,7 +84,7 @@ function emitStruct(table) {
|
|
|
139
84
|
lines.push(` /// Returns nil when a non-nullable column is missing or mistyped.`);
|
|
140
85
|
lines.push(' public init?(row: [String: JSONValue]) {');
|
|
141
86
|
for (const column of table.columns) {
|
|
142
|
-
const name =
|
|
87
|
+
const name = snakeToCamel(column.name);
|
|
143
88
|
const accessor = rowAccessor(column);
|
|
144
89
|
if (column.nullable) {
|
|
145
90
|
lines.push(` self.${name} = ${accessor}`);
|
|
@@ -154,27 +99,18 @@ function emitStruct(table) {
|
|
|
154
99
|
return lines;
|
|
155
100
|
}
|
|
156
101
|
function emitSubscription(sub) {
|
|
157
|
-
const params = [
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
if (value.kind === 'parameter' && !params.includes(value.name)) {
|
|
161
|
-
params.push(value.name);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
102
|
+
const params = [
|
|
103
|
+
...new Set(sub.scopes.flatMap(({ value }) => value.kind === 'parameter' ? [value.name] : [])),
|
|
104
|
+
];
|
|
165
105
|
const lines = [];
|
|
166
|
-
lines.push(` ///
|
|
106
|
+
lines.push(` /// Unit template for the ${stringLiteral(sub.name, 'swift')} subscription (SPEC §4.1).`);
|
|
167
107
|
lines.push(` public enum ${pascalCase(sub.name)} {`);
|
|
168
|
-
lines.push(` public static let name = ${
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
lines.push(` public static func scopes(${args}) -> [String: [String]] {`);
|
|
108
|
+
lines.push(` public static let name = ${stringLiteral(sub.name, 'swift')}`);
|
|
109
|
+
const args = params.map((p) => `${snakeToCamel(p)}: String`).join(', ');
|
|
110
|
+
lines.push(` public static func scopes(${args}) -> [String: String] {`);
|
|
172
111
|
lines.push(' [');
|
|
173
|
-
for (const
|
|
174
|
-
|
|
175
|
-
.map((value) => value.kind === 'literal' ? quote(value.value) : camelCase(value.name))
|
|
176
|
-
.join(', ');
|
|
177
|
-
lines.push(` ${quote(scope.variable)}: [${values}],`);
|
|
112
|
+
for (const { variable, value } of sub.scopes) {
|
|
113
|
+
lines.push(` ${stringLiteral(variable, 'swift')}: ${value.kind === 'literal' ? stringLiteral(value.value, 'swift') : snakeToCamel(value.name)},`);
|
|
178
114
|
}
|
|
179
115
|
lines.push(' ]');
|
|
180
116
|
lines.push(' }');
|
|
@@ -196,13 +132,18 @@ export function emitSwiftModule(ir, hash, enumName) {
|
|
|
196
132
|
schemaLines.push(`/// The generated syncular schema (SPEC §2.4, §3.1) + typed rows.`);
|
|
197
133
|
schemaLines.push(`public enum ${enumName} {`);
|
|
198
134
|
schemaLines.push(` /// ServerSchema-compatible schema — pass to \`SyncularClient(schema:)\`.`);
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
135
|
+
// A multi-line literal strips the closing delimiter's indentation from
|
|
136
|
+
// every line, so the parsed string is the serializeSchema bytes. JSON
|
|
137
|
+
// escapes every quote inside a string, so it never holds the closing
|
|
138
|
+
// delimiter; doubling backslashes also disarms `\(` interpolation.
|
|
139
|
+
schemaLines.push(' public static let schema: JSONValue = try! JSONValue(decoding: """');
|
|
140
|
+
for (const line of serializeSchema(ir).split('\n')) {
|
|
141
|
+
schemaLines.push(` ${line.replaceAll('\\', '\\\\')}`);
|
|
202
142
|
}
|
|
143
|
+
schemaLines.push(' """)');
|
|
203
144
|
schemaLines.push('');
|
|
204
145
|
// Subscriptions namespace nested in the schema enum.
|
|
205
|
-
schemaLines.push(' /// Typed
|
|
146
|
+
schemaLines.push(' /// Typed unit-template builders per subscription (SPEC §4.1).');
|
|
206
147
|
schemaLines.push(' public enum subscriptions {');
|
|
207
148
|
if (ir.subscriptions.length === 0) {
|
|
208
149
|
schemaLines.push(' // (no subscriptions declared)');
|
package/dist/emit.d.ts
CHANGED
|
@@ -6,12 +6,12 @@
|
|
|
6
6
|
* - a `<Table>Row` interface per table,
|
|
7
7
|
* - `<Table>Insert` / `<Table>Update` mutation-input shapes honoring
|
|
8
8
|
* nullability,
|
|
9
|
-
* - a `<name>Subscription` helper constant per manifest
|
|
10
|
-
* a typed
|
|
9
|
+
* - a `<name>Subscription` helper constant per manifest unit template with
|
|
10
|
+
* a typed builder of its unit scope map (SPEC §4.1).
|
|
11
11
|
*
|
|
12
12
|
* The header carries the IR hash so `--check` verifies freshness
|
|
13
13
|
* byte-exactly.
|
|
14
14
|
*/
|
|
15
|
-
import type
|
|
15
|
+
import { type IrDocument } from './ir.js';
|
|
16
16
|
import { type NamingMode } from './naming.js';
|
|
17
17
|
export declare function emitModule(ir: IrDocument, hash: string, naming?: NamingMode): string;
|