Skip to content

Commit

Permalink
Fix issues with TypedValueReference where it indices values of other …
Browse files Browse the repository at this point in the history
…types.
  • Loading branch information
jurmous committed Feb 19, 2024
1 parent d5cf02c commit 296b09d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ class TypedValueReference<E : TypeEnum<T>, T: Any, in CX : IsPropertyContext> in
val type: E,
multiTypeDefinition: IsMultiTypeDefinition<E, T, CX>,
parentReference: CanHaveComplexChildReference<*, *, *, *>?
) : CanHaveComplexChildReference<T, IsSubDefinition<T, CX>,
CanHaveComplexChildReference<*, *, *, *>,
TypedValue<E, T>>(
) : CanHaveComplexChildReference<
T,
IsSubDefinition<T, CX>,
CanHaveComplexChildReference<*, *, *, *>,
TypedValue<E, T>
>(
multiTypeDefinition.definition(type) as IsSubDefinition<T, CX>,
parentReference
),
Expand All @@ -44,8 +47,10 @@ class TypedValueReference<E : TypeEnum<T>, 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<Any, *, *> {
return if (this.propertyDefinition is IsEmbeddedDefinition<*>) {
Expand Down Expand Up @@ -96,5 +101,5 @@ class TypedValueReference<E : TypeEnum<T>, T: Any, in CX : IsPropertyContext> in
)
}

override fun resolve(values: TypedValue<E, T>): T = values.value
override fun resolve(values: TypedValue<E, T>): T? = if (values.type == type) values.value else null
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 296b09d

Please sign in to comment.