use std::fmt::Display;
use crate::{
BigDecimal, BigInt, ByteBuffer, IndexMap, Instant,
schema::{
Document, DocumentError, NULL, SchemaRef, ShapeId, ShapeType, StaticSchemaShape,
default::Value,
},
serde::{
ShapeBuilder,
de::Deserializer,
se::{ListSerializer, MapSerializer, Serializer, StructSerializer},
serializers::{Error, SerializeWithSchema},
},
};
impl SerializeWithSchema for Box<dyn Document> {
fn serialize_with_schema<S: Serializer>(
&self,
schema: &SchemaRef,
serializer: S,
) -> Result<S::Ok, S::Error> {
match self.get_type() {
Some(ShapeType::Blob) => serializer.write_blob(schema, self.as_blob().unwrap()),
Some(ShapeType::Boolean) => serializer.write_boolean(schema, self.as_bool().unwrap()),
Some(ShapeType::String | ShapeType::Enum) => {
serializer.write_string(schema, self.as_string().unwrap())
}
Some(ShapeType::Timestamp) => {
serializer.write_timestamp(schema, self.as_timestamp().unwrap())
}
Some(ShapeType::Byte) => serializer.write_byte(schema, self.as_byte().unwrap()),
Some(ShapeType::Short) => serializer.write_short(schema, self.as_short().unwrap()),
Some(ShapeType::Integer | ShapeType::IntEnum) => {
serializer.write_integer(schema, self.as_integer().unwrap())
}
Some(ShapeType::Long) => serializer.write_long(schema, self.as_long().unwrap()),
Some(ShapeType::Float) => serializer.write_float(schema, self.as_float().unwrap()),
Some(ShapeType::Double) => serializer.write_double(schema, self.as_double().unwrap()),
Some(ShapeType::BigInteger) => {
serializer.write_big_integer(schema, self.as_big_integer().unwrap())
}
Some(ShapeType::BigDecimal) => {
serializer.write_big_decimal(schema, self.as_big_decimal().unwrap())
}
Some(ShapeType::List) => self
.as_list()
.unwrap()
.serialize_with_schema(schema, serializer),
Some(ShapeType::Map) => self
.as_map()
.unwrap()
.serialize_with_schema(schema, serializer),
Some(ShapeType::Structure | ShapeType::Union) => {
let document_map = self.as_map().unwrap();
let mut struct_serializer = serializer.write_struct(schema, self.size())?;
if let Some(discriminator) = &self.discriminator() {
struct_serializer.serialize_discriminator(discriminator)?;
}
for (key, value) in document_map {
if let Some(member_schema) = schema.get_member(key) {
struct_serializer.serialize_member(member_schema, value)?;
} else {
todo!("Add some logging on unknown members");
}
}
struct_serializer.end(schema)
}
None => serializer.write_null(schema),
_ => Err(Error::custom("Unsupported shape type")),
}
}
}
impl<T> From<T> for Box<dyn Document>
where
T: StaticSchemaShape + SerializeWithSchema,
{
fn from(shape: T) -> Self {
shape
.serialize_with_schema(T::schema(), DocumentParser)
.expect(
"Infallible conversion from StaticSchemaShape to Document failed - this is a bug",
)
}
}
impl dyn Document {
#[inline]
pub(crate) fn into_builder<'de, B: ShapeBuilder<'de, S>, S: StaticSchemaShape>(
self: Box<Self>,
) -> Result<B, DocumentError> {
B::deserialize_with_schema(S::schema(), &mut DocumentDeserializer::new(self))
}
}
impl Error for DocumentError {
fn custom<T: Display>(msg: T) -> Self {
DocumentError::CustomError(msg.to_string())
}
}
struct DocumentParser;
impl Serializer for DocumentParser {
type Error = DocumentError;
type Ok = Box<dyn Document>;
type SerializeList = DocumentListAccumulator;
type SerializeMap = DocumentMapAccumulator;
type SerializeStruct = DocumentMapAccumulator;
fn write_struct(
self,
schema: &SchemaRef,
len: usize,
) -> Result<Self::SerializeStruct, Self::Error> {
Ok(DocumentMapAccumulator {
schema: schema.clone(),
values: IndexMap::with_capacity(len),
discriminator: Some(schema.id().clone()),
})
}
fn write_map(self, schema: &SchemaRef, len: usize) -> Result<Self::SerializeMap, Self::Error> {
Ok(DocumentMapAccumulator {
schema: schema.clone(),
values: IndexMap::with_capacity(len),
discriminator: Some(schema.id().clone()),
})
}
fn write_list(
self,
schema: &SchemaRef,
len: usize,
) -> Result<Self::SerializeList, Self::Error> {
Ok(DocumentListAccumulator {
schema: schema.clone(),
values: Vec::with_capacity(len),
discriminator: Some(schema.id().clone()),
})
}
#[inline]
fn write_boolean(self, _schema: &SchemaRef, value: bool) -> Result<Self::Ok, Self::Error> {
Ok(value.into())
}
#[inline]
fn write_byte(self, _schema: &SchemaRef, value: i8) -> Result<Self::Ok, Self::Error> {
Ok(value.into())
}
#[inline]
fn write_short(self, _schema: &SchemaRef, value: i16) -> Result<Self::Ok, Self::Error> {
Ok(value.into())
}
#[inline]
fn write_integer(self, _schema: &SchemaRef, value: i32) -> Result<Self::Ok, Self::Error> {
Ok(value.into())
}
#[inline]
fn write_long(self, _schema: &SchemaRef, value: i64) -> Result<Self::Ok, Self::Error> {
Ok(value.into())
}
#[inline]
fn write_float(self, _schema: &SchemaRef, value: f32) -> Result<Self::Ok, Self::Error> {
Ok(value.into())
}
#[inline]
fn write_double(self, _schema: &SchemaRef, value: f64) -> Result<Self::Ok, Self::Error> {
Ok(value.into())
}
#[inline]
fn write_big_integer(
self,
_schema: &SchemaRef,
value: &BigInt,
) -> Result<Self::Ok, Self::Error> {
Ok(value.clone().into())
}
#[inline]
fn write_big_decimal(
self,
_schema: &SchemaRef,
value: &BigDecimal,
) -> Result<Self::Ok, Self::Error> {
Ok(value.clone().into())
}
#[inline]
fn write_string(self, _schema: &SchemaRef, value: &str) -> Result<Self::Ok, Self::Error> {
Ok(value.into())
}
#[inline]
fn write_blob(self, _schema: &SchemaRef, value: &ByteBuffer) -> Result<Self::Ok, Self::Error> {
Ok(value.clone().into())
}
#[inline]
fn write_timestamp(
self,
_schema: &SchemaRef,
value: &Instant,
) -> Result<Self::Ok, Self::Error> {
Ok(value.into())
}
#[inline]
fn write_document(
self,
_schema: &SchemaRef,
value: &Box<dyn Document>,
) -> Result<Self::Ok, Self::Error> {
Ok(value.clone())
}
#[inline]
fn write_null(self, _schema: &SchemaRef) -> Result<Self::Ok, Self::Error> {
Ok(NULL.clone())
}
#[inline]
fn skip(self, _schema: &SchemaRef) -> Result<Self::Ok, Self::Error> {
Ok(NULL.clone())
}
}
#[doc(hidden)]
pub struct DocumentListAccumulator {
schema: SchemaRef,
values: Vec<Box<dyn Document>>,
discriminator: Option<ShapeId>,
}
impl ListSerializer for DocumentListAccumulator {
type Error = DocumentError;
type Ok = Box<dyn Document>;
fn serialize_element<T>(
&mut self,
element_schema: &SchemaRef,
value: &T,
) -> Result<(), Self::Error>
where
T: SerializeWithSchema,
{
let el = value.serialize_with_schema(element_schema, DocumentParser)?;
self.values.push(el);
Ok(())
}
#[inline]
fn end(self, _schema: &SchemaRef) -> Result<Self::Ok, Self::Error> {
Ok(crate::schema::default::Document {
schema: self.schema,
value: Value::List(self.values),
discriminator: self.discriminator,
}
.into())
}
}
#[doc(hidden)]
pub struct DocumentMapAccumulator {
schema: SchemaRef,
values: IndexMap<String, Box<dyn Document>>,
discriminator: Option<ShapeId>,
}
impl MapSerializer for DocumentMapAccumulator {
type Error = DocumentError;
type Ok = Box<dyn Document>;
fn serialize_entry<K, V>(
&mut self,
key_schema: &SchemaRef,
value_schema: &SchemaRef,
key: &K,
value: &V,
) -> Result<(), Self::Error>
where
K: SerializeWithSchema,
V: SerializeWithSchema,
{
let key_doc = key.serialize_with_schema(key_schema, DocumentParser)?;
let key_str = key_doc
.as_string()
.ok_or_else(|| {
DocumentError::DocumentConversion("Map key must be a string".to_string())
})?
.to_string();
let val = value.serialize_with_schema(value_schema, DocumentParser)?;
self.values.insert(key_str, val);
Ok(())
}
#[inline]
fn end(self, _schema: &SchemaRef) -> Result<Self::Ok, Self::Error> {
Ok(crate::schema::default::Document {
schema: self.schema,
value: Value::Map(self.values),
discriminator: self.discriminator,
}
.into())
}
}
impl StructSerializer for DocumentMapAccumulator {
type Error = DocumentError;
type Ok = Box<dyn Document>;
fn serialize_member<T>(
&mut self,
member_schema: &SchemaRef,
value: &T,
) -> Result<(), Self::Error>
where
T: SerializeWithSchema,
{
let Some(me) = member_schema.as_member() else {
return Err(DocumentError::DocumentConversion(
"Expected member schema!".to_string(),
));
};
let val = value.serialize_with_schema(member_schema, DocumentParser)?;
self.values.insert(me.name.clone(), val);
Ok(())
}
#[inline]
fn end(self, _schema: &SchemaRef) -> Result<Self::Ok, Self::Error> {
Ok(crate::schema::default::Document {
schema: self.schema,
value: Value::Map(self.values),
discriminator: self.discriminator,
}
.into())
}
}
struct DocumentDeserializer {
document: Option<Box<dyn Document>>,
}
impl DocumentDeserializer {
pub fn new(document: Box<dyn Document>) -> Self {
Self {
document: Some(document),
}
}
#[inline]
fn get_inner<T: TryFrom<Box<dyn Document>, Error = DocumentError>>(
&mut self,
) -> Result<T, DocumentError> {
self.document
.take()
.ok_or_else(|| {
DocumentError::DocumentConversion(
"Encountered empty document deserializer".to_string(),
)
})?
.try_into()
}
}
impl Deserializer<'_> for DocumentDeserializer {
type Error = DocumentError;
#[inline]
fn read_bool(&mut self, _schema: &SchemaRef) -> Result<bool, Self::Error> {
self.get_inner()
}
#[inline]
fn read_byte(&mut self, _schema: &SchemaRef) -> Result<i8, Self::Error> {
self.get_inner()
}
#[inline]
fn read_short(&mut self, _schema: &SchemaRef) -> Result<i16, Self::Error> {
self.get_inner()
}
#[inline]
fn read_integer(&mut self, _schema: &SchemaRef) -> Result<i32, Self::Error> {
self.get_inner()
}
#[inline]
fn read_long(&mut self, _schema: &SchemaRef) -> Result<i64, Self::Error> {
self.get_inner()
}
#[inline]
fn read_float(&mut self, _schema: &SchemaRef) -> Result<f32, Self::Error> {
self.get_inner()
}
#[inline]
fn read_double(&mut self, _schema: &SchemaRef) -> Result<f64, Self::Error> {
self.get_inner()
}
#[inline]
fn read_big_integer(&mut self, _schema: &SchemaRef) -> Result<BigInt, Self::Error> {
self.get_inner()
}
#[inline]
fn read_big_decimal(&mut self, _schema: &SchemaRef) -> Result<BigDecimal, Self::Error> {
self.get_inner()
}
#[inline]
fn read_string(&mut self, _schema: &SchemaRef) -> Result<String, Self::Error> {
self.get_inner()
}
#[inline]
fn read_blob(&mut self, _schema: &SchemaRef) -> Result<ByteBuffer, Self::Error> {
self.get_inner()
}
#[inline]
fn read_timestamp(&mut self, _schema: &SchemaRef) -> Result<Instant, Self::Error> {
self.get_inner()
}
#[inline]
fn read_document(&mut self, _schema: &SchemaRef) -> Result<Box<dyn Document>, Self::Error> {
self.document.take().ok_or_else(|| {
DocumentError::DocumentConversion("Encountered empty document deserializer".to_string())
})
}
fn read_struct<B, F>(
&mut self,
schema: &SchemaRef,
mut builder: B,
mut consumer: F,
) -> Result<B, Self::Error>
where
F: FnMut(B, &SchemaRef, &mut Self) -> Result<B, Self::Error>,
{
let map: IndexMap<String, Box<dyn Document>> = self.get_inner()?;
for (key, value) in map {
if let Some(member_schema) = schema.members().get(&key) {
let mut field_deser = DocumentDeserializer::new(value);
builder = consumer(builder, member_schema, &mut field_deser)?;
}
}
Ok(builder)
}
fn read_list<T, F>(
&mut self,
schema: &SchemaRef,
state: &mut T,
mut consumer: F,
) -> Result<(), Self::Error>
where
F: FnMut(&mut T, &SchemaRef, &mut Self) -> Result<(), Self::Error>,
{
let list: Vec<Box<dyn Document>> = self.get_inner()?;
let member_schema = schema.get_member("member").ok_or_else(|| {
DocumentError::DocumentConversion("List missing member schema".to_string())
})?;
for element_doc in list {
let mut elem_deser = DocumentDeserializer::new(element_doc);
consumer(state, member_schema, &mut elem_deser)?;
}
Ok(())
}
fn read_map<T, F>(
&mut self,
_schema: &SchemaRef,
state: &mut T,
mut consumer: F,
) -> Result<(), Self::Error>
where
F: FnMut(&mut T, String, &mut Self) -> Result<(), Self::Error>,
{
let map: IndexMap<String, Box<dyn Document>> = self.get_inner()?;
for (key_str, value_doc) in map {
let mut value_deser = DocumentDeserializer::new(value_doc);
consumer(state, key_str.clone(), &mut value_deser)?;
}
Ok(())
}
fn is_null(&mut self) -> bool {
self.document
.as_ref()
.expect("Empty document deserializer")
.is_null()
}
fn read_null(&mut self) -> Result<(), Self::Error> {
if self.is_null() {
Ok(())
} else {
Err(DocumentError::DocumentConversion(
"Expected null document".to_string(),
))
}
}
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use super::*;
use crate::{derive::SmithyShape, schema::prelude::*, smithy};
smithy!("com.example#Map": {
map MAP_SCHEMA {
key: STRING
value: STRING
}
});
smithy!("com.example#List": {
list LIST_SCHEMA {
member: STRING
}
});
smithy!("com.example#Shape": {
structure SCHEMA {
A: STRING = "a"
B: STRING = "b"
C: STRING = "c"
LIST: LIST_SCHEMA = "list"
MAP: MAP_SCHEMA = "map"
}
});
#[derive(SmithyShape, Clone, PartialEq)]
#[smithy_schema(SCHEMA)]
pub struct SerializeMe {
#[smithy_schema(A)]
pub member_a: String,
#[smithy_schema(B)]
pub member_b: String,
#[smithy_schema(C)]
pub member_optional: Option<String>,
#[smithy_schema(LIST)]
pub member_list: Vec<String>,
#[smithy_schema(MAP)]
pub member_map: IndexMap<String, String>,
}
#[test]
fn struct_to_document() {
let mut map = IndexMap::new();
map.insert(String::from("a"), String::from("b"));
let list = vec!["a".to_string(), "b".to_string()];
let struct_to_convert = SerializeMe {
member_a: "a".to_string(),
member_b: "b".to_string(),
member_optional: Some("c".to_string()),
member_map: map,
member_list: list,
};
let document: Box<dyn Document> = struct_to_convert.into();
assert_eq!(document.discriminator().unwrap(), SCHEMA.id());
if let Some(members) = document.as_map() {
let doc_a = &members.get("a").unwrap();
assert_eq!(doc_a.as_string().unwrap(), "a");
let doc_b = &members.get("b").unwrap();
assert_eq!(doc_b.as_string().unwrap(), "b");
let doc_c = &members.get("c").unwrap();
assert_eq!(doc_c.as_string().unwrap(), "c");
assert!(members.contains_key("map"));
assert!(members.contains_key("list"));
} else {
panic!("Expected document");
}
}
#[test]
fn string_document_value() {
let document_str: Box<dyn Document> = "MyStr".into();
let output_str = document_str.as_string().expect("string");
assert_eq!(output_str, &"MyStr".to_string());
let val: &SchemaRef = &STRING;
assert_eq!(document_str.schema(), val);
}
#[test]
#[ignore]
fn number_document_values() {
}
#[test]
fn roundtrip_shape() {
let mut map = IndexMap::new();
map.insert(String::from("a"), String::from("b"));
let list = vec!["a".to_string(), "b".to_string()];
let struct_to_convert = SerializeMe {
member_a: "a".to_string(),
member_b: "b".to_string(),
member_optional: Some("c".to_string()),
member_map: map,
member_list: list,
};
let document: Box<dyn Document> = struct_to_convert.clone().into();
let builder: SerializeMeBuilder = document.into_builder().unwrap();
let result: SerializeMe = builder.build().unwrap();
assert_eq!(result, struct_to_convert);
}
#[test]
fn roundtrip_bool() {
let original = true;
let doc: Box<dyn Document> = original.into();
let result: bool = doc.try_into().unwrap();
assert_eq!(original, result);
}
#[test]
fn roundtrip_string() {
let doc: Box<dyn Document> = "hello world".into();
let result: String = doc.try_into().unwrap();
assert_eq!("hello world".to_string(), result);
}
#[test]
fn roundtrip_integers() {
let original_byte = 127i8;
let doc: Box<dyn Document> = original_byte.into();
let result: i8 = doc.try_into().unwrap();
assert_eq!(127i8, result);
let original_short = 32000i16;
let doc: Box<dyn Document> = original_short.into();
let result: i16 = doc.try_into().unwrap();
assert_eq!(original_short, result);
let original_int = 123_456_i32;
let doc: Box<dyn Document> = original_int.into();
let result: i32 = doc.try_into().unwrap();
assert_eq!(original_int, result);
let original_long = 9_876_543_210_i64;
let doc: Box<dyn Document> = original_long.into();
let result: i64 = doc.try_into().unwrap();
assert_eq!(original_long, result);
}
#[test]
fn roundtrip_floats() {
let original_float = 1.2345f32;
let doc: Box<dyn Document> = original_float.into();
let result: f32 = doc.try_into().unwrap();
assert_eq!(original_float, result);
let original_double = 1.23456789f64;
let doc: Box<dyn Document> = original_double.into();
let result: f64 = doc.try_into().unwrap();
assert_eq!(original_double, result);
}
#[test]
#[ignore = "BigDecimal/BigInteger serialization not yet implemented"]
fn roundtrip_big_numbers() {
let original_big_int = BigInt::from(123_456_789);
let doc: Box<dyn Document> = original_big_int.clone().into();
let result: BigInt = doc.try_into().unwrap();
assert_eq!(original_big_int, result);
let original_big_dec = BigDecimal::from_str("123.456").unwrap();
let doc: Box<dyn Document> = original_big_dec.clone().into();
let result: BigDecimal = doc.try_into().unwrap();
assert_eq!(original_big_dec, result);
}
#[test]
fn roundtrip_list() {
let original: Vec<String> = vec![
"first".to_string(),
"second".to_string(),
"third".to_string(),
];
let doc: Box<dyn Document> = original.clone().into();
let result: Vec<String> = doc.try_into().unwrap();
assert_eq!(original, result);
}
#[test]
fn roundtrip_map() {
let mut original = IndexMap::new();
original.insert("key1".to_string(), "value1".to_string());
original.insert("key2".to_string(), "value2".to_string());
original.insert("key3".to_string(), "value3".to_string());
let doc: Box<dyn Document> = original.clone().into();
let result: IndexMap<String, String> = doc.try_into().unwrap();
assert_eq!(original, result);
}
#[test]
fn roundtrip_struct() {
let mut original_map = IndexMap::new();
original_map.insert("mapkey1".to_string(), "mapvalue1".to_string());
original_map.insert("mapkey2".to_string(), "mapvalue2".to_string());
let original_list = vec![
"listitem1".to_string(),
"listitem2".to_string(),
"listitem3".to_string(),
];
let original = SerializeMe {
member_a: "value_a".to_string(),
member_b: "value_b".to_string(),
member_optional: Some("value_c".to_string()),
member_map: original_map.clone(),
member_list: original_list.clone(),
};
let doc: Box<dyn Document> = original.into();
let doc_map = doc.as_map().expect("Should be a map");
let member_a_doc = doc_map.get("a").expect("Should have member a").clone();
let a_value: String = member_a_doc.try_into().unwrap();
assert_eq!(a_value, "value_a".to_string());
let member_b_doc = doc_map.get("b").expect("Should have member b").clone();
let b_value: String = member_b_doc.try_into().unwrap();
assert_eq!(b_value, "value_b");
let member_c_doc = doc_map.get("c").expect("Should have member c").clone();
let c_value: String = member_c_doc.try_into().unwrap();
assert_eq!(c_value, "value_c");
let list_doc = doc_map.get("list").expect("Should have list").clone();
let list_value: Vec<String> = list_doc.try_into().unwrap();
assert_eq!(list_value, original_list);
let map_doc = doc_map.get("map").expect("Should have map").clone();
let map_value: IndexMap<String, String> = map_doc.try_into().unwrap();
assert_eq!(map_value, original_map);
}
#[test]
fn roundtrip_option() {
let original_some: Option<String> = Some("test".to_string());
let doc: Box<dyn Document> = original_some.clone().into();
let result: Option<String> = doc.try_into().unwrap();
assert_eq!(original_some, result);
let original_none: Option<String> = None;
let doc: Box<dyn Document> = original_none.clone().into();
let result: Option<String> = doc.try_into().unwrap();
assert_eq!(original_none, result);
}
}