Skip to content

Commit

Permalink
feature: Add a MapVariableValueProvider and move existing NONE provid…
Browse files Browse the repository at this point in the history
…er to an object
  • Loading branch information
kezz committed Oct 3, 2024
1 parent a3eb816 commit 3cda83b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/kotlin/com/noxcrew/smp/SMP.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/com/noxcrew/smp/VariableValueProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Double>,
) : VariableValueProvider {
override suspend fun getValue(name: String): Double {
return backingMap.getValue(name)
}
}
Original file line number Diff line number Diff line change
@@ -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!")
}
}

0 comments on commit 3cda83b

Please sign in to comment.