Skip to content

Commit

Permalink
chore: replace ktlint with ktfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
jenspots committed May 2, 2024
1 parent f070dc9 commit a0a397a
Show file tree
Hide file tree
Showing 19 changed files with 689 additions and 616 deletions.
91 changes: 86 additions & 5 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,96 @@ insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8

[*.{java,kt}]
indent_style = space
indent_size = 4
max_line_length = 80

[*.sparql]
indent_style = space
indent_size = 2

[*.{yml,yaml}]
indent_style = space
indent_size = 2

[{*.kt,*.kts}]
indent_style = space
insert_final_newline = true
max_line_length = 100
indent_size = 2
ij_continuation_indent_size = 4
ij_java_names_count_to_use_import_on_demand = 9999
ij_kotlin_align_in_columns_case_branch = false
ij_kotlin_align_multiline_binary_operation = false
ij_kotlin_align_multiline_extends_list = false
ij_kotlin_align_multiline_method_parentheses = false
ij_kotlin_align_multiline_parameters = true
ij_kotlin_align_multiline_parameters_in_calls = false
ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = true
ij_kotlin_assignment_wrap = normal
ij_kotlin_blank_lines_after_class_header = 0
ij_kotlin_blank_lines_around_block_when_branches = 0
ij_kotlin_blank_lines_before_declaration_with_comment_or_annotation_on_separate_line = 1
ij_kotlin_block_comment_at_first_column = true
ij_kotlin_call_parameters_new_line_after_left_paren = true
ij_kotlin_call_parameters_right_paren_on_new_line = false
ij_kotlin_call_parameters_wrap = on_every_item
ij_kotlin_catch_on_new_line = false
ij_kotlin_class_annotation_wrap = split_into_lines
ij_kotlin_code_style_defaults = KOTLIN_OFFICIAL
ij_kotlin_continuation_indent_for_chained_calls = true
ij_kotlin_continuation_indent_for_expression_bodies = true
ij_kotlin_continuation_indent_in_argument_lists = true
ij_kotlin_continuation_indent_in_elvis = false
ij_kotlin_continuation_indent_in_if_conditions = false
ij_kotlin_continuation_indent_in_parameter_lists = false
ij_kotlin_continuation_indent_in_supertype_lists = false
ij_kotlin_else_on_new_line = false
ij_kotlin_enum_constants_wrap = off
ij_kotlin_extends_list_wrap = normal
ij_kotlin_field_annotation_wrap = split_into_lines
ij_kotlin_finally_on_new_line = false
ij_kotlin_if_rparen_on_new_line = false
ij_kotlin_import_nested_classes = false
ij_kotlin_insert_whitespaces_in_simple_one_line_method = true
ij_kotlin_keep_blank_lines_before_right_brace = 2
ij_kotlin_keep_blank_lines_in_code = 2
ij_kotlin_keep_blank_lines_in_declarations = 2
ij_kotlin_keep_first_column_comment = true
ij_kotlin_keep_indents_on_empty_lines = false
ij_kotlin_keep_line_breaks = true
ij_kotlin_lbrace_on_next_line = false
ij_kotlin_line_comment_add_space = false
ij_kotlin_line_comment_at_first_column = true
ij_kotlin_method_annotation_wrap = split_into_lines
ij_kotlin_method_call_chain_wrap = normal
ij_kotlin_method_parameters_new_line_after_left_paren = true
ij_kotlin_method_parameters_right_paren_on_new_line = true
ij_kotlin_method_parameters_wrap = on_every_item
ij_kotlin_name_count_to_use_star_import = 9999
ij_kotlin_name_count_to_use_star_import_for_members = 9999
ij_kotlin_parameter_annotation_wrap = off
ij_kotlin_space_after_comma = true
ij_kotlin_space_after_extend_colon = true
ij_kotlin_space_after_type_colon = true
ij_kotlin_space_before_catch_parentheses = true
ij_kotlin_space_before_comma = false
ij_kotlin_space_before_extend_colon = true
ij_kotlin_space_before_for_parentheses = true
ij_kotlin_space_before_if_parentheses = true
ij_kotlin_space_before_lambda_arrow = true
ij_kotlin_space_before_type_colon = false
ij_kotlin_space_before_when_parentheses = true
ij_kotlin_space_before_while_parentheses = true
ij_kotlin_spaces_around_additive_operators = true
ij_kotlin_spaces_around_assignment_operators = true
ij_kotlin_spaces_around_equality_operators = true
ij_kotlin_spaces_around_function_type_arrow = true
ij_kotlin_spaces_around_logical_operators = true
ij_kotlin_spaces_around_multiplicative_operators = true
ij_kotlin_spaces_around_range = false
ij_kotlin_spaces_around_relational_operators = true
ij_kotlin_spaces_around_unary_operator = false
ij_kotlin_spaces_around_when_arrow = true
ij_kotlin_variable_annotation_wrap = off
ij_kotlin_while_on_new_line = false
ij_kotlin_wrap_elvis_expressions = 1
ij_kotlin_wrap_expression_body_functions = 1
ij_kotlin_wrap_first_method_in_call_chain = false
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
repos:
- repo: https://github.com/pre-commit/pre-commit
rev: v3.7.0
hooks:
- id: validate_manifest
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: v2.13.0
hooks:
- id: pretty-format-kotlin
args: [ --ktfmt ]
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
# JVM Runner

