Skip to content

Commit

Permalink
Improve field attribute parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGVSV committed Jul 31, 2023
1 parent fa277e3 commit 6a62619
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use crate::REFLECT_ATTRIBUTE_NAME;
use syn::meta::ParseNestedMeta;
use syn::{Attribute, LitStr, Token};
use syn::{Attribute, Token};

pub(crate) static IGNORE_SERIALIZATION_ATTR: &str = "skip_serializing";
pub(crate) static IGNORE_ALL_ATTR: &str = "ignore";
Expand Down Expand Up @@ -98,12 +98,11 @@ fn parse_meta(args: &mut ReflectFieldAttr, meta: ParseNestedMeta) -> Result<(),
if meta.path.is_ident(DEFAULT_ATTR) {
// Allow:
// - `#[reflect(default)]`
// - `#[reflect(default = "path::to::func")]`
// - `#[reflect(default = path::to::func)]`
match args.default {
DefaultBehavior::Required => {
if meta.input.peek(Token![=]) {
let lit = meta.value()?.parse::<LitStr>()?;
args.default = DefaultBehavior::Func(lit.parse()?);
args.default = DefaultBehavior::Func(meta.value()?.parse()?);
} else {
args.default = DefaultBehavior::Default;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_reflect/bevy_reflect_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ pub fn derive_reflect(input: TokenStream) -> TokenStream {
///
/// By default, this attribute denotes that the field's type implements [`Default`].
/// However, it can also take in a path string to a user-defined function that will return the default value.
/// This takes the form: `#[reflect(default = "path::to::my_function")]` where `my_function` is a parameterless
/// This takes the form: `#[reflect(default = path::to::my_function)]` where `my_function` is a parameterless
/// function that must return some default value for the type.
///
/// Specifying a custom default can be used to give different fields their own specialized defaults,
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_reflect/src/from_reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub trait FromReflect: Reflect + Sized {
/// ```
/// # use bevy_reflect::{DynamicTupleStruct, FromReflect, Reflect};
/// #[derive(Reflect, PartialEq, Eq, Debug)]
/// struct Foo(#[reflect(default = "default_value")] usize);
/// struct Foo(#[reflect(default = default_value)] usize);
///
/// fn default_value() -> usize { 123 }
///
Expand All @@ -77,7 +77,7 @@ pub trait FromReflect: Reflect + Sized {
/// ```
/// # use bevy_reflect::{DynamicTupleStruct, Reflect, ReflectFromReflect, Typed, TypeRegistry};
/// # #[derive(Reflect, PartialEq, Eq, Debug)]
/// # struct Foo(#[reflect(default = "default_value")] usize);
/// # struct Foo(#[reflect(default = default_value)] usize);
/// # fn default_value() -> usize { 123 }
/// # let mut registry = TypeRegistry::new();
/// # registry.register::<Foo>();
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_reflect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@
//!
//! Fields can be given default values for when a field is missing in the passed value or even ignored.
//! Ignored fields must either implement [`Default`] or have a default function specified
//! using `#[reflect(default = "path::to::function")]`.
//! using `#[reflect(default = path::to::function)]`.
//!
//! See the [derive macro documentation](derive@crate::FromReflect) for details.
//!
Expand Down Expand Up @@ -772,7 +772,7 @@ mod tests {
foo: String,

// Use `get_bar_default()`
#[reflect(ignore, default = "get_bar_default")]
#[reflect(ignore, default = get_bar_default)]
bar: NotReflect,
}

Expand Down Expand Up @@ -800,7 +800,7 @@ mod tests {
enum MyEnum {
Foo(#[reflect(default)] String),
Bar {
#[reflect(default = "get_baz_default")]
#[reflect(default = get_baz_default)]
#[reflect(ignore)]
baz: usize,
},
Expand Down
2 changes: 1 addition & 1 deletion examples/reflection/reflection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn main() {
/// reflection by using the `#[reflect(ignore)]` attribute.
/// If you choose to ignore a field, you need to let the automatically-derived `FromReflect` implementation
/// how to handle the field.
/// To do this, you can either define a `#[reflect(default = "...")]` attribute on the ignored field, or
/// To do this, you can either define a `#[reflect(default = ...)]` attribute on the ignored field, or
/// opt-out of `FromReflect`'s auto-derive using the `#[reflect(from_reflect = false)]` attribute.
#[derive(Reflect)]
#[reflect(from_reflect = false)]
Expand Down

0 comments on commit 6a62619

Please sign in to comment.