Skip to content

Commit

Permalink
Merge pull request #1811 from hannobraun/service
Browse files Browse the repository at this point in the history
Clean up validation service
  • Loading branch information
hannobraun authored May 3, 2023
2 parents 9ed81d0 + 42d3555 commit 5510bde
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 28 deletions.
18 changes: 9 additions & 9 deletions crates/fj-kernel/src/objects/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ macro_rules! object {
)*
}
}

/// Validate the object
pub fn validate(&self, errors: &mut Vec<ValidationError>) {
match self {
$(
Self::$ty(object) => object.validate(errors),
)*
}
}
}

impl Object<WithHandle> {
Expand All @@ -47,15 +56,6 @@ macro_rules! object {
)*
}
}

/// Validate the object
pub fn validate(&self, errors: &mut Vec<ValidationError>) {
match self {
$(
Self::$ty((_, object)) => object.validate(errors),
)*
}
}
}

impl From<Object<WithHandle>> for Object<BehindHandle> {
Expand Down
7 changes: 5 additions & 2 deletions crates/fj-kernel/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::objects::{Object, Objects, WithHandle};
pub use self::{
objects::{InsertObject, Operation},
service::{Service, State},
validation::{Validation, ValidationFailed},
validation::{Validation, ValidationCommand, ValidationEvent},
};

/// The kernel services
Expand Down Expand Up @@ -46,7 +46,10 @@ impl Services {
.execute(Operation::InsertObject { object }, &mut object_events);

for object_event in object_events {
self.validation.execute(object_event, &mut Vec::new());
let command = ValidationCommand::ValidateObject {
object: object_event.object.into(),
};
self.validation.execute(command, &mut Vec::new());
}
}
}
Expand Down
54 changes: 37 additions & 17 deletions crates/fj-kernel/src/services/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,25 @@ use crate::{
validate::ValidationError,
};

use super::{objects::InsertObject, State};
use super::State;

/// Errors that occurred while validating the objects inserted into the stores
#[derive(Default)]
pub struct Validation(pub BTreeMap<ObjectId, ValidationFailed>);
pub struct Validation {
errors: BTreeMap<ObjectId, ValidationError>,
}

impl Drop for Validation {
fn drop(&mut self) {
let num_errors = self.0.len();
let num_errors = self.errors.len();
if num_errors > 0 {
println!(
"Dropping `Validation` with {num_errors} unhandled validation \
errors:"
);

for event in self.0.values() {
println!("{}", event.err);
for err in self.errors.values() {
println!("{}", err);
}

if !thread::panicking() {
Expand All @@ -33,32 +35,50 @@ impl Drop for Validation {
}

impl State for Validation {
type Command = InsertObject;
type Event = ValidationFailed;
type Command = ValidationCommand;
type Event = ValidationEvent;

fn decide(&self, command: Self::Command, events: &mut Vec<Self::Event>) {
let ValidationCommand::ValidateObject { object } = command;

let mut errors = Vec::new();
command.object.validate(&mut errors);
object.validate(&mut errors);

for err in errors {
events.push(ValidationFailed {
object: command.object.clone().into(),
events.push(ValidationEvent::ValidationFailed {
object: object.clone(),
err,
});
}
}

fn evolve(&mut self, event: &Self::Event) {
self.0.insert(event.object.id(), event.clone());
match event {
ValidationEvent::ValidationFailed { object, err } => {
self.errors.insert(object.id(), err.clone());
}
}
}
}

/// An event produced by the validation service
/// The command accepted by the validation service
pub enum ValidationCommand {
/// Validate the provided object
ValidateObject {
/// The object to validate
object: Object<BehindHandle>,
},
}

/// The event produced by the validation service
#[derive(Clone)]
pub struct ValidationFailed {
/// The object for which validation failed
pub object: Object<BehindHandle>,
pub enum ValidationEvent {
/// Validation of an object failed
ValidationFailed {
/// The object for which validation failed
object: Object<BehindHandle>,

/// The validation error
pub err: ValidationError,
/// The validation error
err: ValidationError,
},
}

0 comments on commit 5510bde

Please sign in to comment.