From 3cda83bfc4e018b9fc5b36e04407191f08436a01 Mon Sep 17 00:00:00 2001 From: Kieran Wallbanks Date: Thu, 3 Oct 2024 13:57:42 +0100 Subject: [PATCH] feature: Add a MapVariableValueProvider and move existing NONE provider to an object --- src/main/kotlin/com/noxcrew/smp/SMP.kt | 3 ++- .../com/noxcrew/smp/VariableValueProvider.kt | 8 +++++++ .../smp/provider/MapVariableValueProvider.kt | 21 +++++++++++++++++++ .../smp/provider/NoOpVariableValueProvider.kt | 14 +++++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/com/noxcrew/smp/provider/MapVariableValueProvider.kt create mode 100644 src/main/kotlin/com/noxcrew/smp/provider/NoOpVariableValueProvider.kt diff --git a/src/main/kotlin/com/noxcrew/smp/SMP.kt b/src/main/kotlin/com/noxcrew/smp/SMP.kt index 1b43e22..2e7b43f 100644 --- a/src/main/kotlin/com/noxcrew/smp/SMP.kt +++ b/src/main/kotlin/com/noxcrew/smp/SMP.kt @@ -1,6 +1,7 @@ package com.noxcrew.smp import com.noxcrew.smp.SMP.Companion.create +import com.noxcrew.smp.provider.NoOpVariableValueProvider import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job @@ -46,7 +47,7 @@ public interface SMP { * @since 1.0 */ public fun create( - variableValueProvider: VariableValueProvider = VariableValueProvider.NONE, + variableValueProvider: VariableValueProvider = NoOpVariableValueProvider, scopeFactory: () -> CoroutineScope = { CoroutineScope(Job() + Dispatchers.Default) }, ): SMP { return Parser(variableValueProvider, scopeFactory) diff --git a/src/main/kotlin/com/noxcrew/smp/VariableValueProvider.kt b/src/main/kotlin/com/noxcrew/smp/VariableValueProvider.kt index 0785f86..a7dea7a 100644 --- a/src/main/kotlin/com/noxcrew/smp/VariableValueProvider.kt +++ b/src/main/kotlin/com/noxcrew/smp/VariableValueProvider.kt @@ -16,6 +16,14 @@ public fun interface VariableValueProvider { * * @since 1.0 */ + @Deprecated( + message = "Moved to an object", + replaceWith = + ReplaceWith( + expression = "NoOpVariableValueProvider", + imports = ["com.noxcrew.smp.provider.NoOpVariableValueProvider"], + ), + ) public val NONE: VariableValueProvider = VariableValueProvider { throw UnsupportedOperationException("Variables are not supported in this parser!") diff --git a/src/main/kotlin/com/noxcrew/smp/provider/MapVariableValueProvider.kt b/src/main/kotlin/com/noxcrew/smp/provider/MapVariableValueProvider.kt new file mode 100644 index 0000000..4afc600 --- /dev/null +++ b/src/main/kotlin/com/noxcrew/smp/provider/MapVariableValueProvider.kt @@ -0,0 +1,21 @@ +package com.noxcrew.smp.provider + +import com.noxcrew.smp.VariableValueProvider + +/** + * A variable value provider that is backed by a map. + * + * If a value is not present in the map for a given key, an exception will be thrown + * as this implementation uses [getValue]. If you wish to avoid this, consider creating + * the backing map using [withDefault]. + * + * @property backingMap The backing map + * @since 1.1 + */ +public data class MapVariableValueProvider( + public val backingMap: Map, +) : VariableValueProvider { + override suspend fun getValue(name: String): Double { + return backingMap.getValue(name) + } +} diff --git a/src/main/kotlin/com/noxcrew/smp/provider/NoOpVariableValueProvider.kt b/src/main/kotlin/com/noxcrew/smp/provider/NoOpVariableValueProvider.kt new file mode 100644 index 0000000..6819544 --- /dev/null +++ b/src/main/kotlin/com/noxcrew/smp/provider/NoOpVariableValueProvider.kt @@ -0,0 +1,14 @@ +package com.noxcrew.smp.provider + +import com.noxcrew.smp.VariableValueProvider + +/** + * A value provider that throws [UnsupportedOperationException] for all calls. + * + * @since 1.1 + */ +public data object NoOpVariableValueProvider : VariableValueProvider { + override suspend fun getValue(name: String): Double { + throw UnsupportedOperationException("Variables are not supported in this parser!") + } +}