Skip to content

Commit

Permalink
Traverse inheritance tree to find defaults in rbx_binary serializer (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethloeffler authored Jul 20, 2024
1 parent 3f689f5 commit 944e747
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
5 changes: 2 additions & 3 deletions rbx_binary/src/serializer/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,8 @@ impl<'dom, 'db, W: Write> SerializerState<'dom, 'db, W> {
let default_value = type_info
.class_descriptor
.and_then(|class| {
class
.default_properties
.get(&canonical_name)
database
.find_default_property(class, &canonical_name)
.map(Cow::Borrowed)
})
.or_else(|| Self::fallback_default_value(serialized_ty).map(Cow::Owned))
Expand Down
2 changes: 2 additions & 0 deletions rbx_reflection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

## Unreleased Changes
* Add `superclasses` method to `ReflectionDatabase` to get a set of superclasses for a given class. ([#402])
* Added method `ReflectionDatabase::find_default_property`, which finds the default value of a property given its name and a class that inherits it. ([#420])

[#402]: https://github.com/rojo-rbx/rbx-dom/pull/402
[#420]: https://github.com/rojo-rbx/rbx-dom/pull/420

## 4.5.0 (2024-01-16)
* Update to rbx_types 1.8.
Expand Down
21 changes: 21 additions & 0 deletions rbx_reflection/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ impl<'a> ReflectionDatabase<'a> {

Some(list)
}

/// Finds the default value of a property given its name and a class that
/// contains or inherits the property. Returns `Some(&Variant)` if a default
/// value exists, None otherwise.
pub fn find_default_property(
&'a self,
mut class: &'a ClassDescriptor<'a>,
property_name: &str,
) -> Option<&'a Variant> {
loop {
match class.default_properties.get(property_name) {
None => {
class = self
.classes
.get(class.superclass.as_ref()?)
.expect("superclass that is Some should exist in reflection database")
}
default_value => return default_value,
}
}
}
}

/// Describes a class of Instance, its properties, and its relation to other
Expand Down

0 comments on commit 944e747

Please sign in to comment.