Skip to content

Commit

Permalink
Fix subscribers_local not being optional (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
MV-GH authored Jan 23, 2024
1 parent c0193b3 commit 36621db
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ kotlin {
implementation("io.ktor:ktor-client-okhttp:$ktorVersion")
}

jvmTest.dependencies {
implementation(kotlin("reflect"))
}


jsMain.dependencies {
implementation("io.ktor:ktor-client-js:$ktorVersion")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ data class CommunityAggregates(
val users_active_week: Int,
val users_active_month: Int,
val users_active_half_year: Int,
val subscribers_local: Int?,
val subscribers_local: Int? = null,
)
42 changes: 42 additions & 0 deletions app/src/jvmTest/kotlin/DatatypesValidation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import kotlin.reflect.full.primaryConstructor
import kotlin.test.Test
import kotlin.test.fail

class DatatypesValidation {
private val versions = listOf("v0x18", "v0x19")

@Test
fun `all datatypes that are nullable should also be optional`() {
versions.flatMap { getClasses(this.javaClass.classLoader, "it/vercruysse/lemmyapi/$it/datatypes") }
.forEach { clazz ->
val kClass = clazz.kotlin
if (kClass.isData) {
val primaryConstructor = kClass.primaryConstructor
primaryConstructor?.parameters?.forEach { parameter ->
if (parameter.type.isMarkedNullable && !parameter.isOptional) {
fail("Parameter ${parameter.name} of class ${clazz.name} is nullable but not optional")
}
}
}
}
}

@Throws(Exception::class)
private fun getClasses(cl: ClassLoader, pack: String): List<Class<*>> {
val dottedPackage = pack.replace("[/]".toRegex(), ".")
val classes: MutableList<Class<*>> = ArrayList()
val upackage = cl.getResource(pack)

val dis = BufferedReader(InputStreamReader(upackage?.getContent() as InputStream))
var line: String?
while ((dis.readLine().also { line = it }) != null) {
if (line!!.endsWith(".class")) {
classes.add(Class.forName(dottedPackage + "." + line!!.substring(0, line!!.lastIndexOf('.'))))
}
}
return classes
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ POM_DEVELOPER_URL=https://github.com/MV-GH

POM_ARTIFACT_ID=lemmy-api
GROUP=it.vercruysse.lemmyapi
VERSION_NAME=0.2.4-SNAPSHOT
VERSION_NAME=0.2.5-SNAPSHOT

0 comments on commit 36621db

Please sign in to comment.