-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
206 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
web/backend/src/main/kotlin/dev/kordex/extra/web/types/Identifier.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package dev.kordex.extra.web.types | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable(with = IdentifierSerializer::class) | ||
public data class Identifier(public val namespace: String, public val id: String) { | ||
init { | ||
if (":" in namespace || ":" in id) { | ||
error("Namespace and ID must not contain a colon character (:)") | ||
} | ||
} | ||
|
||
override fun toString(): String = | ||
"$namespace:$id" | ||
|
||
// Generated methods | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (javaClass != other?.javaClass) return false | ||
|
||
other as Identifier | ||
|
||
if (namespace != other.namespace) return false | ||
if (id != other.id) return false | ||
|
||
return true | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = namespace.hashCode() | ||
result = 31 * result + id.hashCode() | ||
return result | ||
} | ||
} | ||
|
||
public fun Identifier(identifier: String): Identifier { | ||
if (identifier.count { it == ':' } != 1) { | ||
error("Identifiers must contain exactly one colon character (:), separating the namespace and ID.") | ||
} | ||
|
||
val parts = identifier.split(":", limit = 2) | ||
|
||
return Identifier(parts.first(), parts.last()) | ||
} |
26 changes: 26 additions & 0 deletions
26
web/backend/src/main/kotlin/dev/kordex/extra/web/types/IdentifierSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package dev.kordex.extra.web.types | ||
|
||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.descriptors.PrimitiveKind | ||
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
|
||
public class IdentifierSerializer : KSerializer<Identifier> { | ||
override val descriptor: SerialDescriptor = | ||
PrimitiveSerialDescriptor("Identifier", PrimitiveKind.STRING) | ||
|
||
override fun deserialize(decoder: Decoder): Identifier = | ||
Identifier(decoder.decodeString()) | ||
|
||
override fun serialize(encoder: Encoder, value: Identifier) { | ||
encoder.encodeString(value.toString()) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
web/backend/src/main/kotlin/dev/kordex/extra/web/values/TimedContainer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package dev.kordex.extra.web.values | ||
|
||
import kotlinx.datetime.Instant | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
public data class TimedContainer<V : Any?> ( | ||
val value: V, | ||
val time: Instant, | ||
) |
41 changes: 41 additions & 0 deletions
41
web/backend/src/main/kotlin/dev/kordex/extra/web/values/types/SimpleValue.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package dev.kordex.extra.web.values.types | ||
|
||
import dev.kord.common.entity.optional.Optional | ||
import dev.kord.common.entity.optional.optional | ||
import dev.kordex.extra.web.types.Identifier | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.serializer | ||
|
||
public class SimpleValue<T : Any> public constructor( | ||
override val identifier: Identifier, | ||
override val writable: Boolean, | ||
override val serializer: KSerializer<T>, | ||
) : Value<T?, T?, T>() { | ||
private var optional: Optional<T?> = Optional.Missing() | ||
|
||
public override fun read(): T? = | ||
if (optional is Optional.Missing) { | ||
error("Value has not been set.") | ||
} else { | ||
optional.value | ||
} | ||
|
||
public override fun write(value: T?) { | ||
this.optional = value.optional() | ||
} | ||
|
||
public fun clear() { | ||
this.optional = Optional.Missing() | ||
} | ||
} | ||
|
||
public inline fun <reified T : Any> SimpleValue( | ||
identifier: Identifier, | ||
writable: Boolean | ||
): SimpleValue<T> = SimpleValue(identifier, writable, serializer()) |
41 changes: 41 additions & 0 deletions
41
web/backend/src/main/kotlin/dev/kordex/extra/web/values/types/TrackedValue.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package dev.kordex.extra.web.values.types | ||
|
||
import com.kotlindiscord.kord.extensions.utils.collections.FixedLengthQueue | ||
import dev.kordex.extra.web.oldvalues.ValueInterval | ||
import dev.kordex.extra.web.types.Identifier | ||
import dev.kordex.extra.web.values.TimedContainer | ||
import kotlinx.datetime.Clock | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.serializer | ||
|
||
public class TrackedValue<T : Any>( | ||
override val identifier: Identifier, | ||
|
||
public val maxValues: Int = 48, | ||
public val precision: ValueInterval = ValueInterval.HalfHour, | ||
|
||
public override val serializer: KSerializer<T>, | ||
) : Value<T?, List<TimedContainer<T?>>, T>() { | ||
private val values: FixedLengthQueue<TimedContainer<T?>> = FixedLengthQueue(maxValues) | ||
|
||
public override fun read(): List<TimedContainer<T?>> = | ||
values.getAll() | ||
|
||
public override fun write(value: T?) { | ||
values.push( | ||
TimedContainer(value, Clock.System.now()) | ||
) | ||
} | ||
} | ||
|
||
public inline fun <reified T : Any> TrackedValue( | ||
identifier: Identifier, | ||
maxValues: Int = 48, | ||
precision: ValueInterval = ValueInterval.HalfHour, | ||
): TrackedValue<T> = TrackedValue(identifier, maxValues, precision, serializer()) |
20 changes: 20 additions & 0 deletions
20
web/backend/src/main/kotlin/dev/kordex/extra/web/values/types/Value.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package dev.kordex.extra.web.values.types | ||
|
||
import dev.kordex.extra.web.types.Identifier | ||
import kotlinx.serialization.KSerializer | ||
|
||
public abstract class Value<I : Any?, O : Any?, T : Any> { | ||
public abstract val identifier: Identifier | ||
public abstract val serializer: KSerializer<T> | ||
|
||
public open val writable: Boolean = false | ||
|
||
public abstract fun write(value: I) | ||
public abstract fun read(): O | ||
} |