@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.
Files changed (78) hide show
  1. package/README.md +96 -43
  2. package/dist/cli.js +8 -3
  3. package/dist/emit-dart.d.ts +4 -3
  4. package/dist/emit-dart.js +40 -100
  5. package/dist/emit-kotlin.d.ts +4 -4
  6. package/dist/emit-kotlin.js +41 -99
  7. package/dist/emit-queries-dart.d.ts +1 -1
  8. package/dist/emit-queries-dart.js +17 -26
  9. package/dist/emit-queries-kotlin.d.ts +1 -1
  10. package/dist/emit-queries-kotlin.js +15 -24
  11. package/dist/emit-queries-rust.d.ts +1 -1
  12. package/dist/emit-queries-rust.js +18 -19
  13. package/dist/emit-queries-swift.d.ts +1 -1
  14. package/dist/emit-queries-swift.js +22 -26
  15. package/dist/emit-queries.d.ts +1 -1
  16. package/dist/emit-queries.js +18 -27
  17. package/dist/emit-swift.d.ts +4 -4
  18. package/dist/emit-swift.js +43 -102
  19. package/dist/emit.d.ts +3 -3
  20. package/dist/emit.js +37 -113
  21. package/dist/errors.d.ts +6 -2
  22. package/dist/errors.js +10 -2
  23. package/dist/fmt.js +11 -2
  24. package/dist/generate.d.ts +4 -3
  25. package/dist/generate.js +61 -73
  26. package/dist/init.js +5 -6
  27. package/dist/ir.d.ts +14 -2
  28. package/dist/ir.js +67 -49
  29. package/dist/lower.d.ts +0 -7
  30. package/dist/lower.js +0 -62
  31. package/dist/lsp.js +6 -1
  32. package/dist/manifest.d.ts +1 -2
  33. package/dist/manifest.js +13 -12
  34. package/dist/migration-lock.d.ts +32 -23
  35. package/dist/migration-lock.js +115 -92
  36. package/dist/naming.d.ts +25 -0
  37. package/dist/naming.js +92 -0
  38. package/dist/query.d.ts +94 -14
  39. package/dist/query.js +642 -461
  40. package/dist/sql.js +18 -3
  41. package/dist/syql-ast.d.ts +2 -0
  42. package/dist/syql-ast.js +7 -1
  43. package/dist/syql-browser.js +7 -6
  44. package/dist/syql-diagnostics.d.ts +1 -0
  45. package/dist/syql-diagnostics.js +1 -0
  46. package/dist/syql-lowering.js +11 -1
  47. package/dist/syql-parser.d.ts +4 -1
  48. package/dist/syql-parser.js +42 -5
  49. package/dist/syql-validator.js +13 -220
  50. package/package.json +3 -3
  51. package/src/cli.ts +9 -3
  52. package/src/emit-dart.ts +44 -116
  53. package/src/emit-kotlin.ts +41 -118
  54. package/src/emit-queries-dart.ts +23 -32
  55. package/src/emit-queries-kotlin.ts +20 -30
  56. package/src/emit-queries-rust.ts +29 -31
  57. package/src/emit-queries-swift.ts +26 -32
  58. package/src/emit-queries.ts +26 -47
  59. package/src/emit-swift.ts +49 -120
  60. package/src/emit.ts +45 -135
  61. package/src/errors.ts +13 -2
  62. package/src/fmt.ts +10 -2
  63. package/src/generate.ts +113 -107
  64. package/src/init.ts +8 -6
  65. package/src/ir.ts +91 -54
  66. package/src/lower.ts +0 -53
  67. package/src/lsp.ts +11 -4
  68. package/src/manifest.ts +22 -18
  69. package/src/migration-lock.ts +192 -137
  70. package/src/naming.ts +154 -0
  71. package/src/query.ts +864 -502
  72. package/src/sql.ts +30 -3
  73. package/src/syql-ast.ts +9 -0
  74. package/src/syql-browser.ts +8 -7
  75. package/src/syql-diagnostics.ts +2 -0
  76. package/src/syql-lowering.ts +16 -1
  77. package/src/syql-parser.ts +73 -8
  78. package/src/syql-validator.ts +21 -279
