Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
chore: misc stuff (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
brizzbuzz authored Jan 11, 2023
1 parent 15177b7 commit 0276025
Show file tree
Hide file tree
Showing 18 changed files with 433 additions and 386 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.squareup.kotlinpoet.ksp.toClassName
import com.squareup.kotlinpoet.ksp.toTypeName
import io.bkbn.lerasium.core.Domain
import io.bkbn.lerasium.core.Relation
import io.bkbn.lerasium.utils.KotlinPoetUtils.isEnum
import io.bkbn.lerasium.utils.KotlinPoetUtils.isSupportedScalar
import io.bkbn.lerasium.utils.KotlinPoetUtils.toParameter
import io.bkbn.lerasium.utils.KotlinPoetUtils.toProperty
Expand All @@ -32,6 +33,7 @@ import io.bkbn.lerasium.utils.LerasiumUtils.getDomain
import io.bkbn.lerasium.utils.LerasiumUtils.getDomainOrNull
import io.bkbn.lerasium.utils.LerasiumUtils.isCollection
import io.bkbn.lerasium.utils.LerasiumUtils.isDomain
import kotlinx.serialization.Serializable

class RootDomainVisitor(private val fileBuilder: FileSpec.Builder, private val logger: KSPLogger) : KSVisitorVoid() {
private lateinit var containingFile: KSFile
Expand Down Expand Up @@ -62,6 +64,7 @@ class RootDomainVisitor(private val fileBuilder: FileSpec.Builder, private val l
.filterNot { it.type.isSupportedScalar() }
.filterNot { it.type.isCollection() }
.filterNot { (it.type.resolve().declaration as KSClassDeclaration).isAnnotationPresent(Domain::class) }
.filterNot { it.type.isEnum() }
.forEach {
nestedDomainVisitor.visitTypeReference(
it.type, NestedDomainVisitor.Data(parentCharter = charter)
Expand All @@ -71,91 +74,77 @@ class RootDomainVisitor(private val fileBuilder: FileSpec.Builder, private val l
}

private fun TypeSpec.Builder.domainPrimaryConstructor(charter: LerasiumCharter) {
val properties = charter.classDeclaration.getAllProperties()
.filter {
it.type.isDomain()
|| it.type.isSupportedScalar()
|| (it.type.isCollection() && it.type.getCollectionType().isDomain())
}
val nestedProps = charter.classDeclaration.getAllProperties()
.filterNot { it.type.isSupportedScalar() }
.filterNot { it.isAnnotationPresent(Relation::class) }
.filterNot { it.type.isDomain() }
.filterNot { it.type.isCollection() && it.type.getCollectionType().isDomain() }
val properties = charter.classDeclaration.collectProperties()
primaryConstructor(FunSpec.constructorBuilder().apply {
properties.forEach {
val param = when (it.type.isSupportedScalar()) {
true -> it.toParameter()
false -> {
val n = it.simpleName.getShortName()
val domain = it.getDomainOrNull()
if (domain != null) {
val domainType = it.getDomainType()
ParameterSpec.builder(n, domainType).build()
} else {
val tn = it.type.resolve().declaration.simpleName.getShortName()
val pn = charter.classDeclaration.toClassName().canonicalName
val t = ClassName(pn, tn)
ParameterSpec.builder(n, t).build()
}
}
properties.scalars.map { it.toParameter() }.forEach { addParameter(it) }
properties.domain.map {
val n = it.simpleName.getShortName()
val domain = it.getDomainOrNull()
if (domain != null) {
val domainType = it.getDomainType()
ParameterSpec.builder(n, domainType).build()
} else {
val tn = it.type.resolve().declaration.simpleName.getShortName()
val pn = charter.classDeclaration.toClassName().canonicalName
val t = ClassName(pn, tn)
ParameterSpec.builder(n, t).build()
}
addParameter(param)
}
nestedProps.forEach { prop ->
}.forEach { addParameter(it) }
properties.nested.map { prop ->
val n = prop.simpleName.getShortName()
val t = prop.type.resolve().toClassName()
addParameter(ParameterSpec.builder(n, t).build())
}
ParameterSpec.builder(n, t).build()
}.forEach { addParameter(it) }
properties.enums.map { prop ->
val n = prop.simpleName.getShortName()
val t = prop.type.resolve().toClassName()
// TODO Check if serializable
ParameterSpec.builder(n, t).build()
}.forEach { addParameter(it) }
}.build())
}

@Suppress("NestedBlockDepth")
private fun TypeSpec.Builder.domainProperties(charter: LerasiumCharter) {
val properties = charter.classDeclaration.getAllProperties()
.filter {
it.type.isDomain()
|| it.type.isSupportedScalar()
|| (it.type.isCollection() && it.type.getCollectionType().isDomain())
}
val nestedProps = charter.classDeclaration.getAllProperties()
.filterNot { it.type.isSupportedScalar() }
.filterNot { it.isAnnotationPresent(Relation::class) }
.filterNot { it.type.isDomain() }
.filterNot { it.type.isCollection() && it.type.getCollectionType().isDomain() }
properties.forEach {
val prop = when (it.type.isSupportedScalar()) {
true -> it.toProperty(isOverride = true, serializable = false)
false -> {
val n = it.simpleName.getShortName()
val domain = it.getDomainOrNull()
if (domain != null) {
val domainType = it.getDomainType()
PropertySpec.builder(n, domainType).apply {
addModifiers(KModifier.OVERRIDE)
initializer(n)
}.build()
} else {
val tn = it.type.resolve().declaration.simpleName.getShortName()
val pn = charter.classDeclaration.toClassName().canonicalName
val t = ClassName(pn, tn)
PropertySpec.builder(n, t).apply {
addModifiers(KModifier.OVERRIDE)
initializer(n)
}.build()
}
}
val properties = charter.classDeclaration.collectProperties()
properties.scalars.map { it.toProperty(isOverride = true, serializable = false) }.forEach { addProperty(it) }
properties.domain.map {
val n = it.simpleName.getShortName()
val domain = it.getDomainOrNull()
if (domain != null) {
val domainType = it.getDomainType()
PropertySpec.builder(n, domainType).apply {
addModifiers(KModifier.OVERRIDE)
initializer(n)
}.build()
} else {
// TODO Does this ever get hit?
val tn = it.type.resolve().declaration.simpleName.getShortName()
val pn = charter.classDeclaration.toClassName().canonicalName
val t = ClassName(pn, tn)
PropertySpec.builder(n, t).apply {
addModifiers(KModifier.OVERRIDE)
initializer(n)
}.build()
}
addProperty(prop)
}
nestedProps.forEach { prop ->
}.forEach { addProperty(it) }
properties.nested.map { prop ->
val n = prop.simpleName.getShortName()
val t = prop.type.resolve().toClassName()
addProperty(PropertySpec.builder(n, t).apply {
PropertySpec.builder(n, t).apply {
addModifiers(KModifier.OVERRIDE)
initializer(n)
}.build())
}
}.build()
}.forEach { addProperty(it) }
properties.enums.map { prop ->
val n = prop.simpleName.getShortName()
val t = prop.type.resolve().toClassName()
// TODO Check if serializable
PropertySpec.builder(n, t).apply {
addModifiers(KModifier.OVERRIDE)
initializer(n)
}.build()
}.forEach { addProperty(it) }
}

private fun KSPropertyDeclaration.getDomainOrNull() = if (type.isCollection()) {
Expand All @@ -169,4 +158,27 @@ class RootDomainVisitor(private val fileBuilder: FileSpec.Builder, private val l
} else {
type.toTypeName()
}

private fun KSClassDeclaration.collectProperties(): Properties {
val scalars = getAllProperties().filter { it.type.isSupportedScalar() }
val domain = getAllProperties().filter {
it.type.isDomain() || (it.type.isCollection() && it.type.getCollectionType().isDomain())
}
// TODO Cleaner way?
val nestedProps = getAllProperties()
.filterNot { it.type.isSupportedScalar() }
.filterNot { it.isAnnotationPresent(Relation::class) }
.filterNot { it.type.isDomain() }
.filterNot { it.type.isCollection() && it.type.getCollectionType().isDomain() }
.filterNot { it.type.isEnum() }
val enums = getAllProperties().filter { it.type.isEnum() }
return Properties(scalars, domain, nestedProps, enums)
}

private data class Properties(
val scalars: Sequence<KSPropertyDeclaration>,
val domain: Sequence<KSPropertyDeclaration>,
val nested: Sequence<KSPropertyDeclaration>,
val enums: Sequence<KSPropertyDeclaration>
)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.bkbn.lerasium.core.processor

import io.bkbn.lerasium.core.processor.Specs.domainWithDeeplyNestedModel
import io.bkbn.lerasium.core.processor.Specs.domainWithEnum
import io.bkbn.lerasium.core.processor.Specs.domainWithNestedModel
import io.bkbn.lerasium.core.processor.Specs.domainWithOneToManyReference
import io.bkbn.lerasium.core.processor.Specs.domainWithSensitiveField
Expand Down Expand Up @@ -75,5 +76,14 @@ class DomainVisitorTest : DescribeSpec({
fileSnapshot = "T007__domain_simple_reference.txt"
)
}
it("Can build a domain with an enum type") {
verifyGeneratedCode(
source = domainWithEnum,
provider = DomainProcessorProvider(),
expectedFileCount = 1,
fileUnderTest = "UserDomain.kt",
fileSnapshot = "T008__domain_enum.txt"
)
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,31 @@ object Specs {
}
""".trimIndent()
)

val domainWithEnum = SourceFile.kotlin(
name = "Spec.kt",
contents = """
package test
import io.bkbn.lerasium.core.Domain
import io.bkbn.lerasium.core.Sensitive
import kotlinx.serialization.Serializable
import java.util.UUID
@Domain("User")
interface User {
val firstName: String
val lastName: String
val email: String
val gender: Gender
@Serializable
enum class Gender {
MALE,
FEMALE,
OTHER
}
}
""".trimIndent()
)
}
11 changes: 11 additions & 0 deletions core-processor/src/test/resources/T008__domain_enum.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.bkbn.lerasium.generated.domain

import kotlin.String
import test.User

public data class UserDomain(
public override val firstName: String,
public override val lastName: String,
public override val email: String,
public override val gender: User.Gender,
) : User
2 changes: 0 additions & 2 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ dependencies {
// Versions
val kotlinxSerializationVersion: String by project
val konformVersion: String by project
// val bouncerVersion: String by project

api("org.jetbrains.kotlinx:kotlinx-serialization-core:$kotlinxSerializationVersion")
api("io.konform:konform-jvm:$konformVersion")
// TODO api("io.bkbn:bouncer-core:$bouncerVersion")
}

testing {
Expand Down
42 changes: 9 additions & 33 deletions playground/src/main/kotlin/io/bkbn/lerasium/playground/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import io.bkbn.kompendium.oas.info.Info
import io.bkbn.kompendium.oas.info.License
import io.bkbn.kompendium.oas.server.Server
import io.bkbn.lerasium.generated.api.config.lerasiumConfig
import io.bkbn.lerasium.generated.api.controller.AuthorController.authorHandler
import io.bkbn.lerasium.generated.api.controller.BookController.bookHandler
import io.bkbn.lerasium.generated.api.controller.BookReviewController.bookReviewHandler
import io.bkbn.lerasium.generated.api.controller.ProfileController.profileHandler
import io.bkbn.lerasium.generated.api.controller.OrganizationController.organizationHandler
import io.bkbn.lerasium.generated.api.controller.OrganizationRoleController.organizationRoleHandler
import io.bkbn.lerasium.generated.api.controller.ProjectController.projectHandler
import io.bkbn.lerasium.generated.api.controller.RepositoryController.repositoryHandler
import io.bkbn.lerasium.generated.api.controller.UserController.userHandler
import io.bkbn.lerasium.generated.persistence.config.PostgresConfig
import io.ktor.server.application.Application
Expand All @@ -26,22 +26,10 @@ import kotlin.reflect.typeOf

fun main(args: Array<String>) {
val logger = logger("main")
logger.info { "Initializing database and performing any necessary migrations" }
PostgresConfig.database
logger.info { "Performing migrations" }
PostgresConfig.flyway.clean()

// transaction {
// val statements = SchemaUtils.createStatements(AuthorTable, BookTable, UserTable, BookReviewTable)
// println("-------------")
// statements.forEach { println(it.plus("\n")) }
// println("-------------")
// }

PostgresConfig.flyway.migrate()

// Inject some dummy data
// TODO

logger.info { "Launching API" }
EngineMain.main(args)
}
Expand Down Expand Up @@ -79,21 +67,9 @@ fun Application.module() {
routing {
redoc("The Playground")
userHandler()
bookHandler()
bookReviewHandler()
authorHandler()
profileHandler()
organizationHandler()
organizationRoleHandler()
projectHandler()
repositoryHandler()
}
}

//object Testerino {
// private val byFirstName: (String) -> Expression<Boolean> = { firstName -> UserTable.firstName eq firstName }
// private val byEmail: (String) -> Expression<Boolean> = { email -> UserTable.email eq email }
// private val composition: SqlExpressionBuilder.(String, String) -> Op<Boolean> =
// { email, firstName -> byFirstName(firstName) and byEmail(email) }
//
// fun readByEmail(email: String, firstName: String) = transaction {
// val users = UserEntity.find { composition(email, firstName) }
// users.first()
// }
//}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 0276025

Please sign in to comment.