Skip to content

Commit 74ae770

Browse files
committed
Improved shape of registry
1 parent f69f99b commit 74ae770

File tree

3 files changed

+50
-32
lines changed

3 files changed

+50
-32
lines changed

java/arcs/core/data/SchemaRegistry.kt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
1+
/*
2+
* Copyright 2020 Google LLC.
3+
*
4+
* This code may only be used under the BSD style license found at
5+
* http://polymer.github.io/LICENSE.txt
6+
*
7+
* Code distributed by Google as part of this project is also subject to an additional IP rights
8+
* grant found at
9+
* http://polymer.github.io/PATENTS.txt
10+
*/
111
package arcs.core.data
212

13+
/**
14+
* A registry for generated [Schema]s.
15+
*/
316
object SchemaRegistry {
4-
var schemas: MutableMap<String, Schema> = mutableMapOf()
17+
private val schemas: MutableMap<String, Schema> = mutableMapOf()
18+
19+
internal fun register(schema: Schema) {
20+
schemas[schema.hash] = schema
21+
}
22+
23+
/** Given a schema hash as a String, return the schema for that has, if it exists. */
24+
fun fromHash(hash: String) = schemas[hash]
525
}

src/tools/schema2kotlin.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {Schema2Base, ClassGenerator} from './schema2base.js';
1111
import {SchemaNode} from './schema2graph.js';
1212
import {ParticleSpec} from '../runtime/particle-spec.js';
1313
import minimist from 'minimist';
14+
import {UnifiedStore} from '../runtime/storageNG/unified-store.js';
1415

1516
// TODO: use the type lattice to generate interfaces
1617

@@ -219,10 +220,10 @@ Schema(
219220
)`;
220221
}
221222

222-
leftPad(input: string, indent: number) {
223+
leftPad(input: string, indent: number, skipFirst: boolean = false) {
223224
return input
224225
.split('\n')
225-
.map(line => ' '.repeat(indent) + line)
226+
.map((line: string, idx: number) => (idx === 0 && skipFirst) ? line : ' '.repeat(indent) + line)
226227
.join('\n');
227228
}
228229

@@ -303,11 +304,10 @@ class ${name}_Spec() : ${this.getType('EntitySpec')}<${name}> {
303304
304305
${this.opts.wasm ? '' : `\
305306
companion object {
307+
val schema = ${this.leftPad(this.createSchema(schemaHash), 12, true)}
308+
306309
init {
307-
SchemaRegistry.schemas.plusAssign(mapOf(
308-
"${schemaHash}" to
309-
${this.leftPad(this.createSchema(schemaHash), 16)}
310-
))
310+
SchemaRegistry.register(schema)
311311
}
312312
}
313313
`}

src/tools/tests/goldens/generated-schemas.jvm.kt

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,17 @@ class GoldInternal1() : Entity {
7474
class GoldInternal1_Spec() : EntitySpec<GoldInternal1> {
7575

7676
companion object {
77+
val schema = Schema(
78+
listOf(),
79+
SchemaFields(
80+
singletons = mapOf("val" to FieldType.Text),
81+
collections = emptyMap()
82+
),
83+
"485712110d89359a3e539dac987329cd2649d889"
84+
)
85+
7786
init {
78-
SchemaRegistry.schemas.plusAssign(mapOf(
79-
"485712110d89359a3e539dac987329cd2649d889" to
80-
Schema(
81-
listOf(),
82-
SchemaFields(
83-
singletons = mapOf("val" to FieldType.Text),
84-
collections = emptyMap()
85-
),
86-
"485712110d89359a3e539dac987329cd2649d889"
87-
)
88-
))
87+
SchemaRegistry.register(schema)
8988
}
9089
}
9190

@@ -199,21 +198,20 @@ class Gold_Data() : Entity {
199198
class Gold_Data_Spec() : EntitySpec<Gold_Data> {
200199

201200
companion object {
201+
val schema = Schema(
202+
listOf(),
203+
SchemaFields(
204+
singletons = mapOf("num" to FieldType.Number,
205+
"txt" to FieldType.Text,
206+
"lnk" to FieldType.Text,
207+
"flg" to FieldType.Boolean),
208+
collections = emptyMap()
209+
),
210+
"d8058d336e472da47b289eafb39733f77eadb111"
211+
)
212+
202213
init {
203-
SchemaRegistry.schemas.plusAssign(mapOf(
204-
"d8058d336e472da47b289eafb39733f77eadb111" to
205-
Schema(
206-
listOf(),
207-
SchemaFields(
208-
singletons = mapOf("num" to FieldType.Number,
209-
"txt" to FieldType.Text,
210-
"lnk" to FieldType.Text,
211-
"flg" to FieldType.Boolean),
212-
collections = emptyMap()
213-
),
214-
"d8058d336e472da47b289eafb39733f77eadb111"
215-
)
216-
))
214+
SchemaRegistry.register(schema)
217215
}
218216
}
219217

0 commit comments

Comments
 (0)