moai-lang-kotlin by modu-ai/moai-adk
npx skills add https://github.com/modu-ai/moai-adk --skill moai-lang-kotlinKotlin 2.0+ 专家 - 集成 Context7 的 K2 编译器、协程、Ktor、Compose Multiplatform。
自动触发:Kotlin 文件(.kt, .kts)、Gradle Kotlin DSL(build.gradle.kts, settings.gradle.kts)
核心能力:
协程和 Flow:使用 coroutineScope 配合 async 进行并行操作。用 async 创建延迟值,然后在每个值上调用 来获取结果。将结果组合成数据类。对于响应式流,在 循环内部使用 块和 调用。使用 设置间隔,使用 指定调度器。
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
awaitwhileflowemitdelayflowOn密封类和值类:使用泛型类型参数定义密封接口。为成功情况创建数据类实现,为无状态情况(如 Loading)创建数据对象。使用 @JvmInline 注解包装基本类型的值类。添加带有 require 的 init 块进行验证。
应用设置:使用 Netty、端口和主机参数调用 embeddedServer。在 lambda 内部,调用用于配置 Koin、安全、路由和内容协商的函数。使用 wait 等于 true 调用 start。
对于 Koin 配置,安装 Koin 插件并使用 single 声明定义模块以创建单例。对于安全配置,安装 Authentication 插件并使用领域、验证器和验证回调配置 JWT。对于内容协商,使用 json 配置安装 ContentNegotiation。
带认证的路由:在 Application 上定义 routing 函数。在 routing 块内部,使用 route 处理路径前缀。使用 post 和 call.receive 创建无需认证的端点以获取请求体。使用带有验证器名称的 authenticate 块来保护路由。在 route 块内部,定义使用 call.parameters 获取路径/查询参数的 get 端点。使用 call.respond 返回状态码和响应体。
表和实体:定义继承 LongIdTable 并指定表名的 object。使用 varchar、enumerationByName 和 timestamp 函数声明列。使用 uniqueIndex() 和 defaultExpression 设置默认值。
创建继承 LongEntity 的实体类,其伴生对象继承 LongEntityClass。使用 by 语法和表列引用声明属性。创建 toModel 函数将实体映射到领域模型。
支持协程的仓库:创建接收 Database 参数的仓库实现。实现 suspend 函数,在 dbQuery 辅助函数中包装 Exposed 操作。使用 findById 查找单个实体。使用 Entity.new 进行插入。使用 newSuspendedTransaction 和 IO 调度器定义私有的 dbQuery 函数。
WebFlux 控制器:使用 @RestController 和 @RequestMapping 注解类。为端点创建带有 @GetMapping 和 @PostMapping 的 suspend 函数。使用 map 转换实体,返回 Flow 表示集合。返回带有状态码的 ResponseEntity。使用 @Valid 进行请求验证。
共享 UI 组件:创建接收 ViewModel 和回调参数的 @Composable 函数。使用 collectAsState 将 uiState 收集为状态。在密封状态上使用 when 表达式,为 Loading、Success 和 Error 显示不同的可组合项。
对于列表项,创建带有 Modifier.fillMaxWidth 和 clickable 的 Card 可组合项。使用带内边距的 Row、用于圆形裁剪头像的 AsyncImage 以及用于文本内容的 Column,并应用 MaterialTheme.typography。
使用 mockk 创建依赖项的测试类。在声明中使用 mock 初始化服务。使用带有 runTest 的 @Test 进行协程测试。使用带有 coAnswers 的 coEvery 进行带有延迟的异步模拟。使用 assertThat 进行断言。对于 Flow 测试,使用 toList 收集发射值并断言其大小和内容。
在 plugins 块中使用带版本字符串的 kotlin("jvm") 和 kotlin("plugin.serialization")。添加 ktor.plugin 的 id。在 kotlin 块中配置 jvmToolchain。在 dependencies 块中,添加 ktor 服务器模块、kotlinx 协程、exposed 模块和 postgresql 驱动。添加用于 mockk、coroutines-test 和 turbine 的测试依赖。
最新文档的库映射:
/ktorio/ktor - Ktor 3.0 服务器/客户端文档/jetbrains/exposed - Exposed SQL 框架/JetBrains/kotlin - Kotlin 2.0 语言参考/Kotlin/kotlinx.coroutines - 协程库/jetbrains/compose-multiplatform - Compose Multiplatform/arrow-kt/arrow - Arrow 函数式编程用法:使用 context7CompatibleLibraryID、特定领域的 topic 字符串以及控制响应大小的 tokens 参数调用 mcp__context7__get_library_docs。
在以下情况使用 Kotlin:
在以下情况考虑替代方案:
moai-lang-java - Java 互操作性和 Spring Boot 模式moai-domain-backend - REST API、GraphQL、微服务架构moai-domain-database - JPA、Exposed、R2DBC 模式moai-quality-testing - JUnit 5、MockK、TestContainers 集成moai-infra-docker - JVM 容器优化K2 编译器:将 kotlin.experimental.tryK2=true 添加到 gradle.properties。清除 .gradle 目录以进行完全重建。
协程:避免在 suspend 上下文中使用 runBlocking。对阻塞操作使用 Dispatchers.IO。
Ktor:确保已安装 ContentNegotiation。检查 JWT 验证器配置。验证路由层次结构。
Exposed:确保所有数据库操作在事务上下文中运行。注意事务外部的延迟实体加载。
全面的参考资料:
最后更新:2026-01-11 状态:生产就绪 (v1.1.0)
每周安装次数
70
代码仓库
GitHub 星标数
906
首次出现
2026 年 1 月 22 日
安全审计
已安装于
opencode62
gemini-cli59
codex59
claude-code55
antigravity53
github-copilot52
Kotlin 2.0+ Expert - K2 compiler, coroutines, Ktor, Compose Multiplatform with Context7 integration.
Auto-Triggers: Kotlin files (.kt, .kts), Gradle Kotlin DSL (build.gradle.kts, settings.gradle.kts)
Core Capabilities:
Coroutines and Flow: Use coroutineScope with async for parallel operations. Create deferred values with async, then call await on each to get results. Combine results into data classes. For reactive streams, create flow blocks with emit calls inside while loops. Use delay for intervals and flowOn to specify dispatcher.
Sealed Classes and Value Classes: Define sealed interface with generic type parameter. Create data class implementations for success and data object for stateless cases like Loading. Use @JvmInline annotation with value class wrapping a primitive. Add init blocks with require for validation.
Application Setup: Call embeddedServer with Netty, port, and host parameters. Inside the lambda, call configuration functions for Koin, security, routing, and content negotiation. Call start with wait equals true.
For Koin configuration, install Koin plugin and define modules with single declarations for singletons. For security, install Authentication plugin and configure JWT with realm, verifier, and validate callback. For content negotiation, install ContentNegotiation with json configuration.
Routing with Authentication: Define routing function on Application. Inside routing block, use route for path prefixes. Create unauthenticated endpoints with post and call.receive for request body. Use authenticate block with verifier name for protected routes. Inside route blocks, define get endpoints with call.parameters for path/query params. Use call.respond with status code and response body.
Table and Entity: Define object extending LongIdTable with table name. Declare columns with varchar, enumerationByName, and timestamp functions. Use uniqueIndex() and defaultExpression for defaults.
Create entity class extending LongEntity with companion object extending LongEntityClass. Declare properties with by syntax using table column references. Create toModel function to map entity to domain model.
Repository with Coroutines: Create repository implementation taking Database parameter. Implement suspend functions wrapping Exposed operations in dbQuery helper. Use findById for single entity lookup. Use Entity.new for inserts. Define private dbQuery function using newSuspendedTransaction with IO dispatcher.
WebFlux Controller: Annotate class with @RestController and @RequestMapping. Create suspend functions for endpoints with @GetMapping and @PostMapping. Return Flow for collections using map to convert entities. Return ResponseEntity with status codes. Use @Valid for request validation.
Shared UI Component: Create @Composable function taking ViewModel and callback parameters. Collect uiState as state with collectAsState. Use when expression on sealed state to show different composables for Loading, Success, and Error.
For list items, create Card composables with Modifier.fillMaxWidth and clickable. Use Row with padding, AsyncImage for avatars with CircleShape clip, and Column for text content with MaterialTheme.typography.
Create test class with mockk for dependencies. Initialize service with mock in declaration. Use @Test with runTest for coroutine tests. Use coEvery with coAnswers for async mocking with delay. Use assertThat for assertions. For Flow testing, use toList to collect emissions and assert on size and content.
Use plugins block with kotlin("jvm") and kotlin("plugin.serialization") with version strings. Add id for ktor.plugin. Configure kotlin block with jvmToolchain. In dependencies block, add ktor server modules, kotlinx coroutines, exposed modules, and postgresql driver. Add test dependencies for mockk, coroutines-test, and turbine.
Library mappings for latest documentation:
/ktorio/ktor - Ktor 3.0 server/client documentation/jetbrains/exposed - Exposed SQL framework/JetBrains/kotlin - Kotlin 2.0 language reference/Kotlin/kotlinx.coroutines - Coroutines library/jetbrains/compose-multiplatform - Compose Multiplatform/arrow-kt/arrow - Arrow functional programmingUsage: Call mcp__context7__get_library_docs with context7CompatibleLibraryID, topic string for specific areas, and tokens parameter for response size.
Use Kotlin When:
Consider Alternatives When:
moai-lang-java - Java interoperability and Spring Boot patternsmoai-domain-backend - REST API, GraphQL, microservices architecturemoai-domain-database - JPA, Exposed, R2DBC patternsmoai-quality-testing - JUnit 5, MockK, TestContainers integrationmoai-infra-docker - JVM container optimizationK2 Compiler: Add kotlin.experimental.tryK2=true to gradle.properties. Clear .gradle directory for full rebuild.
Coroutines: Avoid runBlocking in suspend contexts. Use Dispatchers.IO for blocking operations.
Ktor: Ensure ContentNegotiation is installed. Check JWT verifier configuration. Verify routing hierarchy.
Exposed: Ensure all DB operations run within transaction context. Be aware of lazy entity loading outside transactions.
For comprehensive reference materials:
Last Updated: 2026-01-11 Status: Production Ready (v1.1.0)
Weekly Installs
70
Repository
GitHub Stars
906
First Seen
Jan 22, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode62
gemini-cli59
codex59
claude-code55
antigravity53
github-copilot52
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
120,000 周安装