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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
//! Types related to [DynamoDB update expressions][1]. For more, see [`Update`].
//!
//! [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html
pub mod add;
pub mod delete;
pub mod remove;
pub mod set;
use core::fmt;
pub use self::{
add::Add,
delete::Delete,
remove::Remove,
set::{Assign, IfNotExists, ListAppend, Math, Set, SetAction},
};
/// Represents a [DynamoDB update expression][1].
///
/// # Examples
///
/// ```
/// use dynamodb_expression::{
/// update::{Remove, Update},
/// Path,
/// };
/// # use pretty_assertions::assert_eq;
///
/// let update = Update::from(Path::new_name("foo").math().add(7));
/// assert_eq!("SET foo = foo + 7", update.to_string());
///
/// let update = Update::from(Path::new_name("foo").if_not_exists().assign("a value"));
/// assert_eq!(
/// r#"SET foo = if_not_exists(foo, "a value")"#,
/// update.to_string()
/// );
///
/// let update = Update::from(Remove::new_name("foo"));
/// assert_eq!(r#"REMOVE foo"#, update.to_string());
///
/// let update = Update::from("foo[3].bar[0]".parse::<Path>().unwrap().remove());
/// assert_eq!(r#"REMOVE foo[3].bar[0]"#, update.to_string());
///
/// let update = Update::from(Remove::from_iter(
/// ["foo", "bar", "baz"].into_iter().map(Path::new_name),
/// ));
/// assert_eq!(r#"REMOVE foo, bar, baz"#, update.to_string());
/// ```
///
/// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Update {
Set(Set),
Remove(Remove),
Add(Add),
Delete(Delete),
}
impl Update {
/// A new update expression for a [`Set`] statement.
pub fn new_set<T>(set: T) -> Self
where
T: Into<Set>,
{
set.into().into()
}
/// A new update expression for a [`Remove`] statement.
pub fn new_remove<T>(remove: T) -> Self
where
T: Into<Remove>,
{
remove.into().into()
}
/// A new update expression for an [`Add`] statement.
pub fn new_add<T>(add: T) -> Self
where
T: Into<Add>,
{
add.into().into()
}
/// A new update expression for a [`Delete`] statement.
pub fn new_delete<T>(delete: T) -> Self
where
T: Into<Delete>,
{
delete.into().into()
}
}
impl fmt::Display for Update {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Update::Set(update) => update.fmt(f),
Update::Remove(update) => update.fmt(f),
Update::Add(update) => update.fmt(f),
Update::Delete(update) => update.fmt(f),
}
}
}
impl From<Set> for Update {
fn from(value: Set) -> Self {
Self::Set(value)
}
}
impl From<SetAction> for Update {
fn from(value: SetAction) -> Self {
Self::Set(value.into())
}
}
impl From<Assign> for Update {
fn from(value: Assign) -> Self {
Self::Set(value.into())
}
}
impl From<Math> for Update {
fn from(value: Math) -> Self {
Self::Set(value.into())
}
}
impl From<ListAppend> for Update {
fn from(value: ListAppend) -> Self {
Self::Set(value.into())
}
}
impl From<IfNotExists> for Update {
fn from(value: IfNotExists) -> Self {
Self::Set(value.into())
}
}
impl From<Remove> for Update {
fn from(value: Remove) -> Self {
Self::Remove(value)
}
}
impl From<Add> for Update {
fn from(value: Add) -> Self {
Self::Add(value)
}
}
impl From<Delete> for Update {
fn from(value: Delete) -> Self {
Self::Delete(value)
}
}
#[cfg(test)]
mod test {
#[test]
#[ignore = "This is just to help with formatting the example for `Update`"]
fn example() {
use crate::{
update::{Remove, Update},
Path,
};
use pretty_assertions::assert_eq;
let update = Update::from(Path::new_name("foo").math().add(7));
assert_eq!("SET foo = foo + 7", update.to_string());
let update = Update::from(Path::new_name("foo").if_not_exists().assign("a value"));
assert_eq!(
r#"SET foo = if_not_exists(foo, "a value")"#,
update.to_string()
);
let update = Update::from(Remove::new_name("foo"));
assert_eq!(r#"REMOVE foo"#, update.to_string());
let update = Update::from("foo[3].bar[0]".parse::<Path>().unwrap().remove());
assert_eq!(r#"REMOVE foo[3].bar[0]"#, update.to_string());
let update = Update::from(Remove::from_iter(
["foo", "bar", "baz"].into_iter().map(Path::new_name),
));
assert_eq!(r#"REMOVE foo, bar, baz"#, update.to_string());
}
}