Changeset View
Changeset View
Standalone View
Standalone View
services/tunnelbroker/src/database/message.rs
- This file was added.
use std::collections::HashMap; | |||||
use aws_sdk_dynamodb::types::AttributeValue; | |||||
use crate::constants::dynamodb::undelivered_messages::{ | |||||
CREATED_AT, DEVICE_ID, PAYLOAD, | |||||
}; | |||||
#[derive(Debug)] | |||||
pub struct DeviceMessage { | |||||
pub device_id: String, | |||||
pub created_at: String, | |||||
pub payload: String, | |||||
} | |||||
#[derive(Debug, derive_more::Display)] | |||||
pub enum MessageErrors { | |||||
SerializationError, | |||||
} | |||||
bartek: Maybe we could use types from `comm-services-lib/database.rs`? | |||||
impl DeviceMessage { | |||||
pub fn from_hashmap( | |||||
hashmap: HashMap<String, AttributeValue>, | |||||
) -> Result<DeviceMessage, MessageErrors> { | |||||
let device_id: String = hashmap | |||||
.get(DEVICE_ID) | |||||
.ok_or(MessageErrors::SerializationError)? | |||||
.as_s() | |||||
.map_err(|_| MessageErrors::SerializationError)? | |||||
.to_string(); | |||||
let created_at: String = hashmap | |||||
.get(CREATED_AT) | |||||
.ok_or(MessageErrors::SerializationError)? | |||||
.as_n() | |||||
.map_err(|_| MessageErrors::SerializationError)? | |||||
.to_string(); | |||||
let payload: String = hashmap | |||||
.get(PAYLOAD) | |||||
.ok_or(MessageErrors::SerializationError)? | |||||
.as_s() | |||||
.map_err(|_| MessageErrors::SerializationError)? | |||||
.to_string(); | |||||
bartekUnsubmitted Done Inline ActionsSame as above, in comm-services-lib we have things as parse_string_attribute(), parse_number() etc... Perhaps they can be used here? bartek: Same as above, in `comm-services-lib` we have things as `parse_string_attribute()`… | |||||
jonAuthorUnsubmitted Done Inline ActionsI tried to use these utilities. But ran into some issues with imports. comm-services-lib still uses aws-sdk-dynamodb v0.24, while I'm currently using v0.27. They changed where AttributeValue is located. In v0.24, AttributeValue is in dynamodb::model, but in v0.27 it's dynamodb::types. Although reverting would be pretty easy for this diff. It would be a big change for much of my other code. I think the best path forward would be to update comm-services-lib, and then apply your suggestion. jon: I tried to use these utilities. But ran into some issues with imports. comm-services-lib still… | |||||
Ok(DeviceMessage { | |||||
device_id, | |||||
created_at, | |||||
payload, | |||||
}) | |||||
} | |||||
} |
Maybe we could use types from comm-services-lib/database.rs?