Skip to content

Commit

Permalink
editoast: replace serde_json::Value by appropriate structs
Browse files Browse the repository at this point in the history
  • Loading branch information
hamz2a committed Dec 22, 2023
1 parent 9d02efb commit 33aaa84
Show file tree
Hide file tree
Showing 15 changed files with 270 additions and 110 deletions.
9 changes: 2 additions & 7 deletions editoast/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -859,9 +859,7 @@ components:
name:
type: string
power_restrictions:
additionalProperties:
type: string
type: object
$ref: '#/components/schemas/DieselJson'
railjson_version:
type: string
rolling_resistance:
Expand Down Expand Up @@ -1874,10 +1872,7 @@ components:
name:
type: string
power_restrictions:
additionalProperties:
type: string
description: Mapping of power restriction code to power class
type: object
$ref: '#/components/schemas/DieselJson'
raise_pantograph_time:
description: The time it takes to raise this train's pantograph in seconds. Is null if the train is not electric.
example: 15.0
Expand Down
9 changes: 5 additions & 4 deletions editoast/src/models/rolling_stock/light_rolling_stock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use crate::models::rolling_stock::rolling_stock_livery::RollingStockLiveryMetada
use crate::schema::rolling_stock::light_rolling_stock::{
LightEffortCurves, LightRollingStock, LightRollingStockWithLiveries,
};
use crate::schema::rolling_stock::{EnergySource, Gamma, RollingResistance, RollingStockMetadata};
use crate::schema::rolling_stock::{
EnergySource, Gamma, PowerRestriction, RollingResistance, RollingStockMetadata,
};
use crate::tables::rolling_stock;
use crate::views::pagination::{Paginate, PaginatedResponse};
use crate::DbPool;
Expand All @@ -14,7 +16,6 @@ use diesel_async::RunQueryDsl;
use diesel_json::Json as DieselJson;
use editoast_derive::Model;
use serde::Serialize;
use serde_json::Value as JsonValue;
use utoipa::ToSchema;

