Skip to content

Commit 4de5da2

Browse files
authored
Merge pull request anomalyco#309 from cookiee339/feat/kotlin-ecosystem
feat(kotlin): add Kotlin/Ktor/Exposed ecosystem
2 parents 3010f75 + 99d443b commit 4de5da2

14 files changed

Lines changed: 3905 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
description: "Kotlin coding style extending common rules"
3+
globs: ["**/*.kt", "**/*.kts", "**/build.gradle.kts"]
4+
alwaysApply: false
5+
---
6+
# Kotlin Coding Style
7+
8+
> This file extends the common coding style rule with Kotlin-specific content.
9+
10+
## Formatting
11+
12+
- Auto-formatting via **ktfmt** or **ktlint** (configured in `kotlin-hooks.md`)
13+
- Use trailing commas in multiline declarations
14+
15+
## Immutability
16+
17+
The global immutability requirement is enforced in the common coding style rule.
18+
For Kotlin specifically:
19+
20+
- Prefer `val` over `var`
21+
- Use immutable collection types (`List`, `Map`, `Set`)
22+
- Use `data class` with `copy()` for immutable updates
23+
24+
## Null Safety
25+
26+
- Avoid `!!` -- use `?.`, `?:`, `require`, or `checkNotNull`
27+
- Handle platform types explicitly at Java interop boundaries
28+
29+
## Expression Bodies
30+
31+
Prefer expression bodies for single-expression functions:
32+
33+
```kotlin
34+
fun isAdult(age: Int): Boolean = age >= 18
35+
```
36+
37+
## Reference
38+
39+
See skill: `kotlin-patterns` for comprehensive Kotlin idioms and patterns.

‎.cursor/rules/kotlin-hooks.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
description: "Kotlin hooks extending common rules"
3+
globs: ["**/*.kt", "**/*.kts", "**/build.gradle.kts"]
4+
alwaysApply: false
5+
---
6+
# Kotlin Hooks
7+
8+
> This file extends the common hooks rule with Kotlin-specific content.
9+
10+
## PostToolUse Hooks
11+
12+
Configure in `~/.claude/settings.json`:
13+
14+
- **ktfmt/ktlint**: Auto-format `.kt` and `.kts` files after edit
15+
- **detekt**: Run static analysis after editing Kotlin files
16+
- **./gradlew build**: Verify compilation after changes

‎.cursor/rules/kotlin-patterns.md‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
description: "Kotlin patterns extending common rules"
3+
globs: ["**/*.kt", "**/*.kts", "**/build.gradle.kts"]
4+
alwaysApply: false
5+
---
6+
# Kotlin Patterns
7+
8+
> This file extends the common patterns rule with Kotlin-specific content.
9+
10+
## Sealed Classes
11+
12+
Use sealed classes/interfaces for exhaustive type hierarchies:
13+
14+
```kotlin
15+
sealed class Result<out T> {
16+
data class Success<T>(val data: T) : Result<T>()
17+
data class Failure(val error: AppError) : Result<Nothing>()
18+
}
19+
```
20+
21+
## Extension Functions
22+
23+
Add behavior without inheritance, scoped to where they're used:
24+
25+
```kotlin
26+
fun String.toSlug(): String =
27+
lowercase().replace(Regex("[^a-z0-9\\s-]"), "").replace(Regex("\\s+"), "-")
28+
```
29+
30+
## Scope Functions
31+
32+
- `let`: Transform nullable or scoped result
33+
- `apply`: Configure an object
34+
- `also`: Side effects
35+
- Avoid nesting scope functions
36+
37+
## Dependency Injection
38+
39+
Use Koin for DI in Ktor projects:
40+
41+
```kotlin
42+
val appModule = module {
43+
single<UserRepository> { ExposedUserRepository(get()) }
44+
single { UserService(get()) }
45+
}
46+
```
47+
48+
## Reference
49+
50+
See skill: `kotlin-patterns` for comprehensive Kotlin patterns including coroutines, DSL builders, and delegation.

