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 42e59ab
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 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 value to be a TypedValue of type $type but was $value")

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 @@ -24,9 +24,15 @@ class TypedValueReferenceTest {

@Test
fun getValueFromMap() {
val typedValue = T1("string")
val typedValueWrong = T1("string")

expect("string") { this.typedValueReference.resolveFromAny(typedValue) }
assertFailsWith<UnexpectedValueException> {
this.typedValueReference.resolveFromAny(typedValueWrong)
}

val typedValue = S2(15.toShort())

expect(15.toShort()) { this.typedValueReference.resolveFromAny(typedValue) }

assertFailsWith<UnexpectedValueException> {
this.typedValueReference.resolveFromAny("wrongInput")
Expand Down
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 42e59ab

Please sign in to comment.