Skip to content

Commit

Permalink
Use serde tag syntax to embed objectType field
Browse files Browse the repository at this point in the history
Signed-off-by: Maximilian Pohl <[email protected]>
  • Loading branch information
pohlm01 committed Oct 23, 2024
1 parent c9fa400 commit 50f8cbb
Show file tree
Hide file tree
Showing 19 changed files with 24 additions and 122 deletions.
7 changes: 1 addition & 6 deletions openadr-client/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ use crate::{
error::{Error, Result},
ClientRef, ReportClient,
};
use openadr_wire::{
event::EventContent,
report::{ReportContent, ReportObjectType},
Event, Report,
};
use openadr_wire::{event::EventContent, report::ReportContent, Event, Report};

#[derive(Debug)]
pub struct EventClient {
Expand Down Expand Up @@ -63,7 +59,6 @@ impl EventClient {
/// Create a new report object
pub fn new_report(&self, client_name: String) -> ReportContent {
ReportContent {
object_type: Some(ReportObjectType::Report),
program_id: self.content().program_id.clone(),
event_id: self.id().clone(),
client_name,
Expand Down
6 changes: 1 addition & 5 deletions openadr-client/src/program.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use openadr_wire::{
event::{EventObjectType, Priority},
Program,
};
use openadr_wire::{event::Priority, Program};

use crate::{
error::{Error, Result},
Expand Down Expand Up @@ -89,7 +86,6 @@ impl ProgramClient {
/// Create a new event object within the program
pub fn new_event(&self) -> EventContent {
EventContent {
object_type: Some(EventObjectType::Event),
program_id: self.id().clone(),
event_name: None,
priority: Priority::UNSPECIFIED,
Expand Down
1 change: 0 additions & 1 deletion openadr-client/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ pub async fn setup_program_client(program_name: impl ToString, db: PgPool) -> Pr
let client = setup_client(db).await;

let program_content = ProgramContent {
object_type: None,
program_name: program_name.to_string(),
program_long_name: Some("program_long_name".to_string()),
retailer_name: Some("retailer_name".to_string()),
Expand Down
1 change: 0 additions & 1 deletion openadr-client/tests/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ mod common;

fn default_content(program_id: &ProgramId) -> EventContent {
EventContent {
object_type: None,
program_id: program_id.clone(),
event_name: Some("event_name".to_string()),
priority: Priority::MAX,
Expand Down
1 change: 0 additions & 1 deletion openadr-client/tests/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ mod common;

fn default_content() -> ProgramContent {
ProgramContent {
object_type: None,
program_name: "program_name".to_string(),
program_long_name: Some("program_long_name".to_string()),
retailer_name: Some("retailer_name".to_string()),
Expand Down
16 changes: 10 additions & 6 deletions openadr-client/tests/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ async fn crud() {
let ven = ctx.create_ven(new).await.unwrap();

// Create
let new = ResourceContent::new("test-resource".to_string(), None, None);
let new = ResourceContent {
resource_name: "test-resource".to_string(),
attributes: None,
targets: None,
};
let created_resource = ven.create_resource(new.clone()).await.unwrap();
assert_eq!(created_resource.content().resource_name, "test-resource");

Expand Down Expand Up @@ -54,11 +58,11 @@ async fn crud() {
// Retrieve one by name
{
let resource2 = ven
.create_resource(ResourceContent::new(
"test-resource2".to_string(),
None,
None,
))
.create_resource(ResourceContent {
resource_name: "test-resource2".to_string(),
attributes: None,
targets: None,
})
.await
.unwrap();
let get_resource = ven.get_resource_by_name("test-resource").await.unwrap();
Expand Down
1 change: 0 additions & 1 deletion openadr-vtn/src/api/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ mod test {

fn default_event_content() -> EventContent {
EventContent {
object_type: None,
program_id: ProgramId::new("program-1").unwrap(),
event_name: Some("event_name".to_string()),
priority: Priority::MAX,
Expand Down
1 change: 0 additions & 1 deletion openadr-vtn/src/api/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ mod test {

fn default_content() -> ProgramContent {
ProgramContent {
object_type: Default::default(),
program_name: "program_name".to_string(),
program_long_name: Some("program_long_name".to_string()),
retailer_name: Some("retailer_name".to_string()),
Expand Down
1 change: 0 additions & 1 deletion openadr-vtn/src/api/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ mod test {

fn default() -> ReportContent {
ReportContent {
object_type: None,
program_id: "asdf".parse().unwrap(),
event_id: "asdf".parse().unwrap(),
client_name: "".to_string(),
Expand Down
4 changes: 2 additions & 2 deletions openadr-vtn/src/api/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ mod test {
let test = ApiTest::new(db, vec![AuthRole::AnyBusiness]);

let resources = [
ResourceContent::new("".to_string(), None, None),
ResourceContent::new("This is more than 128 characters long and should be rejected This is more than 128 characters long and should be rejected asdfasd".to_string(), None, None),
ResourceContent{resource_name: "".to_string(), targets: None, attributes:None},
ResourceContent{resource_name: "This is more than 128 characters long and should be rejected This is more than 128 characters long and should be rejected asdfasd".to_string(),targets: None, attributes:None},
];

for resource in &resources {
Expand Down
3 changes: 0 additions & 3 deletions openadr-vtn/src/data_source/postgres/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ impl TryFrom<PostgresEvent> for Event {
created_date_time: value.created_date_time,
modification_date_time: value.modification_date_time,
content: EventContent {
object_type: Default::default(),
program_id: value.program_id.parse()?,
event_name: value.event_name,
priority: value.priority,
Expand Down Expand Up @@ -440,7 +439,6 @@ mod tests {
created_date_time: "2024-07-25 08:31:10.776000 +00:00".parse().unwrap(),
modification_date_time: "2024-07-25 08:31:10.776000 +00:00".parse().unwrap(),
content: EventContent {
object_type: Default::default(),
program_id: "program-1".parse().unwrap(),
event_name: Some("event-1-name".to_string()),
priority: Some(4).into(),
Expand Down Expand Up @@ -483,7 +481,6 @@ mod tests {
created_date_time: "2024-07-25 08:31:10.776000 +00:00".parse().unwrap(),
modification_date_time: "2024-07-25 08:31:10.776000 +00:00".parse().unwrap(),
content: EventContent {
object_type: Default::default(),
program_id: "program-2".parse().unwrap(),
event_name: Some("event-2-name".to_string()),
priority: None.into(),
Expand Down
3 changes: 0 additions & 3 deletions openadr-vtn/src/data_source/postgres/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ impl TryFrom<PostgresProgram> for Program {
created_date_time: value.created_date_time,
modification_date_time: value.modification_date_time,
content: ProgramContent {
object_type: Default::default(),
program_name: value.program_name,
program_long_name: value.program_long_name,
retailer_name: value.retailer_name,
Expand Down Expand Up @@ -536,7 +535,6 @@ mod tests {
created_date_time: "2024-07-25 08:31:10.776000 +00:00".parse().unwrap(),
modification_date_time: "2024-07-25 08:31:10.776000 +00:00".parse().unwrap(),
content: ProgramContent {
object_type: Default::default(),
program_name: "program-1".to_string(),
program_long_name: Some("program long name".to_string()),
retailer_name: Some("retailer name".to_string()),
Expand Down Expand Up @@ -576,7 +574,6 @@ mod tests {
created_date_time: "2024-07-25 08:31:10.776000 +00:00".parse().unwrap(),
modification_date_time: "2024-07-25 08:31:10.776000 +00:00".parse().unwrap(),
content: ProgramContent {
object_type: Default::default(),
program_name: "program-2".to_string(),
program_long_name: None,
retailer_name: None,
Expand Down
1 change: 0 additions & 1 deletion openadr-vtn/src/data_source/postgres/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ impl TryFrom<PostgresReport> for Report {
created_date_time: value.created_date_time,
modification_date_time: value.modification_date_time,
content: ReportContent {
object_type: Default::default(),
program_id: value.program_id.parse()?,
event_id: value.event_id.parse()?,
client_name: value.client_name,
Expand Down
6 changes: 5 additions & 1 deletion openadr-vtn/src/data_source/postgres/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ impl TryFrom<PostgresResource> for Resource {
created_date_time: value.created_date_time,
modification_date_time: value.modification_date_time,
ven_id: value.ven_id.parse()?,
content: ResourceContent::new(value.resource_name, attributes, targets),
content: ResourceContent {
resource_name: value.resource_name,
attributes,
targets,
},
})
}
}
Expand Down
18 changes: 1 addition & 17 deletions openadr-wire/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@ pub struct Event {

#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Validate)]
#[serde(rename_all = "camelCase")]
#[serde(rename_all = "camelCase", tag = "objectType", rename = "EVENT")]
pub struct EventContent {
/// Used as discriminator, e.g. notification.object
// TODO: remove this?
pub object_type: Option<EventObjectType>,
/// URL safe VTN assigned object ID.
#[serde(rename = "programID")]
pub program_id: ProgramId,
Expand Down Expand Up @@ -65,7 +62,6 @@ impl EventContent {
);

Self {
object_type: None,
program_id,
event_name: None,
priority: Priority::UNSPECIFIED,
Expand Down Expand Up @@ -139,16 +135,6 @@ impl FromStr for EventId {
}
}

/// Used as discriminator, e.g. notification.object
#[derive(
Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize,
)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum EventObjectType {
#[default]
Event,
}

/// Relative priority of an event
///
/// 0 indicates the highest priority.
Expand Down Expand Up @@ -348,7 +334,6 @@ mod tests {
assert_eq!(
serde_json::from_str::<EventContent>(example).unwrap(),
EventContent {
object_type: None,
program_id: ProgramId("foo".parse().unwrap()),
event_name: None,
priority: Priority::MIN,
Expand Down Expand Up @@ -404,7 +389,6 @@ mod tests {
created_date_time: "2023-06-15T09:30:00Z".parse().unwrap(),
modification_date_time: "2023-06-15T09:30:00Z".parse().unwrap(),
content: EventContent {
object_type: Some(EventObjectType::Event),
program_id: ProgramId("object-999".parse().unwrap()),
event_name: Some("price event 11-18-2022".into()),
priority: Priority::MAX,
Expand Down
20 changes: 1 addition & 19 deletions openadr-wire/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,8 @@ pub struct Program {

#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Validate)]
#[serde(rename_all = "camelCase")]
#[serde(rename_all = "camelCase", tag = "objectType", rename = "PROGRAM")]
pub struct ProgramContent {
/// Used as discriminator, e.g. notification.object
///
/// VTN provisioned on object creation.
// TODO: Maybe remove this? It is more part of the enum containing this
pub object_type: Option<ProgramObjectType>,
/// Short name to uniquely identify program.
#[serde(deserialize_with = "crate::string_within_range_inclusive::<1, 128, _>")]
pub program_name: String,
Expand Down Expand Up @@ -86,7 +81,6 @@ pub struct ProgramContent {
impl ProgramContent {
pub fn new(name: impl ToString) -> ProgramContent {
ProgramContent {
object_type: Some(ProgramObjectType::Program),
program_name: name.to_string(),
program_long_name: Default::default(),
retailer_name: Default::default(),
Expand Down Expand Up @@ -133,16 +127,6 @@ impl FromStr for ProgramId {
}
}

/// Used as discriminator, e.g. notification.object
#[derive(
Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize,
)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ProgramObjectType {
#[default]
Program,
}

#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize, Validate)]
pub struct ProgramDescription {
/// A human or machine readable program description
Expand Down Expand Up @@ -198,7 +182,6 @@ mod test {
created_date_time: "2023-06-15T09:30:00Z".parse().unwrap(),
modification_date_time: "2023-06-15T09:30:00Z".parse().unwrap(),
content: ProgramContent {
object_type: Some(ProgramObjectType::Program),
program_name: "ResTOU".into(),
program_long_name: Some("Residential Time of Use-A".into()),
retailer_name: Some("ACME".into()),
Expand Down Expand Up @@ -230,7 +213,6 @@ mod test {
assert_eq!(
serde_json::from_str::<ProgramContent>(example).unwrap(),
ProgramContent {
object_type: None,
program_name: "test".to_string(),
program_long_name: None,
retailer_name: None,
Expand Down
16 changes: 1 addition & 15 deletions openadr-wire/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ pub struct Report {

#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Validate)]
#[serde(rename_all = "camelCase")]
#[serde(rename_all = "camelCase", tag = "objectType", rename = "REPORT")]
pub struct ReportContent {
/// Used as discriminator, e.g. notification.object
pub object_type: Option<ReportObjectType>,
// FIXME Must likely be EITHER a programID OR an eventID
/// ID attribute of the program object this report is associated with.
#[serde(rename = "programID")]
Expand Down Expand Up @@ -107,16 +105,6 @@ impl FromStr for ReportId {
}
}

/// Used as discriminator, e.g. notification.object
#[derive(
Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize,
)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ReportObjectType {
#[default]
Report,
}

/// Report data associated with a resource.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -357,7 +345,6 @@ mod tests {
fn parses_minimal_report() {
let example = r#"{"programID":"p1","eventID":"e1","clientName":"c","resources":[]}"#;
let expected = ReportContent {
object_type: None,
program_id: ProgramId("p1".parse().unwrap()),
event_id: EventId("e1".parse().unwrap()),
client_name: "c".to_string(),
Expand Down Expand Up @@ -440,7 +427,6 @@ mod tests {
created_date_time: "2023-06-15T09:30:00Z".parse().unwrap(),
modification_date_time: "2023-06-15T09:30:00Z".parse().unwrap(),
content: ReportContent {
object_type: Some(ReportObjectType::Report),
program_id: ProgramId("object-999".parse().unwrap()),
event_id: EventId("object-999".parse().unwrap()),
client_name: "VEN-999".into(),
Expand Down
27 changes: 1 addition & 26 deletions openadr-wire/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ pub struct Resource {

#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Validate)]
#[serde(rename_all = "camelCase")]
#[serde(rename_all = "camelCase", tag = "objectType", rename = "RESOURCE")]
pub struct ResourceContent {
/// Used as discriminator, e.g. notification.object
object_type: Option<ObjectType>,
/// User generated identifier, resource may be configured with identifier out-of-band.
#[serde(deserialize_with = "crate::string_within_range_inclusive::<1, 128, _>")]
pub resource_name: String,
Expand All @@ -42,29 +40,6 @@ pub struct ResourceContent {
pub targets: Option<TargetMap>,
}

impl ResourceContent {
pub fn new(
resource_name: String,
attributes: Option<Vec<ValuesMap>>,
targets: Option<TargetMap>,
) -> Self {
Self {
object_type: Some(Default::default()),
resource_name,
attributes,
targets,
}
}
}

/// Used as discriminator, e.g. notification.object
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ObjectType {
#[default]
Resource,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Hash, Eq)]
pub struct ResourceId(pub(crate) Identifier);

Expand Down
Loading

0 comments on commit 50f8cbb

Please sign in to comment.