‎.cursor/rules/kotlin-security.md‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
description: "Kotlin security extending common rules"
3+
globs: ["**/*.kt", "**/*.kts", "**/build.gradle.kts"]
4+
alwaysApply: false
5+
---
6+
# Kotlin Security
7+
8+
> This file extends the common security rule with Kotlin-specific content.
9+
10+
## Secret Management
11+
12+
```kotlin
13+
val apiKey = System.getenv("API_KEY")
14+
?: throw IllegalStateException("API_KEY not configured")
15+
```
16+
17+
## SQL Injection Prevention
18+
19+
Always use Exposed's parameterized queries:
20+
21+
```kotlin
22+
// Good: Parameterized via Exposed DSL
23+
UsersTable.selectAll().where { UsersTable.email eq email }
24+
25+
// Bad: String interpolation in raw SQL
26+
exec("SELECT * FROM users WHERE email = '$email'")
27+
```
28+
29+
## Authentication
30+
31+
Use Ktor's Auth plugin with JWT:
32+
33+
```kotlin
34+
install(Authentication) {
35+
jwt("jwt") {
36+
verifier(
37+
JWT.require(Algorithm.HMAC256(secret))
38+
.withAudience(audience)
39+
.withIssuer(issuer)
40+
.build()
41+
)
42+
validate { credential ->
43+
val payload = credential.payload
44+
if (payload.audience.contains(audience) &&
45+
payload.issuer == issuer &&
46+
payload.subject != null) {
47+
JWTPrincipal(payload)
48+
} else {
49+
null
50+
}
51+
}
52+
}
53+
}
54+
```
55+
56+
## Null Safety as Security
57+
58+
Kotlin's type system prevents null-related vulnerabilities -- avoid `!!` to maintain this guarantee.

‎.cursor/rules/kotlin-testing.md‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
description: "Kotlin testing extending common rules"
3+
globs: ["**/*.kt", "**/*.kts", "**/build.gradle.kts"]
4+
alwaysApply: false
5+
---
6+
# Kotlin Testing
7+
8+
> This file extends the common testing rule with Kotlin-specific content.
9+
10+
## Framework
11+
12+
Use **Kotest** with spec styles (StringSpec, FunSpec, BehaviorSpec) and **MockK** for mocking.
13+
14+
## Coroutine Testing
15+
16+
Use `runTest` from `kotlinx-coroutines-test`:
17+
18+
```kotlin
19+
test("async operation completes") {
20+
runTest {
21+
val result = service.fetchData()
22+
result.shouldNotBeEmpty()
23+
}
24+
}
25+
```
26+
27+
## Coverage
28+
29+
Use **Kover** for coverage reporting:
30+
31+
```bash
32+
./gradlew koverHtmlReport
33+
./gradlew koverVerify
34+
```
35+
36+
## Reference
37+
38+
See skill: `kotlin-testing` for detailed Kotest patterns, MockK usage, and property-based testing.