package/src/emit-dart.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  /**
2
2
  * Dart emitter: neutral IR → one standalone `.dart` file exporting
3
3
  *
4
- * - `const syncularSchema` — the ServerSchema-compatible `Map<String,
5
- * Object?>` built from the IR (fed straight into `SyncularClient.create(
4
+ * - `final syncularSchema` — the ServerSchema-compatible `Map<String,
5
+ * Object?>`: the exact `serializeSchema` JSON the TS module embeds,
6
+ * decoded from a string literal (fed straight into `SyncularClient.create(
6
7
  * schema: …)`),
7
8
  * - one class per table with typed fields (per the six §2.4 column types +
8
9
  * blob_ref/crdt) and a `fromRow(Map<String, Object?>)` factory,
@@ -13,8 +14,15 @@
13
14
  * `query`/`readRows` return. The header carries the IR hash so `--check`
14
15
  * verifies freshness byte-exactly.
15
16
  */
16
- import type { IrColumnType, IrDocument, IrSubscription, IrTable } from './ir';
17
- import { snakeToCamel } from './naming';
17
+ import {
18
+ appColumnType,
19
+ type IrColumnType,
20
+ type IrDocument,
21
+ type IrSubscription,
22
+ type IrTable,
23
+ serializeSchema,
24
+ } from './ir';
25
+ import { pascalCase, snakeToCamel, stringLiteral } from './naming';
18
26
 
19
27
  /** §2.4 column type → honest Dart type. `json`/`blob_ref` are the raw
20
28
  * canonical JSON string; `bytes`/`crdt` are opaque `List<int>`. */
@@ -29,90 +37,9 @@ const DART_TYPE: Readonly<Record<IrColumnType, string>> = {
29
37
  crdt: 'List<int>',
30
38
  };
31
39
 
32
- function appColumnType(column: IrTable['columns'][number]): IrColumnType {
33
- return column.encrypted === true && column.declaredType !== undefined
34
- ? column.declaredType
35
- : column.type;
36
- }
37
-
38
- /** §5.11: app-side Dart type — declaredType for an encrypted column. */
39
- function appDartType(column: IrTable['columns'][number]): string {
40
- return DART_TYPE[appColumnType(column)];
41
- }
42
-
43
- function pascalCase(name: string): string {
44
- return name
45
- .split(/[_-]+/)
46
- .filter((part) => part.length > 0)
47
- .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
48
- .join('');
49
- }
50
-
51
- /** Language-facing field name — the pinned §12 naming map. */
52
- function camelCase(name: string): string {
53
- return snakeToCamel(name);
54
- }
55
-
56
- function quote(value: string): string {
57
- return `'${value.replace(/\\/g, '\\\\').replace(/'/g, "\\'").replace(/\$/g, '\\$')}'`;
58
- }
59
-
60
- function emitSchemaValue(ir: IrDocument, indent: string): string[] {
61
- const lines: string[] = [];
62
- const i = indent;
63
- lines.push(`${i}<String, Object?>{`);
64
- lines.push(`${i} 'version': ${ir.schemaVersion},`);
65
- lines.push(`${i} 'tables': [`);
66
- for (const table of ir.tables) {
67
- lines.push(`${i} {`);
68
- lines.push(`${i} 'name': ${quote(table.name)},`);
69
- lines.push(`${i} 'primaryKey': ${quote(table.primaryKey)},`);
70
- lines.push(`${i} 'columns': [`);
71
- for (const column of table.columns) {
72
- const parts = [
73
- `'name': ${quote(column.name)}`,
74
- `'type': ${quote(column.type)}`,
75
- `'nullable': ${column.nullable}`,
76
- ];
77
- if (column.crdtType !== undefined) {
78
- parts.push(`'crdtType': ${quote(column.crdtType)}`);
79
- }
80
- if (column.encrypted === true) {
81
- parts.push("'encrypted': true");
82
- parts.push(
83
- `'declaredType': ${quote(column.declaredType ?? column.type)}`,
84
- );
85
- }
86
- lines.push(`${i} {${parts.join(', ')}},`);
87
- }
88
- lines.push(`${i} ],`);
89
- lines.push(`${i} 'scopes': [`);
90
- for (const scope of table.scopes) {
91
- lines.push(
92
- `${i} {'pattern': ${quote(scope.pattern)}, 'column': ${quote(scope.column)}},`,
93
- );
94
- }
95
- lines.push(`${i} ],`);
96
- if (table.ftsIndexes.length > 0) {
97
- lines.push(`${i} 'ftsIndexes': [`);
98
- for (const index of table.ftsIndexes) {
99
- const columns = index.columns.map((column) => quote(column)).join(', ');
100
- lines.push(
101
- `${i} {'name': ${quote(index.name)}, 'columns': [${columns}], 'tokenize': ${quote(index.tokenize)}},`,
102
- );
103
- }
104
- lines.push(`${i} ],`);
105
- }
106
- lines.push(`${i} },`);
107
- }
108
- lines.push(`${i} ],`);
109
- lines.push(`${i}}`);
110
- return lines;
111
- }
112
-
113
40
  /** The accessor lifting a §2.4 value out of a `Map<String, Object?>` row. */
