Skip to content

Commit 8e830b6

Browse files
committed
feat(util/config): new config parser
1 parent 183ce21 commit 8e830b6

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.osfans.trime.util.config
2+
3+
import com.charleskorn.kaml.YamlList
4+
import com.charleskorn.kaml.YamlMap
5+
import com.charleskorn.kaml.YamlNode
6+
import com.charleskorn.kaml.YamlNull
7+
import com.charleskorn.kaml.YamlScalar
8+
import com.charleskorn.kaml.yamlList
9+
import com.charleskorn.kaml.yamlMap
10+
import com.charleskorn.kaml.yamlScalar
11+
import timber.log.Timber
12+
13+
/**
14+
* New YAML config parser intended to replace the old one.
15+
*/
16+
class Config(private val data: ConfigData = ConfigData()) {
17+
18+
fun loadFromFile(fileName: String) = data.loadFromFile(fileName)
19+
20+
fun isNull(path: String): Boolean {
21+
val p = data.traverse(path)
22+
return p == null || p is YamlNull
23+
}
24+
25+
fun isValue(path: String): Boolean {
26+
val p = data.traverse(path)
27+
return p == null || p is YamlScalar
28+
}
29+
30+
fun isList(path: String): Boolean {
31+
val p = data.traverse(path)
32+
return p == null || p is YamlList
33+
}
34+
35+
fun isMap(path: String): Boolean {
36+
val p = data.traverse(path)
37+
return p == null || p is YamlMap
38+
}
39+
40+
fun getBool(path: String, defValue: Boolean = false): Boolean {
41+
Timber.d("read: $path")
42+
val p = data.traverse(path)?.yamlScalar
43+
return p?.toBoolean() ?: defValue
44+
}
45+
46+
fun getInt(path: String, defValue: Int = 0): Int {
47+
Timber.d("read: $path")
48+
val p = data.traverse(path)?.yamlScalar
49+
return p?.toInt() ?: defValue
50+
}
51+
52+
fun getFloat(path: String, defValue: Float = 0f): Float {
53+
Timber.d("read: $path")
54+
val p = data.traverse(path)?.yamlScalar
55+
return p?.toFloat() ?: defValue
56+
}
57+
58+
fun getString(path: String, defValue: String = ""): String {
59+
Timber.d("read: $path")
60+
val p = data.traverse(path)?.yamlScalar
61+
return p?.content ?: defValue
62+
}
63+
64+
fun getNode(path: String): YamlNode? {
65+
Timber.d("read: $path")
66+
return data.traverse(path)
67+
}
68+
69+
fun getValue(path: String): YamlScalar? {
70+
Timber.d("read: $path")
71+
return data.traverse(path)?.yamlScalar
72+
}
73+
74+
fun getList(path: String): YamlList? {
75+
Timber.d("read: $path")
76+
return data.traverse(path)?.yamlList
77+
}
78+
79+
fun getMap(path: String): YamlMap? {
80+
Timber.d("read: $path")
81+
return data.traverse(path)?.yamlMap
82+
}
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.osfans.trime.util.config
2+
3+
import com.charleskorn.kaml.Yaml
4+
import com.charleskorn.kaml.YamlConfiguration
5+
import com.charleskorn.kaml.YamlException
6+
import com.charleskorn.kaml.YamlMap
7+
import com.charleskorn.kaml.YamlNode
8+
import com.charleskorn.kaml.yamlMap
9+
import timber.log.Timber
10+
import java.io.File
11+
12+
/**
13+
* The wrapper of parsed YAML node.
14+
*/
15+
class ConfigData {
16+
var root: YamlNode? = null
17+
18+
private val yaml = Yaml(
19+
configuration = YamlConfiguration(
20+
strictMode = false,
21+
),
22+
)
23+
24+
fun loadFromFile(fileName: String): Boolean {
25+
val configFile = File(fileName)
26+
if (!configFile.exists()) {
27+
Timber.w("Nonexistent config file $fileName")
28+
return false
29+
}
30+
Timber.i("Loading config file $fileName")
31+
try {
32+
val doc = configFile
33+
.inputStream()
34+
.bufferedReader()
35+
.use { it.readText() }
36+
root = yaml.parseToYamlNode(doc)
37+
} catch (e: YamlException) {
38+
Timber.e("Error parsing YAML: ${e.message}")
39+
return false
40+
}
41+
return true
42+
}
43+
44+
fun traverse(path: String): YamlNode? {
45+
Timber.d("traverse: $path")
46+
if (path.isEmpty() || path == "/") return root
47+
val keys = path.trimEnd('/').split('/')
48+
var p = root
49+
for (key in keys) {
50+
if (p == null || p !is YamlMap) {
51+
return null
52+
}
53+
p = p.yamlMap[key]
54+
}
55+
return p
56+
}
57+
}

0 commit comments

Comments
 (0)