@@ -56,6 +56,10 @@ package ${this.scope}
56
56
import arcs.sdk.*
57
57
import arcs.core.data.*
58
58
${ 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
+ }
59
63
` ;
60
64
}
61
65
@@ -148,6 +152,8 @@ class KotlinGenerator implements ClassGenerator {
148
152
singletonSchemaFields : string [ ] = [ ] ;
149
153
collectionSchemaFields : string [ ] = [ ] ;
150
154
155
+ public schemaRegistry : string [ ] ;
156
+
151
157
constructor ( readonly node : SchemaNode , private readonly opts : minimist . ParsedArgs ) { }
152
158
153
159
// TODO: allow optional fields in kotlin
@@ -192,6 +198,32 @@ class KotlinGenerator implements ClassGenerator {
192
198
193
199
}
194
200
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
+
195
227
generate ( schemaHash : string , fieldCount : number ) : string {
196
228
const { name, aliases} = this . node ;
197
229
@@ -205,8 +237,6 @@ class KotlinGenerator implements ClassGenerator {
205
237
const withFields = ( populate : string ) => fieldCount === 0 ? '' : populate ;
206
238
const withoutFields = ( populate : string ) => fieldCount === 0 ? populate : '' ;
207
239
208
- const schemaNames = this . node . schema . names . map ( n => `SchemaName("${ n } ")` ) ;
209
-
210
240
return `\
211
241
212
242
class ${ name } () : ${ this . getType ( 'Entity' ) } {
@@ -266,21 +296,14 @@ ${this.opts.wasm ? `
266
296
override fun toString() = "${ name } (${ this . fieldsForToString . join ( ', ' ) } )"
267
297
}
268
298
299
+
269
300
class ${ name } _Spec() : ${ this . getType ( 'EntitySpec' ) } <${ name } > {
270
301
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
+ )
284
307
}
285
308
286
309
override fun create() = ${ name } ()
0 commit comments