Skip to content

Commit 46b4f17

Browse files
committed
Merge branch 'master' of github.com:PolymerLabs/arcs into kt-gen-utils
2 parents cfcf317 + 8621c58 commit 46b4f17

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

java/arcs/sdk/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ arcs_kt_jvm_library(
1818
]),
1919
visibility = ["//visibility:public"],
2020
exports = [
21+
"//java/arcs/core/data",
2122
"//java/arcs/core/data:rawentity",
2223
"//java/arcs/core/data/util:data-util",
2324
"//java/arcs/core/host",

src/tools/schema2kotlin.ts

+36-1
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,41 @@ export class KotlinGenerator implements ClassGenerator {
198198
}
199199
}
200200

201+
mapOf(items: string[]): string {
202+
switch (items.length) {
203+
case 0:
204+
return `emptyMap()`;
205+
case 1:
206+
return `mapOf(${items[0]})`;
207+
default:
208+
return `\
209+
mapOf(
210+
${this.leftPad(items.join(',\n'), 4)}
211+
)`;
212+
}
213+
214+
}
215+
216+
createSchema(schemaHash: string): string {
217+
const schemaNames = this.node.schema.names.map(n => `SchemaName("${n}")`);
218+
return `\
219+
Schema(
220+
listOf(${schemaNames.join(',\n' + ' '.repeat(8))}),
221+
SchemaFields(
222+
singletons = ${this.leftPad(this.mapOf(this.singletonSchemaFields), 8, true)},
223+
collections = ${this.leftPad(this.mapOf(this.collectionSchemaFields), 8, true)}
224+
),
225+
"${schemaHash}"
226+
)`;
227+
}
228+
229+
leftPad(input: string, indent: number, skipFirst: boolean = false) {
230+
return input
231+
.split('\n')
232+
.map((line: string, idx: number) => (idx === 0 && skipFirst) ? line : ' '.repeat(indent) + line)
233+
.join('\n');
234+
}
235+
201236

202237
createSchema(schemaHash: string): string {
203238
const schemaNames = this.node.schema.names.map(n => `SchemaName("${n}")`);
@@ -289,7 +324,7 @@ class ${name}_Spec() : ${this.getType('EntitySpec')}<${name}> {
289324
${this.opts.wasm ? '' : `\
290325
291326
companion object {
292-
val schema = ${leftPad(this.createSchema(schemaHash), 8, true)}
327+
val schema = ${this.leftPad(this.createSchema(schemaHash), 8, true)}
293328
294329
init {
295330
SchemaRegistry.register(schema)

src/tools/tests/schema2kotlin-test.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2020 Google Inc. All rights reserved.
4+
* This code may only be used under the BSD style license found at
5+
* http://polymer.github.io/LICENSE.txt
6+
* Code distributed by Google as part of this project is also
7+
* subject to an additional IP rights grant found at
8+
* http://polymer.github.io/PATENTS.txt
9+
*/
10+
11+
12+
import {assert} from '../../platform/chai-node.js';
13+
import {KotlinGenerator} from '../schema2kotlin.js';
14+
import {SchemaNode} from '../schema2graph.js';
15+
import {Schema} from '../../runtime/schema.js';
16+
17+
18+
describe('schema2wasm', () => {
19+
describe('kotlin-generator', () => {
20+
const ktGen = new KotlinGenerator(new SchemaNode(new Schema([], {}), 'dummyNode'), {arg: '', _: []});
21+
it('when no items are present, it creates an empty map', () => {
22+
const actual = ktGen.mapOf([]);
23+
24+
assert.strictEqual('emptyMap()', actual);
25+
});
26+
it('when one item is present, it creates a single-line map', () => {
27+
const actual = ktGen.mapOf([`"a" to "b"`]);
28+
29+
assert.strictEqual('mapOf("a" to "b")', actual);
30+
});
31+
it('when multiple items are present, it creates a multi-line map', () => {
32+
const actual = ktGen.mapOf([`"a" to "b"`, `"b" to "c"`]);
33+
34+
assert.strictEqual(`\
35+
mapOf(
36+
"a" to "b",
37+
"b" to "c"
38+
)`, actual);
39+
});
40+
});
41+
});

0 commit comments

Comments
 (0)