diff options
author | Linnea Gräf <nea@nea.moe> | 2024-12-13 17:42:27 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-12-13 17:42:27 +0100 |
commit | c4e4985b8b96ed156851c6be6d4a8d5e110c3040 (patch) | |
tree | 4dc7d19ccff486a9f612738db7ea196beffb38d1 /src/main/kotlin/moe/nea/ledger/utils/telemetry/ContextValue.kt | |
parent | 4e986409ad65aa5c11baf7e8571b78b89ec260a4 (diff) | |
download | LocalTransactionLedger-c4e4985b8b96ed156851c6be6d4a8d5e110c3040.tar.gz LocalTransactionLedger-c4e4985b8b96ed156851c6be6d4a8d5e110c3040.tar.bz2 LocalTransactionLedger-c4e4985b8b96ed156851c6be6d4a8d5e110c3040.zip |
Add error reporting framework
Diffstat (limited to 'src/main/kotlin/moe/nea/ledger/utils/telemetry/ContextValue.kt')
-rw-r--r-- | src/main/kotlin/moe/nea/ledger/utils/telemetry/ContextValue.kt | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/ledger/utils/telemetry/ContextValue.kt b/src/main/kotlin/moe/nea/ledger/utils/telemetry/ContextValue.kt new file mode 100644 index 0000000..b5891fc --- /dev/null +++ b/src/main/kotlin/moe/nea/ledger/utils/telemetry/ContextValue.kt @@ -0,0 +1,70 @@ +package moe.nea.ledger.utils.telemetry + +import com.google.gson.JsonElement +import com.google.gson.JsonObject + +interface ContextValue { + companion object { + fun <T : Collatable<T>> lazyCollatable(value: () -> Collatable<T>): Collatable<T> { + return LazyCollatable(value) + } + + fun lazy(value: () -> ContextValue): ContextValue { + return object : ContextValue { + val value by kotlin.lazy(value) + override fun serialize(): JsonElement { + return this.value.serialize() + } + } + } + + fun bool(boolean: Boolean): ContextValue { + return BooleanContext(boolean) + } + + fun string(message: String): ContextValue { + return StringContext(message) + } + + fun jsonObject(vararg pairs: Pair<String, JsonElement>): ContextValue { + val obj = JsonObject() + for ((l, r) in pairs) { + obj.add(l, r) + } + return JsonElementContext(obj) + } + + fun compound(vararg pairs: Pair<String, String>): ContextValue { + val obj = JsonObject() + for ((l, r) in pairs) { + obj.addProperty(l, r) + } + // TODO: should this be its own class? + return JsonElementContext(obj) + } + } + + // TODO: allow other serialization formats + fun serialize(): JsonElement + interface Collatable<T : Collatable<T>> : ContextValue { + fun combineWith(overrides: T): T + fun actualize(): T + } + + private class LazyCollatable<T : Collatable<T>>( + provider: () -> Collatable<T>, + ) : Collatable<T> { + val value by kotlin.lazy(provider) + override fun actualize(): T { + return value.actualize() + } + + override fun combineWith(overrides: T): T { + return value.combineWith(overrides) + } + + override fun serialize(): JsonElement { + return value.serialize() + } + } +} |