Skip to content

Fixes for cbor annotation processor. #576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.android.identity.cbor.annotation

@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.SOURCE)
@Retention(AnnotationRetention.BINARY)
annotation class CborSerializable(
/**
* Optional parameter for sealed class hierarchies to define the key that carries type id.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,14 @@ class CborSymbolProcessor(
}

emptyLine()
block("fun ${deserializerName(classDeclaration)}(dataItem: DataItem): $baseName") {
val deserializer = deserializerName(classDeclaration, true)
block("fun $deserializer(dataItem: DataItem): $baseName") {
val typeKey = getTypeKey(annotation)
line("val type = dataItem[\"$typeKey\"].asTstr")
block("return when (type)") {
for (subclass in subclasses) {
val typeId = getTypeId(classDeclaration, subclass)
line("\"$typeId\" -> ${deserializerName(subclass)}(dataItem)")
line("\"$typeId\" -> ${deserializerName(subclass, false)}(dataItem)")
}
line("else -> throw IllegalArgumentException(\"wrong type: \$type\")")
}
Expand Down Expand Up @@ -200,7 +201,8 @@ class CborSymbolProcessor(
val dataItem = varName("dataItem")

emptyLine()
block("fun ${deserializerName(classDeclaration)}($dataItem: DataItem): $baseName") {
val deserializer = deserializerName(classDeclaration, true)
block("fun $deserializer($dataItem: DataItem): $baseName") {
val constructorParameters = mutableListOf<String>()
classDeclaration.getAllProperties().forEach { property ->
val fieldName = property.simpleName.asString()
Expand Down Expand Up @@ -314,10 +316,16 @@ class CborSymbolProcessor(
return null
}

private fun deserializerName(classDeclaration: KSClassDeclaration): String {
private fun deserializerName(
classDeclaration: KSClassDeclaration, forDeclaration: Boolean): String {
val baseName = classDeclaration.simpleName.asString()
return if (hasCompanion(classDeclaration)) {
"${baseName}.Companion.fromDataItem"
if (forDeclaration) {
"${baseName}.Companion.fromDataItem"
} else {
// for call
"${baseName}.fromDataItem"
}
} else {
"${baseName}_fromDataItem"
}
Expand All @@ -334,7 +342,8 @@ class CborSymbolProcessor(
type: KSType
): String {
val declaration = type.declaration
when (declaration.qualifiedName!!.asString()) {
val qualifiedName = declaration.qualifiedName!!.asString()
when (qualifiedName) {
"kotlin.collections.Map" ->
with(codeBuilder) {
val map = varName("map")
Expand Down Expand Up @@ -388,6 +397,10 @@ class CborSymbolProcessor(
) {
"$code.name"
} else {
codeBuilder.importQualifiedName(qualifiedName)
if (findAnnotation(declaration, annotationSerializable) != null) {
codeBuilder.importFunctionName("toDataItem", declaration.packageName.asString())
}
"$code.toDataItem"
}
}
Expand Down Expand Up @@ -461,7 +474,12 @@ class CborSymbolProcessor(
"${typeRef(codeBuilder, type)}.valueOf($code.asTstr)"
} else {
codeBuilder.importQualifiedName(qualifiedName)
"${deserializerName(declaration as KSClassDeclaration)}($code)"
val deserializer = deserializerName(declaration as KSClassDeclaration, false)
if (findAnnotation(declaration, annotationSerializable) != null) {
val shortName = deserializer.substring(deserializer.lastIndexOf(".") + 1)
codeBuilder.importFunctionName(shortName, declaration.packageName.asString())
}
"${deserializer}($code)"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@ import java.io.Writer
*/
class CodeBuilder(
private val classesToImport: MutableMap<String, String> = mutableMapOf(), // simple to fully qualified name
private val functionsToImport: MutableMap<String, MutableSet<String>> = mutableMapOf(), // function name to package set
private var indentDepth: Int = 0,
private val varCounts: MutableMap<String, Int> = mutableMapOf()
) {
private val code: MutableList<Any> = mutableListOf()

/**
* Add and import for a function from a given package.
*/
fun importFunctionName(function: String, packageName: String) {
functionsToImport.computeIfAbsent(function) { mutableSetOf() }.add(packageName);
}

/**
* Add given class to import list, simple class name can then be used in the code
* to refer to it.
Expand Down Expand Up @@ -66,7 +74,8 @@ class CodeBuilder(
* called).
*/
fun insertionPoint(): CodeBuilder {
val builder = CodeBuilder(classesToImport, indentDepth, varCounts)
val builder = CodeBuilder(
classesToImport, functionsToImport, indentDepth, varCounts)
code.add(builder)
return builder
}
Expand Down Expand Up @@ -195,6 +204,11 @@ class CodeBuilder(
classesToImport.forEach { (_, qualifiedName) ->
file.write("import $qualifiedName\n")
}
functionsToImport.forEach { (functionName, packageNames) ->
packageNames.forEach { packageName ->
file.write("import $packageName.$functionName\n")
}
}
file.write("\n")
writeCodeTo(file)
file.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import com.android.identity.securearea.PassphraseConstraints
* @param length Length of the PIN
*/
data class EvidenceRequestCreatePassphrase (
// TODO: use PassphraseConstraints instead when cbor-processor is fixed
val passphraseMinLength: Int,
val passphraseMaxLength: Int,
val passphraseRequireNumerical: Boolean,
val passphraseConstraints: PassphraseConstraints,
val message: String,
val verifyMessage: String,
val assets: Map<String, ByteArray>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ class SimpleIssuingAuthorityProofingGraph {
passphraseConstraints: PassphraseConstraints,
) {
val evidenceRequest = EvidenceRequestCreatePassphrase(
passphraseMinLength = passphraseConstraints.minLength,
passphraseMaxLength = passphraseConstraints.maxLength,
passphraseRequireNumerical = passphraseConstraints.requireNumerical,
passphraseConstraints = passphraseConstraints,
message = message,
verifyMessage = verifyMessage,
assets = assets)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,7 @@ fun EvidenceRequestCreatePassphraseView(
var verifiedPassphrase by remember { mutableStateOf("") }
var showMatchErrorText by remember { mutableStateOf(false) }

val constraints = PassphraseConstraints(
minLength = evidenceRequest.passphraseMinLength,
maxLength = evidenceRequest.passphraseMaxLength,
requireNumerical = evidenceRequest.passphraseRequireNumerical,
)
val constraints = evidenceRequest.passphraseConstraints

Row(
modifier = Modifier.fillMaxWidth(),
Expand Down
Loading