Skip to content

Commit 84d2efd

Browse files
authored
Added imports for generated serializer/deserializer functions. (#576)
Signed-off-by: Peter Sorotokin <sorotokin@gmail.com>
1 parent 5c3c06d commit 84d2efd

File tree

6 files changed

+44
-21
lines changed

6 files changed

+44
-21
lines changed

cbor-processor/src/main/kotlin/com/android/identity/cbor/annotation/CborSerializable.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.android.identity.cbor.annotation
22

33
@Target(AnnotationTarget.CLASS)
4-
@Retention(AnnotationRetention.SOURCE)
4+
@Retention(AnnotationRetention.BINARY)
55
annotation class CborSerializable(
66
/**
77
* Optional parameter for sealed class hierarchies to define the key that carries type id.

cbor-processor/src/main/kotlin/com/android/identity/cbor/processor/CborSymbolProcessor.kt

+25-7
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,14 @@ class CborSymbolProcessor(
121121
}
122122

123123
emptyLine()
124-
block("fun ${deserializerName(classDeclaration)}(dataItem: DataItem): $baseName") {
124+
val deserializer = deserializerName(classDeclaration, true)
125+
block("fun $deserializer(dataItem: DataItem): $baseName") {
125126
val typeKey = getTypeKey(annotation)
126127
line("val type = dataItem[\"$typeKey\"].asTstr")
127128
block("return when (type)") {
128129
for (subclass in subclasses) {
129130
val typeId = getTypeId(classDeclaration, subclass)
130-
line("\"$typeId\" -> ${deserializerName(subclass)}(dataItem)")
131+
line("\"$typeId\" -> ${deserializerName(subclass, false)}(dataItem)")
131132
}
132133
line("else -> throw IllegalArgumentException(\"wrong type: \$type\")")
133134
}
@@ -200,7 +201,8 @@ class CborSymbolProcessor(
200201
val dataItem = varName("dataItem")
201202

202203
emptyLine()
203-
block("fun ${deserializerName(classDeclaration)}($dataItem: DataItem): $baseName") {
204+
val deserializer = deserializerName(classDeclaration, true)
205+
block("fun $deserializer($dataItem: DataItem): $baseName") {
204206
val constructorParameters = mutableListOf<String>()
205207
classDeclaration.getAllProperties().forEach { property ->
206208
val fieldName = property.simpleName.asString()
@@ -314,10 +316,16 @@ class CborSymbolProcessor(
314316
return null
315317
}
316318

317-
private fun deserializerName(classDeclaration: KSClassDeclaration): String {
319+
private fun deserializerName(
320+
classDeclaration: KSClassDeclaration, forDeclaration: Boolean): String {
318321
val baseName = classDeclaration.simpleName.asString()
319322
return if (hasCompanion(classDeclaration)) {
320-
"${baseName}.Companion.fromDataItem"
323+
if (forDeclaration) {
324+
"${baseName}.Companion.fromDataItem"
325+
} else {
326+
// for call
327+
"${baseName}.fromDataItem"
328+
}
321329
} else {
322330
"${baseName}_fromDataItem"
323331
}
@@ -334,7 +342,8 @@ class CborSymbolProcessor(
334342
type: KSType
335343
): String {
336344
val declaration = type.declaration
337-
when (declaration.qualifiedName!!.asString()) {
345+
val qualifiedName = declaration.qualifiedName!!.asString()
346+
when (qualifiedName) {
338347
"kotlin.collections.Map" ->
339348
with(codeBuilder) {
340349
val map = varName("map")
@@ -388,6 +397,10 @@ class CborSymbolProcessor(
388397
) {
389398
"$code.name"
390399
} else {
400+
codeBuilder.importQualifiedName(qualifiedName)
401+
if (findAnnotation(declaration, annotationSerializable) != null) {
402+
codeBuilder.importFunctionName("toDataItem", declaration.packageName.asString())
403+
}
391404
"$code.toDataItem"
392405
}
393406
}
@@ -461,7 +474,12 @@ class CborSymbolProcessor(
461474
"${typeRef(codeBuilder, type)}.valueOf($code.asTstr)"
462475
} else {
463476
codeBuilder.importQualifiedName(qualifiedName)
464-
"${deserializerName(declaration as KSClassDeclaration)}($code)"
477+
val deserializer = deserializerName(declaration as KSClassDeclaration, false)
478+
if (findAnnotation(declaration, annotationSerializable) != null) {
479+
val shortName = deserializer.substring(deserializer.lastIndexOf(".") + 1)
480+
codeBuilder.importFunctionName(shortName, declaration.packageName.asString())
481+
}
482+
"${deserializer}($code)"
465483
}
466484
}
467485
}

cbor-processor/src/main/kotlin/com/android/identity/cbor/processor/CodeBuilder.kt

+15-1
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,19 @@ import java.io.Writer
1515
*/
1616
class CodeBuilder(
1717
private val classesToImport: MutableMap<String, String> = mutableMapOf(), // simple to fully qualified name
18+
private val functionsToImport: MutableMap<String, MutableSet<String>> = mutableMapOf(), // function name to package set
1819
private var indentDepth: Int = 0,
1920
private val varCounts: MutableMap<String, Int> = mutableMapOf()
2021
) {
2122
private val code: MutableList<Any> = mutableListOf()
2223

24+
/**
25+
* Add and import for a function from a given package.
26+
*/
27+
fun importFunctionName(function: String, packageName: String) {
28+
functionsToImport.computeIfAbsent(function) { mutableSetOf() }.add(packageName);
29+
}
30+
2331
/**
2432
* Add given class to import list, simple class name can then be used in the code
2533
* to refer to it.
@@ -66,7 +74,8 @@ class CodeBuilder(
6674
* called).
6775
*/
6876
fun insertionPoint(): CodeBuilder {
69-
val builder = CodeBuilder(classesToImport, indentDepth, varCounts)
77+
val builder = CodeBuilder(
78+
classesToImport, functionsToImport, indentDepth, varCounts)
7079
code.add(builder)
7180
return builder
7281
}
@@ -195,6 +204,11 @@ class CodeBuilder(
195204
classesToImport.forEach { (_, qualifiedName) ->
196205
file.write("import $qualifiedName\n")
197206
}
207+
functionsToImport.forEach { (functionName, packageNames) ->
208+
packageNames.forEach { packageName ->
209+
file.write("import $packageName.$functionName\n")
210+
}
211+
}
198212
file.write("\n")
199213
writeCodeTo(file)
200214
file.close()

wallet/src/main/java/com/android/identity/issuance/evidence/EvidenceRequestCreatePassphrase.kt

+1-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ import com.android.identity.securearea.PassphraseConstraints
1111
* @param length Length of the PIN
1212
*/
1313
data class EvidenceRequestCreatePassphrase (
14-
// TODO: use PassphraseConstraints instead when cbor-processor is fixed
15-
val passphraseMinLength: Int,
16-
val passphraseMaxLength: Int,
17-
val passphraseRequireNumerical: Boolean,
14+
val passphraseConstraints: PassphraseConstraints,
1815
val message: String,
1916
val verifyMessage: String,
2017
val assets: Map<String, ByteArray>,

wallet/src/main/java/com/android/identity/issuance/simple/SimpleIssuingAuthorityProofingGraph.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ class SimpleIssuingAuthorityProofingGraph {
7676
passphraseConstraints: PassphraseConstraints,
7777
) {
7878
val evidenceRequest = EvidenceRequestCreatePassphrase(
79-
passphraseMinLength = passphraseConstraints.minLength,
80-
passphraseMaxLength = passphraseConstraints.maxLength,
81-
passphraseRequireNumerical = passphraseConstraints.requireNumerical,
79+
passphraseConstraints = passphraseConstraints,
8280
message = message,
8381
verifyMessage = verifyMessage,
8482
assets = assets)

wallet/src/main/java/com/android/identity_credential/wallet/ui/destination/provisioncredential/EvidenceRequest.kt

+1-5
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,7 @@ fun EvidenceRequestCreatePassphraseView(
238238
var verifiedPassphrase by remember { mutableStateOf("") }
239239
var showMatchErrorText by remember { mutableStateOf(false) }
240240

241-
val constraints = PassphraseConstraints(
242-
minLength = evidenceRequest.passphraseMinLength,
243-
maxLength = evidenceRequest.passphraseMaxLength,
244-
requireNumerical = evidenceRequest.passphraseRequireNumerical,
245-
)
241+
val constraints = evidenceRequest.passphraseConstraints
246242

247243
Row(
248244
modifier = Modifier.fillMaxWidth(),

0 commit comments

Comments
 (0)