JsonPatch apply now accepts json reader promise

This commit is contained in:
DBotThePony 2024-05-22 19:01:49 +07:00
parent 69e0a8737e
commit d1865900f6
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -5,11 +5,13 @@ import com.google.gson.JsonElement
import com.google.gson.JsonNull import com.google.gson.JsonNull
import com.google.gson.JsonObject import com.google.gson.JsonObject
import com.google.gson.JsonSyntaxException import com.google.gson.JsonSyntaxException
import com.google.gson.stream.JsonReader
import kotlinx.coroutines.future.await import kotlinx.coroutines.future.await
import org.apache.logging.log4j.LogManager import org.apache.logging.log4j.LogManager
import ru.dbotthepony.kommons.gson.get import ru.dbotthepony.kommons.gson.get
import ru.dbotthepony.kstarbound.IStarboundFile import ru.dbotthepony.kstarbound.IStarboundFile
import ru.dbotthepony.kstarbound.Starbound import ru.dbotthepony.kstarbound.Starbound
import java.util.concurrent.CompletableFuture
enum class JsonPatch(val key: String) { enum class JsonPatch(val key: String) {
TEST("test") { TEST("test") {
@ -165,5 +167,25 @@ enum class JsonPatch(val key: String) {
return base return base
} }
suspend fun applyAsync(reader: CompletableFuture<JsonReader>, source: Collection<IStarboundFile>?): JsonElement {
source ?: return Starbound.ELEMENTS_ADAPTER.read(reader.await())
val loaded = source.map { it.asyncJsonReader() }
var base = Starbound.ELEMENTS_ADAPTER.read(reader.await())
val itr = source.iterator()
for (patch in loaded) {
val file = itr.next()
val read = Starbound.ELEMENTS_ADAPTER.read(patch.await())
if (read !is JsonArray) {
LOGGER.error("$patch root element is not an array")
} else {
base = apply(base, read, file)
}
}
return base
}
} }
} }