Skip to content

Commit f3b9ae5

Browse files
committed
packaging in a schema registry
1 parent 2ce6da2 commit f3b9ae5

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

src/tools/schema2kotlin.ts

+38-15
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ package ${this.scope}
5656
import arcs.sdk.*
5757
import arcs.core.data.*
5858
${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'}
59+
60+
object SchemaRegistry {
61+
var schemas: Map<String, Schema> = mutableMapOf()
62+
}
5963
`;
6064
}
6165

@@ -148,6 +152,8 @@ class KotlinGenerator implements ClassGenerator {
148152
singletonSchemaFields: string[] = [];
149153
collectionSchemaFields: string[] = [];
150154

155+
public schemaRegistry: string[];
156+
151157
constructor(readonly node: SchemaNode, private readonly opts: minimist.ParsedArgs) {}
152158

153159
// TODO: allow optional fields in kotlin
@@ -192,6 +198,32 @@ class KotlinGenerator implements ClassGenerator {
192198

193199
}
194200

201+
private mapOf(items: string[], indent: number): string {
202+
if (items.length === 0) return `emptyMap()`;
203+
204+
return `mapOf(${items.join(',\n' + ' '.repeat(indent))})`;
205+
}
206+
207+
createSchema(schemaHash: string): string {
208+
const schemaNames = this.node.schema.names.map(n => `SchemaName("${n}")`);
209+
return `\
210+
Schema(
211+
listOf(${schemaNames.join(',\n' + ' '.repeat(8))}),
212+
SchemaFields(
213+
singletons = ${this.mapOf(this.singletonSchemaFields, 12)},
214+
collections = ${this.mapOf(this.collectionSchemaFields, 12)}
215+
),
216+
"${schemaHash}"
217+
)`;
218+
}
219+
220+
leftPad(input: string, indent: number) {
221+
return input
222+
.split('\n')
223+
.map(line => ' '.repeat(indent) + line)
224+
.join('\n');
225+
}
226+
195227
generate(schemaHash: string, fieldCount: number): string {
196228
const {name, aliases} = this.node;
197229

@@ -205,8 +237,6 @@ class KotlinGenerator implements ClassGenerator {
205237
const withFields = (populate: string) => fieldCount === 0 ? '' : populate;
206238
const withoutFields = (populate: string) => fieldCount === 0 ? populate : '';
207239

208-
const schemaNames = this.node.schema.names.map(n => `SchemaName("${n}")`);
209-
210240
return `\
211241
212242
class ${name}() : ${this.getType('Entity')} {
@@ -266,21 +296,14 @@ ${this.opts.wasm ? `
266296
override fun toString() = "${name}(${this.fieldsForToString.join(', ')})"
267297
}
268298
299+
269300
class ${name}_Spec() : ${this.getType('EntitySpec')}<${name}> {
270301
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-
)
302+
init {
303+
SchemaRegistry.schemas += mapOf(
304+
"${schemaHash}" to
305+
${this.leftPad(this.createSchema(schemaHash), 12)}
306+
)
284307
}
285308
286309
override fun create() = ${name}()

0 commit comments

Comments
 (0)