#[derive(Debug, Model, Queryable, QueryableByName, Serialize, ToSchema)]
Expand Down Expand Up @@ -43,8 +44,8 @@ pub struct LightRollingStockModel {
rolling_resistance: DieselJson<RollingResistance>,
#[schema(value_type = LoadingGaugeType)]
loading_gauge: String,
#[schema(value_type = HashMap<String, String>)]
power_restrictions: Option<JsonValue>,
#[schema(value_type = PowerRestriction)]
power_restrictions: Option<DieselJson<PowerRestriction>>,
#[schema(value_type = Vec<EnergySource>)]
energy_sources: DieselJson<Vec<EnergySource>>,
locked: bool,
Expand Down
13 changes: 6 additions & 7 deletions editoast/src/models/rolling_stock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use crate::error::{InternalError, Result};
use crate::models::rolling_stock::rolling_stock_livery::RollingStockLiveryMetadata;
use crate::models::{Create, Identifiable, TextArray, Update};
use crate::schema::rolling_stock::{
EffortCurves, EnergySource, Gamma, RollingResistance, RollingStock, RollingStockCommon,
RollingStockMetadata, RollingStockWithLiveries,
EffortCurves, EnergySource, Gamma, PowerRestriction, RollingResistance, RollingStock,
RollingStockCommon, RollingStockMetadata, RollingStockWithLiveries,
};
use crate::schema::track_section::LoadingGaugeType;
use crate::tables::rolling_stock;
Expand All @@ -27,7 +27,6 @@ use diesel_async::{AsyncPgConnection as PgConnection, RunQueryDsl};
use diesel_json::Json as DieselJson;
use editoast_derive::Model;
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use validator::{Validate, ValidationError};

crate::schemas! {
Expand Down Expand Up @@ -109,9 +108,9 @@ pub struct RollingStockModel {
#[diesel(deserialize_as = String)]
#[schema(value_type = LoadingGaugeType)]
pub loading_gauge: Option<String>,
#[diesel(deserialize_as = Option<JsonValue>)]
#[schema(value_type = Option<HashMap<String, String>>)]
pub power_restrictions: Option<Option<JsonValue>>,
#[diesel(deserialize_as = Option<DieselJson<PowerRestriction>>)]
#[schema(value_type = Option<DieselJson<PowerRestriction>>)]
pub power_restrictions: Option<DieselJson<PowerRestriction>>,
#[diesel(deserialize_as = DieselJson<Vec<EnergySource>>)]
#[schema(value_type = EnergySource)]
pub energy_sources: Option<DieselJson<Vec<EnergySource>>>,
Expand Down Expand Up @@ -289,7 +288,7 @@ impl From<RollingStockModel> for RollingStockCommon {
mass: rolling_stock_model.mass.unwrap(),
rolling_resistance: rolling_stock_model.rolling_resistance.unwrap().0,
loading_gauge: rolling_stock_model.loading_gauge.unwrap(),
power_restrictions: rolling_stock_model.power_restrictions.unwrap(),
power_restrictions: rolling_stock_model.power_restrictions,
energy_sources: rolling_stock_model.energy_sources.unwrap().0,
electrical_power_startup_time: rolling_stock_model
.electrical_power_startup_time
Expand Down
39 changes: 19 additions & 20 deletions editoast/src/schema/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ use serde::{Deserialize, Serialize};
use strum_macros::EnumVariantNames;

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct InfraError {
obj_id: String,
obj_type: ObjectType,
field: Option<String>,
is_warning: bool,
#[serde(flatten)]
sub_type: InfraErrorType,
error_type: InfraErrorType,
}

#[derive(Serialize, Deserialize, PartialEq, Debug, EnumVariantNames, Clone)]
Expand Down Expand Up @@ -71,7 +70,7 @@ impl InfraError {
obj_type: obj.get_type(),
field: Some(field.as_ref().into()),
is_warning: false,
sub_type: InfraErrorType::InvalidReference { reference },
error_type: InfraErrorType::InvalidReference { reference },
}
}

Expand All @@ -86,7 +85,7 @@ impl InfraError {
obj_type: obj.get_type(),
field: Some(field.as_ref().into()),
is_warning: false,
sub_type: InfraErrorType::OutOfRange {
error_type: InfraErrorType::OutOfRange {
position,
expected_range,
},
Expand All @@ -99,7 +98,7 @@ impl InfraError {
obj_type: obj.get_type(),
field: None, // This error concern the whole object consistency
is_warning: false,
sub_type: InfraErrorType::InvalidRoute,
error_type: InfraErrorType::InvalidRoute,
}
}

Expand All @@ -109,7 +108,7 @@ impl InfraError {
obj_type: obj.get_type(),
field: Some(field.as_ref().into()),
is_warning: true,
sub_type: InfraErrorType::EmptyObject,
error_type: InfraErrorType::EmptyObject,
}
}

Expand All @@ -123,7 +122,7 @@ impl InfraError {
obj_type: route.get_type(),
field: Some(field.as_ref().into()),
is_warning: false,
sub_type: InfraErrorType::ObjectOutOfPath { reference },
error_type: InfraErrorType::ObjectOutOfPath { reference },
}
}

Expand All @@ -133,7 +132,7 @@ impl InfraError {
obj_type: ObjectType::TrackSection,
field: Default::default(),
is_warning: true,
sub_type: InfraErrorType::MissingRoute,
error_type: InfraErrorType::MissingRoute,
}
}

Expand All @@ -148,7 +147,7 @@ impl InfraError {
obj_type: obj.get_type(),
field: Some(field.as_ref().into()),
is_warning: false,
sub_type: InfraErrorType::UnknownPortName { port_name },
error_type: InfraErrorType::UnknownPortName { port_name },
}
}

Expand All @@ -158,7 +157,7 @@ impl InfraError {
obj_type: obj.get_type(),
field: Some(field.as_ref().into()),
is_warning: false,
sub_type: InfraErrorType::InvalidSwitchPorts,
error_type: InfraErrorType::InvalidSwitchPorts,
}
}

