Skip to content

Commit 2ce6da2

Browse files
committed
First iteration of schema gen on entities
1 parent 78bf4f0 commit 2ce6da2

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

src/tools/schema2base.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {SchemaGraph, SchemaNode} from './schema2graph.js';
1616
import {ParticleSpec} from '../runtime/particle-spec.js';
1717

1818
export interface ClassGenerator {
19-
addField(field: string, typeChar: string, isOptional: boolean, refClassName: string|null): void;
19+
addField(field: string, typeChar: string, isOptional: boolean, refClassName: string|null, isCollection: boolean): void;
2020
generate(schemaHash: string, fieldCount: number): string;
2121
}
2222

@@ -78,14 +78,20 @@ export abstract class Schema2Base {
7878
for (const [field, descriptor] of fields) {
7979
if (descriptor.kind === 'schema-primitive') {
8080
if (['Text', 'URL', 'Number', 'Boolean'].includes(descriptor.type)) {
81-
generator.addField(field, descriptor.type[0], false, null);
81+
generator.addField(field, descriptor.type[0], false, null, false);
8282
} else {
8383
throw new Error(`Schema type '${descriptor.type}' for field '${field}' is not supported`);
8484
}
8585
} else if (descriptor.kind === 'schema-reference') {
86-
generator.addField(field, 'R', false, node.refs.get(field).name);
86+
generator.addField(field, 'R', false, node.refs.get(field).name, false);
8787
} else if (descriptor.kind === 'schema-collection' && descriptor.schema.kind === 'schema-reference') {
8888
// TODO: support collections of references
89+
} else if (descriptor.kind === 'schema-collection') {
90+
const schema = descriptor.schema;
91+
if (!['Text', 'URL', 'Number', 'Boolean'].includes(schema.type)) {
92+
throw new Error(`Schema type '${schema.type}' for field '${field}' is not supported`);
93+
}
94+
generator.addField(field, schema.type[0], false, null, true);
8995
} else {
9096
throw new Error(`Schema kind '${descriptor.kind}' for field '${field}' is not supported`);
9197
}

src/tools/schema2cpp.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class CppGenerator implements ClassGenerator {
113113

114114
constructor(readonly node: SchemaNode, readonly namespace: string) {}
115115

116-
addField(field: string, typeChar: string, isOptional: boolean, refClassName: string|null) {
116+
addField(field: string, typeChar: string, isOptional: boolean, refClassName: string|null, isCollection: boolean = false) {
117117
const fixed = fixName(field);
118118
const valid = `${field}_valid_`;
119119
let {type, defaultVal, isString} = typeMap[typeChar];

src/tools/schema2kotlin.ts

+31-5
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ const keywords = [
2828
];
2929

3030
const typeMap = {
31-
'T': {type: 'String', decodeFn: 'decodeText()', defaultVal: `""`},
32-
'U': {type: 'String', decodeFn: 'decodeText()', defaultVal: `""`},
33-
'N': {type: 'Double', decodeFn: 'decodeNum()', defaultVal: '0.0'},
34-
'B': {type: 'Boolean', decodeFn: 'decodeBool()', defaultVal: 'false'},
31+
'T': {type: 'String', decodeFn: 'decodeText()', defaultVal: `""`, schemaType: 'FieldType.Text'},
32+
'U': {type: 'String', decodeFn: 'decodeText()', defaultVal: `""`, schemaType: 'FieldType.Text'},
33+
'N': {type: 'Double', decodeFn: 'decodeNum()', defaultVal: '0.0', schemaType: 'FieldType.Number'},
34+
'B': {type: 'Boolean', decodeFn: 'decodeBool()', defaultVal: 'false', schemaType: 'FieldType.Boolean'},
3535
};
3636

3737
export class Schema2Kotlin extends Schema2Base {
@@ -54,6 +54,7 @@ package ${this.scope}
5454
// Current implementation doesn't support references or optional field detection
5555
5656
import arcs.sdk.*
57+
import arcs.core.data.*
5758
${this.opts.wasm ? 'import arcs.sdk.wasm.*' : 'import arcs.core.storage.api.toPrimitiveValue\nimport arcs.core.data.RawEntity\nimport arcs.core.data.util.toReferencable\nimport arcs.core.data.util.ReferencablePrimitive'}
5859
`;
5960
}
@@ -144,11 +145,13 @@ class KotlinGenerator implements ClassGenerator {
144145
fieldSerializes: string[] = [];
145146
fieldDeserializes: string[] = [];
146147
fieldsForToString: string[] = [];
148+
singletonSchemaFields: string[] = [];
149+
collectionSchemaFields: string[] = [];
147150

148151
constructor(readonly node: SchemaNode, private readonly opts: minimist.ParsedArgs) {}
149152

150153
// TODO: allow optional fields in kotlin
151-
addField(field: string, typeChar: string, isOptional: boolean, refClassName: string|null) {
154+
addField(field: string, typeChar: string, isOptional: boolean, refClassName: string|null, isCollection: boolean = false) {
152155
// TODO: support reference types in kotlin
153156
if (typeChar === 'R') return;
154157

@@ -181,6 +184,12 @@ class KotlinGenerator implements ClassGenerator {
181184
this.fieldSerializes.push(`"${field}" to ${fixed}.toReferencable()`);
182185
this.fieldDeserializes.push(`${fixed} = data.singletons["${fixed}"].toPrimitiveValue(${type}::class, ${defaultVal})`);
183186
this.fieldsForToString.push(`${fixed} = $${fixed}`);
187+
if (isCollection) {
188+
this.collectionSchemaFields.push(`"${field}" to ${typeMap[typeChar].schemaType}`);
189+
} else {
190+
this.singletonSchemaFields.push(`"${field}" to ${typeMap[typeChar].schemaType}`);
191+
}
192+
184193
}
185194

186195
generate(schemaHash: string, fieldCount: number): string {
@@ -196,6 +205,8 @@ class KotlinGenerator implements ClassGenerator {
196205
const withFields = (populate: string) => fieldCount === 0 ? '' : populate;
197206
const withoutFields = (populate: string) => fieldCount === 0 ? populate : '';
198207

208+
const schemaNames = this.node.schema.names.map(n => `SchemaName("${n}")`);
209+
199210
return `\
200211
201212
class ${name}() : ${this.getType('Entity')} {
@@ -257,6 +268,21 @@ ${this.opts.wasm ? `
257268
258269
class ${name}_Spec() : ${this.getType('EntitySpec')}<${name}> {
259270
271+
companion object {
272+
val schema = Schema(
273+
listOf(${schemaNames.join('\n ')}),
274+
SchemaFields(
275+
singletons = mapOf(
276+
${this.singletonSchemaFields.join(',\n ')}
277+
),
278+
collections = mapOf(
279+
${this.collectionSchemaFields.join(',\n ')}
280+
)
281+
),
282+
"${schemaHash}"
283+
)
284+
}
285+
260286
override fun create() = ${name}()
261287
${!this.opts.wasm ? `
262288
override fun deserialize(data: RawEntity): ${name} {

0 commit comments

Comments
 (0)