diff --git a/core/src/commonMain/kotlin/maryk/core/properties/references/TypedValueReference.kt b/core/src/commonMain/kotlin/maryk/core/properties/references/TypedValueReference.kt index fa4a49a6..2c98585e 100644 --- a/core/src/commonMain/kotlin/maryk/core/properties/references/TypedValueReference.kt +++ b/core/src/commonMain/kotlin/maryk/core/properties/references/TypedValueReference.kt @@ -28,9 +28,12 @@ class TypedValueReference, T: Any, in CX : IsPropertyContext> in val type: E, multiTypeDefinition: IsMultiTypeDefinition, parentReference: CanHaveComplexChildReference<*, *, *, *>? -) : CanHaveComplexChildReference, - CanHaveComplexChildReference<*, *, *, *>, - TypedValue>( +) : CanHaveComplexChildReference< + T, + IsSubDefinition, + CanHaveComplexChildReference<*, *, *, *>, + TypedValue + >( multiTypeDefinition.definition(type) as IsSubDefinition, parentReference ), @@ -44,8 +47,10 @@ class TypedValueReference, T: Any, in CX : IsPropertyContext> in "${it.completeName}.*${type.name}" } ?: "*${type.name}" - override fun resolveFromAny(value: Any) = (value as? TypedValue<*, *>)?.value - ?: throw UnexpectedValueException("Expected typed value to get value by reference") + 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") override fun getEmbedded(name: String, context: IsPropertyContext?): IsPropertyReference { return if (this.propertyDefinition is IsEmbeddedDefinition<*>) { @@ -96,5 +101,5 @@ class TypedValueReference, T: Any, in CX : IsPropertyContext> in ) } - override fun resolve(values: TypedValue): T = values.value + override fun resolve(values: TypedValue): T? = if (values.type == type) values.value else null } diff --git a/store/shared/src/commonMain/kotlin/maryk/datastore/shared/readValue.kt b/store/shared/src/commonMain/kotlin/maryk/datastore/shared/readValue.kt index 5284ddad..57429471 100644 --- a/store/shared/src/commonMain/kotlin/maryk/datastore/shared/readValue.kt +++ b/store/shared/src/commonMain/kotlin/maryk/datastore/shared/readValue.kt @@ -42,10 +42,10 @@ fun readValue( TypeIndicator.SimpleTypeIndicator.byte -> { val typeDefinition = TypeValue.castDefinition(definition) - val valueDefinition = - typeDefinition.definition(type) as IsSimpleValueDefinition<*, *> - val typeEnum = typeDefinition.typeEnum.resolve(type) ?: - throw StorageException("Unknown type $type for $typeDefinition") + val valueDefinition = typeDefinition.definition(type) as? IsSimpleValueDefinition<*, *> + ?: throw StorageException("Unknown type $type for $typeDefinition. Was it added to the EnumDefinition?") + val typeEnum = typeDefinition.typeEnum.resolve(type) + ?: throw StorageException("Unknown type $type for $typeDefinition") val value = valueDefinition.readStorageBytes(valueBytesLeft(), reader) typeEnum.invoke(value) }