‎agents/kotlin-build-resolver.md‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
---
2+
name: kotlin-build-resolver
3+
description: Kotlin/Gradle build, compilation, and dependency error resolution specialist. Fixes build errors, Kotlin compiler errors, and Gradle issues with minimal changes. Use when Kotlin builds fail.
4+
tools: ["Read", "Write", "Edit", "Bash", "Grep", "Glob"]
5+
model: sonnet
6+
---
7+
8+
# Kotlin Build Error Resolver
9+
10+
You are an expert Kotlin/Gradle build error resolution specialist. Your mission is to fix Kotlin build errors, Gradle configuration issues, and dependency resolution failures with **minimal, surgical changes**.
11+
12+
## Core Responsibilities
13+
14+
1. Diagnose Kotlin compilation errors
15+
2. Fix Gradle build configuration issues
16+
3. Resolve dependency conflicts and version mismatches
17+
4. Handle Kotlin compiler errors and warnings
18+
5. Fix detekt and ktlint violations
19+
20+
## Diagnostic Commands
21+
22+
Run these in order:
23+
24+
```bash
25+
./gradlew build 2>&1
26+
./gradlew detekt 2>&1 || echo "detekt not configured"
27+
./gradlew ktlintCheck 2>&1 || echo "ktlint not configured"
28+
./gradlew dependencies --configuration runtimeClasspath 2>&1 | head -100
29+
```
30+
31+
## Resolution Workflow
32+
33+
```text
34+
1. ./gradlew build -> Parse error message
35+
2. Read affected file -> Understand context
36+
3. Apply minimal fix -> Only what's needed
37+
4. ./gradlew build -> Verify fix
38+
5. ./gradlew test -> Ensure nothing broke
39+
```
40+
41+
## Common Fix Patterns
42+
43+
| Error | Cause | Fix |
44+
|-------|-------|-----|
45+
| `Unresolved reference: X` | Missing import, typo, missing dependency | Add import or dependency |
46+
| `Type mismatch: Required X, Found Y` | Wrong type, missing conversion | Add conversion or fix type |
47+
| `None of the following candidates is applicable` | Wrong overload, wrong argument types | Fix argument types or add explicit cast |
48+
| `Smart cast impossible` | Mutable property or concurrent access | Use local `val` copy or `let` |
49+
| `'when' expression must be exhaustive` | Missing branch in sealed class `when` | Add missing branches or `else` |
50+
| `Suspend function can only be called from coroutine` | Missing `suspend` or coroutine scope | Add `suspend` modifier or launch coroutine |
51+
| `Cannot access 'X': it is internal in 'Y'` | Visibility issue | Change visibility or use public API |
52+
| `Conflicting declarations` | Duplicate definitions | Remove duplicate or rename |
53+
| `Could not resolve: group:artifact:version` | Missing repository or wrong version | Add repository or fix version |
54+
| `Execution failed for task ':detekt'` | Code style violations | Fix detekt findings |
55+
56+
## Gradle Troubleshooting
57+
58+
```bash
59+
# Check dependency tree for conflicts
60+
./gradlew dependencies --configuration runtimeClasspath
61+
62+
# Force refresh dependencies
63+
./gradlew build --refresh-dependencies
64+
65+
# Clear project-local Gradle build cache
66+
./gradlew clean && rm -rf .gradle/build-cache/
67+
68+
# Check Gradle version compatibility
69+
./gradlew --version
70+
71+
# Run with debug output
72+
./gradlew build --debug 2>&1 | tail -50
73+
74+
# Check for dependency conflicts
75+
./gradlew dependencyInsight --dependency <name> --configuration runtimeClasspath
76+
```
77+
78+
## Kotlin Compiler Flags
79+
80+
```kotlin
81+
// build.gradle.kts - Common compiler options
82+
kotlin {
83+
compilerOptions {
84+
freeCompilerArgs.add("-Xjsr305=strict") // Strict Java null safety
85+
allWarningsAsErrors = true
86+
}
87+
}
88+
```
89+
90+
## Key Principles
91+
92+
- **Surgical fixes only** -- don't refactor, just fix the error
93+
- **Never** suppress warnings without explicit approval
94+
- **Never** change function signatures unless necessary
95+
- **Always** run `./gradlew build` after each fix to verify
96+
- Fix root cause over suppressing symptoms
97+
- Prefer adding missing imports over wildcard imports
98+
99+
## Stop Conditions
100+
101+
Stop and report if:
102+
- Same error persists after 3 fix attempts
103+
- Fix introduces more errors than it resolves
104+
- Error requires architectural changes beyond scope
105+
- Missing external dependencies that need user decision
106+
107+
## Output Format
108+
109+
```text
110+
[FIXED] src/main/kotlin/com/example/service/UserService.kt:42
111+
Error: Unresolved reference: UserRepository
112+
Fix: Added import com.example.repository.UserRepository
113+
Remaining errors: 2
114+
```
115+
116+
Final: `Build Status: SUCCESS/FAILED | Errors Fixed: N | Files Modified: list`
117+
118+
For detailed Kotlin patterns and code examples, see `skill: kotlin-patterns`.

0 commit comments

Comments
 (0)