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/Context.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/Context.kt')
-rw-r--r-- | src/main/kotlin/moe/nea/ledger/utils/telemetry/Context.kt | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/ledger/utils/telemetry/Context.kt b/src/main/kotlin/moe/nea/ledger/utils/telemetry/Context.kt new file mode 100644 index 0000000..3c30a52 --- /dev/null +++ b/src/main/kotlin/moe/nea/ledger/utils/telemetry/Context.kt @@ -0,0 +1,57 @@ +package moe.nea.ledger.utils.telemetry + +import com.google.gson.JsonObject + +class Context(val data: MutableMap<String, ContextValue> = mutableMapOf()) : ContextValue.Collatable<Context> { + + inline fun <reified T : ContextValue> getT(key: String): T? { + return get(key) as? T + } + + fun get(key: String): ContextValue? { + return data[key] + } + + fun add(key: String, value: ContextValue) { + data[key] = value + } + + @Suppress("NOTHING_TO_INLINE") + private inline fun <T : ContextValue.Collatable<T>> cope( + left: ContextValue.Collatable<T>, + right: ContextValue + ): ContextValue { + return try { + left.combineWith(right as T) + } catch (ex: Exception) { + // TODO: cope with this better + right + } + } + + override fun combineWith(overrides: Context): Context { + val copy = data.toMutableMap() + for ((key, overrideValue) in overrides.data) { + copy.merge(key, overrideValue) { old, new -> + if (old is ContextValue.Collatable<*>) { + cope(old, new) + } else { + new + } + } + } + return Context(copy) + } + + override fun actualize(): Context { + return this + } + + override fun serialize(): JsonObject { + val obj = JsonObject() + data.forEach { (k, v) -> + obj.add(k, v.serialize()) + } + return obj + } +}
\ No newline at end of file |