mirror of
https://github.com/hannobraun/Fornjot
synced 2025-10-05 07:27:57 +00:00
Merge pull request #1811 from hannobraun/service
Clean up validation service
This commit is contained in:
commit
5510bde38a
@ -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> {
|
||||
@ -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> {
|
||||
|
@ -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
|
||||
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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() {
|
||||
@ -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
|
||||
#[derive(Clone)]
|
||||
pub struct ValidationFailed {
|
||||
/// The object for which validation failed
|
||||
pub object: Object<BehindHandle>,
|
||||
|
||||
/// The validation error
|
||||
pub err: ValidationError,
|
||||
/// 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 enum ValidationEvent {
|
||||
/// Validation of an object failed
|
||||
ValidationFailed {
|
||||
/// The object for which validation failed
|
||||
object: Object<BehindHandle>,
|
||||
|
||||
/// The validation error
|
||||
err: ValidationError,
|
||||
},
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user