@@ -20,6 +20,9 @@ class ModelToKotlinGenerator(
20
20
}
21
21
22
22
private fun FileDeclaration.generateKotlinFiles (): List <FileGenerator > {
23
+ additionalPublicImports.clear()
24
+ additionalInternalImports.clear()
25
+
23
26
return listOf (
24
27
generatePublicKotlinFile(),
25
28
generateInternalKotlinFile(),
@@ -28,12 +31,12 @@ class ModelToKotlinGenerator(
28
31
29
32
private fun FileDeclaration.generatePublicKotlinFile (): FileGenerator {
30
33
return file(codeGenerationParameters, logger = logger) {
31
- filename = name.simpleName
32
- packageName = name .packageName
33
- packagePath = name .packageName
34
+ filename = this @generatePublicKotlinFile.name
35
+ packageName = this @generatePublicKotlinFile .packageName.fullName()
36
+ packagePath = this @generatePublicKotlinFile .packageName.fullName()
34
37
35
38
dependencies.forEach { dependency ->
36
- importPackage(dependency.name. packageName)
39
+ importPackage(dependency.packageName.fullName() )
37
40
}
38
41
39
42
generatePublicDeclaredEntities(this @generatePublicKotlinFile)
@@ -46,23 +49,28 @@ class ModelToKotlinGenerator(
46
49
47
50
private fun FileDeclaration.generateInternalKotlinFile (): FileGenerator {
48
51
return file(codeGenerationParameters, logger = logger) {
49
- filename = name.simpleName
50
- packageName = name .packageName
51
- packagePath = name .packageName.packageNameSuffixed(RPC_INTERNAL_PACKAGE_SUFFIX )
52
+ filename = this @generateInternalKotlinFile.name
53
+ packageName = this @generateInternalKotlinFile .packageName.fullName()
54
+ packagePath = this @generateInternalKotlinFile .packageName.fullName() .packageNameSuffixed(RPC_INTERNAL_PACKAGE_SUFFIX )
52
55
53
56
fileOptIns = listOf (" ExperimentalRpcApi::class" , " InternalRpcApi::class" )
54
57
55
58
dependencies.forEach { dependency ->
56
- importPackage(dependency.name. packageName)
59
+ importPackage(dependency.packageName.fullName() )
57
60
}
58
61
59
62
generateInternalDeclaredEntities(this @generateInternalKotlinFile)
60
63
61
64
import(" kotlinx.rpc.internal.utils.*" )
65
+
66
+ additionalInternalImports.forEach {
67
+ import(it)
68
+ }
62
69
}
63
70
}
64
71
65
72
private val additionalPublicImports = mutableSetOf<String >()
73
+ private val additionalInternalImports = mutableSetOf<String >()
66
74
67
75
private fun CodeGenerator.generatePublicDeclaredEntities (fileDeclaration : FileDeclaration ) {
68
76
fileDeclaration.messageDeclarations.forEach { generatePublicMessage(it) }
@@ -79,7 +87,7 @@ class ModelToKotlinGenerator(
79
87
}
80
88
81
89
private fun MessageDeclaration.fields () = actualFields.map {
82
- it.transformToFieldDeclaration() to it.type.defaultValue
90
+ it.transformToFieldDeclaration() to it.type
83
91
}
84
92
85
93
@Suppress(" detekt.CyclomaticComplexMethod" )
@@ -88,8 +96,8 @@ class ModelToKotlinGenerator(
88
96
name = declaration.name.simpleName,
89
97
declarationType = DeclarationType .Interface ,
90
98
) {
91
- declaration.fields().forEach {
92
- code(" val ${it.first} " )
99
+ declaration.fields().forEach { (fieldDeclaration, _) ->
100
+ code(" val $fieldDeclaration " )
93
101
newLine()
94
102
}
95
103
@@ -119,8 +127,15 @@ class ModelToKotlinGenerator(
119
127
declarationType = DeclarationType .Class ,
120
128
superTypes = listOf (declaration.name.simpleName),
121
129
) {
122
- declaration.fields().forEach {
123
- code(" override var ${it.first} = ${it.second} " )
130
+ declaration.fields().forEach { (fieldDeclaration, type) ->
131
+ val value = if (type is FieldType .Reference ) {
132
+ additionalInternalImports.add(" kotlin.properties.Delegates" )
133
+ " by Delegates.notNull()"
134
+ } else {
135
+ " = ${type.defaultValue} "
136
+ }
137
+
138
+ code(" override var $fieldDeclaration $value " )
124
139
newLine()
125
140
}
126
141
}
@@ -135,7 +150,7 @@ class ModelToKotlinGenerator(
135
150
code(" return ${declaration.name.simpleName} Builder().apply(body)" )
136
151
}
137
152
138
- val platformType = " ${declaration.outerClassName.simpleName } .${declaration.name.simpleName} "
153
+ val platformType = " ${declaration.outerClassName.fullName() } .${declaration.name.simpleName} "
139
154
140
155
function(
141
156
name = " toPlatform" ,
@@ -164,27 +179,25 @@ class ModelToKotlinGenerator(
164
179
}
165
180
166
181
private fun FieldDeclaration.toPlatformCast (): String {
167
- val type = type as ? FieldType .IntegralType ? : return " "
168
-
169
182
return when (type) {
170
183
FieldType .IntegralType .FIXED32 -> " .toInt()"
171
184
FieldType .IntegralType .FIXED64 -> " .toLong()"
172
185
FieldType .IntegralType .UINT32 -> " .toInt()"
173
186
FieldType .IntegralType .UINT64 -> " .toLong()"
174
187
FieldType .IntegralType .BYTES -> " .let { bytes -> com.google.protobuf.ByteString.copyFrom(bytes) }"
188
+ is FieldType .Reference -> " .toPlatform()"
175
189
else -> " "
176
190
}
177
191
}
178
192
179
193
private fun FieldDeclaration.toKotlinCast (): String {
180
- val type = type as ? FieldType .IntegralType ? : return " "
181
-
182
194
return when (type) {
183
195
FieldType .IntegralType .FIXED32 -> " .toUInt()"
184
196
FieldType .IntegralType .FIXED64 -> " .toULong()"
185
197
FieldType .IntegralType .UINT32 -> " .toUInt()"
186
198
FieldType .IntegralType .UINT64 -> " .toULong()"
187
199
FieldType .IntegralType .BYTES -> " .toByteArray()"
200
+ is FieldType .Reference -> " .toKotlin()"
188
201
else -> " "
189
202
}
190
203
}
@@ -196,9 +209,10 @@ class ModelToKotlinGenerator(
196
209
private fun FieldDeclaration.typeFqName (): String {
197
210
return when (type) {
198
211
// KRPC-156 Reference Types
199
- // is FieldType.Reference -> {
200
- // type.value.simpleName
201
- // }
212
+ is FieldType .Reference -> {
213
+ val value by type.value
214
+ value.fullName()
215
+ }
202
216
203
217
is FieldType .IntegralType -> {
204
218
type.fqName.simpleName
@@ -269,10 +283,10 @@ class ModelToKotlinGenerator(
269
283
val inputType by method.inputType
270
284
val outputType by method.outputType
271
285
function(
272
- name = method.name.simpleName ,
286
+ name = method.name,
273
287
modifiers = " suspend" ,
274
- args = " message: ${inputType.name.simpleName } " ,
275
- returnType = outputType.name.simpleName ,
288
+ args = " message: ${inputType.name.fullName() } " ,
289
+ returnType = outputType.name.fullName() ,
276
290
)
277
291
}
278
292
}
@@ -284,7 +298,7 @@ class ModelToKotlinGenerator(
284
298
modifiers = " private" ,
285
299
name = " ${service.name.simpleName} Delegate" ,
286
300
declarationType = DeclarationType .Object ,
287
- superTypes = listOf (" kotlinx.rpc.grpc.descriptor.GrpcDelegate<${service.name.simpleName } >" ),
301
+ superTypes = listOf (" kotlinx.rpc.grpc.descriptor.GrpcDelegate<${service.name.fullName() } >" ),
288
302
) {
289
303
function(
290
304
name = " clientProvider" ,
@@ -298,7 +312,7 @@ class ModelToKotlinGenerator(
298
312
function(
299
313
name = " definitionFor" ,
300
314
modifiers = " override" ,
301
- args = " impl: ${service.name.simpleName } " ,
315
+ args = " impl: ${service.name.fullName() } " ,
302
316
returnType = " kotlinx.rpc.grpc.ServerServiceDefinition" ,
303
317
) {
304
318
scope(" return ${service.name.simpleName} ServerDelegate(impl).bindService()" )
@@ -311,10 +325,10 @@ class ModelToKotlinGenerator(
311
325
name = " ${service.name.simpleName} ServerDelegate" ,
312
326
declarationType = DeclarationType .Class ,
313
327
superTypes = listOf (" ${service.name.simpleName} GrpcKt.${service.name.simpleName} CoroutineImplBase()" ),
314
- constructorArgs = listOf (" private val impl: ${service.name.simpleName } " ),
328
+ constructorArgs = listOf (" private val impl: ${service.name.fullName() } " ),
315
329
) {
316
330
service.methods.forEach { method ->
317
- val grpcName = method.name.simpleName. replaceFirstChar { it.lowercase() }
331
+ val grpcName = method.name.replaceFirstChar { it.lowercase() }
318
332
319
333
val inputType by method.inputType
320
334
val outputType by method.outputType
@@ -325,7 +339,7 @@ class ModelToKotlinGenerator(
325
339
args = " request: ${inputType.toPlatformMessageType()} " ,
326
340
returnType = outputType.toPlatformMessageType(),
327
341
) {
328
- code(" return impl.${method.name.simpleName } (request.toKotlin()).toPlatform()" )
342
+ code(" return impl.${method.name} (request.toKotlin()).toPlatform()" )
329
343
}
330
344
}
331
345
}
@@ -362,9 +376,9 @@ class ModelToKotlinGenerator(
362
376
scope(" return when (call.callableName)" ) {
363
377
service.methods.forEach { method ->
364
378
val inputType by method.inputType
365
- val grpcName = method.name.simpleName. replaceFirstChar { it.lowercase() }
366
- val result = " stub.$grpcName ((message as ${inputType.name.simpleName } ).toPlatform())"
367
- code(" \" ${method.name.simpleName } \" -> $result .toKotlin() as R" )
379
+ val grpcName = method.name.replaceFirstChar { it.lowercase() }
380
+ val result = " stub.$grpcName ((message as ${inputType.name.fullName() } ).toPlatform())"
381
+ code(" \" ${method.name} \" -> $result .toKotlin() as R" )
368
382
}
369
383
370
384
code(" else -> error(\" Illegal call: \$ {call.callableName}\" )" )
@@ -384,7 +398,7 @@ class ModelToKotlinGenerator(
384
398
}
385
399
386
400
private fun MessageDeclaration.toPlatformMessageType (): String {
387
- return " ${outerClassName.simpleName } .${name.simpleName.removePrefix(name.parentNameAsPrefix) } "
401
+ return " ${outerClassName.fullName() } .${name.simpleName} "
388
402
}
389
403
}
390
404
0 commit comments