7

我有一个巨大的@OpenApi注释(基本上它是Javalin /Kotlin 端点的文档),它占据了很多行:

@OpenApi(
   summary = "",
   description = "Lists all customers",
   path = "customers",
   queryParams =
   // ...........
   // ...........
   // etc
)
override fun handle(context: Context) {
   // body of the REST handler
}

我必须滚动很多才能看到实际的处理程序。因此,我想以某种方式隔离它:

@GetCustomersDoc
override fun handle(context: Context) {
   // body of the REST handler
}

我对使文档转到其他地方的其他解决方案感到满意。

这将使代码更清晰,文档被隔离。

4

2 回答 2

1

您可以定义单独的注释:

annotation class MyOwnApi(
    val openApi: OpenApi = OpenApi(
          summary = "",
          description = "Lists all customers",
          path = "customers",
          queryParams =
          // ...........
          // ...........
          // etc
        )
)

annotation class UserOpenApi(
        val openApi: OpenApi = OpenApi(
              summary = "Something",
              description = "Lists all users",
              // ...........
              // ...........
              // etc
            )
    )

优点:

  • 代码分离
  • 可重用的注释类
  • 您甚至可以创建一个构建器类并对其进行测试

缺点:

  • 令人困惑
  • 注解不能继承、扩展类或实现接口
  • 如果直接检查类/对象,可能无法@OpenApi实现或需要复杂的代码更改。在这种情况下,您将需要另一个反射搜索来从字段中提取注释!
于 2019-09-30T10:46:47.827 回答
0

好的,所以您想要的是将@OpenApi文档与 REST 处理程序代码分开。您可以通过移开实现而不是移开注释来做到这一点。

@OpenApi因此,在所有注释与 REST 处理程序代码混合的当前文件中,您调用辅助函数,如下所示:

@OpenApi(
   summary = "",
   description = "Lists all customers",
   path = "customers",
   queryParams =
   // ...........
   // ...........
   // etc
)
override fun handle(context: Context) {
   handleGetCustomers(context)
}

然后您将所有 REST 处理程序放在该文件的顶部或另一个文件中以进行更多隔离,这使您可以在它们之间滚动而没有@OpenApi注释的噪音:

// Collected at the top of the file, or in a separate file
fun handleGetCustomers(context: Context) {
    // body of the REST handler
}

然后,您可以轻松地在 REST 处理程序代码之间滚动,而不会受到@OpenApi噪音的困扰。

请注意,您应该使用 Android Studio 的Go To -> Definition功能来避免滚动到handleGetCustomers().

于 2019-09-21T08:21:47.023 回答