mirror of
https://github.com/GayPizzaSpecifications/dough.git
synced 2025-08-03 13:31:33 +00:00
dough-fs fixes and cleanup
This commit is contained in:
@ -0,0 +1,3 @@
|
||||
package gay.pizza.dough.fs
|
||||
|
||||
object DefaultFsPathSerializer : FsPathSerializer({ FsPath(it) })
|
@ -22,11 +22,29 @@ interface FsOperations {
|
||||
fun readBytesChunked(path: FsPath, block: (ByteArray, Int) -> Unit)
|
||||
fun <T> readJsonFile(path: FsPath, deserializer: DeserializationStrategy<T>): T
|
||||
|
||||
fun readLines(path: FsPath, block: (String) -> Unit)
|
||||
fun readLines(path: FsPath, block: (String) -> Unit) {
|
||||
readLines(path).forEach(block)
|
||||
}
|
||||
|
||||
fun readLines(path: FsPath): Sequence<String>
|
||||
|
||||
fun <T> readJsonLinesToList(path: FsPath, deserializer: DeserializationStrategy<T>, block: (T) -> Unit)
|
||||
fun <T> readJsonLinesToList(path: FsPath, deserializer: DeserializationStrategy<T>): List<T>
|
||||
fun readLinesToList(path: FsPath): List<String> {
|
||||
val lines = mutableListOf<String>()
|
||||
readLines(path) { line -> lines.add(line) }
|
||||
return lines
|
||||
}
|
||||
|
||||
fun <T> readJsonLines(path: FsPath, deserializer: DeserializationStrategy<T>): Sequence<T>
|
||||
|
||||
fun <T> readJsonLines(path: FsPath, deserializer: DeserializationStrategy<T>, block: (T) -> Unit) {
|
||||
readJsonLines(path, deserializer).forEach(block)
|
||||
}
|
||||
|
||||
fun <T> readJsonLinesToList(path: FsPath, deserializer: DeserializationStrategy<T>): List<T> {
|
||||
val results = mutableListOf<T>()
|
||||
readJsonLines(path, deserializer) { item -> results.add(item) }
|
||||
return results
|
||||
}
|
||||
|
||||
fun writeString(path: FsPath, content: String)
|
||||
fun writeAllBytes(path: FsPath, bytes: ByteArray)
|
||||
|
@ -2,7 +2,7 @@ package gay.pizza.dough.fs
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable(with = FsPathSerializer::class)
|
||||
@Serializable(with = DefaultFsPathSerializer::class)
|
||||
interface FsPath : Comparable<FsPath> {
|
||||
val fullPathString: String
|
||||
val entityNameString: String
|
||||
|
@ -6,11 +6,11 @@ import kotlinx.serialization.descriptors.SerialDescriptor
|
||||
import kotlinx.serialization.encoding.Decoder
|
||||
import kotlinx.serialization.encoding.Encoder
|
||||
|
||||
object FsPathSerializer : KSerializer<FsPath> {
|
||||
open class FsPathSerializer(val construct: (String) -> FsPath) : KSerializer<FsPath> {
|
||||
override val descriptor: SerialDescriptor = String.serializer().descriptor
|
||||
|
||||
override fun deserialize(decoder: Decoder): FsPath {
|
||||
return FsPath(decoder.decodeString())
|
||||
return construct(decoder.decodeString())
|
||||
}
|
||||
|
||||
override fun serialize(encoder: Encoder, value: FsPath) {
|
||||
|
@ -45,20 +45,20 @@ fun FsPath.readBytesChunked(block: (ByteArray, Int) -> Unit) =
|
||||
fun <T> FsPath.readJsonFile(deserializer: DeserializationStrategy<T>): T =
|
||||
operations.readJsonFile(this, deserializer)
|
||||
|
||||
fun FsPath.readLines(block: (String) -> Unit): Unit =
|
||||
operations.readLines(this, block)
|
||||
|
||||
fun FsPath.readLines(): Sequence<String> =
|
||||
operations.readLines(this)
|
||||
|
||||
fun FsPath.readLinesToList(): List<String> {
|
||||
val lines = mutableListOf<String>()
|
||||
readLines { line -> lines.add(line) }
|
||||
return lines
|
||||
}
|
||||
fun FsPath.readLines(block: (String) -> Unit): Unit =
|
||||
operations.readLines(this, block)
|
||||
|
||||
fun <T> FsPath.readJsonLinesToList(deserializer: DeserializationStrategy<T>, block: (T) -> Unit) =
|
||||
operations.readJsonLinesToList(this, deserializer, block)
|
||||
fun FsPath.readLinesToList(): List<String> =
|
||||
operations.readLinesToList(this)
|
||||
|
||||
fun <T> FsPath.readJsonLines(deserializer: DeserializationStrategy<T>): Sequence<T> =
|
||||
operations.readJsonLines(this, deserializer)
|
||||
|
||||
fun <T> FsPath.readJsonLines(deserializer: DeserializationStrategy<T>, block: (T) -> Unit) =
|
||||
operations.readJsonLines(this, deserializer, block)
|
||||
|
||||
fun <T> FsPath.readJsonLinesToList(deserializer: DeserializationStrategy<T>): List<T> =
|
||||
operations.readJsonLinesToList(this, deserializer)
|
||||
|
@ -39,30 +39,15 @@ object JavaFsOperations : FsOperations {
|
||||
override fun <T> readJsonFile(path: FsPath, deserializer: DeserializationStrategy<T>): T =
|
||||
Json.decodeFromString(deserializer, readString(path))
|
||||
|
||||
override fun readLines(path: FsPath, block: (String) -> Unit) {
|
||||
readLines(path).forEach(block)
|
||||
}
|
||||
|
||||
override fun readLines(path: FsPath): Sequence<String> {
|
||||
val stream = path.toJavaPath().bufferedReader()
|
||||
return stream.lineSequence()
|
||||
}
|
||||
|
||||
override fun <T> readJsonLinesToList(path: FsPath, deserializer: DeserializationStrategy<T>, block: (T) -> Unit) {
|
||||
readLines(path) { line ->
|
||||
val trimmed = line.trim()
|
||||
val item = Json.decodeFromString(deserializer, trimmed)
|
||||
block(item)
|
||||
override fun <T> readJsonLines(path: FsPath, deserializer: DeserializationStrategy<T>): Sequence<T> =
|
||||
readLines(path).map { line -> line.trim() }.map { line ->
|
||||
Json.decodeFromString(deserializer, line)
|
||||
}
|
||||
}
|
||||
|
||||
override fun <T> readJsonLinesToList(path: FsPath, deserializer: DeserializationStrategy<T>): List<T> {
|
||||
val results = mutableListOf<T>()
|
||||
readJsonLinesToList(path, deserializer) { item ->
|
||||
results.add(item)
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
override fun writeString(path: FsPath, content: String): Unit = Files.writeString(path.toJavaPath(), content).run {}
|
||||
override fun writeAllBytes(path: FsPath, bytes: ByteArray): Unit = Files.write(path.toJavaPath(), bytes).run {}
|
||||
|
Reference in New Issue
Block a user