Skip to content

Commit

Permalink
@DefaultComponent should work for template components too (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
Squiry authored Aug 7, 2023
1 parent fe225be commit cc80615
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,14 @@ public static List<ComponentDeclaration> findDependencyDeclarationsFromTemplate(
d.type(),
dependencyClaim.type()
)).toList();
if (exactMatch.isEmpty()) {
return declarations;
} else {
if (!exactMatch.isEmpty()) {
return exactMatch;
}
var nonDefault = declarations.stream().filter(Predicate.not(ComponentDeclaration::isDefault)).toList();
if (!nonDefault.isEmpty()) {
return nonDefault;
}
return declarations;
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,28 @@ public interface ExampleApplication {
assertThat(g.get(draw.getNodes().get(0))).isInstanceOf(this.compileResult.loadClass("TestImpl2"));
}

@Test
public void testDefaultComponentTemplateOverride() throws ClassNotFoundException {
var draw = compile("""
public interface TestInterface <T> {}
""", """
public class TestImpl1 <T> implements TestInterface<T> {}
""", """
public class TestImpl2 <T> implements TestInterface<T> {}
""", """
@KoraApp
public interface ExampleApplication {
@Root
default String root(TestInterface<String> t) {return "";}
@DefaultComponent
default <T> TestImpl1<T> testImpl1() { return new TestImpl1<>(); }
default <T> TestImpl2<T> testImpl2() { return new TestImpl2<>(); }
}
""");
assertThat(draw.getNodes()).hasSize(2);
var g = draw.init().block();
assertThat(g.get(draw.getNodes().get(0))).isInstanceOf(this.compileResult.loadClass("TestImpl2"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,16 @@ object GraphResolutionHelper {
if (result.isEmpty()) {
return result
}
if (result.size > 1) {
val exactMatch = result.filter { it.type == dependencyClaim.type }
if (exactMatch.isEmpty()) {
return result
} else {
return exactMatch
}
if (result.size == 1) {
return result
}
val exactMatch = result.filter { it.type == dependencyClaim.type }
if (exactMatch.isNotEmpty()) {
return exactMatch
}
val nonDefault = result.filter { !it.isDefault() }
if (nonDefault.isNotEmpty()) {
return nonDefault
}
return result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,31 @@ class ConflictResolutionTest : AbstractSymbolProcessorTest() {

assertThat(compileResult.isFailed()).isFalse()
}

@Test
fun testDefaultComponentTemplateOverride() {
compile(
listOf<SymbolProcessorProvider>(KoraAppProcessorProvider()),
"""
interface TestInterface <T>
""".trimIndent(), """
class TestImpl1 <T> : TestInterface <T> {}
""".trimIndent(), """
class TestImpl2 <T> : TestInterface <T> {}
""".trimIndent(), """
@KoraApp
interface ExampleApplication {
@Root
fun root(t: TestInterface<String>) = ""
fun <T> testImpl1() = TestImpl1<T>()
@DefaultComponent
fun <T> testImpl2() = TestImpl2<T>()
}
""".trimIndent()
)

compileResult.assertSuccess()
}
}

0 comments on commit cc80615

Please sign in to comment.