Skip to content

Commit

Permalink
don't include module in refs for own module (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
worstell authored Nov 9, 2023
1 parent 089808c commit 0b6fa08
Showing 1 changed file with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import xyz.block.ftl.Ingress
import xyz.block.ftl.Method
import xyz.block.ftl.schemaextractor.SchemaExtractor.Companion.moduleName
import xyz.block.ftl.v1.schema.*
import xyz.block.ftl.v1.schema.Array
import java.io.File
import java.io.FileOutputStream
import java.nio.file.Path
Expand Down Expand Up @@ -60,8 +61,8 @@ class ExtractSchemaRule(config: Config) : Rule(config) {
}

runCatching {
val extractor = SchemaExtractor(this.bindingContext, annotationEntry)
val moduleName = annotationEntry.containingKtFile.packageFqName.moduleName()
val extractor = SchemaExtractor(this.bindingContext, moduleName, annotationEntry)
val moduleData = extractor.extract()
modules[moduleName]?.let { it.decls += moduleData.decls }
?: run { modules[moduleName] = moduleData }
Expand Down Expand Up @@ -97,7 +98,11 @@ class ExtractSchemaRule(config: Config) : Rule(config) {
}

class IgnoredModuleException : Exception()
class SchemaExtractor(val bindingContext: BindingContext, annotation: KtAnnotationEntry) {
class SchemaExtractor(
private val bindingContext: BindingContext,
private val moduleName: String,
annotation: KtAnnotationEntry
) {
private val verb: KtNamedFunction
private val module: KtDeclaration
private val decls: MutableSet<Decl> = mutableSetOf()
Expand Down Expand Up @@ -192,7 +197,8 @@ class SchemaExtractor(val bindingContext: BindingContext, annotation: KtAnnotati
"Could not extract module name for outgoing verb call from ${verb.name}"
}
// TODO(worstell): Figure out how to get module name when not imported from another Kt file
val moduleRefName = imports.filter { it.toString().contains(req) }.firstOrNull()?.moduleName()
val moduleRefName = imports.firstOrNull { import -> import.toString().contains(req) }
?.moduleName().takeIf { refModule -> refModule != moduleName }

VerbRef(
name = verbCall.split("::")[1].trim(),
Expand Down Expand Up @@ -261,7 +267,7 @@ class SchemaExtractor(val bindingContext: BindingContext, annotation: KtAnnotati
return Type(
dataRef = DataRef(
name = this.toClassDescriptor().name.asString(),
module = this.fqNameOrNull()!!.moduleName()
module = this.fqNameOrNull()!!.moduleName().takeIf { it != moduleName } ?: "",
)
)
}
Expand All @@ -273,7 +279,6 @@ class SchemaExtractor(val bindingContext: BindingContext, annotation: KtAnnotati
?: throw IllegalStateException("Could not resolve type ${this.text}")

init {
val moduleName = annotation.containingKtFile.packageFqName.moduleName()
requireNotNull(annotation.getElementParentDeclaration()) { "Could not extract $moduleName verb definition" }.let {
require(it is KtNamedFunction) { "Verbs must be functions" }
verb = it
Expand Down

0 comments on commit 0b6fa08

Please sign in to comment.