Skip to content

Commit e55738e

Browse files
committed
refactor: simplify class constructors and method return types for improved readability
1 parent 216b04f commit e55738e

9 files changed

Lines changed: 40 additions & 71 deletions

File tree

kirc-blocking/src/main/kotlin/de/cmdjulian/kirc/client/BlockingContainerImageClientFactory.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@ object BlockingContainerImageClientFactory {
3030
keystore: KeyStore? = null,
3131
timeout: Duration = Duration.ofSeconds(15),
3232
tmpPath: Path = Path(SystemTemporaryDirectory.toString()),
33-
): BlockingContainerImageRegistryClient {
34-
return SuspendingContainerImageClientFactory
35-
.create(url, credentials, proxy, skipTlsVerify, keystore, timeout, tmpPath)
36-
.toBlockingClient()
37-
}
33+
): BlockingContainerImageRegistryClient = SuspendingContainerImageClientFactory
34+
.create(url, credentials, proxy, skipTlsVerify, keystore, timeout, tmpPath)
35+
.toBlockingClient()
3836

3937
@JvmStatic
4038
@JvmOverloads

kirc-image/src/main/kotlin/de/cmdjulian/kirc/image/Reference.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ sealed interface Reference {
2626
* Example: "repository:latest"
2727
*/
2828
@ReflectionHint
29-
class Tag(@JsonValue private val value: String) : Reference, Comparable<Tag> {
29+
class Tag(@JsonValue private val value: String) :
30+
Reference,
31+
Comparable<Tag> {
3032
init {
3133
require(value.matches(Regex("\\w[\\w.\\-]{0,127}"))) { "invalid tag" }
3234
}
@@ -64,7 +66,9 @@ class Tag(@JsonValue private val value: String) : Reference, Comparable<Tag> {
6466
* Example: "repository@sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf"
6567
*/
6668
@ReflectionHint
67-
class Digest(@JsonValue private val value: String) : Reference, Comparable<Digest> {
69+
class Digest(@JsonValue private val value: String) :
70+
Reference,
71+
Comparable<Digest> {
6872
init {
6973
require(value.matches(Regex("sha256:[\\da-fA-F]{32,}"))) { "invalid digest" }
7074
}

kirc-reactive/src/main/kotlin/de/cmdjulian/kirc/client/ReactiveContainerImageRegistryClient.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,10 @@ fun SuspendingContainerImageRegistryClient.toReactiveClient() = object : Reactiv
133133
repository: Repository,
134134
reference: Reference,
135135
manifest: ManifestSingle?,
136-
): Mono<ReactiveContainerImageClient> {
137-
return if (manifest == null) {
138-
mono { this@toReactiveClient.toImageClient(repository, reference).toReactiveClient() }
139-
} else {
140-
Mono.just(this@toReactiveClient.toImageClient(repository, reference, manifest).toReactiveClient())
141-
}
136+
): Mono<ReactiveContainerImageClient> = if (manifest == null) {
137+
mono { this@toReactiveClient.toImageClient(repository, reference).toReactiveClient() }
138+
} else {
139+
Mono.just(this@toReactiveClient.toImageClient(repository, reference, manifest).toReactiveClient())
142140
}
143141

144142
override fun upload(

kirc-suspending/src/main/kotlin/de/cmdjulian/kirc/impl/KircApiError.kt

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,25 @@ internal sealed class KircApiError(
1717
val detailMessage = "$message (HTTP statusCode=$statusCode method=$method url=$url)"
1818

1919
/** Error as returned by the registry. */
20-
class Registry(
21-
statusCode: Int,
22-
url: Url,
23-
method: HttpMethod,
24-
val body: RegistryErrorResponse,
25-
) : KircApiError(
26-
statusCode,
27-
url,
28-
method,
29-
null,
30-
"Registry error: [${
31-
body.errors.joinToString(
32-
prefix = "(",
33-
postfix = ")",
34-
) { "code=${it.code}, message=${it.message}, detail=${it.detail}" }
35-
}]",
36-
) {
20+
class Registry(statusCode: Int, url: Url, method: HttpMethod, val body: RegistryErrorResponse) :
21+
KircApiError(
22+
statusCode,
23+
url,
24+
method,
25+
null,
26+
"Registry error: [${
27+
body.errors.joinToString(
28+
prefix = "(",
29+
postfix = ")",
30+
) { "code=${it.code}, message=${it.message}, detail=${it.detail}" }
31+
}]",
32+
) {
3733
override fun toString(): String = "KircApiError.Registry -> ${this@Registry.message}"
3834
}
3935

4036
/** Error caused by network issues, e.g. no connection, timeout, etc. */
41-
class Network(
42-
url: Url,
43-
method: HttpMethod,
44-
override val cause: Throwable,
45-
) : KircApiError(-1, url, method, cause, cause.message ?: "Unknown network error") {
37+
class Network(url: Url, method: HttpMethod, override val cause: Throwable) :
38+
KircApiError(-1, url, method, cause, cause.message ?: "Unknown network error") {
4639
override fun toString(): String = "KircApiError.Network -> ${this@Network.message}"
4740
}
4841

@@ -53,12 +46,8 @@ internal sealed class KircApiError(
5346
*
5447
* Appears while manually extracting fields from responses (no deserialization)
5548
*/
56-
class Header(
57-
statusCode: Int,
58-
url: Url,
59-
method: HttpMethod,
60-
override val message: String,
61-
) : KircApiError(statusCode, url, method, null, message) {
49+
class Header(statusCode: Int, url: Url, method: HttpMethod, override val message: String) :
50+
KircApiError(statusCode, url, method, null, message) {
6251
override fun toString(): String = "KircApiError.Header -> ${this@Header.message}"
6352
}
6453

@@ -67,25 +56,16 @@ internal sealed class KircApiError(
6756
*
6857
* E.g. when deserializing error responses from the registry or the token response from the auth server.
6958
*/
70-
class Json(
71-
statusCode: Int,
72-
url: Url,
73-
method: HttpMethod,
74-
override val cause: Throwable,
75-
message: String,
76-
) : KircApiError(statusCode, url, method, cause, message) {
59+
class Json(statusCode: Int, url: Url, method: HttpMethod, override val cause: Throwable, message: String) :
60+
KircApiError(statusCode, url, method, cause, message) {
7761
override fun toString(): String = "KircApiError.Json -> ${this@Json.message}"
7862
}
7963

8064
/**
8165
* Thrown when an authentication bearer token could not be retrieved from the auth server.
8266
*/
83-
class Bearer(
84-
statusCode: Int,
85-
url: Url,
86-
method: HttpMethod,
87-
override val message: String,
88-
) : KircApiError(statusCode, url, method, null, message) {
67+
class Bearer(statusCode: Int, url: Url, method: HttpMethod, override val message: String) :
68+
KircApiError(statusCode, url, method, null, message) {
8969
override fun toString(): String = "KircApiError.Bearer -> ${this@Bearer.message}"
9070
}
9171

kirc-suspending/src/main/kotlin/de/cmdjulian/kirc/impl/delegate/ImageDownloader.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,7 @@ internal class ImageDownloader(private val client: SuspendingContainerImageRegis
270270
val configStream: Source,
271271
) : ResolvedManifest
272272

273-
data class List(
274-
override val manifest: ManifestList,
275-
override val digest: Digest,
276-
override val size: Long,
277-
) : ResolvedManifest
273+
data class List(override val manifest: ManifestList, override val digest: Digest, override val size: Long) :
274+
ResolvedManifest
278275
}
279276
}

kirc-suspending/src/main/kotlin/de/cmdjulian/kirc/utils/KtorInitUtils.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,7 @@ private suspend fun RefreshTokensParams.bearerAuth(credentials: RegistryCredenti
109109

110110
// HELPER
111111

112-
private data class RegistryAuthCredentials(
113-
val realm: String,
114-
val scope: String?,
115-
val service: String?,
116-
)
112+
private data class RegistryAuthCredentials(val realm: String, val scope: String?, val service: String?)
117113

118114
private fun RefreshTokensParams.parseWWWAuthHeader(header: String?): RegistryAuthCredentials {
119115
val wwwAuth = header?.runCatching(HttpAuthCredentials::parse)?.getOrElse {

kirc-suspending/src/test/kotlin/de/cmdjulian/kirc/JacksonExtensions.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@ package de.cmdjulian.kirc
22

33
import de.cmdjulian.kirc.impl.serialization.JsonMapper
44

5-
internal inline fun <reified T : Any> String.unmarshal(): T {
6-
return JsonMapper.readValue(this, T::class.java)
7-
}
5+
internal inline fun <reified T : Any> String.unmarshal(): T = JsonMapper.readValue(this, T::class.java)

kirc-suspending/src/test/kotlin/de/cmdjulian/kirc/testcontainer/AuthServerTestContainer.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import org.testcontainers.containers.Network
55
import org.testcontainers.containers.wait.strategy.Wait
66
import org.testcontainers.utility.MountableFile
77

8-
class AuthServerTestContainer(
9-
network: Network,
10-
) : GenericContainer<AuthServerTestContainer>("cesanta/docker_auth:1") {
8+
class AuthServerTestContainer(network: Network) : GenericContainer<AuthServerTestContainer>("cesanta/docker_auth:1") {
119

1210
init {
1311
withNetwork(network)

kirc-suspending/src/test/kotlin/de/cmdjulian/kirc/testcontainer/RegistryTestContainer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import org.testcontainers.containers.GenericContainer
44
import org.testcontainers.containers.wait.strategy.Wait
55
import org.testcontainers.utility.MountableFile
66

7-
class RegistryTestContainer() : GenericContainer<RegistryTestContainer>("registry:2") {
7+
class RegistryTestContainer : GenericContainer<RegistryTestContainer>("registry:2") {
88

99
private val registryDataFolder = "/var/lib/registry"
1010

0 commit comments

Comments
 (0)