Skip to content

Commit 1f767d9

Browse files
Merge pull request #51 from aPureBase/file-path-mapping
Add std libmappings, Path, File, ...
2 parents 27040e2 + 6af9e4e commit 1f767d9

File tree

3 files changed

+112
-4
lines changed

3 files changed

+112
-4
lines changed

arkenv/src/main/kotlin/com/apurebase/arkenv/ArkenvMapper.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package com.apurebase.arkenv
22

33
import com.apurebase.arkenv.util.split
4+
import java.io.File
5+
import java.net.URI
6+
import java.net.URL
7+
import java.nio.file.Path
8+
import java.nio.file.Paths
49
import kotlin.reflect.KClass
510

611
/**
@@ -35,6 +40,13 @@ internal object ArkenvMapper {
3540
DoubleArray::class -> split().map(String::toDouble).toDoubleArray()
3641
BooleanArray::class -> split().map(String::toBoolean).toBooleanArray()
3742
ByteArray::class -> split().map(String::toByte).toByteArray()
43+
Path::class -> Paths.get(this)
44+
File::class -> File(this)
45+
URL::class -> URL(this)
46+
URI::class -> URI(this)
47+
IntRange::class -> split("..").let { IntRange(it[0].toInt(), it[1].toInt()) }
48+
LongRange::class -> split("..").let { LongRange(it[0].toLong(), it[1].toLong()) }
49+
CharRange::class -> CharRange(this[0], this[3])
3850
else -> throw UnsupportedMappingException(key, clazz)
3951
}
4052
}

arkenv/src/test/kotlin/com/apurebase/arkenv/MappingTests.kt

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ package com.apurebase.arkenv
33
import com.apurebase.arkenv.test.expectThat
44
import com.apurebase.arkenv.test.parse
55
import com.apurebase.arkenv.util.argument
6+
import com.apurebase.arkenv.util.parse
67
import org.junit.jupiter.api.Test
78
import org.junit.jupiter.api.TestInstance
9+
import strikt.api.expectThat
810
import strikt.assertions.isEqualTo
11+
import java.io.File
12+
import java.net.URI
13+
import java.net.URL
14+
import java.nio.file.Path
915

1016
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
1117
internal class MappingTests {
@@ -17,8 +23,8 @@ internal class MappingTests {
1723
val list: List<String> by argument()
1824
val collection: Collection<String> by argument()
1925
}.parse("LIST", input, "COLLECTION", input).expectThat {
20-
get { list }.isEqualTo(output)
21-
get { collection }.isEqualTo(output)
26+
get { list } isEqualTo output
27+
get { collection } isEqualTo output
2228
}
2329
}
2430

@@ -94,6 +100,83 @@ internal class MappingTests {
94100
}
95101
}
96102

103+
@Test fun `Path should map`() {
104+
val expectedPath = "C:\\"
105+
val configuration = object {
106+
val path: Path by argument()
107+
}
108+
109+
Arkenv.parse(configuration, arrayOf("--path", expectedPath))
110+
111+
expectThat(configuration).get { path.toString() } isEqualTo expectedPath
112+
}
113+
114+
@Test fun `File should map`() {
115+
val expectedPath = "C:\\"
116+
val configuration = object {
117+
val file: File by argument()
118+
}
119+
120+
Arkenv.parse(configuration, arrayOf("--file", expectedPath))
121+
122+
expectThat(configuration).get { file.toString() } isEqualTo expectedPath
123+
}
124+
125+
@Test fun url() {
126+
val expectedUrl = "https://arkenv.io/features/mapping/"
127+
val configuration = object {
128+
val url: URL by argument()
129+
}
130+
131+
Arkenv.parse(configuration, arrayOf("--url", expectedUrl))
132+
133+
expectThat(configuration).get { url.toString() } isEqualTo expectedUrl
134+
}
135+
136+
@Test fun uri() {
137+
val expectedUri = "arkenv.io/features/mapping"
138+
val configuration = object {
139+
val uri: URI by argument()
140+
}
141+
142+
Arkenv.parse(configuration, arrayOf("--uri", expectedUri))
143+
144+
expectThat(configuration).get { uri.toString() } isEqualTo expectedUri
145+
}
146+
147+
@Test fun intRange() {
148+
val expectedRange = -5..101
149+
val configuration = object {
150+
val range: IntRange by argument()
151+
}
152+
153+
Arkenv.parse(configuration, arrayOf("--range", "-5..101"))
154+
155+
expectThat(configuration).get { range } isEqualTo expectedRange
156+
}
157+
158+
@Test fun longRange() {
159+
val expectedRange = -5L..101
160+
val configuration = object {
161+
val range: LongRange by argument()
162+
}
163+
164+
Arkenv.parse(configuration, arrayOf("--range", "-5..101"))
165+
166+
expectThat(configuration).get { range } isEqualTo expectedRange
167+
}
168+
169+
@Test fun charRange() {
170+
val expectedRange = CharRange('z', 'a')
171+
val configuration = object {
172+
val range: CharRange by argument()
173+
}
174+
175+
Arkenv.parse(configuration, arrayOf("--range", "z..a"))
176+
177+
expectThat(configuration).get { range } isEqualTo expectedRange
178+
}
179+
97180
private val floatingPointInput = "1.1,29.92,-387.9999"
98181
private val floatingPointOutput = listOf(1.1, 29.92, -387.9999)
99182
private val numericInput = "1,-29,387"

docs/features/mapping.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ nav_order: 13
1010
The following mappings are supported by default:
1111

1212
```kotlin
13-
object Ark : Arkenv() {
13+
object Configuration {
1414
val int: Int by argument()
1515
val long: Long by argument()
1616
val string: String by argument()
1717
val char: Char by argument()
18+
19+
// arrays
1820
val intArray: IntArray by argument()
1921
val shortArray: ShortArray by argument()
2022
val charArray: CharArray by argument()
@@ -25,7 +27,18 @@ object Ark : Arkenv() {
2527
val byteArray: ByteArray by argument()
2628

2729
val stringList: List<String> by argument()
28-
val stringCollection: Collection<String> by argument()
30+
val stringCollection: Collection<String> by argument()
31+
32+
// IO
33+
val file: File by argument()
34+
val path: Path by argument()
35+
val url: URL by argument()
36+
val uri: URI by argument()
37+
38+
// ranges
39+
val intRange: IntRange by argument() // -5..5
40+
val longRange: LongRange by argument() // -5..5
41+
val charRange: CharRange by argument() // a..z
2942
}
3043
```
3144

0 commit comments

Comments
 (0)