Skip to content

Commit

Permalink
macros: Fix field ids when deriving Introspectable for structs
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis-hamester committed Nov 29, 2024
1 parent 10a7a96 commit 755d7fd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
6 changes: 6 additions & 0 deletions macros/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Fixed

- Fix custom ids (`[aldrin(id = ...)]`) for struct fields.

## [0.10.0] - 2024-11-26

- Bump for Aldrin 0.10.0 release.
Expand Down
2 changes: 1 addition & 1 deletion macros/src/derive/introspectable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fn gen_field(
}
};

Ok((layout, references, default_id))
Ok((layout, references, id))
}

fn gen_enum(
Expand Down
33 changes: 33 additions & 0 deletions macros/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use aldrin::core::introspection::Introspection;
use aldrin::Introspectable;

mod raw_identifiers {
#![allow(non_camel_case_types)]

Expand Down Expand Up @@ -40,3 +43,33 @@ mod raw_identifiers {
#[aldrin(schema = "raw_identifiers")]
enum r#false {}
}

#[test]
fn introspection_non_default_field_ids() {
#[allow(dead_code)]
#[derive(Introspectable)]
#[aldrin(schema = "test")]
struct NonDefaultFieldIds {
field0: (),

#[aldrin(id = 2)]
field2: (),

field3: (),
}

let introspection = Introspection::new::<NonDefaultFieldIds>();
let layout = introspection.as_struct_layout().unwrap();

let field0 = layout.fields().get(&0).unwrap();
assert_eq!(field0.id(), 0);
assert_eq!(field0.name(), "field0");

let field2 = layout.fields().get(&2).unwrap();
assert_eq!(field2.id(), 2);
assert_eq!(field2.name(), "field2");

let field3 = layout.fields().get(&3).unwrap();
assert_eq!(field3.id(), 3);
assert_eq!(field3.name(), "field3");
}

0 comments on commit 755d7fd

Please sign in to comment.