[![Test Suite](https://github.com/rdf-connect/jvm-runner/actions/workflows/test.yml/badge.svg)](https://github.com/rdf-connect/jvm-runner/actions/workflows/test.yml) [![ktlint](https://img.shields.io/badge/ktlint%20code--style-%E2%9D%A4-FF4081)](https://pinterest.github.io/ktlint/)
[![Test Suite](https://github.com/rdf-connect/jvm-runner/actions/workflows/test.yml/badge.svg)](https://github.com/rdf-connect/jvm-runner/actions/workflows/test.yml)

A proof-of-concept implementation of a Kotlin-based JVM runner.

#### Notes

###### Pre-Commit Hooks

This repository supports `pre-commit` hooks. To install the hooks, run the following command.

```shell
pre-commit install
```

###### Formatting

The code in this repository is formatted using Meta's `ktfmt` tool, mainly due to the following feature.

> `ktfmt` ignores most existing formatting. It respects existing newlines in some places, but in general, its output is deterministic and is independent of the input code.
No feature flags are used. Invoke using the following command.

```shell
ktfmt ./**/*.kt
```
24 changes: 12 additions & 12 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package technology.idlab

import technology.idlab.runner.Pipeline
import java.io.File
import kotlin.system.exitProcess
import technology.idlab.runner.Pipeline

fun main(args: Array<String>) {
// Parse arguments.
if (args.size != 1) {
println("Usage: jvm-runner <config>")
exitProcess(0)
}
// Parse arguments.
if (args.size != 1) {
println("Usage: jvm-runner <config>")
exitProcess(0)
}

// Parse and load the configuration.
val configPath = args[0]
val config = File(configPath)
val pipeline = Pipeline(config)
// Parse and load the configuration.
val configPath = args[0]
val config = File(configPath)
val pipeline = Pipeline(config)

// Execute all functions.
pipeline.executeSync()
// Execute all functions.
pipeline.executeSync()
}
46 changes: 24 additions & 22 deletions src/main/kotlin/bridge/HttpReader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import kotlinx.coroutines.channels.ClosedReceiveChannelException
import kotlinx.coroutines.runBlocking
import technology.idlab.logging.Log

class HttpReader: Reader {
private val buffer = Channel<ByteArray>(Channel.Factory.UNLIMITED);
class HttpReader : Reader {
private val buffer = Channel<ByteArray>(Channel.Factory.UNLIMITED)

private val embeddedServer = embeddedServer(Netty, port = 8080) {
routing {
post("/") {
private val embeddedServer =
embeddedServer(Netty, port = 8080) {
routing {
post("/") {
Log.shared.debug("Incoming request")
val body = call.receive<ByteArray>()
Log.shared.debug("Received ${body.size} bytes")
Expand All @@ -27,26 +28,27 @@ class HttpReader: Reader {
call.response.status(HttpStatusCode.OK)
call.respondText("OK")
Log.shared.debug("Response sent")
}
}
}
}.start(wait = false)
}
.start(wait = false)

override suspend fun read(): Reader.Result {
try {
val result = buffer.receive()
return Reader.Result.success(result)
} catch (e: ClosedReceiveChannelException) {
return Reader.Result.closed()
} catch (e: Exception) {
Log.shared.fatal(e)
}
override suspend fun read(): Reader.Result {
try {
val result = buffer.receive()
return Reader.Result.success(result)
} catch (e: ClosedReceiveChannelException) {
return Reader.Result.closed()
} catch (e: Exception) {
Log.shared.fatal(e)
}
}

override fun readSync(): Reader.Result {
return runBlocking { read() }
}
override fun readSync(): Reader.Result {
return runBlocking { read() }
}

override fun isClosed(): Boolean {
return false;
}
override fun isClosed(): Boolean {
return false
}
}
36 changes: 17 additions & 19 deletions src/main/kotlin/bridge/HttpWriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,26 @@ import io.ktor.client.statement.*
import kotlinx.coroutines.runBlocking
import technology.idlab.logging.Log

class HttpWriter(private val endpoint: String): Writer {
private val client = HttpClient(CIO)
class HttpWriter(private val endpoint: String) : Writer {
private val client = HttpClient(CIO)

override suspend fun push(value: ByteArray) {
// Create request.
Log.shared.debug("POST $endpoint (${value.size} bytes)")
val res = client.post(endpoint) {
setBody(value)
}
Log.shared.debug("Received response: ${res.status.value} - ${res.bodyAsText()}")
override suspend fun push(value: ByteArray) {
// Create request.
Log.shared.debug("POST $endpoint (${value.size} bytes)")
val res = client.post(endpoint) { setBody(value) }
Log.shared.debug("Received response: ${res.status.value} - ${res.bodyAsText()}")

// Check status code.
if (res.status.value != 200) {
Log.shared.fatal("ERROR: Status code ${res.status.value} received from $endpoint")
}
// Check status code.
if (res.status.value != 200) {
Log.shared.fatal("ERROR: Status code ${res.status.value} received from $endpoint")
}
}

override fun pushSync(value: ByteArray) {
runBlocking { push(value) }
}
override fun pushSync(value: ByteArray) {
runBlocking { push(value) }
}

override fun close() {
client.close()
}
override fun close() {
client.close()
}
}
72 changes: 36 additions & 36 deletions src/main/kotlin/bridge/MemoryReader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,51 @@ import kotlinx.coroutines.channels.ClosedReceiveChannelException
import kotlinx.coroutines.runBlocking
import technology.idlab.logging.Log

class MemoryReader: Reader {
private var channel: Channel<ByteArray>? = null
class MemoryReader : Reader {
private var channel: Channel<ByteArray>? = null

fun setChannel(channel: Channel<ByteArray>) {
if (this.channel != null) {
Log.shared.fatal("Channel already set")
}

this.channel = channel
fun setChannel(channel: Channel<ByteArray>) {
if (this.channel != null) {
Log.shared.fatal("Channel already set")
}

override fun readSync(): Reader.Result {
val channel = this.channel ?: Log.shared.fatal("Channel not set")

val result = runBlocking { channel.receiveCatching() }
this.channel = channel
}

// Check if the channel got closed.
if (result.isClosed) {
return Reader.Result.closed()
}
override fun readSync(): Reader.Result {
val channel = this.channel ?: Log.shared.fatal("Channel not set")

// If an error occurred, the runner must handle it itself.
if (result.isFailure) {
Log.shared.fatal("Failed to read bytes")
}
val result = runBlocking { channel.receiveCatching() }

val bytes = result.getOrThrow()
return Reader.Result.success(bytes)
// Check if the channel got closed.
if (result.isClosed) {
return Reader.Result.closed()
}

override suspend fun read(): Reader.Result {
val channel = this.channel ?: Log.shared.fatal("Channel not set")

return try {
val result = channel.receive()
Reader.Result.success(result)
} catch (e: ClosedReceiveChannelException) {
Reader.Result.closed()
} catch (e: Exception) {
Log.shared.fatal(e)
}
// If an error occurred, the runner must handle it itself.
if (result.isFailure) {
Log.shared.fatal("Failed to read bytes")
}

override fun isClosed(): Boolean {
val channel = this.channel ?: Log.shared.fatal("Channel not set")
return channel.isClosedForSend
val bytes = result.getOrThrow()
return Reader.Result.success(bytes)
}

override suspend fun read(): Reader.Result {
val channel = this.channel ?: Log.shared.fatal("Channel not set")

return try {
val result = channel.receive()
Reader.Result.success(result)
} catch (e: ClosedReceiveChannelException) {
Reader.Result.closed()
} catch (e: Exception) {
Log.shared.fatal(e)
}
}

override fun isClosed(): Boolean {
val channel = this.channel ?: Log.shared.fatal("Channel not set")
return channel.isClosedForSend
}
}
Loading

0 comments on commit a0a397a

Please sign in to comment.