Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…r-iroha#3068: Use the auto-generated event sets instead of handwritten matchers

Signed-off-by: Nikita Strygin <[email protected]>
  • Loading branch information
DCNick3 committed Mar 18, 2024
1 parent d436e12 commit 8b2361a
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 631 deletions.
6 changes: 3 additions & 3 deletions client/tests/integration/triggers/data_trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn must_execute_both_triggers() -> Result<()> {
Repeats::Indefinitely,
account_id.clone(),
TriggeringEventFilterBox::Data(DataEventFilter::Account(
AccountEventFilter::new().only_events(AccountEventMatcher::Created),
AccountEventFilter::new().only_events(AccountEventSet::Created),
)),
),
));
Expand All @@ -37,7 +37,7 @@ fn must_execute_both_triggers() -> Result<()> {
Repeats::Indefinitely,
account_id,
TriggeringEventFilterBox::Data(DataEventFilter::Domain(
DomainEventFilter::new().only_events(DomainEventMatcher::Created),
DomainEventFilter::new().only_events(DomainEventSet::Created),
)),
),
));
Expand Down Expand Up @@ -89,7 +89,7 @@ fn domain_scoped_trigger_must_be_executed_only_on_events_in_its_domain() -> Resu
Repeats::Indefinitely,
account_id,
TriggeringEventFilterBox::Data(DataEventFilter::Account(
AccountEventFilter::new().only_events(AccountEventMatcher::Created),
AccountEventFilter::new().only_events(AccountEventSet::Created),
)),
),
));
Expand Down
2 changes: 1 addition & 1 deletion client/tests/integration/triggers/event_trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn test_mint_asset_when_new_asset_definition_created() -> Result<()> {
Repeats::Indefinitely,
account_id,
TriggeringEventFilterBox::Data(DataEventFilter::AssetDefinition(
AssetDefinitionEventFilter::new().only_events(AssetDefinitionEventMatcher::Created),
AssetDefinitionEventFilter::new().only_events(AssetDefinitionEventSet::Created),
)),
),
));
Expand Down
36 changes: 18 additions & 18 deletions data_model/derive/src/event_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use darling::{FromDeriveInput, FromVariant};
use iroha_macro_utils::Emitter;
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use syn2::{DeriveInput, Variant};
use syn::{DeriveInput, Variant};

enum FieldsStyle {
Unit,
Expand All @@ -24,14 +24,14 @@ impl ToTokens for FieldsStyle {
}

struct EventSetVariant {
event_ident: syn2::Ident,
flag_ident: syn2::Ident,
event_ident: syn::Ident,
flag_ident: syn::Ident,
fields_style: FieldsStyle,
}

impl FromVariant for EventSetVariant {
fn from_variant(variant: &Variant) -> darling::Result<Self> {
let syn2::Variant {
let syn::Variant {
attrs: _,
ident: event_ident,
fields,
Expand All @@ -41,27 +41,27 @@ impl FromVariant for EventSetVariant {
// a nested event is an event within an event (like `AccountEvent::Asset`, which bears an `AssetEvent`)
// we detect those by checking whether the payload type (if any) ends with `Event`
let is_nested = match fields {
syn2::Fields::Unnamed(fields) => {
syn::Fields::Unnamed(fields) => {
fields.unnamed.len() == 1
&& matches!(&fields.unnamed[0].ty, syn2::Type::Path(p) if p.path.segments.last().unwrap().ident.to_string().ends_with("Event"))
&& matches!(&fields.unnamed[0].ty, syn::Type::Path(p) if p.path.segments.last().unwrap().ident.to_string().ends_with("Event"))
}
syn2::Fields::Unit |
syn::Fields::Unit |
// just a fail-safe, we don't use named fields in events
syn2::Fields::Named(_) => false,
syn::Fields::Named(_) => false,
};

// we have a different naming convention for nested events
// to signify that there are actually multiple types of events inside
let flag_ident = if is_nested {
syn2::Ident::new(&format!("Any{event_ident}"), event_ident.span())
syn::Ident::new(&format!("Any{event_ident}"), event_ident.span())
} else {
event_ident.clone()
};

let fields_style = match fields {
syn2::Fields::Unnamed(_) => FieldsStyle::Unnamed,
syn2::Fields::Named(_) => FieldsStyle::Named,
syn2::Fields::Unit => FieldsStyle::Unit,
syn::Fields::Unnamed(_) => FieldsStyle::Unnamed,
syn::Fields::Named(_) => FieldsStyle::Named,
syn::Fields::Unit => FieldsStyle::Unit,
};

Ok(Self {
Expand All @@ -73,15 +73,15 @@ impl FromVariant for EventSetVariant {
}

struct EventSetEnum {
vis: syn2::Visibility,
event_enum_ident: syn2::Ident,
set_ident: syn2::Ident,
vis: syn::Visibility,
event_enum_ident: syn::Ident,
set_ident: syn::Ident,
variants: Vec<EventSetVariant>,
}

impl FromDeriveInput for EventSetEnum {
fn from_derive_input(input: &DeriveInput) -> darling::Result<Self> {
let syn2::DeriveInput {
let syn::DeriveInput {
attrs: _,
vis,
ident: event_ident,
Expand Down Expand Up @@ -115,7 +115,7 @@ impl FromDeriveInput for EventSetEnum {
accumulator.finish_with(Self {
vis: vis.clone(),
event_enum_ident: event_ident.clone(),
set_ident: syn2::Ident::new(&format!("{event_ident}Set"), event_ident.span()),
set_ident: syn::Ident::new(&format!("{event_ident}Set"), event_ident.span()),
variants,
})
}
Expand Down Expand Up @@ -384,7 +384,7 @@ impl ToTokens for EventSetEnum {
}
}

pub fn impl_event_set_derive(emitter: &mut Emitter, input: &syn2::DeriveInput) -> TokenStream {
pub fn impl_event_set_derive(emitter: &mut Emitter, input: &syn::DeriveInput) -> TokenStream {
let Some(enum_) = emitter.handle(EventSetEnum::from_derive_input(input)) else {
return quote! {};
};
Expand Down
2 changes: 1 addition & 1 deletion data_model/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ pub fn has_origin_derive(input: TokenStream) -> TokenStream {
pub fn event_set_derive(input: TokenStream) -> TokenStream {
let mut emitter = Emitter::new();

let Some(input) = emitter.handle(syn2::parse2(input)) else {
let Some(input) = emitter.handle(syn::parse2(input)) else {
return emitter.finish_token_stream();
};

Expand Down
20 changes: 11 additions & 9 deletions data_model/src/events/data/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![allow(missing_docs)]

use getset::Getters;
use iroha_data_model_derive::{model, HasOrigin};
use iroha_data_model_derive::{model, EventSet, HasOrigin};
use iroha_primitives::numeric::Numeric;

pub use self::model::*;
Expand All @@ -19,6 +19,7 @@ macro_rules! data_event {
PartialOrd,
Ord,
HasOrigin,
EventSet,
parity_scale_codec::Decode,
parity_scale_codec::Encode,
serde::Deserialize,
Expand Down Expand Up @@ -648,18 +649,19 @@ impl DataEvent {

pub mod prelude {
pub use super::{
account::{AccountEvent, AccountPermissionChanged, AccountRoleChanged},
account::{AccountEvent, AccountEventSet, AccountPermissionChanged, AccountRoleChanged},
asset::{
AssetChanged, AssetDefinitionEvent, AssetDefinitionOwnerChanged,
AssetDefinitionTotalQuantityChanged, AssetEvent,
AssetChanged, AssetDefinitionEvent, AssetDefinitionEventSet,
AssetDefinitionOwnerChanged, AssetDefinitionTotalQuantityChanged, AssetEvent,
AssetEventSet,
},
config::ConfigurationEvent,
domain::{DomainEvent, DomainOwnerChanged},
config::{ConfigurationEvent, ConfigurationEventSet},
domain::{DomainEvent, DomainEventSet, DomainOwnerChanged},
executor::{ExecutorEvent, ExecutorFilter},
peer::PeerEvent,
peer::{PeerEvent, PeerEventSet},
permission::PermissionTokenSchemaUpdateEvent,
role::{RoleEvent, RolePermissionChanged},
trigger::{TriggerEvent, TriggerNumberOfExecutionsChanged},
role::{RoleEvent, RoleEventSet, RolePermissionChanged},
trigger::{TriggerEvent, TriggerEventSet, TriggerNumberOfExecutionsChanged},
DataEvent, HasOrigin, MetadataChanged,
};
}
Loading

0 comments on commit 8b2361a

Please sign in to comment.