-
Notifications
You must be signed in to change notification settings - Fork 276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor #[derive(Filter)]
for better parsing and readability
#2437
Comments
…os for Identifiable, Eq, Hash, Ord Signed-off-by: Ilia Churin <[email protected]>
…os for Identifiable, Eq, Hash, Ord Signed-off-by: Ilia Churin <[email protected]>
…os for Identifiable, Eq, Hash, Ord Signed-off-by: Ilia Churin <[email protected]>
…os for Identifiable, Eq, Hash, Ord Signed-off-by: Ilia Churin <[email protected]>
…os for Identifiable, Eq, Hash, Ord Signed-off-by: Ilia Churin <[email protected]>
…os for Identifiable, Eq, Hash, Ord Signed-off-by: Ilia Churin <[email protected]>
…os for Identifiable, Eq, Hash, Ord Signed-off-by: Ilia Churin <[email protected]>
…os for Identifiable, Eq, Hash, Ord Signed-off-by: Ilia Churin <[email protected]>
…#2476) * [feature] #1988, #2437: Derive macros for Identifiable, Eq, Hash, Ord Signed-off-by: Ilia Churin <[email protected]> * Refactor `OrdEqHash` into `Identifiable` Signed-off-by: Ilia Churin <[email protected]> * Refactor `IdOrdEqHash` and `Filter` into separate files Signed-off-by: Ilia Churin <[email protected]>
…os for Identifiable, Eq, Hash, Ord (hyperledger-iroha#2476) * [feature] hyperledger-iroha#1988, hyperledger-iroha#2437: Derive macros for Identifiable, Eq, Hash, Ord Signed-off-by: Ilia Churin <[email protected]> * Refactor `OrdEqHash` into `Identifiable` Signed-off-by: Ilia Churin <[email protected]> * Refactor `IdOrdEqHash` and `Filter` into separate files Signed-off-by: Ilia Churin <[email protected]>
…os for Identifiable, Eq, Hash, Ord (hyperledger-iroha#2476) * [feature] hyperledger-iroha#1988, hyperledger-iroha#2437: Derive macros for Identifiable, Eq, Hash, Ord Signed-off-by: Ilia Churin <[email protected]> * Refactor `OrdEqHash` into `Identifiable` Signed-off-by: Ilia Churin <[email protected]> * Refactor `IdOrdEqHash` and `Filter` into separate files Signed-off-by: Ilia Churin <[email protected]>
…os for Identifiable, Eq, Hash, Ord (hyperledger-iroha#2476) * [feature] hyperledger-iroha#1988, hyperledger-iroha#2437: Derive macros for Identifiable, Eq, Hash, Ord Signed-off-by: Ilia Churin <[email protected]> * Refactor `OrdEqHash` into `Identifiable` Signed-off-by: Ilia Churin <[email protected]> * Refactor `IdOrdEqHash` and `Filter` into separate files Signed-off-by: Ilia Churin <[email protected]>
…os for Identifiable, Eq, Hash, Ord (hyperledger-iroha#2476) * [feature] hyperledger-iroha#1988, hyperledger-iroha#2437: Derive macros for Identifiable, Eq, Hash, Ord Signed-off-by: Ilia Churin <[email protected]> * Refactor `OrdEqHash` into `Identifiable` Signed-off-by: Ilia Churin <[email protected]> * Refactor `IdOrdEqHash` and `Filter` into separate files Signed-off-by: Ilia Churin <[email protected]>
…os for Identifiable, Eq, Hash, Ord (hyperledger-iroha#2476) * [feature] hyperledger-iroha#1988, hyperledger-iroha#2437: Derive macros for Identifiable, Eq, Hash, Ord Signed-off-by: Ilia Churin <[email protected]> * Refactor `OrdEqHash` into `Identifiable` Signed-off-by: Ilia Churin <[email protected]> * Refactor `IdOrdEqHash` and `Filter` into separate files Signed-off-by: Ilia Churin <[email protected]>
…os for Identifiable, Eq, Hash, Ord (hyperledger-iroha#2476) * [feature] hyperledger-iroha#1988, hyperledger-iroha#2437: Derive macros for Identifiable, Eq, Hash, Ord Signed-off-by: Ilia Churin <[email protected]> * Refactor `OrdEqHash` into `Identifiable` Signed-off-by: Ilia Churin <[email protected]> * Refactor `IdOrdEqHash` and `Filter` into separate files Signed-off-by: Ilia Churin <[email protected]>
…os for Identifiable, Eq, Hash, Ord (hyperledger-iroha#2476) * [feature] hyperledger-iroha#1988, hyperledger-iroha#2437: Derive macros for Identifiable, Eq, Hash, Ord Signed-off-by: Ilia Churin <[email protected]> * Refactor `OrdEqHash` into `Identifiable` Signed-off-by: Ilia Churin <[email protected]> * Refactor `IdOrdEqHash` and `Filter` into separate files Signed-off-by: Ilia Churin <[email protected]>
…os for Identifiable, Eq, Hash, Ord (hyperledger-iroha#2476) * [feature] hyperledger-iroha#1988, hyperledger-iroha#2437: Derive macros for Identifiable, Eq, Hash, Ord Signed-off-by: Ilia Churin <[email protected]> * Refactor `OrdEqHash` into `Identifiable` Signed-off-by: Ilia Churin <[email protected]> * Refactor `IdOrdEqHash` and `Filter` into separate files Signed-off-by: Ilia Churin <[email protected]>
I made some research about the There is an example of how macro expands from this: use iroha_data_model_derive::{Filter, IdEqOrdHash};
use iroha_data_model::prelude::{HasOrigin, Identifiable};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Filter, Deserialize, Serialize)]
pub enum LayerEvent {
SubLayer(SubLayerEvent),
Created(LayerId),
}
pub enum SubLayerEvent {
Created(SubLayerId),
}
pub struct LayerId {
name: u32,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SubLayerId {
name: u32,
parent_id: LayerId,
}
#[derive(Debug, Clone, IdEqOrdHash)]
pub struct Layer {
id: <Self as Identifiable>::Id,
}
#[derive(Debug, Clone, IdEqOrdHash)]
pub struct SubLayer {
id: <Self as Identifiable>::Id,
}
impl HasOrigin for LayerEvent {
type Origin = Layer;
fn origin_id(&self) -> &<Layer as Identifiable>::Id {
match self {
Self::SubLayer(sub_layer) => &sub_layer.origin_id().parent_id,
Self::Created(id) => id,
}
}
}
impl HasOrigin for SubLayerEvent {
type Origin = SubLayer;
fn origin_id(&self) -> &<SubLayer as Identifiable>::Id {
match self {
Self::Created(id) => id,
}
}
} To this: #[doc = " Filter for LayerEvent entity"]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, derive_more::Constructor, Decode, Encode, Deserialize, Serialize, IntoSchema)]
pub struct LayerFilter {
origin_filter:
crate::prelude::FilterOpt<crate::prelude::OriginFilter<crate::prelude::LayerEvent>>,
event_filter: crate::prelude::FilterOpt<LayerEventFilter>,
}
impl LayerFilter {
#[doc = " Construct new LayerFilter"]
pub const fn new(
origin_filter: crate::prelude::FilterOpt<
crate::prelude::OriginFilter<crate::prelude::LayerEvent>,
>,
event_filter: crate::prelude::FilterOpt<LayerEventFilter>,
) -> Self {
Self {
origin_filter,
event_filter,
}
}
#[doc = r" Get `origin_filter`"]
#[inline]
pub const fn origin_filter(
&self,
) -> &crate::prelude::FilterOpt<crate::prelude::OriginFilter<crate::prelude::LayerEvent>> {
&self.origin_filter
}
#[doc = r" Get `event_filter`"]
#[inline]
pub const fn event_filter(&self) -> &crate::prelude::FilterOpt<LayerEventFilter> {
&self.event_filter
}
}
impl crate::prelude::Filter for LayerFilter {
type EventType = crate::prelude::LayerEvent;
fn matches(&self, event: &Self::EventType) -> bool {
self.origin_filter.matches(event) && self.event_filter.matches(event)
}
}
#[doc = " Event filter for LayerEvent entity"]
#[allow(clippy::enum_variant_names, missing_docs)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Decode, Encode, Deserialize, Serialize, IntoSchema)]
pub enum LayerEventFilter {
ByCreated,
BySubLayer(crate::prelude::FilterOpt<SubLayerFilter>),
}
impl crate::prelude::Filter for LayerEventFilter {
type EventType = crate::prelude::LayerEvent;
fn matches(&self, event: &crate::prelude::LayerEvent) -> bool {
match (self, event) {
(Self::ByCreated, crate::prelude::LayerEvent::Created(_)) => true,
(Self::BySubLayer(filter_opt), crate::prelude::LayerEvent::SubLayer(event)) => {
filter_opt.matches(event)
}
_ => false,
}
}
} However, when I expand it with @ilchu, could you comment on it? Also, I think that the example is cumbersome and docstring should contain something shorter and more expressive. |
Several ideas I have to improve the macro:
Apart from this, I don't see what else to refactor in the |
….filter_maps & get rid of unnecessary .except in derive(Filter) This addresses the last of the concerns raised in hyperledger-iroha#2437 Signed-off-by: Nikita Strygin <[email protected]>
….filter_maps & get rid of unnecessary .except in derive(Filter) This addresses the last of the concerns raised in hyperledger-iroha#2437 Signed-off-by: Nikita Strygin <[email protected]>
….filter_maps & get rid of unnecessary .except in derive(Filter) This addresses the last of the concerns raised in hyperledger-iroha#2437 Signed-off-by: Nikita Strygin <[email protected]>
….filter_maps & get rid of unnecessary .except in derive(Filter) This addresses the last of the concerns raised in hyperledger-iroha#2437 Signed-off-by: Nikita Strygin <[email protected]>
….filter_maps & get rid of unnecessary .except in derive(Filter) This addresses the last of the concerns raised in hyperledger-iroha#2437 Signed-off-by: Nikita Strygin <[email protected]>
….filter_maps & get rid of unnecessary .except in derive(Filter) This addresses the last of the concerns raised in hyperledger-iroha#2437 Signed-off-by: Nikita Strygin <[email protected]>
….filter_maps & get rid of unnecessary .except in derive(Filter) This addresses the last of the concerns raised in hyperledger-iroha#2437 Signed-off-by: Nikita Strygin <[email protected]>
…t rid of unnecessary .except in derive(Filter) This addresses the last of the concerns raised in #2437 Signed-off-by: Nikita Strygin <[email protected]>
All of these were addressed in #3897 I think? |
…t rid of unnecessary .except in derive(Filter) This addresses the last of the concerns raised in #2437 Signed-off-by: Nikita Strygin <[email protected]>
…t rid of unnecessary .except in derive(Filter) This addresses the last of the concerns raised in #2437 Signed-off-by: Nikita Strygin <[email protected]>
…t rid of unnecessary .except in derive(Filter) This addresses the last of the concerns raised in #2437 Signed-off-by: Nikita Strygin <[email protected]>
As mentioned in #2431 PR discussion, there are still a few improvements that could be made to the
Filter
derive macro indata_model/derive
:Parse
to make invalid enums fail more predictablyThe text was updated successfully, but these errors were encountered: