Skip to content

Commit

Permalink
Reformat Kotlin code to upgrade ktlint version (#4488)
Browse files Browse the repository at this point in the history
Motivation:

To upgrade `ktlint` version from `0.36.0` to `ktlint-gradle` plugin's default (`0.43.2`).
Armeria's some kotlin code violates the new `ktlint` version's default ruleset, especially, when it comes to adding newlines.

Modifications:

* Run `./gradlew ktlintFormat` task once.
* Manually reformat some code for better readability.
  • Loading branch information
ks-yim authored Dec 2, 2022
1 parent 5733f7b commit 75867ae
Show file tree
Hide file tree
Showing 18 changed files with 352 additions and 305 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,23 @@ open class AnnotatedServiceFlowBenchmark {
@Setup
open fun setup() {
server = Server.builder()
.annotatedService("/benchmark", object {
@Get("/flow")
@ProducesJsonSequences
fun flowBm(): Flow<String> = flow {
(0 until 1000).forEach {
emit("$it")
.annotatedService(
"/benchmark",
object {
@Get("/flow")
@ProducesJsonSequences
fun flowBm(): Flow<String> = flow {
(0 until 1000).forEach {
emit("$it")
}
}
}

@Get("/publisher")
@ProducesJsonSequences
fun publisherBm(): Publisher<String> =
Flux.fromStream(IntStream.range(0, 1000).mapToObj { it.toString() })
})
@Get("/publisher")
@ProducesJsonSequences
fun publisherBm(): Publisher<String> =
Flux.fromStream(IntStream.range(0, 1000).mapToObj { it.toString() })
}
)
.build()
.also { it.start().join() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ class MarkdownDescriptionService {
.build()
.start();
```
""", markup = Markup.MARKDOWN
""",
markup = Markup.MARKDOWN
)
@Get("/markdown")
fun markdown(
Expand All @@ -68,11 +69,14 @@ class MarkdownDescriptionService {
)
}

@Description(value = """
@Description(
value = """
## Structs description
### Structs description subtitle
> Support blockquotes
""", markup = Markup.MARKDOWN)
""",
markup = Markup.MARKDOWN
)
data class MarkdownDescriptionResult(
@Description(value = "result1 description (default)", markup = Markup.MARKDOWN)
val result1: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import com.linecorp.armeria.server.docs.Markup

@CoroutineNameDecorator(name = "default")
class MermaidDescriptionService {
@Description(value = """
@Description(
value = """
gantt
title A Gantt Diagram
dateFormat YYYY-MM-DD
Expand All @@ -33,7 +34,9 @@ class MermaidDescriptionService {
section Another
Task in sec :2014-01-12 , 12d
another task : 24d
""", markup = Markup.MERMAID)
""",
markup = Markup.MERMAID
)
@Get("/mermaid")
fun mermaid(
@Param param1: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,12 @@ class MainService(private val backendClient: WebClient) : HttpService {
require(ctx.eventLoop().inEventLoop())

val nums = mutableListOf<Long>()
for (token in Iterables.concat(
NUM_SPLITTER.split(aggregatedHttpRequest.path().substring(1)),
NUM_SPLITTER.split(aggregatedHttpRequest.contentUtf8())
)) {
for (
token in Iterables.concat(
NUM_SPLITTER.split(aggregatedHttpRequest.path().substring(1)),
NUM_SPLITTER.split(aggregatedHttpRequest.contentUtf8())
)
) {
nums.add(token.toLong())
}
return nums
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ class GraphqlServiceTest {

private fun client(): GraphqlArmeriaClient {
return GraphqlArmeriaClient(
uri = server.httpUri().resolve("/graphql"),
serializer = GraphQLClientJacksonSerializer())
uri = server.httpUri().resolve("/graphql"),
serializer = GraphQLClientJacksonSerializer()
)
}
}

@ParameterizedTest
@CsvSource(
"1,hero",
"2,human",
"3,droid"
"1,hero",
"2,human",
"3,droid"
)
fun testUserDataFetch(id: String, expected: String) {
runBlocking {
Expand Down
4 changes: 2 additions & 2 deletions gradle/scripts/.gitrepo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[subrepo]
remote = https://github.com/line/gradle-scripts
branch = master
commit = 566f6109098bb5c3737f494b67d943e760d804c9
parent = 994c5b2119b58215e71c6a9caff3bf1c628ee6df
commit = 10ef4cfa8800d7b831d48a38a21f7060d654018e
parent = eac8378d8f6dbfe92681dab54d421b27b3c19eb3
method = merge
cmdver = 0.4.5
2 changes: 1 addition & 1 deletion gradle/scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ automatically:
- `publish` - Makes a project publish its artifact to a Maven repository
- `bom` - Makes a project publish Maven BOM based on `dependencies.toml`
- `shade`, `relocate` and `trim` - Makes a Java project produce an additional 'shaded' JAR
- `reactor-grpc` or `rxgrpc` - Enables [`reactor-grpc`](https://github.com/salesforce/reactive-grpc/tree/master/reactor) or [`rxgrpc`](https://github.com/salesforce/reactive-grpc/tree/master/rx-java) support to the project
- `reactor-grpc`, `rxgrpc` or `kotlin-grpc` - Enables [`reactor-grpc`](https://github.com/salesforce/reactive-grpc/tree/master/reactor), [`rxgrpc`](https://github.com/salesforce/reactive-grpc/tree/master/rx-java) or [`kotlin-grpc`](https://github.com/grpc/grpc-kotlin) support to the project

We will learn what these flags exactly do in the following sections.

Expand Down
5 changes: 0 additions & 5 deletions gradle/scripts/lib/java-rpc-proto.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,6 @@ configure(projectsWithFlags('java')) {
}
}

// Add the generated 'grpc' directories to the source sets.
project.sourceSets.all { sourceSet ->
sourceSet.java.srcDir file("${project.ext.genSrcDir}/${sourceSet.name}/grpc")
}

// Make sure protoc runs before the resources are copied so that .dsc files are included.
project.afterEvaluate {
project.sourceSets.each { sourceSet ->
Expand Down
18 changes: 13 additions & 5 deletions gradle/scripts/lib/java.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,20 @@ configure(projectsWithFlags('java')) {
}
}

// Add the generated source directories to the source sets.
project.sourceSets.all { sourceSet ->
sourceSet.java.srcDir file("${project.ext.genSrcDir}/${sourceSet.name}/java")
sourceSet.resources.srcDir file("${project.ext.genSrcDir}/${sourceSet.name}/resources")
}
afterEvaluate {
// Add the generated source directories to the source sets.
// This should run in 'afterEvaluate' so that protobuf plugin which also adds
// generated files to source set applied first.
project.sourceSets.all { sourceSet ->
def javaSrcDir = file("${project.ext.genSrcDir}/${sourceSet.name}/java")
def resourceSrcDir = file("${project.ext.genSrcDir}/${sourceSet.name}/resources")
if (!sourceSet.java.srcDirs.contains(javaSrcDir)) {
sourceSet.java.srcDir javaSrcDir
}
if (!sourceSet.resources.srcDirs.contains(resourceSrcDir)) {
sourceSet.resources.srcDir resourceSrcDir
}
}
Task generateSourcesTask = project.tasks.findByName('generateSources')
if (generateSourcesTask != null) {
tasks.sourcesJar.dependsOn(generateSourcesTask)
Expand Down
6 changes: 2 additions & 4 deletions gradle/scripts/lib/kotlin.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ configure(projectsWithFlags('kotlin')) {

apply plugin: 'kotlin'

def target = "1.8"
def target = project.findProperty('javaTargetCompatibility') ?: '1.8'
def compilerArgs = ['-java-parameters', '-Xjsr305=strict', '-Xskip-prerelease-check']
compileKotlin {
kotlinOptions.jvmTarget = target
Expand All @@ -17,8 +17,6 @@ configure(projectsWithFlags('kotlin')) {
apply plugin: "org.jlleitschuh.gradle.ktlint"

ktlint {
// https://github.com/pinterest/ktlint/issues/764
version.set("0.36.0")
verbose.set(true)
reporters {
reporter "html"
Expand All @@ -27,7 +25,7 @@ configure(projectsWithFlags('kotlin')) {
disabledRules = ["import-ordering"]

filter {
exclude("**/gen-src/**")
exclude { it.file.path.contains("gen-src/") }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,15 @@ class HelloServiceTest {
): ClientCall<I, O> {
return object : SimpleForwardingClientCall<I, O>(next.newCall(method, options)) {
override fun start(responseListener: Listener<O>, headers: Metadata) {
super.start(object : SimpleForwardingClientCallListener<O>(responseListener) {
override fun onClose(status: Status, trailers: Metadata) {
closeCalled.incrementAndGet()
super.onClose(status, trailers)
}
}, headers)
super.start(
object : SimpleForwardingClientCallListener<O>(responseListener) {
override fun onClose(status: Status, trailers: Metadata) {
closeCalled.incrementAndGet()
super.onClose(status, trailers)
}
},
headers
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class ClientCoroutineTest {
assertThat(ex.cause).isInstanceOf(UnsupportedMediaTypeException::class.java)
.hasMessageContaining(
"Content type 'text/plain;charset=utf-8' not supported for " +
"bodyType=com.linecorp.armeria.spring.kotlin.Abnormal"
"bodyType=com.linecorp.armeria.spring.kotlin.Abnormal"
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,24 @@ internal fun isMarkedNullable(element: AnnotatedElement): Boolean {
val executable = element.declaringExecutable
val i = executable.parameters.indexOf(element)
when (executable) {
is Method -> executable
.kotlinFunction
// Should be `valueParameters` to exclude the `this` instance and
// the extension receiver parameter.
?.valueParameters
?.get(i)
?.type
?.isMarkedNullable
?: false
is Constructor<*> -> { executable
.kotlinFunction
?.valueParameters
?.get(i)
?.type
?.isMarkedNullable
?: false
}
is Method ->
executable
.kotlinFunction
// Should be `valueParameters` to exclude the `this` instance and
// the extension receiver parameter.
?.valueParameters
?.get(i)
?.type
?.isMarkedNullable
?: false
is Constructor<*> ->
executable
.kotlinFunction
?.valueParameters
?.get(i)
?.type
?.isMarkedNullable
?: false
else -> false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,17 @@ class RestClientTest {
var server: ServerExtension = object : ServerExtension() {
override fun configure(sb: ServerBuilder) {
sb.service("/rest/{id}") { ctx: ServiceRequestContext, req: HttpRequest ->
HttpResponse.from(req.aggregate().thenApply { agg: AggregatedHttpRequest ->
val restResponse =
RestResponse(
ctx.pathParam("id")!!,
req.method().toString(),
agg.contentUtf8()
)
HttpResponse.ofJson(restResponse)
})
HttpResponse.from(
req.aggregate().thenApply { agg: AggregatedHttpRequest ->
val restResponse =
RestResponse(
ctx.pathParam("id")!!,
req.method().toString(),
agg.contentUtf8()
)
HttpResponse.ofJson(restResponse)
}
)
}
}
}
Expand Down
Loading

0 comments on commit 75867ae

Please sign in to comment.