-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/#12 WispRegistry 코드 생성 기능 구현 #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f1d30c9
add: WispClassName object to manage class names
jm991014 b4dd159
feat: WispRegistry object that holds a map of route paths to their co…
jm991014 3309ab9
chore: uses ROUTE_FACTORY constant in RouteFactoryGenerator class
jm991014 1d2227d
feat: generate WispRegistry and validates routes
jm991014 4b9ce90
add: RegistryGenerator tests
jm991014 7a7e464
fix: lint format
jm991014 488f8bc
refactor: centralize error type definitions
jm991014 c49a493
add: registry lookup function
jm991014 efd128e
refactor: move duplicate path validation logic to validator
jm991014 384ba55
fix: lint format
jm991014 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
wisp-processor/src/main/java/com/angrypodo/wisp/WispClassName.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.angrypodo.wisp | ||
|
|
||
| import com.squareup.kotlinpoet.ClassName | ||
|
|
||
| internal object WispClassName { | ||
| private const val RUNTIME_PACKAGE = "com.angrypodo.wisp.runtime" | ||
| const val GENERATED_PACKAGE = "com.angrypodo.wisp.generated" | ||
|
|
||
| val ROUTE_FACTORY = ClassName(RUNTIME_PACKAGE, "RouteFactory") | ||
|
|
||
| val MISSING_PARAMETER_ERROR = ClassName(RUNTIME_PACKAGE, "WispError", "MissingParameter") | ||
| val INVALID_PARAMETER_ERROR = ClassName(RUNTIME_PACKAGE, "WispError", "InvalidParameter") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 21 additions & 3 deletions
24
wisp-processor/src/main/java/com/angrypodo/wisp/WispValidator.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,37 @@ | ||
| package com.angrypodo.wisp | ||
|
|
||
| import com.angrypodo.wisp.model.RouteInfo | ||
|
|
||
| internal object WispValidator { | ||
| sealed interface ValidationResult { | ||
| data object Success : ValidationResult | ||
| data class Failure(val message: String) : ValidationResult | ||
| data class Failure(val errors: List<String>) : ValidationResult | ||
| } | ||
|
|
||
| fun validate(routeInfo: RouteClassInfo): ValidationResult { | ||
| if (!routeInfo.isSerializable()) { | ||
| return ValidationResult.Failure( | ||
| message = "Wisp Error: Route Class '${routeInfo.qualifiedName}' " + | ||
| "must be annotated with @Serializable." | ||
| listOf( | ||
| "Wisp Error: Route Class '${routeInfo.qualifiedName}' " + | ||
| "must be annotated with @Serializable." | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| return ValidationResult.Success | ||
| } | ||
|
|
||
| fun validateDuplicatePaths(routes: List<RouteInfo>): ValidationResult { | ||
| val duplicates = routes.groupBy { it.wispPath } | ||
| .filter { it.value.size > 1 } | ||
|
|
||
| if (duplicates.isEmpty()) return ValidationResult.Success | ||
|
|
||
| val errorMessages = duplicates.map { (path, routeInfos) -> | ||
| val conflictingClasses = routeInfos.joinToString(", ") { it.routeClassName.simpleName } | ||
| "Wisp Error: The path '$path' is already used by multiple routes: [$conflictingClasses]" | ||
| } | ||
|
|
||
| return ValidationResult.Failure(errorMessages) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
wisp-processor/src/main/java/com/angrypodo/wisp/generator/WispRegistryGenerator.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package com.angrypodo.wisp.generator | ||
|
|
||
| import com.angrypodo.wisp.WispClassName.GENERATED_PACKAGE | ||
| import com.angrypodo.wisp.WispClassName.ROUTE_FACTORY | ||
| import com.angrypodo.wisp.model.RouteInfo | ||
| import com.squareup.kotlinpoet.CodeBlock | ||
| import com.squareup.kotlinpoet.FileSpec | ||
| import com.squareup.kotlinpoet.FunSpec | ||
| import com.squareup.kotlinpoet.KModifier | ||
| import com.squareup.kotlinpoet.MAP | ||
| import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy | ||
| import com.squareup.kotlinpoet.PropertySpec | ||
| import com.squareup.kotlinpoet.STRING | ||
| import com.squareup.kotlinpoet.TypeSpec | ||
|
|
||
| internal object WispRegistryGenerator { | ||
| private const val REGISTRY_NAME = "WispRegistry" | ||
| private const val FACTORIES_PROPERTY_NAME = "factories" | ||
| private const val GET_FACTORY_FUN_NAME = "getRouteFactory" | ||
|
|
||
| fun generate(routes: List<RouteInfo>): FileSpec { | ||
| val mapType = MAP.parameterizedBy(STRING, ROUTE_FACTORY) | ||
|
|
||
| val initializerBlock = CodeBlock.builder() | ||
| .add("mapOf(\n") | ||
| .indent() | ||
|
|
||
| routes.forEach { route -> | ||
| initializerBlock.add("%S to %T,\n", route.wispPath, route.factoryClassName) | ||
| } | ||
|
|
||
| initializerBlock.unindent().add(")") | ||
|
|
||
| val factoriesProperty = PropertySpec.builder(FACTORIES_PROPERTY_NAME, mapType) | ||
| .addModifiers(KModifier.PRIVATE) | ||
| .initializer(initializerBlock.build()) | ||
| .build() | ||
|
|
||
| val getFactoryFun = FunSpec.builder(GET_FACTORY_FUN_NAME) | ||
| .addModifiers(KModifier.INTERNAL) | ||
| .addParameter("path", STRING) | ||
| .returns(ROUTE_FACTORY.copy(nullable = true)) | ||
| .addStatement("return %N[path]", factoriesProperty) | ||
| .build() | ||
|
|
||
| val registryObject = TypeSpec.objectBuilder(REGISTRY_NAME) | ||
| .addModifiers(KModifier.INTERNAL) | ||
| .addProperty(factoriesProperty) | ||
| .addFunction(getFactoryFun) | ||
| .build() | ||
|
|
||
| return FileSpec.builder(GENERATED_PACKAGE, REGISTRY_NAME) | ||
| .addType(registryObject) | ||
| .build() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
wisp-processor/src/test/java/com/angrypodo/wisp/generator/RegistryGeneratorTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package com.angrypodo.wisp.generator | ||
|
|
||
| import com.angrypodo.wisp.model.ClassRouteInfo | ||
| import com.angrypodo.wisp.model.ObjectRouteInfo | ||
| import com.squareup.kotlinpoet.ClassName | ||
| import org.junit.jupiter.api.Assertions.assertTrue | ||
| import org.junit.jupiter.api.DisplayName | ||
| import org.junit.jupiter.api.Test | ||
|
|
||
| internal class RegistryGeneratorTest { | ||
|
|
||
| @Test | ||
| @DisplayName("RouteInfo를 받아 WispRegistry 오브젝트와 맵을 생성한다") | ||
| fun `generate_registry_with_multiple_routes`() { | ||
| // Given: RouteInfo 데이터 2개 | ||
| val homeRoute = ObjectRouteInfo( | ||
| routeClassName = ClassName("com.example", "Home"), | ||
| factoryClassName = ClassName("com.example", "HomeRouteFactory"), | ||
| wispPath = "home" | ||
| ) | ||
|
|
||
| val profileRoute = ClassRouteInfo( | ||
| routeClassName = ClassName("com.example", "Profile"), | ||
| factoryClassName = ClassName("com.example", "ProfileRouteFactory"), | ||
| wispPath = "profile/{id}", | ||
| parameters = emptyList() | ||
| ) | ||
|
|
||
| val routes = listOf(homeRoute, profileRoute) | ||
|
|
||
| // When: 코드 생성 실행 | ||
| val fileSpec = WispRegistryGenerator.generate(routes) | ||
| val generatedCode = fileSpec.toString() | ||
|
|
||
| println(generatedCode) | ||
|
|
||
| // Then: 생성된 WispRegistry 객체를 반환 | ||
| assertTrue(generatedCode.contains("object WispRegistry")) | ||
| assertTrue(generatedCode.contains("val factories: Map<String, RouteFactory> = mapOf(")) | ||
|
|
||
| assertTrue(generatedCode.contains("import com.example.HomeRouteFactory")) | ||
| assertTrue(generatedCode.contains("\"home\" to HomeRouteFactory")) | ||
|
|
||
| assertTrue(generatedCode.contains("import com.example.ProfileRouteFactory")) | ||
| assertTrue(generatedCode.contains("\"profile/{id}\" to ProfileRouteFactory")) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이거 아주 좋네요👍🏻
WispError관련 ClassName도 옮겨도 좋다고 생각해요!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
바로 수정하겠습니다!