@syncular/typegen 0.58.0 → 0.59.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 +43 -25
- package/dist/cli.js +8 -3
- package/dist/emit-dart.d.ts +4 -3
- package/dist/emit-dart.js +39 -88
- package/dist/emit-kotlin.d.ts +3 -3
- package/dist/emit-kotlin.js +39 -86
- 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 +1 -6
- 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 +1 -6
- package/dist/emit-swift.d.ts +3 -3
- package/dist/emit-swift.js +41 -89
- package/dist/emit.d.ts +1 -1
- package/dist/emit.js +33 -98
- package/dist/errors.d.ts +6 -2
- package/dist/errors.js +10 -2
- package/dist/generate.d.ts +4 -3
- package/dist/generate.js +32 -49
- package/dist/init.js +1 -1
- package/dist/ir.d.ts +11 -0
- package/dist/ir.js +66 -47
- package/dist/lower.d.ts +0 -7
- package/dist/lower.js +0 -62
- 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 +54 -14
- package/dist/query.js +484 -462
- package/dist/sql.js +18 -3
- package/dist/syql-lowering.js +10 -1
- package/dist/syql-validator.js +13 -220
- package/package.json +3 -3
- package/src/cli.ts +9 -3
- package/src/emit-dart.ts +41 -102
- package/src/emit-kotlin.ts +38 -102
- package/src/emit-queries-dart.ts +23 -32
- package/src/emit-queries-kotlin.ts +20 -30
- package/src/emit-queries-rust.ts +7 -12
- package/src/emit-queries-swift.ts +26 -32
- package/src/emit-queries.ts +5 -11
- package/src/emit-swift.ts +42 -104
- package/src/emit.ts +33 -116
- package/src/errors.ts +13 -2
- package/src/generate.ts +74 -76
- package/src/init.ts +4 -1
- package/src/ir.ts +87 -50
- package/src/lower.ts +0 -53
- package/src/migration-lock.ts +192 -137
- package/src/naming.ts +154 -0
- package/src/query.ts +615 -504
- package/src/sql.ts +30 -3
- package/src/syql-lowering.ts +15 -1
- package/src/syql-validator.ts +21 -279
package/src/emit-kotlin.ts
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
* Kotlin emitter: neutral IR → one standalone `.kt` file exporting
|
|
3
3
|
*
|
|
4
4
|
* - `object <ObjectName> { val schema: JsonValue }` — the ServerSchema
|
|
5
|
-
* object
|
|
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
9
|
* - a `Subscriptions` object of typed requested-scope builders.
|
|
@@ -12,8 +12,15 @@
|
|
|
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
|
|
16
|
-
|
|
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[${
|
|
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 =
|
|
64
|
+
const kotlin = KOTLIN_TYPE[appColumnType(column)];
|
|
143
65
|
const opt = column.nullable ? '?' : '';
|
|
144
|
-
lines.push(` val ${
|
|
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 =
|
|
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) =>
|
|
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(' }');
|
|
@@ -177,23 +99,29 @@ function emitSubscription(sub: IrSubscription): string[] {
|
|
|
177
99
|
}
|
|
178
100
|
const lines: string[] = [];
|
|
179
101
|
lines.push(
|
|
180
|
-
` /** Requested-scope template for the ${
|
|
102
|
+
` /** Requested-scope template for the ${stringLiteral(sub.name, 'kotlin')} subscription. */`,
|
|
181
103
|
);
|
|
182
104
|
lines.push(` object ${pascalCase(sub.name)} {`);
|
|
183
|
-
lines.push(
|
|
184
|
-
|
|
185
|
-
|
|
105
|
+
lines.push(
|
|
106
|
+
` const val name = ${stringLiteral(sub.name, 'kotlin')}`,
|
|
107
|
+
);
|
|
108
|
+
lines.push(
|
|
109
|
+
` const val table = ${stringLiteral(sub.table, 'kotlin')}`,
|
|
110
|
+
);
|
|
111
|
+
const args = params.map((p) => `${snakeToCamel(p)}: String`).join(', ');
|
|
186
112
|
lines.push(
|
|
187
113
|
` fun scopes(${args}): Map<String, List<String>> = mapOf(`,
|
|
188
114
|
);
|
|
189
115
|
for (const scope of sub.scopes) {
|
|
190
116
|
const values = scope.values
|
|
191
117
|
.map((value) =>
|
|
192
|
-
value.kind === 'literal'
|
|
118
|
+
value.kind === 'literal'
|
|
119
|
+
? stringLiteral(value.value, 'kotlin')
|
|
120
|
+
: snakeToCamel(value.name),
|
|
193
121
|
)
|
|
194
122
|
.join(', ');
|
|
195
123
|
lines.push(
|
|
196
|
-
` ${
|
|
124
|
+
` ${stringLiteral(scope.variable, 'kotlin')} to listOf(${values}),`,
|
|
197
125
|
);
|
|
198
126
|
}
|
|
199
127
|
lines.push(' )');
|
|
@@ -226,8 +154,16 @@ export function emitKotlinModule(
|
|
|
226
154
|
schemaLines.push(
|
|
227
155
|
` /** ServerSchema-compatible schema — pass to \`SyncularClient.create(schema = …)\`. */`,
|
|
228
156
|
);
|
|
229
|
-
|
|
230
|
-
|
|
157
|
+
// A raw string has no escapes; only `$` needs its template form.
|
|
158
|
+
// trimIndent() strips the common indentation and the blank first and last
|
|
159
|
+
// lines, so the parsed string is the serializeSchema bytes.
|
|
160
|
+
schemaLines.push(' val schema: JsonValue = JsonValue.parse(');
|
|
161
|
+
schemaLines.push(' """');
|
|
162
|
+
for (const line of serializeSchema(ir).split('\n')) {
|
|
163
|
+
schemaLines.push(` ${line.replaceAll('$', () => "${'$'}")}`);
|
|
164
|
+
}
|
|
165
|
+
schemaLines.push(' """.trimIndent(),');
|
|
166
|
+
schemaLines.push(' )');
|
|
231
167
|
schemaLines.push('');
|
|
232
168
|
schemaLines.push(
|
|
233
169
|
' /** Typed requested-scope builders per subscription. */',
|
package/src/emit-queries-dart.ts
CHANGED
|
@@ -9,11 +9,11 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import type { IrColumnType } from './ir';
|
|
11
11
|
import { snakeToCamel } from './naming';
|
|
12
|
-
import
|
|
13
|
-
AnalyzedQuery,
|
|
14
|
-
QueryColumn,
|
|
15
|
-
QuerySyqlPlanBind,
|
|
16
|
-
|
|
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 =
|
|
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 =
|
|
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}.${
|
|
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)} ${
|
|
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.${
|
|
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) =>
|
|
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 =
|
|
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}.${
|
|
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 =
|
|
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' ? `${
|
|
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} ${
|
|
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(
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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 =
|
|
320
|
+
const name = snakeToCamel(p.langName);
|
|
330
321
|
return paramValue(p.type, name);
|
|
331
322
|
})
|
|
332
323
|
.join(', ');
|
|
@@ -13,11 +13,11 @@
|
|
|
13
13
|
*/
|
|
14
14
|
import type { IrColumnType } from './ir';
|
|
15
15
|
import { snakeToCamel } from './naming';
|
|
16
|
-
import
|
|
17
|
-
AnalyzedQuery,
|
|
18
|
-
QueryColumn,
|
|
19
|
-
QuerySyqlPlanBind,
|
|
20
|
-
|
|
16
|
+
import {
|
|
17
|
+
type AnalyzedQuery,
|
|
18
|
+
type QueryColumn,
|
|
19
|
+
type QuerySyqlPlanBind,
|
|
20
|
+
syqlInput,
|
|
21
21
|
} from './query';
|
|
22
22
|
|
|
23
23
|
const KOTLIN_TYPE: Readonly<Record<IrColumnType, string>> = {
|
|
@@ -31,11 +31,6 @@ const KOTLIN_TYPE: Readonly<Record<IrColumnType, string>> = {
|
|
|
31
31
|
crdt: 'ByteArray',
|
|
32
32
|
};
|
|
33
33
|
|
|
34
|
-
/** Language-facing field name — the pinned §12 naming map. */
|
|
35
|
-
function camelCase(name: string): string {
|
|
36
|
-
return snakeToCamel(name);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
34
|
function typeName(name: string): string {
|
|
40
35
|
return name.charAt(0).toUpperCase() + name.slice(1);
|
|
41
36
|
}
|
|
@@ -94,19 +89,13 @@ function optionalParamValue(type: IrColumnType, name: string): string {
|
|
|
94
89
|
}
|
|
95
90
|
}
|
|
96
91
|
|
|
97
|
-
function syqlInput(query: AnalyzedQuery, name: string): QuerySyqlPublicInput {
|
|
98
|
-
const input = query.syql?.inputs.find((candidate) => candidate.name === name);
|
|
99
|
-
if (input === undefined) throw new Error(`unknown SYQL input ${name}`);
|
|
100
|
-
return input;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
92
|
function syqlKotlinType(type: IrColumnType, nullable: boolean): string {
|
|
104
93
|
return `${KOTLIN_TYPE[type]}${nullable ? '?' : ''}`;
|
|
105
94
|
}
|
|
106
95
|
|
|
107
96
|
function syqlControlActive(query: AnalyzedQuery, name: string): string {
|
|
108
97
|
const input = syqlInput(query, name);
|
|
109
|
-
const access =
|
|
98
|
+
const access = snakeToCamel(input.langName);
|
|
110
99
|
if (input.kind === 'value' && input.default === false) return access;
|
|
111
100
|
if (input.kind === 'value' && input.nullable) {
|
|
112
101
|
return `${access} is SyncularQueryPresence.Present`;
|
|
@@ -129,7 +118,7 @@ function syqlBindExpr(query: AnalyzedQuery, bind: QuerySyqlPlanBind): string {
|
|
|
129
118
|
return `JsonValue.of(${active})`;
|
|
130
119
|
}
|
|
131
120
|
const input = syqlInput(query, bind.input);
|
|
132
|
-
const access =
|
|
121
|
+
const access = snakeToCamel(input.langName);
|
|
133
122
|
if (bind.kind === 'limit')
|
|
134
123
|
return `JsonValue.of(effective${typeName(access)}.toDouble())`;
|
|
135
124
|
if (bind.kind === 'group-member') {
|
|
@@ -139,7 +128,7 @@ function syqlBindExpr(query: AnalyzedQuery, bind: QuerySyqlPlanBind): string {
|
|
|
139
128
|
);
|
|
140
129
|
if (member === undefined)
|
|
141
130
|
throw new Error(`unknown group member ${bind.member}`);
|
|
142
|
-
const memberName =
|
|
131
|
+
const memberName = snakeToCamel(member.langName);
|
|
143
132
|
const present = member.nullable
|
|
144
133
|
? optionalParamValue(member.type, `value.${memberName}`)
|
|
145
134
|
: paramValue(member.type, `value.${memberName}`);
|
|
@@ -165,7 +154,7 @@ function emitSyqlKotlinTypes(query: AnalyzedQuery): string[] {
|
|
|
165
154
|
lines.push(`data class ${name}(`);
|
|
166
155
|
for (const member of input.members) {
|
|
167
156
|
lines.push(
|
|
168
|
-
` val ${
|
|
157
|
+
` val ${snakeToCamel(member.langName)}: ${syqlKotlinType(member.type, member.nullable)},`,
|
|
169
158
|
);
|
|
170
159
|
}
|
|
171
160
|
lines.push(')', '');
|
|
@@ -175,7 +164,8 @@ function emitSyqlKotlinTypes(query: AnalyzedQuery): string[] {
|
|
|
175
164
|
lines.push(
|
|
176
165
|
`${input.profiles
|
|
177
166
|
.map(
|
|
178
|
-
(profile, index) =>
|
|
167
|
+
(profile, index) =>
|
|
168
|
+
` ${snakeToCamel(profile.langName)}(${index})`,
|
|
179
169
|
)
|
|
180
170
|
.join(',\n')};`,
|
|
181
171
|
);
|
|
@@ -198,7 +188,7 @@ function emitSyqlKotlinRunner(query: AnalyzedQuery): string[] {
|
|
|
198
188
|
);
|
|
199
189
|
const args = ['client: SyncularClient'];
|
|
200
190
|
for (const input of metadata.inputs) {
|
|
201
|
-
const name =
|
|
191
|
+
const name = snakeToCamel(input.langName);
|
|
202
192
|
if (input.kind === 'value') {
|
|
203
193
|
const type = syqlKotlinType(input.type, input.nullable);
|
|
204
194
|
if (input.required) args.push(`${name}: ${type}`);
|
|
@@ -217,7 +207,7 @@ function emitSyqlKotlinRunner(query: AnalyzedQuery): string[] {
|
|
|
217
207
|
input.profiles.find((profile) => profile.name === input.defaultProfile)
|
|
218
208
|
?.langName ?? input.defaultProfile;
|
|
219
209
|
const type = `${typeName(query.name)}${typeName(input.langName)}`;
|
|
220
|
-
args.push(`${name}: ${type} = ${type}.${
|
|
210
|
+
args.push(`${name}: ${type} = ${type}.${snakeToCamel(defaultCase)}`);
|
|
221
211
|
} else {
|
|
222
212
|
args.push(`${name}: Long? = null`);
|
|
223
213
|
}
|
|
@@ -225,7 +215,7 @@ function emitSyqlKotlinRunner(query: AnalyzedQuery): string[] {
|
|
|
225
215
|
lines.push(` fun ${query.name}(${args.join(', ')}): List<${Row}> {`);
|
|
226
216
|
const page = metadata.inputs.find((input) => input.kind === 'limit');
|
|
227
217
|
if (page?.kind === 'limit') {
|
|
228
|
-
const name =
|
|
218
|
+
const name = snakeToCamel(page.langName);
|
|
229
219
|
lines.push(
|
|
230
220
|
` val effective${typeName(name)} = ${name} ?: ${page.defaultSize}L`,
|
|
231
221
|
` if (effective${typeName(name)} < 1L || effective${typeName(name)} > ${page.maxSize}L) {`,
|
|
@@ -244,7 +234,7 @@ function emitSyqlKotlinRunner(query: AnalyzedQuery): string[] {
|
|
|
244
234
|
const sort = metadata.inputs.find((input) => input.kind === 'sort');
|
|
245
235
|
const profileCount = sort?.kind === 'sort' ? sort.profiles.length : 1;
|
|
246
236
|
const sortIndex =
|
|
247
|
-
sort?.kind === 'sort' ? `${
|
|
237
|
+
sort?.kind === 'sort' ? `${snakeToCamel(sort.langName)}.index` : '0';
|
|
248
238
|
const index =
|
|
249
239
|
metadata.plan.backend === 'variants'
|
|
250
240
|
? `activationMask * ${profileCount} + ${sortIndex}`
|
|
@@ -278,14 +268,14 @@ function emitDataClass(query: AnalyzedQuery): string[] {
|
|
|
278
268
|
for (const column of query.columns) {
|
|
279
269
|
const opt = column.nullable ? '?' : '';
|
|
280
270
|
lines.push(
|
|
281
|
-
` val ${
|
|
271
|
+
` val ${snakeToCamel(column.langName)}: ${KOTLIN_TYPE[column.type]}${opt},`,
|
|
282
272
|
);
|
|
283
273
|
}
|
|
284
274
|
lines.push(') {');
|
|
285
275
|
lines.push(' companion object {');
|
|
286
276
|
lines.push(` fun fromRow(row: JsonValue): ${Row}? {`);
|
|
287
277
|
for (const column of query.columns) {
|
|
288
|
-
const name =
|
|
278
|
+
const name = snakeToCamel(column.langName);
|
|
289
279
|
const accessor = rowAccessor(column);
|
|
290
280
|
if (column.nullable) {
|
|
291
281
|
lines.push(` val ${name} = ${accessor}`);
|
|
@@ -293,7 +283,7 @@ function emitDataClass(query: AnalyzedQuery): string[] {
|
|
|
293
283
|
lines.push(` val ${name} = ${accessor} ?: return null`);
|
|
294
284
|
}
|
|
295
285
|
}
|
|
296
|
-
const args = query.columns.map((c) =>
|
|
286
|
+
const args = query.columns.map((c) => snakeToCamel(c.langName)).join(', ');
|
|
297
287
|
lines.push(` return ${Row}(${args})`);
|
|
298
288
|
lines.push(' }');
|
|
299
289
|
lines.push(' }');
|
|
@@ -315,7 +305,7 @@ function emitRunner(query: AnalyzedQuery): string[] {
|
|
|
315
305
|
lines.push(` /** Run the ${query.name} named query (SELECT-only). */`);
|
|
316
306
|
const args: string[] = [];
|
|
317
307
|
for (const p of query.params) {
|
|
318
|
-
const name =
|
|
308
|
+
const name = snakeToCamel(p.langName);
|
|
319
309
|
args.push(`${name}: ${KOTLIN_TYPE[p.type]}`);
|
|
320
310
|
}
|
|
321
311
|
const signature =
|
|
@@ -327,7 +317,7 @@ function emitRunner(query: AnalyzedQuery): string[] {
|
|
|
327
317
|
if (query.params.length > 0) {
|
|
328
318
|
const binds = query.params
|
|
329
319
|
.map((p) => {
|
|
330
|
-
const name =
|
|
320
|
+
const name = snakeToCamel(p.langName);
|
|
331
321
|
return paramValue(p.type, name);
|
|
332
322
|
})
|
|
333
323
|
.join(', ');
|
package/src/emit-queries-rust.ts
CHANGED
|
@@ -6,12 +6,13 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import type { IrColumnType } from './ir';
|
|
8
8
|
import { buildRustNamingMap, rustPascalCase, rustSnakeCase } from './naming';
|
|
9
|
-
import
|
|
10
|
-
AnalyzedQuery,
|
|
11
|
-
QueryColumn,
|
|
12
|
-
QueryParam,
|
|
13
|
-
QuerySyqlPlanBind,
|
|
14
|
-
QuerySyqlPublicInput,
|
|
9
|
+
import {
|
|
10
|
+
type AnalyzedQuery,
|
|
11
|
+
type QueryColumn,
|
|
12
|
+
type QueryParam,
|
|
13
|
+
type QuerySyqlPlanBind,
|
|
14
|
+
type QuerySyqlPublicInput,
|
|
15
|
+
syqlInput,
|
|
15
16
|
} from './query';
|
|
16
17
|
|
|
17
18
|
const RUST_TYPE: Readonly<Record<IrColumnType, string>> = {
|
|
@@ -58,12 +59,6 @@ function rustType(type: IrColumnType, nullable = false): string {
|
|
|
58
59
|
return nullable ? `Option<${base}>` : base;
|
|
59
60
|
}
|
|
60
61
|
|
|
61
|
-
function syqlInput(query: AnalyzedQuery, name: string): QuerySyqlPublicInput {
|
|
62
|
-
const input = query.syql?.inputs.find((candidate) => candidate.name === name);
|
|
63
|
-
if (input === undefined) throw new Error(`unknown SYQL input ${name}`);
|
|
64
|
-
return input;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
62
|
function validateQueryNames(query: AnalyzedQuery): void {
|
|
68
63
|
buildRustNamingMap([query.name], query.file, 'query module');
|
|
69
64
|
buildRustNamingMap(
|