1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//! Flight Plan Parcel
use std::collections::HashMap;

pub use crate::grpc::server::flight_plan_parcel::*;

use crate::common::ArrErr;
use crate::grpc::{GrpcDataObjectType, GrpcField};
use crate::postgres::init::PsqlInitLinkedResource;
use crate::resources::base::simple_resource_linked::*;
use crate::resources::base::{FieldDefinition, ResourceDefinition};
use log::debug;
use tokio_postgres::row::Row;
use tokio_postgres::types::Type as PsqlFieldType;
use uuid::Uuid;

crate::build_generic_resource_linked_impl_from!();
crate::build_grpc_simple_resource_linked_impl!(flight_plan_parcel, parcel);

impl Resource for ResourceObject<Data> {
    fn get_definition() -> ResourceDefinition {
        ResourceDefinition {
            psql_table: "flight_plan_parcel".to_owned(),
            psql_id_cols: vec![String::from("flight_plan_id"), String::from("parcel_id")],
            fields: HashMap::from([
                (
                    "acquire".to_string(),
                    FieldDefinition::new(PsqlFieldType::BOOL, true),
                ),
                (
                    "deliver".to_string(),
                    FieldDefinition::new(PsqlFieldType::BOOL, true),
                ),
            ]),
        }
    }
}

impl GrpcDataObjectType for Data {
    fn get_field_value(&self, key: &str) -> Result<GrpcField, ArrErr> {
        match key {
            "acquire" => Ok(GrpcField::Bool(self.acquire)),
            "deliver" => Ok(GrpcField::Bool(self.deliver)),
            _ => Err(ArrErr::Error(format!(
                "Invalid key specified [{}], no such field found",
                key
            ))),
        }
    }
}

#[cfg(not(tarpaulin_include))]
// no_coverage: Can not be tested in unittest until https://github.com/sfackler/rust-postgres/pull/979 has been merged
impl TryFrom<Row> for Data {
    type Error = ArrErr;

    fn try_from(row: Row) -> Result<Self, ArrErr> {
        let acquire: bool = row.get::<&str, bool>("acquire");
        let deliver: bool = row.get::<&str, bool>("deliver");

        debug!(
            "(try_from) Converting Row to flight_plan_parcel::Data: {:?}",
            row
        );
        Ok(Data { acquire, deliver })
    }
}

impl GrpcDataObjectType for RowData {
    fn get_field_value(&self, key: &str) -> Result<GrpcField, ArrErr> {
        match key {
            "flight_plan_id" => Ok(GrpcField::String(self.flight_plan_id.clone())),
            "parcel_id" => Ok(GrpcField::String(self.parcel_id.clone())),
            "acquire" => Ok(GrpcField::Bool(self.acquire)),
            "deliver" => Ok(GrpcField::Bool(self.deliver)),
            _ => Err(ArrErr::Error(format!(
                "Invalid key specified [{}], no such field found",
                key
            ))),
        }
    }
}

#[cfg(not(tarpaulin_include))]
// no_coverage: Can not be tested in unittest until https://github.com/sfackler/rust-postgres/pull/979 has been merged
impl TryFrom<Row> for RowData {
    type Error = ArrErr;

    fn try_from(row: Row) -> Result<Self, ArrErr> {
        let flight_plan_id: String = row.get::<&str, Uuid>("flight_plan_id").to_string();
        let parcel_id: String = row.get::<&str, Uuid>("parcel_id").to_string();
        let acquire: bool = row.get::<&str, bool>("acquire");
        let deliver: bool = row.get::<&str, bool>("deliver");

        debug!(
            "(try_from) Converting Row to flight_plan_parcel::Data: {:?}",
            row
        );
        Ok(RowData {
            flight_plan_id,
            parcel_id,
            acquire,
            deliver,
        })
    }
}

impl From<RowData> for ResourceObject<Data> {
    fn from(row_data: RowData) -> Self {
        ResourceObject {
            ids: Some(HashMap::from([
                (String::from("flight_plan_id"), row_data.flight_plan_id),
                (String::from("parcel_id"), row_data.parcel_id),
            ])),
            data: Some(Data {
                acquire: row_data.acquire,
                deliver: row_data.deliver,
            }),
            mask: None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::resources::FieldValue;
    use crate::test_util::*;

    #[tokio::test]
    async fn test_flight_plan_parcel_schema() {
        crate::get_log_handle().await;
        ut_info!("(test_flight_plan_parcel_schema) start");

        let definition = <ResourceObject<Data>>::get_definition();
        assert_eq!(definition.get_psql_table(), "flight_plan_parcel");

        let ids = vec![
            FieldValue {
                field: String::from("flight_plan_id"),
                value: Uuid::new_v4().to_string(),
            },
            FieldValue {
                field: String::from("parcel_id"),
                value: Uuid::new_v4().to_string(),
            },
        ];

        let data = mock::get_data_obj();
        let object: ResourceObject<Data> = Object {
            ids,
            data: Some(data.clone()),
        }
        .into();
        test_schema::<ResourceObject<Data>, Data>(object);

        let result = validate::<ResourceObject<Data>>(&data);
        assert!(result.is_ok());
        if let Ok((sql_fields, validation_result)) = result {
            ut_info!("{:?}", sql_fields);
            ut_info!("{:?}", validation_result);
            assert_eq!(validation_result.success, true);
        }
        ut_info!("(test_flight_plan_parcel_schema) success");
    }
}