-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support simple typed value reference. This so they can be used in ind…
…ices and simple comparisons.
- Loading branch information
Showing
14 changed files
with
345 additions
and
17 deletions.
There are no files selected for viewing
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
122 changes: 122 additions & 0 deletions
122
core/src/commonMain/kotlin/maryk/core/properties/references/SimpleTypedValueReference.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,122 @@ | ||
package maryk.core.properties.references | ||
|
||
import maryk.core.exceptions.StorageException | ||
import maryk.core.exceptions.UnexpectedValueException | ||
import maryk.core.extensions.bytes.calculateVarByteLength | ||
import maryk.core.extensions.bytes.writeVarBytes | ||
import maryk.core.models.IsRootDataModel | ||
import maryk.core.properties.IsPropertyContext | ||
import maryk.core.properties.definitions.IsChangeableValueDefinition | ||
import maryk.core.properties.definitions.IsMultiTypeDefinition | ||
import maryk.core.properties.definitions.IsSimpleValueDefinition | ||
import maryk.core.properties.definitions.index.IndexKeyPartType | ||
import maryk.core.properties.definitions.index.toReferenceStorageByteArray | ||
import maryk.core.properties.enum.MultiTypeEnum | ||
import maryk.core.properties.enum.TypeEnum | ||
import maryk.core.properties.exceptions.RequiredException | ||
import maryk.core.properties.types.Bytes | ||
import maryk.core.properties.types.TypedValue | ||
import maryk.core.protobuf.ProtoBuf | ||
import maryk.core.protobuf.WireType.VAR_INT | ||
import maryk.core.protobuf.WriteCacheReader | ||
import maryk.core.protobuf.WriteCacheWriter | ||
import maryk.core.query.pairs.ReferenceValuePair | ||
import maryk.core.values.IsValuesGetter | ||
|
||
/** | ||
* Reference to a simple value by [type] [E] on [parentReference] | ||
* Can be a reference to a type below a multi type wrapper or for like multi types within lists | ||
*/ | ||
class SimpleTypedValueReference<E : TypeEnum<T>, T: Any, in CX : IsPropertyContext> internal constructor( | ||
val type: E, | ||
multiTypeDefinition: IsMultiTypeDefinition<E, T, CX>, | ||
parentReference: CanHaveComplexChildReference<*, *, *, *>? | ||
) : CanHaveSimpleChildReference< | ||
T, | ||
IsSimpleValueDefinition<T, CX>, | ||
CanHaveComplexChildReference<*, *, *, *>, | ||
TypedValue<E, T> | ||
>( | ||
multiTypeDefinition.definition(type) as IsSimpleValueDefinition<T, CX>, | ||
parentReference | ||
), | ||
IsIndexablePropertyReference<T>, | ||
IsPropertyReferenceWithParent<T, IsSimpleValueDefinition<T, CX>, CanHaveComplexChildReference<*, *, *, *>, TypedValue<E, T>> { | ||
|
||
override val indexKeyPartType = IndexKeyPartType.Reference | ||
override val referenceStorageByteArray by lazy { Bytes(this.toReferenceStorageByteArray()) } | ||
|
||
override val completeName: String | ||
get() = this.parentReference?.let { | ||
"${it.completeName}.>${type.name}" | ||
} ?: ">${type.name}" | ||
|
||
override fun resolveFromAny(value: Any) = (value as? TypedValue<*, *>)?.let { | ||
@Suppress("UNCHECKED_CAST") | ||
if (it.type == type) it.value as T? else null | ||
} ?: throw UnexpectedValueException("Expected typed value to get value by reference") | ||
|
||
/** Convenience infix method to create reference [value] pairs */ | ||
@Suppress("UNCHECKED_CAST") | ||
infix fun with(value: T): ReferenceValuePair<T> = | ||
ReferenceValuePair(this as IsPropertyReference<T, IsChangeableValueDefinition<T, IsPropertyContext>, *>, value) | ||
|
||
override fun calculateTransportByteLength(cacher: WriteCacheWriter): Int { | ||
val parentLength = parentReference?.calculateTransportByteLength(cacher) ?: 0 | ||
return parentLength + 1 + type.index.calculateVarByteLength() | ||
} | ||
|
||
override fun writeTransportBytes(cacheGetter: WriteCacheReader, writer: (byte: Byte) -> Unit) { | ||
this.parentReference?.writeTransportBytes(cacheGetter, writer) | ||
ProtoBuf.writeKey(1u, VAR_INT, writer) | ||
type.index.writeVarBytes(writer) | ||
} | ||
|
||
override fun calculateSelfStorageByteLength() = 0 | ||
|
||
override fun writeSelfStorageBytes(writer: (byte: Byte) -> Unit) { | ||
// The storage bytes are purely for getting the stored value and cannot be converted back to reference | ||
} | ||
|
||
override fun resolve(values: TypedValue<E, T>): T? = if (values.type == type) values.value else null | ||
|
||
override fun getValue(values: IsValuesGetter): T { | ||
@Suppress("UNCHECKED_CAST") | ||
val typedValue = values[parentReference as IsPropertyReference<Any, *, *>] | ||
?: throw RequiredException(parentReference) | ||
return if (typedValue is TypedValue<*, *>) { | ||
if (typedValue.type == type) { | ||
@Suppress("UNCHECKED_CAST") | ||
typedValue.value as T | ||
} else throw RequiredException(this) | ||
} else if (typedValue is MultiTypeEnum<*>) { | ||
throw RequiredException(this) | ||
} else throw StorageException("Unknown type for $typedValue") | ||
} | ||
|
||
override fun isForPropertyReference(propertyReference: AnyPropertyReference): Boolean = | ||
propertyReference == this | ||
|
||
override fun toQualifierStorageByteArray() = parentReference?.toStorageByteArray() | ||
|
||
override fun calculateReferenceStorageByteLength(): Int { | ||
return this.parentReference?.calculateStorageByteLength() ?: 0 | ||
} | ||
|
||
override fun writeReferenceStorageBytes(writer: (Byte) -> Unit) { | ||
this.parentReference?.writeStorageBytes(writer) | ||
} | ||
|
||
override fun isCompatibleWithModel(dataModel: IsRootDataModel): Boolean = | ||
dataModel.compatibleWithReference(this) | ||
|
||
override fun readStorageBytes(length: Int, reader: () -> Byte): T = | ||
comparablePropertyDefinition.readStorageBytes(length, reader) | ||
|
||
override fun calculateStorageByteLength(value: T): Int = | ||
comparablePropertyDefinition.calculateStorageByteLength(value) | ||
|
||
override fun writeStorageBytes(value: T, writer: (byte: Byte) -> Unit) { | ||
comparablePropertyDefinition.writeStorageBytes(value, writer) | ||
} | ||
} |
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
78 changes: 78 additions & 0 deletions
78
core/src/commonTest/kotlin/maryk/core/properties/references/SimpleTypedValueReferenceTest.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,78 @@ | ||
package maryk.core.properties.references | ||
|
||
import maryk.core.exceptions.UnexpectedValueException | ||
import maryk.core.properties.types.invoke | ||
import maryk.core.protobuf.WriteCache | ||
import maryk.lib.extensions.toHex | ||
import maryk.test.ByteCollector | ||
import maryk.test.models.MarykTypeEnum.T1 | ||
import maryk.test.models.Measurement | ||
import maryk.test.models.MeasurementType | ||
import kotlin.test.Test | ||
import kotlin.test.assertFailsWith | ||
import kotlin.test.assertSame | ||
import kotlin.test.expect | ||
|
||
class SimpleTypedValueReferenceTest { | ||
private val typedValueReference = | ||
Measurement { measurement simpleRefAtType MeasurementType.Number } | ||
|
||
@Test | ||
fun cacheReferenceTest() { | ||
assertSame(typedValueReference, Measurement { measurement simpleRefAtType MeasurementType.Number }) | ||
} | ||
|
||
@Test | ||
fun getValueFromMap() { | ||
val typedValueWrong = T1("string") | ||
|
||
assertFailsWith<UnexpectedValueException> { | ||
this.typedValueReference.resolveFromAny(typedValueWrong) | ||
} | ||
|
||
val typedValue = MeasurementType.Number(15.toShort()) | ||
|
||
expect(15.toShort()) { this.typedValueReference.resolveFromAny(typedValue) } | ||
|
||
assertFailsWith<UnexpectedValueException> { | ||
this.typedValueReference.resolveFromAny("wrongInput") | ||
} | ||
} | ||
|
||
@Test | ||
fun testCompleteName() { | ||
expect("measurement.>Number") { typedValueReference.completeName } | ||
} | ||
|
||
@Test | ||
fun writeAndReadStringValue() { | ||
expect(typedValueReference) { Measurement.getPropertyReferenceByName(typedValueReference.completeName) } | ||
} | ||
|
||
@Test | ||
fun writeAndReadTransportBytes() { | ||
val bc = ByteCollector() | ||
val cache = WriteCache() | ||
|
||
bc.reserve( | ||
typedValueReference.calculateTransportByteLength(cache) | ||
) | ||
typedValueReference.writeTransportBytes(cache, bc::write) | ||
|
||
expect("020803") { bc.bytes!!.toHex() } | ||
|
||
expect(typedValueReference) { Measurement.getPropertyReferenceByBytes(bc.size, bc::read) } | ||
} | ||
|
||
@Test | ||
fun writeAndReadStorageBytes() { | ||
val bc = ByteCollector() | ||
|
||
bc.reserve( | ||
typedValueReference.calculateStorageByteLength() | ||
) | ||
typedValueReference.writeStorageBytes(bc::write) | ||
|
||
expect("11") { bc.bytes!!.toHex() } | ||
} | ||
} |
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
Oops, something went wrong.