@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
package/dist/emit-kotlin.js
CHANGED
|
@@ -1,4 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Kotlin emitter: neutral IR → one standalone `.kt` file exporting
|
|
3
|
+
*
|
|
4
|
+
* - `object <ObjectName> { val schema: JsonValue }` — the ServerSchema
|
|
5
|
+
* object: the exact `serializeSchema` JSON the TS module embeds, parsed
|
|
6
|
+
* from a raw string (fed straight into `SyncularClient.create(schema = …)`),
|
|
7
|
+
* - one `data class` per table with typed properties (per the six §2.4 column
|
|
8
|
+
* types + blob_ref/crdt) and a `fromRow(row: JsonValue)` companion factory,
|
|
9
|
+
* - a `Subscriptions` object of typed unit-template builders (SPEC §4.1).
|
|
10
|
+
*
|
|
11
|
+
* The row shape is `JsonValue` — the exact type the Kotlin wrapper's
|
|
12
|
+
* `query`/`readRows` return (each row a `JsonValue.Obj`). The header carries
|
|
13
|
+
* the IR hash so `--check` verifies freshness byte-exactly.
|
|
14
|
+
*/
|
|
15
|
+
import { appColumnType, serializeSchema, } from './ir.js';
|
|
16
|
+
import { pascalCase, snakeToCamel, stringLiteral } from './naming.js';
|
|
2
17
|
/** §2.4 column type → honest Kotlin type. `json`/`blob_ref` are the raw
|
|
3
18
|
* canonical JSON string; `bytes`/`crdt` are opaque `ByteArray`. */
|
|
4
19
|
const KOTLIN_TYPE = {
|
|
@@ -11,80 +26,9 @@ const KOTLIN_TYPE = {
|
|
|
11
26
|
blob_ref: 'String',
|
|
12
27
|
crdt: 'ByteArray',
|
|
13
28
|
};
|
|
14
|
-
function appColumnType(column) {
|
|
15
|
-
return column.encrypted === true && column.declaredType !== undefined
|
|
16
|
-
? column.declaredType
|
|
17
|
-
: column.type;
|
|
18
|
-
}
|
|
19
|
-
/** §5.11: app-side Kotlin type — declaredType for an encrypted column. */
|
|
20
|
-
function appKotlinType(column) {
|
|
21
|
-
return KOTLIN_TYPE[appColumnType(column)];
|
|
22
|
-
}
|
|
23
|
-
function pascalCase(name) {
|
|
24
|
-
return name
|
|
25
|
-
.split(/[_-]+/)
|
|
26
|
-
.filter((part) => part.length > 0)
|
|
27
|
-
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
28
|
-
.join('');
|
|
29
|
-
}
|
|
30
|
-
/** Language-facing field name — the pinned §12 naming map. */
|
|
31
|
-
function camelCase(name) {
|
|
32
|
-
return snakeToCamel(name);
|
|
33
|
-
}
|
|
34
|
-
function quote(value) {
|
|
35
|
-
return `"${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\$/g, '\\$')}"`;
|
|
36
|
-
}
|
|
37
|
-
function emitSchemaValue(ir, indent) {
|
|
38
|
-
const lines = [];
|
|
39
|
-
const i = indent;
|
|
40
|
-
lines.push(`${i}JsonValue.obj(`);
|
|
41
|
-
lines.push(`${i} "version" to JsonValue.of(${ir.schemaVersion}),`);
|
|
42
|
-
lines.push(`${i} "tables" to JsonValue.arr(listOf(`);
|
|
43
|
-
for (const table of ir.tables) {
|
|
44
|
-
lines.push(`${i} JsonValue.obj(`);
|
|
45
|
-
lines.push(`${i} "name" to JsonValue.of(${quote(table.name)}),`);
|
|
46
|
-
lines.push(`${i} "primaryKey" to JsonValue.of(${quote(table.primaryKey)}),`);
|
|
47
|
-
lines.push(`${i} "columns" to JsonValue.arr(listOf(`);
|
|
48
|
-
for (const column of table.columns) {
|
|
49
|
-
const parts = [
|
|
50
|
-
`"name" to JsonValue.of(${quote(column.name)})`,
|
|
51
|
-
`"type" to JsonValue.of(${quote(column.type)})`,
|
|
52
|
-
`"nullable" to JsonValue.of(${column.nullable})`,
|
|
53
|
-
];
|
|
54
|
-
if (column.crdtType !== undefined) {
|
|
55
|
-
parts.push(`"crdtType" to JsonValue.of(${quote(column.crdtType)})`);
|
|
56
|
-
}
|
|
57
|
-
if (column.encrypted === true) {
|
|
58
|
-
parts.push('"encrypted" to JsonValue.of(true)');
|
|
59
|
-
parts.push(`"declaredType" to JsonValue.of(${quote(column.declaredType ?? column.type)})`);
|
|
60
|
-
}
|
|
61
|
-
lines.push(`${i} JsonValue.obj(${parts.join(', ')}),`);
|
|
62
|
-
}
|
|
63
|
-
lines.push(`${i} )),`);
|
|
64
|
-
lines.push(`${i} "scopes" to JsonValue.arr(listOf(`);
|
|
65
|
-
for (const scope of table.scopes) {
|
|
66
|
-
lines.push(`${i} JsonValue.obj("pattern" to JsonValue.of(${quote(scope.pattern)}), "column" to JsonValue.of(${quote(scope.column)})),`);
|
|
67
|
-
}
|
|
68
|
-
lines.push(`${i} )),`);
|
|
69
|
-
if (table.ftsIndexes.length > 0) {
|
|
70
|
-
lines.push(`${i} "ftsIndexes" to JsonValue.arr(listOf(`);
|
|
71
|
-
for (const index of table.ftsIndexes) {
|
|
72
|
-
const columns = index.columns
|
|
73
|
-
.map((column) => `JsonValue.of(${quote(column)})`)
|
|
74
|
-
.join(', ');
|
|
75
|
-
lines.push(`${i} JsonValue.obj("name" to JsonValue.of(${quote(index.name)}), "columns" to JsonValue.arr(listOf(${columns})), "tokenize" to JsonValue.of(${quote(index.tokenize)})),`);
|
|
76
|
-
}
|
|
77
|
-
lines.push(`${i} )),`);
|
|
78
|
-
}
|
|
79
|
-
lines.push(`${i} ),`);
|
|
80
|
-
}
|
|
81
|
-
lines.push(`${i} )),`);
|
|
82
|
-
lines.push(`${i})`);
|
|
83
|
-
return lines;
|
|
84
|
-
}
|
|
85
29
|
/** The accessor lifting a §2.4 value out of a row `JsonValue`. */
|
|
86
30
|
function rowAccessor(column) {
|
|
87
|
-
const key = `row[${
|
|
31
|
+
const key = `row[${stringLiteral(column.name, 'kotlin')}]`;
|
|
88
32
|
switch (appColumnType(column)) {
|
|
89
33
|
case 'string':
|
|
90
34
|
case 'json':
|
|
@@ -107,16 +51,16 @@ function emitDataClass(table) {
|
|
|
107
51
|
lines.push(`/** One ${table.name} row (§2.4 column order). */`);
|
|
108
52
|
lines.push(`data class ${type}(`);
|
|
109
53
|
for (const column of table.columns) {
|
|
110
|
-
const kotlin =
|
|
54
|
+
const kotlin = KOTLIN_TYPE[appColumnType(column)];
|
|
111
55
|
const opt = column.nullable ? '?' : '';
|
|
112
|
-
lines.push(` val ${
|
|
56
|
+
lines.push(` val ${snakeToCamel(column.name)}: ${kotlin}${opt},`);
|
|
113
57
|
}
|
|
114
58
|
lines.push(') {');
|
|
115
59
|
lines.push(' companion object {');
|
|
116
60
|
lines.push(` /** Build from a \`query\`/\`readRows\` row. Null on a missing/mistyped non-nullable column. */`);
|
|
117
61
|
lines.push(` fun fromRow(row: JsonValue): ${type}? {`);
|
|
118
62
|
for (const column of table.columns) {
|
|
119
|
-
const name =
|
|
63
|
+
const name = snakeToCamel(column.name);
|
|
120
64
|
const accessor = rowAccessor(column);
|
|
121
65
|
if (column.nullable) {
|
|
122
66
|
lines.push(` val ${name} = ${accessor}`);
|
|
@@ -125,7 +69,7 @@ function emitDataClass(table) {
|
|
|
125
69
|
lines.push(` val ${name} = ${accessor} ?: return null`);
|
|
126
70
|
}
|
|
127
71
|
}
|
|
128
|
-
const args = table.columns.map((c) =>
|
|
72
|
+
const args = table.columns.map((c) => snakeToCamel(c.name)).join(', ');
|
|
129
73
|
lines.push(` return ${type}(${args})`);
|
|
130
74
|
lines.push(' }');
|
|
131
75
|
lines.push(' }');
|
|
@@ -133,26 +77,17 @@ function emitDataClass(table) {
|
|
|
133
77
|
return lines;
|
|
134
78
|
}
|
|
135
79
|
function emitSubscription(sub) {
|
|
136
|
-
const params = [
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
if (value.kind === 'parameter' && !params.includes(value.name)) {
|
|
140
|
-
params.push(value.name);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
}
|
|
80
|
+
const params = [
|
|
81
|
+
...new Set(sub.scopes.flatMap(({ value }) => value.kind === 'parameter' ? [value.name] : [])),
|
|
82
|
+
];
|
|
144
83
|
const lines = [];
|
|
145
|
-
lines.push(` /**
|
|
84
|
+
lines.push(` /** Unit template for the ${stringLiteral(sub.name, 'kotlin')} subscription (SPEC §4.1). */`);
|
|
146
85
|
lines.push(` object ${pascalCase(sub.name)} {`);
|
|
147
|
-
lines.push(` const val name = ${
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
const values = scope.values
|
|
153
|
-
.map((value) => value.kind === 'literal' ? quote(value.value) : camelCase(value.name))
|
|
154
|
-
.join(', ');
|
|
155
|
-
lines.push(` ${quote(scope.variable)} to listOf(${values}),`);
|
|
86
|
+
lines.push(` const val name = ${stringLiteral(sub.name, 'kotlin')}`);
|
|
87
|
+
const args = params.map((p) => `${snakeToCamel(p)}: String`).join(', ');
|
|
88
|
+
lines.push(` fun scopes(${args}): Map<String, String> = mapOf(`);
|
|
89
|
+
for (const { variable, value } of sub.scopes) {
|
|
90
|
+
lines.push(` ${stringLiteral(variable, 'kotlin')} to ${value.kind === 'literal' ? stringLiteral(value.value, 'kotlin') : snakeToCamel(value.name)},`);
|
|
156
91
|
}
|
|
157
92
|
lines.push(' )');
|
|
158
93
|
lines.push(' }');
|
|
@@ -173,11 +108,18 @@ export function emitKotlinModule(ir, hash, packageName, objectName) {
|
|
|
173
108
|
schemaLines.push('/** The generated syncular schema (SPEC §2.4, §3.1). */');
|
|
174
109
|
schemaLines.push(`object ${objectName} {`);
|
|
175
110
|
schemaLines.push(` /** ServerSchema-compatible schema — pass to \`SyncularClient.create(schema = …)\`. */`);
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
111
|
+
// A raw string has no escapes; only `$` needs its template form.
|
|
112
|
+
// trimIndent() strips the common indentation and the blank first and last
|
|
113
|
+
// lines, so the parsed string is the serializeSchema bytes.
|
|
114
|
+
schemaLines.push(' val schema: JsonValue = JsonValue.parse(');
|
|
115
|
+
schemaLines.push(' """');
|
|
116
|
+
for (const line of serializeSchema(ir).split('\n')) {
|
|
117
|
+
schemaLines.push(` ${line.replaceAll('$', () => "${'$'}")}`);
|
|
118
|
+
}
|
|
119
|
+
schemaLines.push(' """.trimIndent(),');
|
|
120
|
+
schemaLines.push(' )');
|
|
179
121
|
schemaLines.push('');
|
|
180
|
-
schemaLines.push(' /** Typed
|
|
122
|
+
schemaLines.push(' /** Typed unit-template builders per subscription (SPEC §4.1). */');
|
|
181
123
|
schemaLines.push(' object Subscriptions {');
|
|
182
124
|
if (ir.subscriptions.length === 0) {
|
|
183
125
|
schemaLines.push(' // (no subscriptions declared)');
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type AnalyzedQuery } from './query.js';
|
|
2
2
|
export declare function emitQueriesDartModule(queries: readonly AnalyzedQuery[], hash: string, irVersion: number): string;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { snakeToCamel } from './naming.js';
|
|
2
|
+
import { syqlInput, } from './query.js';
|
|
2
3
|
const DART_TYPE = {
|
|
3
4
|
string: 'String',
|
|
4
5
|
integer: 'int',
|
|
@@ -16,10 +17,6 @@ function pascalCase(name) {
|
|
|
16
17
|
.map((p) => p.charAt(0).toUpperCase() + p.slice(1))
|
|
17
18
|
.join('');
|
|
18
19
|
}
|
|
19
|
-
/** Language-facing field name — the pinned §12 naming map. */
|
|
20
|
-
function camelCase(name) {
|
|
21
|
-
return snakeToCamel(name);
|
|
22
|
-
}
|
|
23
20
|
function typeName(name) {
|
|
24
21
|
return name.charAt(0).toUpperCase() + name.slice(1);
|
|
25
22
|
}
|
|
@@ -65,18 +62,12 @@ function optionalParamValue(type, name) {
|
|
|
65
62
|
return name;
|
|
66
63
|
}
|
|
67
64
|
}
|
|
68
|
-
function syqlInput(query, name) {
|
|
69
|
-
const input = query.syql?.inputs.find((candidate) => candidate.name === name);
|
|
70
|
-
if (input === undefined)
|
|
71
|
-
throw new Error(`unknown SYQL input ${name}`);
|
|
72
|
-
return input;
|
|
73
|
-
}
|
|
74
65
|
function syqlDartType(type, nullable) {
|
|
75
66
|
return `${DART_TYPE[type]}${nullable ? '?' : ''}`;
|
|
76
67
|
}
|
|
77
68
|
function syqlControlActive(query, name) {
|
|
78
69
|
const input = syqlInput(query, name);
|
|
79
|
-
const access =
|
|
70
|
+
const access = snakeToCamel(input.langName);
|
|
80
71
|
if (input.kind === 'value' && input.default === false)
|
|
81
72
|
return access;
|
|
82
73
|
if (input.kind === 'value' && input.nullable)
|
|
@@ -93,7 +84,7 @@ function syqlBindExpr(query, bind) {
|
|
|
93
84
|
.join(' && ');
|
|
94
85
|
}
|
|
95
86
|
const input = syqlInput(query, bind.input);
|
|
96
|
-
const access =
|
|
87
|
+
const access = snakeToCamel(input.langName);
|
|
97
88
|
if (bind.kind === 'limit')
|
|
98
89
|
return `effective${typeName(access)}`;
|
|
99
90
|
if (bind.kind === 'group-member') {
|
|
@@ -102,7 +93,7 @@ function syqlBindExpr(query, bind) {
|
|
|
102
93
|
const member = input.members.find((candidate) => candidate.name === bind.member);
|
|
103
94
|
if (member === undefined)
|
|
104
95
|
throw new Error(`unknown group member ${bind.member}`);
|
|
105
|
-
const memberAccess = `${access}.${
|
|
96
|
+
const memberAccess = `${access}.${snakeToCamel(member.langName)}`;
|
|
106
97
|
const value = member.nullable
|
|
107
98
|
? optionalParamValue(member.type, memberAccess)
|
|
108
99
|
: paramValue(member.type, memberAccess);
|
|
@@ -128,16 +119,16 @@ function emitSyqlDartTypes(query) {
|
|
|
128
119
|
const name = `${typeName(query.name)}${typeName(input.langName)}`;
|
|
129
120
|
lines.push(`class ${name} {`);
|
|
130
121
|
for (const member of input.members) {
|
|
131
|
-
lines.push(` final ${syqlDartType(member.type, member.nullable)} ${
|
|
122
|
+
lines.push(` final ${syqlDartType(member.type, member.nullable)} ${snakeToCamel(member.langName)};`);
|
|
132
123
|
}
|
|
133
124
|
const args = input.members
|
|
134
|
-
.map((member) => `required this.${
|
|
125
|
+
.map((member) => `required this.${snakeToCamel(member.langName)}`)
|
|
135
126
|
.join(', ');
|
|
136
127
|
lines.push(` const ${name}({${args}});`, '}', '');
|
|
137
128
|
}
|
|
138
129
|
else if (input.kind === 'sort') {
|
|
139
130
|
const name = `${typeName(query.name)}${typeName(input.langName)}`;
|
|
140
|
-
lines.push(`enum ${name} { ${input.profiles.map((profile) =>
|
|
131
|
+
lines.push(`enum ${name} { ${input.profiles.map((profile) => snakeToCamel(profile.langName)).join(', ')} }`, '');
|
|
141
132
|
}
|
|
142
133
|
}
|
|
143
134
|
if (lines[lines.length - 1] === '')
|
|
@@ -154,7 +145,7 @@ function emitSyqlDartRunner(query) {
|
|
|
154
145
|
lines.push(`/// Tables the ${query.name} query reads (exact invalidation set).`, `const List<String> syncular${Pascal}QueryTables = [${query.tables.map(quote).join(', ')}];`, '', `/// Run the ${query.name} revision-1 SYQL query.`);
|
|
155
146
|
const args = [];
|
|
156
147
|
for (const input of metadata.inputs) {
|
|
157
|
-
const name =
|
|
148
|
+
const name = snakeToCamel(input.langName);
|
|
158
149
|
if (input.kind === 'value') {
|
|
159
150
|
const type = syqlDartType(input.type, input.nullable);
|
|
160
151
|
if (input.required)
|
|
@@ -174,7 +165,7 @@ function emitSyqlDartRunner(query) {
|
|
|
174
165
|
const defaultCase = input.profiles.find((profile) => profile.name === input.defaultProfile)
|
|
175
166
|
?.langName ?? input.defaultProfile;
|
|
176
167
|
const type = `${typeName(query.name)}${typeName(input.langName)}`;
|
|
177
|
-
args.push(`${type} ${name} = ${type}.${
|
|
168
|
+
args.push(`${type} ${name} = ${type}.${snakeToCamel(defaultCase)}`);
|
|
178
169
|
}
|
|
179
170
|
else {
|
|
180
171
|
args.push(`int? ${name}`);
|
|
@@ -184,7 +175,7 @@ function emitSyqlDartRunner(query) {
|
|
|
184
175
|
lines.push(`List<${Row}> syncular${Pascal}Query(SyncularClient client${argsClause}) {`);
|
|
185
176
|
const page = metadata.inputs.find((input) => input.kind === 'limit');
|
|
186
177
|
if (page?.kind === 'limit') {
|
|
187
|
-
const name =
|
|
178
|
+
const name = snakeToCamel(page.langName);
|
|
188
179
|
lines.push(` final effective${typeName(name)} = ${name} ?? ${page.defaultSize};`, ` if (effective${typeName(name)} < 1 || effective${typeName(name)} > ${page.maxSize}) {`, ` throw SyqlQueryInputException('SYQL_RUNTIME_INVALID_LIMIT', ${quote(`${query.name}: invalid limit`)});`, ' }');
|
|
189
180
|
}
|
|
190
181
|
if (metadata.plan.backend === 'variants') {
|
|
@@ -195,7 +186,7 @@ function emitSyqlDartRunner(query) {
|
|
|
195
186
|
}
|
|
196
187
|
const sort = metadata.inputs.find((input) => input.kind === 'sort');
|
|
197
188
|
const profileCount = sort?.kind === 'sort' ? sort.profiles.length : 1;
|
|
198
|
-
const sortIndex = sort?.kind === 'sort' ? `${
|
|
189
|
+
const sortIndex = sort?.kind === 'sort' ? `${snakeToCamel(sort.langName)}.index` : '0';
|
|
199
190
|
const index = metadata.plan.backend === 'variants'
|
|
200
191
|
? `activationMask * ${profileCount} + ${sortIndex}`
|
|
201
192
|
: sortIndex;
|
|
@@ -216,25 +207,25 @@ function emitClass(query) {
|
|
|
216
207
|
lines.push(`class ${Row} {`);
|
|
217
208
|
for (const column of query.columns) {
|
|
218
209
|
const opt = column.nullable ? '?' : '';
|
|
219
|
-
lines.push(` final ${DART_TYPE[column.type]}${opt} ${
|
|
210
|
+
lines.push(` final ${DART_TYPE[column.type]}${opt} ${snakeToCamel(column.langName)};`);
|
|
220
211
|
}
|
|
221
212
|
lines.push('');
|
|
222
213
|
const ctorParams = query.columns
|
|
223
|
-
.map((c) => `${c.nullable ? '' : 'required '}this.${
|
|
214
|
+
.map((c) => `${c.nullable ? '' : 'required '}this.${snakeToCamel(c.langName)}`)
|
|
224
215
|
.join(', ');
|
|
225
216
|
lines.push(` const ${Row}({${ctorParams}});`);
|
|
226
217
|
lines.push('');
|
|
227
218
|
lines.push(` static ${Row}? fromRow(Map<String, Object?> row) {`);
|
|
228
219
|
for (const column of query.columns) {
|
|
229
220
|
if (!column.nullable) {
|
|
230
|
-
const name =
|
|
221
|
+
const name = snakeToCamel(column.langName);
|
|
231
222
|
lines.push(` final ${name} = ${rowAccessor(column)};`);
|
|
232
223
|
lines.push(` if (${name} == null) return null;`);
|
|
233
224
|
}
|
|
234
225
|
}
|
|
235
226
|
const args = query.columns
|
|
236
227
|
.map((c) => {
|
|
237
|
-
const name =
|
|
228
|
+
const name = snakeToCamel(c.langName);
|
|
238
229
|
return c.nullable ? `${name}: ${rowAccessor(c)}` : `${name}: ${name}`;
|
|
239
230
|
})
|
|
240
231
|
.join(', ');
|
|
@@ -257,7 +248,7 @@ function emitRunner(query) {
|
|
|
257
248
|
lines.push(`/// Run the ${query.name} named query (SELECT-only).`);
|
|
258
249
|
const args = [];
|
|
259
250
|
for (const p of query.params) {
|
|
260
|
-
const name =
|
|
251
|
+
const name = snakeToCamel(p.langName);
|
|
261
252
|
args.push(`required ${DART_TYPE[p.type]} ${name}`);
|
|
262
253
|
}
|
|
263
254
|
const argsClause = args.length > 0 ? `, {${args.join(', ')}}` : '';
|
|
@@ -266,7 +257,7 @@ function emitRunner(query) {
|
|
|
266
257
|
if (query.params.length > 0) {
|
|
267
258
|
const binds = query.params
|
|
268
259
|
.map((p) => {
|
|
269
|
-
const name =
|
|
260
|
+
const name = snakeToCamel(p.langName);
|
|
270
261
|
return paramValue(p.type, name);
|
|
271
262
|
})
|
|
272
263
|
.join(', ');
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type AnalyzedQuery } from './query.js';
|
|
2
2
|
export declare function emitQueriesKotlinModule(queries: readonly AnalyzedQuery[], hash: string, irVersion: number, packageName: string, objectName: string): string;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { snakeToCamel } from './naming.js';
|
|
2
|
+
import { syqlInput, } from './query.js';
|
|
2
3
|
const KOTLIN_TYPE = {
|
|
3
4
|
string: 'String',
|
|
4
5
|
integer: 'Long',
|
|
@@ -9,10 +10,6 @@ const KOTLIN_TYPE = {
|
|
|
9
10
|
blob_ref: 'String',
|
|
10
11
|
crdt: 'ByteArray',
|
|
11
12
|
};
|
|
12
|
-
/** Language-facing field name — the pinned §12 naming map. */
|
|
13
|
-
function camelCase(name) {
|
|
14
|
-
return snakeToCamel(name);
|
|
15
|
-
}
|
|
16
13
|
function typeName(name) {
|
|
17
14
|
return name.charAt(0).toUpperCase() + name.slice(1);
|
|
18
15
|
}
|
|
@@ -66,18 +63,12 @@ function optionalParamValue(type, name) {
|
|
|
66
63
|
return `${name}?.let { JsonValue.of(it) } ?: JsonValue.Null`;
|
|
67
64
|
}
|
|
68
65
|
}
|
|
69
|
-
function syqlInput(query, name) {
|
|
70
|
-
const input = query.syql?.inputs.find((candidate) => candidate.name === name);
|
|
71
|
-
if (input === undefined)
|
|
72
|
-
throw new Error(`unknown SYQL input ${name}`);
|
|
73
|
-
return input;
|
|
74
|
-
}
|
|
75
66
|
function syqlKotlinType(type, nullable) {
|
|
76
67
|
return `${KOTLIN_TYPE[type]}${nullable ? '?' : ''}`;
|
|
77
68
|
}
|
|
78
69
|
function syqlControlActive(query, name) {
|
|
79
70
|
const input = syqlInput(query, name);
|
|
80
|
-
const access =
|
|
71
|
+
const access = snakeToCamel(input.langName);
|
|
81
72
|
if (input.kind === 'value' && input.default === false)
|
|
82
73
|
return access;
|
|
83
74
|
if (input.kind === 'value' && input.nullable) {
|
|
@@ -99,7 +90,7 @@ function syqlBindExpr(query, bind) {
|
|
|
99
90
|
return `JsonValue.of(${active})`;
|
|
100
91
|
}
|
|
101
92
|
const input = syqlInput(query, bind.input);
|
|
102
|
-
const access =
|
|
93
|
+
const access = snakeToCamel(input.langName);
|
|
103
94
|
if (bind.kind === 'limit')
|
|
104
95
|
return `JsonValue.of(effective${typeName(access)}.toDouble())`;
|
|
105
96
|
if (bind.kind === 'group-member') {
|
|
@@ -108,7 +99,7 @@ function syqlBindExpr(query, bind) {
|
|
|
108
99
|
const member = input.members.find((candidate) => candidate.name === bind.member);
|
|
109
100
|
if (member === undefined)
|
|
110
101
|
throw new Error(`unknown group member ${bind.member}`);
|
|
111
|
-
const memberName =
|
|
102
|
+
const memberName = snakeToCamel(member.langName);
|
|
112
103
|
const present = member.nullable
|
|
113
104
|
? optionalParamValue(member.type, `value.${memberName}`)
|
|
114
105
|
: paramValue(member.type, `value.${memberName}`);
|
|
@@ -134,7 +125,7 @@ function emitSyqlKotlinTypes(query) {
|
|
|
134
125
|
const name = `${typeName(query.name)}${typeName(input.langName)}`;
|
|
135
126
|
lines.push(`data class ${name}(`);
|
|
136
127
|
for (const member of input.members) {
|
|
137
|
-
lines.push(` val ${
|
|
128
|
+
lines.push(` val ${snakeToCamel(member.langName)}: ${syqlKotlinType(member.type, member.nullable)},`);
|
|
138
129
|
}
|
|
139
130
|
lines.push(')', '');
|
|
140
131
|
}
|
|
@@ -142,7 +133,7 @@ function emitSyqlKotlinTypes(query) {
|
|
|
142
133
|
const name = `${typeName(query.name)}${typeName(input.langName)}`;
|
|
143
134
|
lines.push(`enum class ${name}(val index: Int) {`);
|
|
144
135
|
lines.push(`${input.profiles
|
|
145
|
-
.map((profile, index) => ` ${
|
|
136
|
+
.map((profile, index) => ` ${snakeToCamel(profile.langName)}(${index})`)
|
|
146
137
|
.join(',\n')};`);
|
|
147
138
|
lines.push('}', '');
|
|
148
139
|
}
|
|
@@ -160,7 +151,7 @@ function emitSyqlKotlinRunner(query) {
|
|
|
160
151
|
lines.push(` val ${query.name}Tables = listOf(${query.tables.map(quote).join(', ')})`, '', ` /** Run the ${query.name} revision-1 SYQL query. */`);
|
|
161
152
|
const args = ['client: SyncularClient'];
|
|
162
153
|
for (const input of metadata.inputs) {
|
|
163
|
-
const name =
|
|
154
|
+
const name = snakeToCamel(input.langName);
|
|
164
155
|
if (input.kind === 'value') {
|
|
165
156
|
const type = syqlKotlinType(input.type, input.nullable);
|
|
166
157
|
if (input.required)
|
|
@@ -180,7 +171,7 @@ function emitSyqlKotlinRunner(query) {
|
|
|
180
171
|
const defaultCase = input.profiles.find((profile) => profile.name === input.defaultProfile)
|
|
181
172
|
?.langName ?? input.defaultProfile;
|
|
182
173
|
const type = `${typeName(query.name)}${typeName(input.langName)}`;
|
|
183
|
-
args.push(`${name}: ${type} = ${type}.${
|
|
174
|
+
args.push(`${name}: ${type} = ${type}.${snakeToCamel(defaultCase)}`);
|
|
184
175
|
}
|
|
185
176
|
else {
|
|
186
177
|
args.push(`${name}: Long? = null`);
|
|
@@ -189,7 +180,7 @@ function emitSyqlKotlinRunner(query) {
|
|
|
189
180
|
lines.push(` fun ${query.name}(${args.join(', ')}): List<${Row}> {`);
|
|
190
181
|
const page = metadata.inputs.find((input) => input.kind === 'limit');
|
|
191
182
|
if (page?.kind === 'limit') {
|
|
192
|
-
const name =
|
|
183
|
+
const name = snakeToCamel(page.langName);
|
|
193
184
|
lines.push(` val effective${typeName(name)} = ${name} ?: ${page.defaultSize}L`, ` if (effective${typeName(name)} < 1L || effective${typeName(name)} > ${page.maxSize}L) {`, ` throw SyncularQueryInputException("SYQL_RUNTIME_INVALID_LIMIT", ${quote(`${query.name}: invalid limit`)})`, ' }');
|
|
194
185
|
}
|
|
195
186
|
if (metadata.plan.backend === 'variants') {
|
|
@@ -200,7 +191,7 @@ function emitSyqlKotlinRunner(query) {
|
|
|
200
191
|
}
|
|
201
192
|
const sort = metadata.inputs.find((input) => input.kind === 'sort');
|
|
202
193
|
const profileCount = sort?.kind === 'sort' ? sort.profiles.length : 1;
|
|
203
|
-
const sortIndex = sort?.kind === 'sort' ? `${
|
|
194
|
+
const sortIndex = sort?.kind === 'sort' ? `${snakeToCamel(sort.langName)}.index` : '0';
|
|
204
195
|
const index = metadata.plan.backend === 'variants'
|
|
205
196
|
? `activationMask * ${profileCount} + ${sortIndex}`
|
|
206
197
|
: sortIndex;
|
|
@@ -221,13 +212,13 @@ function emitDataClass(query) {
|
|
|
221
212
|
lines.push(`data class ${Row}(`);
|
|
222
213
|
for (const column of query.columns) {
|
|
223
214
|
const opt = column.nullable ? '?' : '';
|
|
224
|
-
lines.push(` val ${
|
|
215
|
+
lines.push(` val ${snakeToCamel(column.langName)}: ${KOTLIN_TYPE[column.type]}${opt},`);
|
|
225
216
|
}
|
|
226
217
|
lines.push(') {');
|
|
227
218
|
lines.push(' companion object {');
|
|
228
219
|
lines.push(` fun fromRow(row: JsonValue): ${Row}? {`);
|
|
229
220
|
for (const column of query.columns) {
|
|
230
|
-
const name =
|
|
221
|
+
const name = snakeToCamel(column.langName);
|
|
231
222
|
const accessor = rowAccessor(column);
|
|
232
223
|
if (column.nullable) {
|
|
233
224
|
lines.push(` val ${name} = ${accessor}`);
|
|
@@ -236,7 +227,7 @@ function emitDataClass(query) {
|
|
|
236
227
|
lines.push(` val ${name} = ${accessor} ?: return null`);
|
|
237
228
|
}
|
|
238
229
|
}
|
|
239
|
-
const args = query.columns.map((c) =>
|
|
230
|
+
const args = query.columns.map((c) => snakeToCamel(c.langName)).join(', ');
|
|
240
231
|
lines.push(` return ${Row}(${args})`);
|
|
241
232
|
lines.push(' }');
|
|
242
233
|
lines.push(' }');
|
|
@@ -254,7 +245,7 @@ function emitRunner(query) {
|
|
|
254
245
|
lines.push(` /** Run the ${query.name} named query (SELECT-only). */`);
|
|
255
246
|
const args = [];
|
|
256
247
|
for (const p of query.params) {
|
|
257
|
-
const name =
|
|
248
|
+
const name = snakeToCamel(p.langName);
|
|
258
249
|
args.push(`${name}: ${KOTLIN_TYPE[p.type]}`);
|
|
259
250
|
}
|
|
260
251
|
const signature = args.length > 0
|
|
@@ -265,7 +256,7 @@ function emitRunner(query) {
|
|
|
265
256
|
if (query.params.length > 0) {
|
|
266
257
|
const binds = query.params
|
|
267
258
|
.map((p) => {
|
|
268
|
-
const name =
|
|
259
|
+
const name = snakeToCamel(p.langName);
|
|
269
260
|
return paramValue(p.type, name);
|
|
270
261
|
})
|
|
271
262
|
.join(', ');
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type AnalyzedQuery } from './query.js';
|
|
2
2
|
export declare function emitQueriesRustModule(queries: readonly AnalyzedQuery[], hash: string, irVersion: number, clientCrate?: string): string;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { buildRustNamingMap, rustPascalCase, rustSnakeCase } from './naming.js';
|
|
2
|
+
import { syqlInput, windowBases, } from './query.js';
|
|
2
3
|
const RUST_TYPE = {
|
|
3
4
|
string: 'String',
|
|
4
5
|
integer: 'i64',
|
|
@@ -43,12 +44,6 @@ function rustType(type, nullable = false) {
|
|
|
43
44
|
const base = RUST_TYPE[type];
|
|
44
45
|
return nullable ? `Option<${base}>` : base;
|
|
45
46
|
}
|
|
46
|
-
function syqlInput(query, name) {
|
|
47
|
-
const input = query.syql?.inputs.find((candidate) => candidate.name === name);
|
|
48
|
-
if (input === undefined)
|
|
49
|
-
throw new Error(`unknown SYQL input ${name}`);
|
|
50
|
-
return input;
|
|
51
|
-
}
|
|
52
47
|
function validateQueryNames(query) {
|
|
53
48
|
buildRustNamingMap([query.name], query.file, 'query module');
|
|
54
49
|
buildRustNamingMap(query.columns.map((column) => column.langName), query.file, `query ${query.name} projection`);
|
|
@@ -383,23 +378,26 @@ function emitDependencies(query) {
|
|
|
383
378
|
return lines;
|
|
384
379
|
}
|
|
385
380
|
function emitCoverage(query) {
|
|
386
|
-
const
|
|
387
|
-
coverage.fixedScopes.some((scope) => scope.params.length > 0));
|
|
381
|
+
const coverage = windowBases(query.reactive.coverage);
|
|
388
382
|
const lines = [
|
|
389
|
-
` pub fn coverage(${
|
|
383
|
+
` pub fn coverage(${coverage.length > 0 ? 'params' : '_params'}: &Params) -> Vec<WindowCoverage> {`,
|
|
390
384
|
' vec![',
|
|
391
385
|
];
|
|
392
|
-
for (const
|
|
393
|
-
const fixed =
|
|
394
|
-
|
|
395
|
-
.map((param) => reactiveParam(query, param))
|
|
396
|
-
.join(', ');
|
|
397
|
-
return `(${quote(scope.variable)}.to_owned(), vec![${values}])`;
|
|
398
|
-
});
|
|
399
|
-
const units = coverage.units
|
|
386
|
+
for (const entry of coverage) {
|
|
387
|
+
const fixed = entry.fixedScopes.map((scope) => `(${quote(scope.variable)}.to_owned(), ${reactiveParam(query, scope.param)})`);
|
|
388
|
+
const units = entry.units
|
|
400
389
|
.map((param) => reactiveParam(query, param))
|
|
401
390
|
.join(', ');
|
|
402
|
-
|
|
391
|
+
const fixedLine = ` fixed_scopes: UnitScopes::from([${fixed.join(', ')}]),`;
|
|
392
|
+
lines.push(' WindowCoverage {', ' base: WindowBase {', ` variable: ${quote(entry.variable)}.to_owned(),`, ...(fixed.length === 0
|
|
393
|
+
? [' fixed_scopes: UnitScopes::new(),']
|
|
394
|
+
: fixedLine.length <= 100
|
|
395
|
+
? [fixedLine]
|
|
396
|
+
: [
|
|
397
|
+
' fixed_scopes: UnitScopes::from([',
|
|
398
|
+
...fixed.map((pair) => ` ${pair},`),
|
|
399
|
+
' ]),',
|
|
400
|
+
]), ' },', ` units: vec![${units}],`, ' },');
|
|
403
401
|
}
|
|
404
402
|
lines.push(' ]', ' }');
|
|
405
403
|
return lines;
|
|
@@ -498,7 +496,8 @@ function emitSupport(clientCrate) {
|
|
|
498
496
|
'use std::error::Error;',
|
|
499
497
|
'use std::fmt;',
|
|
500
498
|
`use ${clientCrate}::{`,
|
|
501
|
-
' CoverageSnapshot, QueryOwner, QueryRow, QueryValue, SyncClient,
|
|
499
|
+
' CoverageSnapshot, QueryOwner, QueryRow, QueryValue, SyncClient, UnitScopes, WindowBase,',
|
|
500
|
+
' WindowCoverage,',
|
|
502
501
|
'};',
|
|
503
502
|
'',
|
|
504
503
|
'#[derive(Debug, Clone, PartialEq, Eq, Default)]',
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type AnalyzedQuery } from './query.js';
|
|
2
2
|
export declare function emitQueriesSwiftModule(queries: readonly AnalyzedQuery[], hash: string, irVersion: number, enumName: string): string;
|