mirror of
https://github.com/hannobraun/Fornjot
synced 2025-10-10 01:48:08 +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> {
|
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> {
|
impl From<Object<WithHandle>> for Object<BehindHandle> {
|
||||||
|
@ -11,7 +11,7 @@ use crate::objects::{Object, Objects, WithHandle};
|
|||||||
pub use self::{
|
pub use self::{
|
||||||
objects::{InsertObject, Operation},
|
objects::{InsertObject, Operation},
|
||||||
service::{Service, State},
|
service::{Service, State},
|
||||||
validation::{Validation, ValidationFailed},
|
validation::{Validation, ValidationCommand, ValidationEvent},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// The kernel services
|
/// The kernel services
|
||||||
@ -46,7 +46,10 @@ impl Services {
|
|||||||
.execute(Operation::InsertObject { object }, &mut object_events);
|
.execute(Operation::InsertObject { object }, &mut object_events);
|
||||||
|
|
||||||
for object_event in 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,
|
validate::ValidationError,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{objects::InsertObject, State};
|
use super::State;
|
||||||
|
|
||||||
/// Errors that occurred while validating the objects inserted into the stores
|
/// Errors that occurred while validating the objects inserted into the stores
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Validation(pub BTreeMap<ObjectId, ValidationFailed>);
|
pub struct Validation {
|
||||||
|
errors: BTreeMap<ObjectId, ValidationError>,
|
||||||
|
}
|
||||||
|
|
||||||
impl Drop for Validation {
|
impl Drop for Validation {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
let num_errors = self.0.len();
|
let num_errors = self.errors.len();
|
||||||
if num_errors > 0 {
|
if num_errors > 0 {
|
||||||
println!(
|
println!(
|
||||||
"Dropping `Validation` with {num_errors} unhandled validation \
|
"Dropping `Validation` with {num_errors} unhandled validation \
|
||||||
errors:"
|
errors:"
|
||||||
);
|
);
|
||||||
|
|
||||||
for event in self.0.values() {
|
for err in self.errors.values() {
|
||||||
println!("{}", event.err);
|
println!("{}", err);
|
||||||
}
|
}
|
||||||
|
|
||||||
if !thread::panicking() {
|
if !thread::panicking() {
|
||||||
@ -33,32 +35,50 @@ impl Drop for Validation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl State for Validation {
|
impl State for Validation {
|
||||||
type Command = InsertObject;
|
type Command = ValidationCommand;
|
||||||
type Event = ValidationFailed;
|
type Event = ValidationEvent;
|
||||||
|
|
||||||
fn decide(&self, command: Self::Command, events: &mut Vec<Self::Event>) {
|
fn decide(&self, command: Self::Command, events: &mut Vec<Self::Event>) {
|
||||||
|
let ValidationCommand::ValidateObject { object } = command;
|
||||||
|
|
||||||
let mut errors = Vec::new();
|
let mut errors = Vec::new();
|
||||||
command.object.validate(&mut errors);
|
object.validate(&mut errors);
|
||||||
|
|
||||||
for err in errors {
|
for err in errors {
|
||||||
events.push(ValidationFailed {
|
events.push(ValidationEvent::ValidationFailed {
|
||||||
object: command.object.clone().into(),
|
object: object.clone(),
|
||||||
err,
|
err,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn evolve(&mut self, event: &Self::Event) {
|
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
|
||||||
#[derive(Clone)]
|
pub enum ValidationCommand {
|
||||||
pub struct ValidationFailed {
|
/// Validate the provided object
|
||||||
/// The object for which validation failed
|
ValidateObject {
|
||||||
pub object: Object<BehindHandle>,
|
/// The object to validate
|
||||||
|
object: Object<BehindHandle>,
|
||||||
/// The validation error
|
},
|
||||||
pub err: ValidationError,
|
}
|
||||||
|
|
||||||
|
/// 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