Skip to content

Commit 1c476c1

Browse files
committed
Fix
1 parent 6fc10d2 commit 1c476c1

File tree

4 files changed

+105
-87
lines changed

4 files changed

+105
-87
lines changed

.sdkmanrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Enable auto-env through the sdkman_auto_env config
22
# Add key=value pairs of SDKs to use below
3-
java=22-graalce
3+
java=21.0.9-tem

src/main/kotlin/org/alexn/hook/AppConfig.kt

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import arrow.core.Either
66
import com.charleskorn.kaml.Yaml
77
import com.charleskorn.kaml.YamlConfiguration
88
import com.typesafe.config.ConfigFactory
9+
import kotlinx.serialization.ExperimentalSerializationApi
910
import kotlinx.serialization.Serializable
1011
import kotlinx.serialization.hocon.Hocon
12+
import kotlinx.serialization.hocon.decodeFromConfig
1113
import java.io.File
1214
import kotlin.time.Duration
13-
import kotlinx.serialization.ExperimentalSerializationApi
14-
import kotlinx.serialization.hocon.decodeFromConfig
1515

1616
@Serializable
1717
data class AppConfig(
@@ -47,19 +47,21 @@ data class AppConfig(
4747
when (file.extension.lowercase()) {
4848
"hocon", "conf" -> parseHocon(file)
4949
"yaml", "yml" -> parseYaml(file)
50-
else -> Either.Left(
51-
ConfigException(
52-
"Unsupported configuration file format: ${file.extension}",
53-
),
54-
)
50+
else ->
51+
Either.Left(
52+
ConfigException(
53+
"Unsupported configuration file format: ${file.extension}",
54+
),
55+
)
5556
}
5657

5758
fun parseHocon(string: String): Either<ConfigException, AppConfig> =
5859
try {
59-
val r = Hocon.decodeFromConfig(
60-
serializer(),
61-
ConfigFactory.parseString(string).resolve()
62-
)
60+
val r =
61+
Hocon.decodeFromConfig(
62+
serializer(),
63+
ConfigFactory.parseString(string).resolve(),
64+
)
6365
Either.Right(r)
6466
} catch (ex: Exception) {
6567
Either.Left(
@@ -85,15 +87,19 @@ data class AppConfig(
8587

8688
fun parseYaml(string: String): Either<ConfigException, AppConfig> =
8789
try {
88-
Either.Right(yamlParser.decodeFromString(
89-
serializer(),
90-
string,
91-
));
90+
Either.Right(
91+
yamlParser.decodeFromString(
92+
serializer(),
93+
string,
94+
),
95+
)
9296
} catch (ex: Exception) {
93-
Either.Left(ConfigException(
94-
"Failed to parse YAML configuration",
95-
ex,
96-
))
97+
Either.Left(
98+
ConfigException(
99+
"Failed to parse YAML configuration",
100+
ex,
101+
),
102+
)
97103
}
98104

99105
fun parseYaml(file: File): Either<ConfigException, AppConfig> =
@@ -118,3 +124,12 @@ data class AppConfig(
118124
)
119125
}
120126
}
127+
128+
/**
129+
* Exception thrown when there is a configuration error,
130+
* see [AppConfig].
131+
*/
132+
class ConfigException(
133+
message: String,
134+
cause: Throwable? = null,
135+
) : Exception(message, cause)

src/main/kotlin/org/alexn/hook/exceptions.kt

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/test/kotlin/org/alexn/hook/AppConfigTest.kt

Lines changed: 70 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
package org.alexn.hook
44

55
import arrow.core.getOrElse
6+
import kotlinx.serialization.ExperimentalSerializationApi
7+
import kotlinx.serialization.json.Json
68
import kotlin.test.Test
79
import kotlin.test.assertEquals
810
import kotlin.time.Duration.Companion.seconds
9-
import kotlinx.serialization.ExperimentalSerializationApi
10-
import kotlinx.serialization.json.Json
1111

1212
class AppConfigTest {
1313
val expected =
@@ -85,7 +85,8 @@ class AppConfigTest {
8585

8686
@Test
8787
fun parseHoconConfig() {
88-
val config = """
88+
val config =
89+
"""
8990
http {
9091
path = "/"
9192
port = 8080
@@ -104,7 +105,7 @@ class AppConfigTest {
104105
secret = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
105106
}
106107
}
107-
""".trimIndent()
108+
""".trimIndent()
108109

109110
assertEquals(
110111
AppConfig(
@@ -133,64 +134,76 @@ class AppConfigTest {
133134

134135
@Test
135136
fun parseFileYamlAndHocon() {
136-
val yamlConfig = """
137-
http:
138-
path: "/"
139-
port: 8080
140-
141-
runtime:
142-
workers: 2
143-
output: stdout
144-
145-
projects:
146-
myproject:
147-
ref: "refs/heads/gh-pages"
148-
directory: "/var/www/myproject"
149-
command: "git pull"
150-
secret: "xxxxxxxxxxxxxxxxxxxxxxxxxx"
151-
""".trimIndent()
152-
153-
val hoconConfig = """
154-
http {
155-
path = "/"
156-
port = 8080
157-
}
137+
val yamlConfig =
138+
"""
139+
http:
140+
path: "/"
141+
port: 8080
158142
159-
runtime {
160-
workers = 2
161-
output = "stdout"
162-
}
143+
runtime:
144+
workers: 2
145+
output: stdout
163146
164-
projects {
165-
myproject {
166-
ref = "refs/heads/gh-pages"
167-
directory = "/var/www/myproject"
168-
command = "git pull"
169-
secret = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
147+
projects:
148+
myproject:
149+
ref: "refs/heads/gh-pages"
150+
directory: "/var/www/myproject"
151+
command: "git pull"
152+
secret: "xxxxxxxxxxxxxxxxxxxxxxxxxx"
153+
""".trimIndent()
154+
155+
val hoconConfig =
156+
"""
157+
http {
158+
path = "/"
159+
port = 8080
170160
}
171-
}
172-
""".trimIndent()
173161
174-
val expectedConfig = AppConfig(
175-
http = AppConfig.Http(
176-
host = null,
177-
port = 8080,
178-
path = "/",
179-
),
180-
projects = mapOf(
181-
"myproject" to AppConfig.Project(
182-
action = null,
183-
ref = "refs/heads/gh-pages",
184-
directory = "/var/www/myproject",
185-
command = "git pull",
186-
timeout = null,
187-
secret = "xxxxxxxxxxxxxxxxxxxxxxxxxx",
188-
),
189-
),
190-
)
162+
runtime {
163+
workers = 2
164+
output = "stdout"
165+
}
191166
192-
val yamlFile = kotlin.io.path.createTempFile(suffix = ".yaml").toFile()
193-
val hoconFile = kotlin.io.path.createTempFile(suffix = ".conf").toFile()
167+
projects {
168+
myproject {
169+
ref = "refs/heads/gh-pages"
170+
directory = "/var/www/myproject"
171+
command = "git pull"
172+
secret = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
173+
}
174+
}
175+
""".trimIndent()
176+
177+
val expectedConfig =
178+
AppConfig(
179+
http =
180+
AppConfig.Http(
181+
host = null,
182+
port = 8080,
183+
path = "/",
184+
),
185+
projects =
186+
mapOf(
187+
"myproject" to
188+
AppConfig.Project(
189+
action = null,
190+
ref = "refs/heads/gh-pages",
191+
directory = "/var/www/myproject",
192+
command = "git pull",
193+
timeout = null,
194+
secret = "xxxxxxxxxxxxxxxxxxxxxxxxxx",
195+
),
196+
),
197+
)
198+
199+
val yamlFile =
200+
kotlin.io.path
201+
.createTempFile(suffix = ".yaml")
202+
.toFile()
203+
val hoconFile =
204+
kotlin.io.path
205+
.createTempFile(suffix = ".conf")
206+
.toFile()
194207
try {
195208
yamlFile.writeText(yamlConfig)
196209
hoconFile.writeText(hoconConfig)

0 commit comments

Comments
 (0)