Expand All @@ -172,7 +171,7 @@ impl InfraError {
obj_type: obj.get_type(),
field: Some(field.as_ref().into()),
is_warning: true,
sub_type: InfraErrorType::UnusedPort {
error_type: InfraErrorType::UnusedPort {
port_name: port_name.as_ref().into(),
},
}
Expand All @@ -188,7 +187,7 @@ impl InfraError {
obj_type: obj.get_type(),
field: Some(field.as_ref().into()),
is_warning: true,
sub_type: InfraErrorType::DuplicatedGroup {
error_type: InfraErrorType::DuplicatedGroup {
original_group_path,
},
}
Expand All @@ -205,7 +204,7 @@ impl InfraError {
obj_type: obj.get_type(),
field: Some(field.as_ref().into()),
is_warning: false,
sub_type: InfraErrorType::InvalidGroup {
error_type: InfraErrorType::InvalidGroup {
group: group.as_ref().into(),
switch_type: switch_type.as_ref().into(),
},
Expand All @@ -218,7 +217,7 @@ impl InfraError {
obj_type: obj.get_type(),
field: None,
is_warning: true,
sub_type: InfraErrorType::MissingBufferStop { endpoint },
error_type: InfraErrorType::MissingBufferStop { endpoint },
}
}

Expand All @@ -228,7 +227,7 @@ impl InfraError {
obj_type: obj.get_type(),
field: None,
is_warning: true,
sub_type: InfraErrorType::OddBufferStopLocation,
error_type: InfraErrorType::OddBufferStopLocation,
}
}

Expand All @@ -248,7 +247,7 @@ impl InfraError {
obj_type: ObjectType::SpeedSection,
field: Default::default(),
is_warning: true,
sub_type: InfraErrorType::OverlappingSpeedSections { reference },
error_type: InfraErrorType::OverlappingSpeedSections { reference },
}
}

Expand All @@ -263,7 +262,7 @@ impl InfraError {
obj_type: ObjectType::Catenary,
field: Default::default(),
is_warning: true,
sub_type: InfraErrorType::OverlappingCatenaries { reference },
error_type: InfraErrorType::OverlappingCatenaries { reference },
}
}

Expand All @@ -274,12 +273,12 @@ impl InfraError {
obj_type: obj.get_type(),
field: Default::default(),
is_warning: false,
sub_type: InfraErrorType::OverlappingSwitches { reference },
error_type: InfraErrorType::OverlappingSwitches { reference },
}
}

pub fn get_sub_type(&self) -> &InfraErrorType {
&self.sub_type
pub fn get_error_type(&self) -> &InfraErrorType {
&self.error_type
}
}

Expand Down
2 changes: 1 addition & 1 deletion editoast/src/schema/geo_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use mvt::GeomType;
use serde::{Deserialize, Serialize};

