@@ -28,10 +28,10 @@ const keywords = [
28
28
] ;
29
29
30
30
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' } ,
35
35
} ;
36
36
37
37
export class Schema2Kotlin extends Schema2Base {
@@ -54,6 +54,7 @@ package ${this.scope}
54
54
// Current implementation doesn't support references or optional field detection
55
55
56
56
import arcs.sdk.*
57
+ import arcs.core.data.*
57
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' }
58
59
` ;
59
60
}
@@ -144,11 +145,13 @@ class KotlinGenerator implements ClassGenerator {
144
145
fieldSerializes : string [ ] = [ ] ;
145
146
fieldDeserializes : string [ ] = [ ] ;
146
147
fieldsForToString : string [ ] = [ ] ;
148
+ singletonSchemaFields : string [ ] = [ ] ;
149
+ collectionSchemaFields : string [ ] = [ ] ;
147
150
148
151
constructor ( readonly node : SchemaNode , private readonly opts : minimist . ParsedArgs ) { }
149
152
150
153
// 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 ) {
152
155
// TODO: support reference types in kotlin
153
156
if ( typeChar === 'R' ) return ;
154
157
@@ -181,6 +184,12 @@ class KotlinGenerator implements ClassGenerator {
181
184
this . fieldSerializes . push ( `"${ field } " to ${ fixed } .toReferencable()` ) ;
182
185
this . fieldDeserializes . push ( `${ fixed } = data.singletons["${ fixed } "].toPrimitiveValue(${ type } ::class, ${ defaultVal } )` ) ;
183
186
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
+
184
193
}
185
194
186
195
generate ( schemaHash : string , fieldCount : number ) : string {
@@ -196,6 +205,8 @@ class KotlinGenerator implements ClassGenerator {
196
205
const withFields = ( populate : string ) => fieldCount === 0 ? '' : populate ;
197
206
const withoutFields = ( populate : string ) => fieldCount === 0 ? populate : '' ;
198
207
208
+ const schemaNames = this . node . schema . names . map ( n => `SchemaName("${ n } ")` ) ;
209
+
199
210
return `\
200
211
201
212
class ${ name } () : ${ this . getType ( 'Entity' ) } {
@@ -257,6 +268,21 @@ ${this.opts.wasm ? `
257
268
258
269
class ${ name } _Spec() : ${ this . getType ( 'EntitySpec' ) } <${ name } > {
259
270
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
+
260
286
override fun create() = ${ name } ()
261
287
${ ! this . opts . wasm ? `
262
288
override fun deserialize(data: RawEntity): ${ name } {
0 commit comments