Add documentation to definitions
This commit is contained in:
parent
8dc8db1866
commit
fcc4d87610
@ -10,6 +10,10 @@ expect class Sha512State
|
||||
expect class GenericHashState
|
||||
|
||||
expect class Crypto internal constructor() {
|
||||
/**
|
||||
* Initialize the SHA256 hash
|
||||
* returns sha 256 state
|
||||
*/
|
||||
fun crypto_hash_sha256_init(): Sha256State
|
||||
|
||||
fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray)
|
||||
|
@ -14,6 +14,10 @@ actual typealias Sha512State = Any
|
||||
actual typealias GenericHashState = Any
|
||||
|
||||
actual class Crypto internal actual constructor() {
|
||||
/**
|
||||
* Initialize the SHA256 hash
|
||||
* returns sha 256 state
|
||||
*/
|
||||
actual fun crypto_hash_sha256_init(): dynamic {
|
||||
println("Debug crypto_hash_sha256_init")
|
||||
val result = js("getSodium().crypto_hash_sha256_init()")
|
||||
|
@ -15,6 +15,10 @@ actual typealias Sha512State = Hash.State512
|
||||
actual typealias GenericHashState = ByteArray
|
||||
|
||||
actual class Crypto internal actual constructor() {
|
||||
/**
|
||||
* Initialize the SHA256 hash
|
||||
* returns sha 256 state
|
||||
*/
|
||||
actual fun crypto_hash_sha256_init(): Sha256State {
|
||||
val state = debug.test.Sha256State()
|
||||
println("Debug crypto_hash_sha256_init")
|
||||
|
@ -27,6 +27,10 @@ actual class Crypto internal actual constructor() {
|
||||
|
||||
val _emitByteArray: ByteArray = ByteArray(0)
|
||||
|
||||
/**
|
||||
* Initialize the SHA256 hash
|
||||
* returns sha 256 state
|
||||
*/
|
||||
actual fun crypto_hash_sha256_init(): Sha256State {
|
||||
val allocated = sodium_malloc(debug.test.Sha256State.size.convert())!!
|
||||
val state = allocated.reinterpret<debug.test.Sha256State>().pointed
|
||||
|
@ -25,6 +25,7 @@ class KotlinFileDefinition(
|
||||
|
||||
class ClassDefinition(
|
||||
val name: String,
|
||||
val codeDocumentation: String = "",
|
||||
val innerClasses: MutableList<InnerClassDefinition> = mutableListOf(),
|
||||
val methods: MutableList<FunctionDefinition> = mutableListOf()
|
||||
) {
|
||||
@ -46,6 +47,7 @@ class InnerClassDefinition(
|
||||
val javaName: String,
|
||||
val jsName: String,
|
||||
val nativeName: String,
|
||||
val codeDocumentation: String = "",
|
||||
val functions: MutableList<FunctionDefinition> = mutableListOf()
|
||||
)
|
||||
|
||||
@ -54,6 +56,7 @@ class FunctionDefinition(
|
||||
val javaName: String,
|
||||
val jsName: String,
|
||||
val nativeName: String,
|
||||
val codeDocumentation: String = "",
|
||||
val parameterList: MutableList<ParameterDefinition> = mutableListOf(),
|
||||
val returnType: GeneralTypeDefinition,
|
||||
val dynamicJsReturn: Boolean = false,
|
||||
@ -75,6 +78,10 @@ class ParameterDefinition(
|
||||
val specificJvmInitializer: String? = null,
|
||||
)
|
||||
|
||||
class CodeBlockDefinition(
|
||||
codeBlock: String
|
||||
)
|
||||
|
||||
interface GeneralTypeDefinition {
|
||||
val typeName: TypeName
|
||||
}
|
||||
@ -98,17 +105,24 @@ fun fileDef(name: String, body: KotlinFileDefinition.() -> Unit): KotlinFileDefi
|
||||
}
|
||||
|
||||
|
||||
fun classDef(name: String, body: ClassDefinition.() -> Unit): ClassDefinition {
|
||||
val commonClass = ClassDefinition(name)
|
||||
fun classDef(name: String, codeDocumentation: String = "",body: ClassDefinition.() -> Unit): ClassDefinition {
|
||||
val commonClass = ClassDefinition(name, codeDocumentation)
|
||||
commonClass.body()
|
||||
return commonClass
|
||||
}
|
||||
|
||||
fun codeBlock(codeBlock: String) : CodeBlockDefinition {
|
||||
val codeBlockDefinition = CodeBlockDefinition(codeBlock)
|
||||
return codeBlockDefinition
|
||||
|
||||
}
|
||||
|
||||
fun innerClassDef(
|
||||
name: String,
|
||||
javaName: String,
|
||||
jsName: String,
|
||||
nativeName: String,
|
||||
codeDocumentation: String = "",
|
||||
specificConstructor : String? = null,
|
||||
body: InnerClassDefinition.() -> Unit = {}
|
||||
): InnerClassDefinition {
|
||||
@ -116,7 +130,8 @@ fun innerClassDef(
|
||||
name,
|
||||
javaName,
|
||||
jsName,
|
||||
nativeName
|
||||
nativeName,
|
||||
codeDocumentation
|
||||
)
|
||||
genClass.body()
|
||||
return genClass
|
||||
@ -127,6 +142,7 @@ fun funcDef(
|
||||
javaName: String,
|
||||
jsName: String,
|
||||
nativeName: String,
|
||||
codeDocumentation: String = "",
|
||||
returnType: GeneralTypeDefinition,
|
||||
dynamicJsReturn: Boolean = false,
|
||||
isStateCreationFunction: Boolean = false,
|
||||
@ -138,6 +154,7 @@ fun funcDef(
|
||||
javaName,
|
||||
jsName,
|
||||
nativeName,
|
||||
codeDocumentation,
|
||||
returnType = returnType,
|
||||
dynamicJsReturn = dynamicJsReturn,
|
||||
isStateCreationFunction = isStateCreationFunction,
|
||||
@ -149,6 +166,7 @@ fun funcDef(
|
||||
|
||||
fun funcDef(
|
||||
name: String,
|
||||
codeDocumentation: String = "",
|
||||
returnType: GeneralTypeDefinition,
|
||||
dynamicJsReturn: Boolean = false,
|
||||
isStateCreationFunction: Boolean = false,
|
||||
@ -161,6 +179,7 @@ fun funcDef(
|
||||
name,
|
||||
name,
|
||||
name,
|
||||
codeDocumentation,
|
||||
returnType = returnType,
|
||||
dynamicJsReturn = dynamicJsReturn,
|
||||
isStateCreationFunction = isStateCreationFunction,
|
||||
|
@ -21,8 +21,8 @@ fun ClassDefinition.defineGenericHashFunctions() {
|
||||
|
||||
+funcDef(
|
||||
"crypto_generichash_init",
|
||||
CustomTypeDefinition(ClassName(packageName, "GenericHashState")),
|
||||
true,
|
||||
returnType = CustomTypeDefinition(ClassName(packageName, "GenericHashState")),
|
||||
dynamicJsReturn = true,
|
||||
isStateCreationFunction = true
|
||||
) {
|
||||
+ParameterDefinition(
|
||||
|
@ -19,7 +19,11 @@ fun ClassDefinition.defineHashFunctions() {
|
||||
)
|
||||
+funcDef(
|
||||
"crypto_hash_sha256_init",
|
||||
CustomTypeDefinition(ClassName(packageName, "Sha256State")),
|
||||
codeDocumentation = """
|
||||
Initialize the SHA256 hash
|
||||
returns sha 256 state
|
||||
""".trimIndent(),
|
||||
returnType = CustomTypeDefinition(ClassName(packageName, "Sha256State")),
|
||||
dynamicJsReturn = true,
|
||||
isStateCreationFunction = true
|
||||
) {
|
||||
@ -31,7 +35,7 @@ fun ClassDefinition.defineHashFunctions() {
|
||||
)
|
||||
}
|
||||
|
||||
+funcDef("crypto_hash_sha256_update", TypeDefinition.UNIT) {
|
||||
+funcDef("crypto_hash_sha256_update", returnType = TypeDefinition.UNIT) {
|
||||
+ParameterDefinition(
|
||||
"state",
|
||||
CustomTypeDefinition((withPackageName("Sha256State"))),
|
||||
@ -40,7 +44,7 @@ fun ClassDefinition.defineHashFunctions() {
|
||||
+ParameterDefinition("input", TypeDefinition.ARRAY_OF_UBYTES_LONG_SIZE)
|
||||
}
|
||||
|
||||
+funcDef("crypto_hash_sha256_final", TypeDefinition.ARRAY_OF_UBYTES, outputLengthWhenArray = 32) {
|
||||
+funcDef("crypto_hash_sha256_final", returnType = TypeDefinition.ARRAY_OF_UBYTES, outputLengthWhenArray = 32) {
|
||||
+ParameterDefinition("state", CustomTypeDefinition((withPackageName("Sha256State"))))
|
||||
+ParameterDefinition("out", TypeDefinition.ARRAY_OF_UBYTES_NO_SIZE, isActuallyAnOutputParam = true, dropParameterFromDefinition = true)
|
||||
}
|
||||
@ -56,8 +60,8 @@ fun ClassDefinition.defineHashFunctions() {
|
||||
)
|
||||
+funcDef(
|
||||
"crypto_hash_sha512_init",
|
||||
CustomTypeDefinition(ClassName(packageName, "Sha512State")),
|
||||
true,
|
||||
returnType = CustomTypeDefinition(ClassName(packageName, "Sha512State")),
|
||||
dynamicJsReturn = true,
|
||||
isStateCreationFunction = true
|
||||
) {
|
||||
+ParameterDefinition(
|
||||
@ -68,12 +72,12 @@ fun ClassDefinition.defineHashFunctions() {
|
||||
)
|
||||
}
|
||||
|
||||
+funcDef("crypto_hash_sha512_update", TypeDefinition.UNIT) {
|
||||
+funcDef("crypto_hash_sha512_update", returnType = TypeDefinition.UNIT) {
|
||||
+ParameterDefinition("state", CustomTypeDefinition((withPackageName("Sha512State"))))
|
||||
+ParameterDefinition("input", TypeDefinition.ARRAY_OF_UBYTES_LONG_SIZE)
|
||||
}
|
||||
|
||||
+funcDef("crypto_hash_sha512_final", TypeDefinition.ARRAY_OF_UBYTES, outputLengthWhenArray = 64) {
|
||||
+funcDef("crypto_hash_sha512_final", returnType = TypeDefinition.ARRAY_OF_UBYTES, outputLengthWhenArray = 64) {
|
||||
+ParameterDefinition("state", CustomTypeDefinition((withPackageName("Sha512State"))))
|
||||
+ParameterDefinition("out", TypeDefinition.ARRAY_OF_UBYTES_NO_SIZE, isActuallyAnOutputParam = true, dropParameterFromDefinition = true)
|
||||
}
|
||||
|
@ -0,0 +1,30 @@
|
||||
package com.ionspin.kotlin.crypto.generator.libsodium.definitions
|
||||
|
||||
import com.squareup.kotlinpoet.ClassName
|
||||
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic (jovanovic.ugljesa@gmail.com) on 14/Aug/2020
|
||||
*/
|
||||
fun ClassDefinition.defineSecretStreamFunctions() {
|
||||
+innerClassDef(
|
||||
"SecretStreamState",
|
||||
"com.goterl.lazycode.lazysodium.interfaces.SecretStream.State",
|
||||
"SecretStreamState",
|
||||
"crypto_hash_sha256_state"
|
||||
)
|
||||
+funcDef(
|
||||
"crypto_secretstream_xchacha20poly1305_init_push",
|
||||
returnType = TypeDefinition.ARRAY_OF_UBYTES,
|
||||
dynamicJsReturn = true,
|
||||
isStateCreationFunction = true
|
||||
) {
|
||||
+ParameterDefinition(
|
||||
"state",
|
||||
CustomTypeDefinition((withPackageName("SecretStreamState"))),
|
||||
dropParameterFromDefinition = true,
|
||||
isActuallyAnOutputParam = true
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
package com.ionspin.kotlin.crypto.generator.libsodium.definitions
|
||||
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic (jovanovic.ugljesa@gmail.com) on 14/Aug/2020
|
||||
*/
|
||||
fun ClassDefinition.defineSecretStreamFunctions() {
|
||||
|
||||
|
||||
|
||||
}
|
@ -49,7 +49,7 @@ object CommonLibsodiumGenerator {
|
||||
return innerClassBuilder.build()
|
||||
}
|
||||
|
||||
fun createCommonMethodSpec(methodDefinition: FunctionDefinition): FunSpec {
|
||||
fun createCommonMethodSpec(methodDefinition: FunctionDefinition): FunSpec.Builder {
|
||||
val methodBuilder = FunSpec.builder(methodDefinition.name)
|
||||
var actualReturnType : TypeName = Any::class.asTypeName()
|
||||
var actualReturnTypeFound : Boolean = false
|
||||
@ -69,7 +69,7 @@ object CommonLibsodiumGenerator {
|
||||
} else {
|
||||
methodBuilder.returns(methodDefinition.returnType.typeName)
|
||||
}
|
||||
return methodBuilder.build()
|
||||
return methodBuilder
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ object JsLibsodiumGenerator {
|
||||
return innerClassBuilder.build()
|
||||
}
|
||||
|
||||
fun createJsFunctionImplementation(methodDefinition: FunctionDefinition): FunSpec {
|
||||
fun createJsFunctionImplementation(methodDefinition: FunctionDefinition): FunSpec.Builder {
|
||||
val methodBuilder = FunSpec.builder(methodDefinition.name)
|
||||
|
||||
var returnModifierFound = false
|
||||
@ -121,7 +121,7 @@ object JsLibsodiumGenerator {
|
||||
}
|
||||
}
|
||||
methodBuilder.addStatement(constructJsCall.toString())
|
||||
return methodBuilder.build()
|
||||
return methodBuilder
|
||||
}
|
||||
|
||||
fun paramsToString(methodDefinition: FunctionDefinition): String {
|
||||
|
@ -55,7 +55,7 @@ object JvmLibsodiumGenerator {
|
||||
return innerClassBuilder.build()
|
||||
}
|
||||
|
||||
fun createJvmFunctionImplementation(methodDefinition: FunctionDefinition): FunSpec {
|
||||
fun createJvmFunctionImplementation(methodDefinition: FunctionDefinition): FunSpec.Builder {
|
||||
val methodBuilder = FunSpec.builder(methodDefinition.name)
|
||||
methodBuilder.modifiers += MultiplatformModifier.ACTUAL.modifierList
|
||||
var returnModifierFound = false
|
||||
@ -141,7 +141,7 @@ object JvmLibsodiumGenerator {
|
||||
}
|
||||
}
|
||||
methodBuilder.returns(methodDefinition.returnType.typeName)
|
||||
return methodBuilder.build()
|
||||
return methodBuilder
|
||||
}
|
||||
|
||||
fun createOutputParam(outputParam: ParameterDefinition, length: String?, methodBuilder: FunSpec.Builder) {
|
||||
|
@ -70,7 +70,7 @@ object NativeLibsodiumGenerator {
|
||||
return innerClassBuilder.build()
|
||||
}
|
||||
|
||||
fun createNativeFunctionImplementation(methodDefinition: FunctionDefinition): FunSpec {
|
||||
fun createNativeFunctionImplementation(methodDefinition: FunctionDefinition): FunSpec.Builder {
|
||||
val methodBuilder = FunSpec.builder(methodDefinition.name)
|
||||
methodBuilder.modifiers += MultiplatformModifier.ACTUAL.modifierList
|
||||
var returnModifierFound = false
|
||||
@ -159,7 +159,7 @@ object NativeLibsodiumGenerator {
|
||||
|
||||
methodBuilder.returns(methodDefinition.returnType.typeName)
|
||||
|
||||
return methodBuilder.build()
|
||||
return methodBuilder
|
||||
}
|
||||
|
||||
fun createStateParam(stateParameterDefinition: ParameterDefinition, methodBuilder: FunSpec.Builder) {
|
||||
|
@ -15,7 +15,7 @@ import com.squareup.kotlinpoet.TypeSpec
|
||||
fun createClass(
|
||||
classDefinition: ClassDefinition,
|
||||
multiplatformModifier: MultiplatformModifier,
|
||||
methodCreator: (FunctionDefinition) -> FunSpec
|
||||
methodCreator: (FunctionDefinition) -> FunSpec.Builder
|
||||
): TypeSpec.Builder {
|
||||
val commonClassBuilder = TypeSpec.classBuilder(classDefinition.name)
|
||||
// Ugly
|
||||
@ -29,9 +29,15 @@ fun createClass(
|
||||
commonClassBuilder.primaryConstructor(primaryConstructor.build())
|
||||
commonClassBuilder.modifiers += multiplatformModifier.modifierList
|
||||
for (methodDefinition in classDefinition.methods) {
|
||||
commonClassBuilder.addFunction(methodCreator(methodDefinition))
|
||||
val builder = methodCreator(methodDefinition)
|
||||
generateDocumentationForMethod(builder, methodDefinition)
|
||||
commonClassBuilder.addFunction(builder.build())
|
||||
}
|
||||
return commonClassBuilder
|
||||
}
|
||||
|
||||
fun generateDocumentationForMethod(builder: FunSpec.Builder, methodSpec: FunctionDefinition) {
|
||||
builder.addKdoc(methodSpec.codeDocumentation)
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user