/// GeoJson representation
#[derive(Clone, Serialize, Deserialize, Debug)]
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
#[serde(tag = "type")]
pub enum GeoJson {
Point { coordinates: (f64, f64) },
Expand Down
7 changes: 3 additions & 4 deletions editoast/src/schema/rolling_stock/light_rolling_stock.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use diesel::sql_types::{Array, BigInt, Bool, Double, Jsonb, Nullable, Text};
use diesel_json::Json as DieselJson;
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use std::collections::HashMap;
use utoipa::ToSchema;

use crate::models::rolling_stock::rolling_stock_livery::RollingStockLiveryMetadata;

use super::{EnergySource, Gamma, RollingResistance, RollingStockMetadata};
use super::{EnergySource, Gamma, PowerRestriction, RollingResistance, RollingStockMetadata};

crate::schemas! {
LightRollingStock,
Expand Down Expand Up @@ -60,8 +59,8 @@ pub struct LightRollingStock {
#[schema(value_type = RollingStockMetadata)]
pub metadata: DieselJson<RollingStockMetadata>,
#[diesel(sql_type = Nullable<Jsonb>)]
#[schema(value_type = HashMap<String, String>)]
pub power_restrictions: Option<JsonValue>,
#[schema(value_type = DieselJson<PowerRestriction>)]
pub power_restrictions: Option<DieselJson<PowerRestriction>>,
#[diesel(sql_type = Jsonb)]
#[schema(value_type = Vec<EnergySource>)]
pub energy_sources: DieselJson<Vec<EnergySource>>,
Expand Down
9 changes: 6 additions & 3 deletions editoast/src/schema/rolling_stock/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub mod light_rolling_stock;
pub mod rolling_stock_livery;

use diesel_json::Json as DieselJson;
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value as JsonValue;
use std::collections::HashMap;
use strum_macros::{Display, EnumString};
use utoipa::ToSchema;
Expand Down Expand Up @@ -52,8 +52,8 @@ pub struct RollingStockCommon {
#[schema(value_type = LoadingGaugeType)]
pub loading_gauge: String,
/// Mapping of power restriction code to power class
#[schema(value_type = HashMap<String, String>)]
pub power_restrictions: Option<JsonValue>,
#[schema(value_type = DieselJson<PowerRestriction>)]
pub power_restrictions: Option<DieselJson<PowerRestriction>>,
#[serde(default)]
pub energy_sources: Vec<EnergySource>,
/// The time the train takes before actually using electrical power (in seconds). Is null if the train is not electric.
Expand Down Expand Up @@ -261,6 +261,9 @@ pub enum EnergySource {
},
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PowerRestriction(HashMap<String, String>);

#[cfg(test)]
mod tests {
use crate::schema::rolling_stock::EffortCurve;
Expand Down
2 changes: 1 addition & 1 deletion editoast/src/tests/example_rolling_stock_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@
"unit": ""
},
"energy_sources": [],
"power_restrictions": {"C2":"2"},
"power_restrictions": {"C2":"2", "C5":"5"},
"electrical_power_startup_time": 5.0,
"raise_pantograph_time": 15.0
}
12 changes: 6 additions & 6 deletions editoast/src/views/infra/auto_fixes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fn get_operations_fixing_error(
error: &InfraError,
infra_cache: &InfraCache,
) -> Result<Vec<Operation>> {
match error.get_sub_type() {
match error.get_error_type() {
InfraErrorType::InvalidReference { reference } => {
get_operations_fixing_invalid_reference(error, reference, infra_cache)
}
Expand Down Expand Up @@ -284,11 +284,11 @@ mod test {
// Check the only initial issues are "overlapping_speed_sections" warnings
let infra_errors_before_all: PaginatedResponse<crate::views::infra::errors::InfraError> =
read_body_json(call_service(&app, errors_request(small_infra_id)).await).await;
assert!(infra_errors_before_all.results.iter().all(|e| e
.information
.get("error_type")
.unwrap()
== "overlapping_speed_sections"));

assert!(infra_errors_before_all.results.iter().all(|e| matches!(
e.information.get_error_type(),
InfraErrorType::OverlappingSpeedSections { .. }
)));

// Remove a track
let deletion = Operation::Delete(DeleteOperation {
Expand Down
11 changes: 4 additions & 7 deletions editoast/src/views/infra/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ use actix_web::dev::HttpServiceFactory;
use actix_web::get;
use actix_web::web::{Data, Json as WebJson, Path, Query};
use diesel::sql_query;
use diesel::sql_types::{BigInt, Json, Text};
use diesel::sql_types::{BigInt, Jsonb, Text};
use editoast_derive::EditoastError;
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use strum::VariantNames;
use thiserror::Error;

Expand Down Expand Up @@ -65,10 +64,9 @@ enum ListErrorsErrors {
}

#[derive(QueryableByName, Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct InfraError {
#[diesel(sql_type = Json)]
pub information: JsonValue,
#[diesel(sql_type = Jsonb)]
pub information: diesel_json::Json<crate::schema::InfraError>,
}

#[derive(Default, Debug, Clone, PartialEq, Eq, Deserialize)]
Expand All @@ -87,8 +85,7 @@ async fn get_paginated_infra_errors(
per_page: i64,
params: &QueryParams,
) -> Result<PaginatedResponse<InfraError>> {
let mut query =
String::from("SELECT information::text FROM infra_layer_error WHERE infra_id = $1");
let mut query = String::from("SELECT information FROM infra_layer_error WHERE infra_id = $1");
if params.level == Level::Warnings {
query += " AND information->>'is_warning' = 'true'"
} else if params.level == Level::Errors {
Expand Down
Loading

0 comments on commit 33aaa84

Please sign in to comment.