114
41
  function rowAccessor(column: IrTable['columns'][number]): string {
115
- const key = `row[${quote(column.name)}]`;
42
+ const key = `row[${stringLiteral(column.name, 'dart')}]`;
116
43
  switch (appColumnType(column)) {
117
44
  case 'string':
118
45
  case 'json':
@@ -136,13 +63,13 @@ function emitClass(table: IrTable): string[] {
136
63
  lines.push(`/// One ${table.name} row (§2.4 column order).`);
137
64
  lines.push(`class ${type} {`);
138
65
  for (const column of table.columns) {
139
- const dart = appDartType(column);
66
+ const dart = DART_TYPE[appColumnType(column)];
140
67
  const opt = column.nullable ? '?' : '';
141
- lines.push(` final ${dart}${opt} ${camelCase(column.name)};`);
68
+ lines.push(` final ${dart}${opt} ${snakeToCamel(column.name)};`);
142
69
  }
143
70
  lines.push('');
144
71
  const ctorParams = table.columns
145
- .map((c) => `${c.nullable ? '' : 'required '}this.${camelCase(c.name)}`)
72
+ .map((c) => `${c.nullable ? '' : 'required '}this.${snakeToCamel(c.name)}`)
146
73
  .join(', ');
147
74
  lines.push(` const ${type}({${ctorParams}});`);
148
75
  lines.push('');
@@ -153,14 +80,14 @@ function emitClass(table: IrTable): string[] {
153
80
  lines.push(` static ${type}? fromRow(Map<String, Object?> row) {`);
154
81
  for (const column of table.columns) {
155
82
  if (!column.nullable) {
156
- const name = camelCase(column.name);
83
+ const name = snakeToCamel(column.name);
157
84
  lines.push(` final ${name} = ${rowAccessor(column)};`);
158
85
  lines.push(` if (${name} == null) return null;`);
159
86
  }
160
87
  }
161
88
  const args = table.columns
162
89
  .map((c) => {
163
- const name = camelCase(c.name);
90
+ const name = snakeToCamel(c.name);
164
91
  return c.nullable ? `${name}: ${rowAccessor(c)}` : `${name}: ${name}`;
165
92
  })
166
93
  .join(', ');
@@ -171,31 +98,30 @@ function emitClass(table: IrTable): string[] {
171
98
  }
172
99
 
173
100
  function emitSubscription(sub: IrSubscription): string[] {
174
- const params: string[] = [];
175
- for (const scope of sub.scopes) {
176
- for (const value of scope.values) {
177
- if (value.kind === 'parameter' && !params.includes(value.name)) {
178
- params.push(value.name);
179
- }
180
- }
181
- }
101
+ const params = [
102
+ ...new Set(
103
+ sub.scopes.flatMap(({ value }) =>
104
+ value.kind === 'parameter' ? [value.name] : [],
105
+ ),
106
+ ),
107
+ ];
182
108
  const lines: string[] = [];
183
109
  lines.push(
184
- `/// Requested-scope template for the ${quote(sub.name)} subscription.`,
110
+ `/// Unit template for the ${stringLiteral(sub.name, 'dart')} subscription (SPEC §4.1).`,
185
111
  );
186
112
  lines.push(`class Syncular${pascalCase(sub.name)}Subscription {`);
187
- lines.push(` static const String name = ${quote(sub.name)};`);
188
- lines.push(` static const String table = ${quote(sub.table)};`);
189
- const args = params.map((p) => `required String ${camelCase(p)}`).join(', ');
113
+ lines.push(
114
+ ` static const String name = ${stringLiteral(sub.name, 'dart')};`,
115
+ );
116
+ const args = params
117
+ .map((p) => `required String ${snakeToCamel(p)}`)
118
+ .join(', ');
190
119
  const argsClause = args.length > 0 ? `{${args}}` : '';
191
- lines.push(` static Map<String, List<String>> scopes(${argsClause}) => {`);
192
- for (const scope of sub.scopes) {
193
- const values = scope.values
194
- .map((value) =>
195
- value.kind === 'literal' ? quote(value.value) : camelCase(value.name),
196
- )
197
- .join(', ');
198
- lines.push(` ${quote(scope.variable)}: [${values}],`);
120
+ lines.push(` static Map<String, String> scopes(${argsClause}) => {`);
121
+ for (const { variable, value } of sub.scopes) {
122
+ lines.push(
123
+ ` ${stringLiteral(variable, 'dart')}: ${value.kind === 'literal' ? stringLiteral(value.value, 'dart') : snakeToCamel(value.name)},`,
124
+ );
199
125
  }
200
126
  lines.push(' };');
201
127
  lines.push('}');
@@ -210,6 +136,8 @@ export function emitDartModule(ir: IrDocument, hash: string): string {
210
136
  `// irVersion: ${ir.irVersion}`,
211
137
  `// irHash: ${hash}`,
212
138
  '// ignore_for_file: type=lint',
139
+ '',
140
+ "import 'dart:convert';",
213
141
  ].join('\n'),
214
142
  );
215
143
 
@@ -217,11 +145,11 @@ export function emitDartModule(ir: IrDocument, hash: string): string {
217
145
  const schemaLines: string[] = [];
218
146
  schemaLines.push('/// ServerSchema-compatible schema (SPEC §2.4, §3.1) —');
219
147
  schemaLines.push('/// pass to `SyncularClient.create(schema: …)`.');
220
- const schemaValue = emitSchemaValue(ir, '');
221
- schemaValue[0] = `const Map<String, Object?> syncularSchema = ${schemaValue[0]}`;
222
- schemaValue[schemaValue.length - 1] =
223
- `${schemaValue[schemaValue.length - 1]};`;
224
- for (const line of schemaValue) schemaLines.push(line);
148
+ // A multi-line literal drops the line break after its opening quotes, so
149
+ // the decoded string is the serializeSchema bytes.
150
+ schemaLines.push(
151
+ `final Map<String, Object?> syncularSchema = jsonDecode('''\n${stringLiteral(serializeSchema(ir), 'dart').slice(1, -1)}''') as Map<String, Object?>;`,
152
+ );
225
153
  parts.push(schemaLines.join('\n'));
226
154
 
227
155
  // Shared row-decode helpers — emitted only when a column type references
@@ -2,18 +2,25 @@
2
2
  * Kotlin emitter: neutral IR → one standalone `.kt` file exporting
3
3
  *
4
4
  * - `object <ObjectName> { val schema: JsonValue }` — the ServerSchema
5
- * object built from the IR (fed straight into `SyncularClient.create(
6
- * schema = …)`),
5
+ * object: the exact `serializeSchema` JSON the TS module embeds, parsed
6
+ * from a raw string (fed straight into `SyncularClient.create(schema = …)`),
7
7
  * - one `data class` per table with typed properties (per the six §2.4 column
8
8
  * types + blob_ref/crdt) and a `fromRow(row: JsonValue)` companion factory,
9
- * - a `Subscriptions` object of typed requested-scope builders.
9
+ * - a `Subscriptions` object of typed unit-template builders (SPEC §4.1).
10
10
  *
11
11
  * The row shape is `JsonValue` — the exact type the Kotlin wrapper's
12
12
  * `query`/`readRows` return (each row a `JsonValue.Obj`). The header carries
13
13
  * the IR hash so `--check` verifies freshness byte-exactly.
14
14
  */
15
- import type { IrColumnType, IrDocument, IrSubscription, IrTable } from './ir';
16
- import { snakeToCamel } from './naming';
15
+ import {
16
+ appColumnType,
17
+ type IrColumnType,
18
+ type IrDocument,
19
+ type IrSubscription,
20
+ type IrTable,
21
+ serializeSchema,
22
+ } from './ir';
23
+ import { pascalCase, snakeToCamel, stringLiteral } from './naming';
17
24
 
18
25
  /** §2.4 column type → honest Kotlin type. `json`/`blob_ref` are the raw
19
26
  * canonical JSON string; `bytes`/`crdt` are opaque `ByteArray`. */
@@ -28,94 +35,9 @@ const KOTLIN_TYPE: Readonly<Record<IrColumnType, string>> = {
28
35
  crdt: 'ByteArray',
29
36
  };
30
37
 
31
- function appColumnType(column: IrTable['columns'][number]): IrColumnType {
32
- return column.encrypted === true && column.declaredType !== undefined
33
- ? column.declaredType
34
- : column.type;
35
- }
36
-
37
- /** §5.11: app-side Kotlin type — declaredType for an encrypted column. */
38
- function appKotlinType(column: IrTable['columns'][number]): string {
39
- return KOTLIN_TYPE[appColumnType(column)];
40
- }
41
-
42
- function pascalCase(name: string): string {
43
- return name
44
- .split(/[_-]+/)
45
- .filter((part) => part.length > 0)
46
- .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
47
- .join('');
48
- }
49
-
50
- /** Language-facing field name — the pinned §12 naming map. */
51
- function camelCase(name: string): string {
52
- return snakeToCamel(name);
53
- }
54
-
55
- function quote(value: string): string {
56
- return `"${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\$/g, '\\$')}"`;
57
- }
58
-
59
- function emitSchemaValue(ir: IrDocument, indent: string): string[] {
60
- const lines: string[] = [];
61
- const i = indent;
62
- lines.push(`${i}JsonValue.obj(`);
63
- lines.push(`${i} "version" to JsonValue.of(${ir.schemaVersion}),`);
64
- lines.push(`${i} "tables" to JsonValue.arr(listOf(`);
65
- for (const table of ir.tables) {
66
- lines.push(`${i} JsonValue.obj(`);
67
- lines.push(`${i} "name" to JsonValue.of(${quote(table.name)}),`);
68
- lines.push(
69
- `${i} "primaryKey" to JsonValue.of(${quote(table.primaryKey)}),`,
70
- );
71
- lines.push(`${i} "columns" to JsonValue.arr(listOf(`);
72
- for (const column of table.columns) {
73
- const parts = [
74
- `"name" to JsonValue.of(${quote(column.name)})`,
75
- `"type" to JsonValue.of(${quote(column.type)})`,
76
- `"nullable" to JsonValue.of(${column.nullable})`,
77
- ];
78
- if (column.crdtType !== undefined) {
79
- parts.push(`"crdtType" to JsonValue.of(${quote(column.crdtType)})`);
80
- }
81
- if (column.encrypted === true) {
82
- parts.push('"encrypted" to JsonValue.of(true)');
83
- parts.push(
84
- `"declaredType" to JsonValue.of(${quote(column.declaredType ?? column.type)})`,
85
- );
86
- }
87
- lines.push(`${i} JsonValue.obj(${parts.join(', ')}),`);
88
- }
89
- lines.push(`${i} )),`);
90
- lines.push(`${i} "scopes" to JsonValue.arr(listOf(`);
91
- for (const scope of table.scopes) {
92
- lines.push(
93
- `${i} JsonValue.obj("pattern" to JsonValue.of(${quote(scope.pattern)}), "column" to JsonValue.of(${quote(scope.column)})),`,
94
- );
95
- }
96
- lines.push(`${i} )),`);
97
- if (table.ftsIndexes.length > 0) {
98
- lines.push(`${i} "ftsIndexes" to JsonValue.arr(listOf(`);
99
- for (const index of table.ftsIndexes) {
100
- const columns = index.columns
101
- .map((column) => `JsonValue.of(${quote(column)})`)
102
- .join(', ');
103
- lines.push(
104
- `${i} JsonValue.obj("name" to JsonValue.of(${quote(index.name)}), "columns" to JsonValue.arr(listOf(${columns})), "tokenize" to JsonValue.of(${quote(index.tokenize)})),`,
105
- );
106
- }
107
- lines.push(`${i} )),`);
108
- }
109
- lines.push(`${i} ),`);
110
- }
111
- lines.push(`${i} )),`);
112
- lines.push(`${i})`);
113
- return lines;
114
- }
115
-
116
38
  /** The accessor lifting a §2.4 value out of a row `JsonValue`. */
117
39
  function rowAccessor(column: IrTable['columns'][number]): string {
118
- const key = `row[${quote(column.name)}]`;
40
+ const key = `row[${stringLiteral(column.name, 'kotlin')}]`;
119
41
  switch (appColumnType(column)) {
120
42
  case 'string':
121
43
  case 'json':
@@ -139,9 +61,9 @@ function emitDataClass(table: IrTable): string[] {
139
61
  lines.push(`/** One ${table.name} row (§2.4 column order). */`);
140
62
  lines.push(`data class ${type}(`);
141
63
  for (const column of table.columns) {
142
- const kotlin = appKotlinType(column);
64
+ const kotlin = KOTLIN_TYPE[appColumnType(column)];
143
65
  const opt = column.nullable ? '?' : '';
144
- lines.push(` val ${camelCase(column.name)}: ${kotlin}${opt},`);
66
+ lines.push(` val ${snakeToCamel(column.name)}: ${kotlin}${opt},`);
145
67
  }
146
68
  lines.push(') {');
147
69
  lines.push(' companion object {');
@@ -150,7 +72,7 @@ function emitDataClass(table: IrTable): string[] {
150
72
  );
151
73
  lines.push(` fun fromRow(row: JsonValue): ${type}? {`);
152
74
  for (const column of table.columns) {
153
- const name = camelCase(column.name);
75
+ const name = snakeToCamel(column.name);
154
76
  const accessor = rowAccessor(column);
155
77
  if (column.nullable) {
156
78
  lines.push(` val ${name} = ${accessor}`);
@@ -158,7 +80,7 @@ function emitDataClass(table: IrTable): string[] {
158
80
  lines.push(` val ${name} = ${accessor} ?: return null`);
159
81
  }
160
82
  }
161
- const args = table.columns.map((c) => camelCase(c.name)).join(', ');
83
+ const args = table.columns.map((c) => snakeToCamel(c.name)).join(', ');
162
84
  lines.push(` return ${type}(${args})`);
163
85
  lines.push(' }');
164
86
  lines.push(' }');
@@ -167,33 +89,26 @@ function emitDataClass(table: IrTable): string[] {
167
89
  }
168
90
 
169
91
  function emitSubscription(sub: IrSubscription): string[] {
170
- const params: string[] = [];
171
- for (const scope of sub.scopes) {
172
- for (const value of scope.values) {
173
- if (value.kind === 'parameter' && !params.includes(value.name)) {
174
- params.push(value.name);
175
- }
176
- }
177
- }
92
+ const params = [
93
+ ...new Set(
94
+ sub.scopes.flatMap(({ value }) =>
95
+ value.kind === 'parameter' ? [value.name] : [],
96
+ ),
97
+ ),
98
+ ];
178
99
  const lines: string[] = [];
179
100
  lines.push(
180
- ` /** Requested-scope template for the ${quote(sub.name)} subscription. */`,
101
+ ` /** Unit template for the ${stringLiteral(sub.name, 'kotlin')} subscription (SPEC §4.1). */`,
181
102
  );
182
103
  lines.push(` object ${pascalCase(sub.name)} {`);
183
- lines.push(` const val name = ${quote(sub.name)}`);
184
- lines.push(` const val table = ${quote(sub.table)}`);
185
- const args = params.map((p) => `${camelCase(p)}: String`).join(', ');
186
104
  lines.push(
187
- ` fun scopes(${args}): Map<String, List<String>> = mapOf(`,
105
+ ` const val name = ${stringLiteral(sub.name, 'kotlin')}`,
188
106
  );
189
- for (const scope of sub.scopes) {
190
- const values = scope.values
191
- .map((value) =>
192
- value.kind === 'literal' ? quote(value.value) : camelCase(value.name),
193
- )
194
- .join(', ');
107
+ const args = params.map((p) => `${snakeToCamel(p)}: String`).join(', ');
108
+ lines.push(` fun scopes(${args}): Map<String, String> = mapOf(`);
109
+ for (const { variable, value } of sub.scopes) {
195
110
  lines.push(
196
- ` ${quote(scope.variable)} to listOf(${values}),`,
111
+ ` ${stringLiteral(variable, 'kotlin')} to ${value.kind === 'literal' ? stringLiteral(value.value, 'kotlin') : snakeToCamel(value.name)},`,
197
112
  );
198
113
  }
199
114
  lines.push(' )');
@@ -226,11 +141,19 @@ export function emitKotlinModule(
226
141
  schemaLines.push(
227
142
  ` /** ServerSchema-compatible schema — pass to \`SyncularClient.create(schema = …)\`. */`,
228
143
  );
229
- schemaLines.push(' val schema: JsonValue =');
230
- for (const line of emitSchemaValue(ir, ' ')) schemaLines.push(line);
144
+ // A raw string has no escapes; only `$` needs its template form.
145
+ // trimIndent() strips the common indentation and the blank first and last
146
+ // lines, so the parsed string is the serializeSchema bytes.
147
+ schemaLines.push(' val schema: JsonValue = JsonValue.parse(');
148
+ schemaLines.push(' """');
149
+ for (const line of serializeSchema(ir).split('\n')) {
150
+ schemaLines.push(` ${line.replaceAll('$', () => "${'$'}")}`);
151
+ }
152
+ schemaLines.push(' """.trimIndent(),');
153
+ schemaLines.push(' )');
231
154
  schemaLines.push('');
232
155
  schemaLines.push(
233
- ' /** Typed requested-scope builders per subscription. */',
156
+ ' /** Typed unit-template builders per subscription (SPEC §4.1). */',
234
157
  );
235
158
  schemaLines.push(' object Subscriptions {');
236
159
  if (ir.subscriptions.length === 0) {
@@ -9,11 +9,11 @@
9
9
  */
10
10
  import type { IrColumnType } from './ir';
11
11
  import { snakeToCamel } from './naming';
12
- import type {
13
- AnalyzedQuery,
14
- QueryColumn,
15
- QuerySyqlPlanBind,
16
- QuerySyqlPublicInput,
12
+ import {
13
+ type AnalyzedQuery,
14
+ type QueryColumn,
15
+ type QuerySyqlPlanBind,
16
+ syqlInput,
17
17
  } from './query';
18
18
 
19
19
  const DART_TYPE: Readonly<Record<IrColumnType, string>> = {
@@ -35,11 +35,6 @@ function pascalCase(name: string): string {
35
35
  .join('');
36
36
  }
37
37
 
38
- /** Language-facing field name — the pinned §12 naming map. */
39
- function camelCase(name: string): string {
40
- return snakeToCamel(name);
41
- }
42
-
43
38
  function typeName(name: string): string {
44
39
  return name.charAt(0).toUpperCase() + name.slice(1);
45
40
  }
@@ -90,19 +85,13 @@ function optionalParamValue(type: IrColumnType, name: string): string {
90
85
  }
91
86
  }
92
87
 
93
- function syqlInput(query: AnalyzedQuery, name: string): QuerySyqlPublicInput {
94
- const input = query.syql?.inputs.find((candidate) => candidate.name === name);
95
- if (input === undefined) throw new Error(`unknown SYQL input ${name}`);
96
- return input;
97
- }
98
-
99
88
  function syqlDartType(type: IrColumnType, nullable: boolean): string {
100
89
  return `${DART_TYPE[type]}${nullable ? '?' : ''}`;
101
90
  }
102
91
 
103
92
  function syqlControlActive(query: AnalyzedQuery, name: string): string {
104
93
  const input = syqlInput(query, name);
105
- const access = camelCase(input.langName);
94
+ const access = snakeToCamel(input.langName);
106
95
  if (input.kind === 'value' && input.default === false) return access;
107
96
  if (input.kind === 'value' && input.nullable) return `${access}.isPresent`;
108
97
  if (input.kind === 'value' || input.kind === 'group') {
@@ -118,7 +107,7 @@ function syqlBindExpr(query: AnalyzedQuery, bind: QuerySyqlPlanBind): string {
118
107
  .join(' && ');
119
108
  }
120
109
  const input = syqlInput(query, bind.input);
121
- const access = camelCase(input.langName);
110
+ const access = snakeToCamel(input.langName);
122
111
  if (bind.kind === 'limit') return `effective${typeName(access)}`;
123
112
  if (bind.kind === 'group-member') {
124
113
  if (input.kind !== 'group') throw new Error('group bind/input mismatch');
@@ -127,7 +116,7 @@ function syqlBindExpr(query: AnalyzedQuery, bind: QuerySyqlPlanBind): string {
127
116
  );
128
117
  if (member === undefined)
129
118
  throw new Error(`unknown group member ${bind.member}`);
130
- const memberAccess = `${access}.${camelCase(member.langName)}`;
119
+ const memberAccess = `${access}.${snakeToCamel(member.langName)}`;
131
120
  const value = member.nullable
132
121
  ? optionalParamValue(member.type, memberAccess)
133
122
  : paramValue(member.type, memberAccess);
@@ -153,17 +142,17 @@ function emitSyqlDartTypes(query: AnalyzedQuery): string[] {
153
142
  lines.push(`class ${name} {`);
154
143
  for (const member of input.members) {
155
144
  lines.push(
156
- ` final ${syqlDartType(member.type, member.nullable)} ${camelCase(member.langName)};`,
145
+ ` final ${syqlDartType(member.type, member.nullable)} ${snakeToCamel(member.langName)};`,
157
146
  );
158
147
  }
159
148
  const args = input.members
160
- .map((member) => `required this.${camelCase(member.langName)}`)
149
+ .map((member) => `required this.${snakeToCamel(member.langName)}`)
161
150
  .join(', ');
162
151
  lines.push(` const ${name}({${args}});`, '}', '');
163
152
  } else if (input.kind === 'sort') {
164
153
  const name = `${typeName(query.name)}${typeName(input.langName)}`;
165
154
  lines.push(
166
- `enum ${name} { ${input.profiles.map((profile) => camelCase(profile.langName)).join(', ')} }`,
155
+ `enum ${name} { ${input.profiles.map((profile) => snakeToCamel(profile.langName)).join(', ')} }`,
167
156
  '',
168
157
  );
169
158
  }
@@ -186,7 +175,7 @@ function emitSyqlDartRunner(query: AnalyzedQuery): string[] {
186
175
  );
187
176
  const args: string[] = [];
188
177
  for (const input of metadata.inputs) {
189
- const name = camelCase(input.langName);
178
+ const name = snakeToCamel(input.langName);
190
179
  if (input.kind === 'value') {
191
180
  const type = syqlDartType(input.type, input.nullable);
192
181
  if (input.required) args.push(`required ${type} ${name}`);
@@ -203,7 +192,7 @@ function emitSyqlDartRunner(query: AnalyzedQuery): string[] {
203
192
  input.profiles.find((profile) => profile.name === input.defaultProfile)
204
193
  ?.langName ?? input.defaultProfile;
205
194
  const type = `${typeName(query.name)}${typeName(input.langName)}`;
206
- args.push(`${type} ${name} = ${type}.${camelCase(defaultCase)}`);
195
+ args.push(`${type} ${name} = ${type}.${snakeToCamel(defaultCase)}`);
207
196
  } else {
208
197
  args.push(`int? ${name}`);
209
198
  }
@@ -214,7 +203,7 @@ function emitSyqlDartRunner(query: AnalyzedQuery): string[] {
214
203
  );
215
204
  const page = metadata.inputs.find((input) => input.kind === 'limit');
216
205
  if (page?.kind === 'limit') {
217
- const name = camelCase(page.langName);
206
+ const name = snakeToCamel(page.langName);
218
207
  lines.push(
219
208
  ` final effective${typeName(name)} = ${name} ?? ${page.defaultSize};`,
220
209
  ` if (effective${typeName(name)} < 1 || effective${typeName(name)} > ${page.maxSize}) {`,
@@ -233,7 +222,7 @@ function emitSyqlDartRunner(query: AnalyzedQuery): string[] {
233
222
  const sort = metadata.inputs.find((input) => input.kind === 'sort');
234
223
  const profileCount = sort?.kind === 'sort' ? sort.profiles.length : 1;
235
224
  const sortIndex =
236
- sort?.kind === 'sort' ? `${camelCase(sort.langName)}.index` : '0';
225
+ sort?.kind === 'sort' ? `${snakeToCamel(sort.langName)}.index` : '0';
237
226
  const index =
238
227
  metadata.plan.backend === 'variants'
239
228
  ? `activationMask * ${profileCount} + ${sortIndex}`
@@ -269,26 +258,28 @@ function emitClass(query: AnalyzedQuery): string[] {
269
258
  for (const column of query.columns) {
270
259
  const opt = column.nullable ? '?' : '';
271
260
  lines.push(
272
- ` final ${DART_TYPE[column.type]}${opt} ${camelCase(column.langName)};`,
261
+ ` final ${DART_TYPE[column.type]}${opt} ${snakeToCamel(column.langName)};`,
273
262
  );
274
263
  }
275
264
  lines.push('');
276
265
  const ctorParams = query.columns
277
- .map((c) => `${c.nullable ? '' : 'required '}this.${camelCase(c.langName)}`)
266
+ .map(
267
+ (c) => `${c.nullable ? '' : 'required '}this.${snakeToCamel(c.langName)}`,
268
+ )
278
269
  .join(', ');
279
270
  lines.push(` const ${Row}({${ctorParams}});`);
280
271
  lines.push('');
281
272
  lines.push(` static ${Row}? fromRow(Map<String, Object?> row) {`);
282
273
  for (const column of query.columns) {
283
274
  if (!column.nullable) {
284
- const name = camelCase(column.langName);
275
+ const name = snakeToCamel(column.langName);
285
276
  lines.push(` final ${name} = ${rowAccessor(column)};`);
286
277
  lines.push(` if (${name} == null) return null;`);
287
278
  }
288
279
  }
289
280
  const args = query.columns
290
281
  .map((c) => {
291
- const name = camelCase(c.langName);
282
+ const name = snakeToCamel(c.langName);
292
283
  return c.nullable ? `${name}: ${rowAccessor(c)}` : `${name}: ${name}`;
293
284
  })
294
285
  .join(', ');
@@ -315,7 +306,7 @@ function emitRunner(query: AnalyzedQuery): string[] {
315
306
  lines.push(`/// Run the ${query.name} named query (SELECT-only).`);
316
307
  const args: string[] = [];
317
308
  for (const p of query.params) {
318
- const name = camelCase(p.langName);
309
+ const name = snakeToCamel(p.langName);
319
310
  args.push(`required ${DART_TYPE[p.type]} ${name}`);
320
311
  }
321
312
  const argsClause = args.length > 0 ? `, {${args.join(', ')}}` : '';
@@ -326,7 +317,7 @@ function emitRunner(query: AnalyzedQuery): string[] {
326
317
  if (query.params.length > 0) {
327
318
  const binds = query.params
328
319
  .map((p) => {
329
- const name = camelCase(p.langName);
320
+ const name = snakeToCamel(p.langName);
330
321
  return paramValue(p.type, name);
331
322
  })
332
323
  .join(', ');