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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
use core::fmt;
use crate::{
path::Path,
update::Set,
value::{List, ValueOrRef},
};
/// Represents an update expression to [append elements to a list][1].
///
/// See also: [`Path::list_append`]
///
/// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html#Expressions.UpdateExpressions.SET.UpdatingListElements
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ListAppend {
/// The field to set the newly combined list to
pub(crate) dst: Path,
/// The field to get the current list from
pub(crate) src: Option<Path>,
/// The value(s) to add to the list
pub(crate) list: ValueOrRef,
/// Whether to add the new values to the beginning or end of the source list
after: bool,
}
impl ListAppend {
pub fn builder<T>(dst: T) -> Builder
where
T: Into<Path>,
{
Builder {
dst: dst.into(),
src: None,
after: true,
}
}
/// Add an additional action to this `SET` statement.
///
/// ```
/// use dynamodb_expression::{Num, Path};
/// # use pretty_assertions::assert_eq;
///
/// let set = Path::new_name("foo").list_append().list([7, 8, 9].map(Num::new))
/// .and(Path::new_name("bar").assign("a value"));
/// assert_eq!(r#"SET foo = list_append(foo, [7, 8, 9]), bar = "a value""#, set.to_string());
/// ```
pub fn and<T>(self, action: T) -> Set
where
T: Into<Set>,
{
Set::from(self).and(action)
}
}
impl fmt::Display for ListAppend {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let Self {
dst,
src,
list,
after,
} = self;
// If no source field is specified, default to using the destination.
let src = src.as_ref().unwrap_or(dst);
write!(f, "{dst} = list_append(")?;
if *after {
write!(f, "{src}, {list})")
} else {
write!(f, "{list}, {src})")
}
}
}
/// Builds an [`ListAppend`] instance.
///
/// See also: [`Path::list_append`]
#[must_use = "Consume this `Builder` by using its `.list()` method"]
#[derive(Debug, Clone)]
pub struct Builder {
dst: Path,
src: Option<Path>,
after: bool,
}
impl Builder {
/// Sets the source [`Path`] to read the initial list from.
///
/// Defaults to the [`Path`] the combined list is being assigned to.
///
/// ```
/// # use dynamodb_expression::{Num, Path};
/// # use pretty_assertions::assert_eq;
/// #
/// let list_append = Path::new_name("foo")
/// .list_append()
/// .src(Path::new_name("bar"))
/// .list([1, 2, 3].map(Num::new));
/// assert_eq!("foo = list_append(bar, [1, 2, 3])", list_append.to_string());
/// ```
///
/// Compare with what happens without specifying a source [`Path`]:
///
/// ```
/// # use dynamodb_expression::{Num, Path};
/// # use pretty_assertions::assert_eq;
/// #
/// let list_append = Path::new_name("foo")
/// .list_append()
/// .list([1, 2, 3].map(Num::new));
/// assert_eq!("foo = list_append(foo, [1, 2, 3])", list_append.to_string());
/// ```
pub fn src<T>(mut self, src: T) -> Self
where
T: Into<Path>,
{
self.src = Some(src.into());
self
}
/// The new values will be appended to the end of the existing values.
///
/// This is the default.
///
/// ```
/// # use dynamodb_expression::{Num, Path};
/// # use pretty_assertions::assert_eq;
/// #
/// let list_append = Path::new_name("foo").list_append().after().list([1, 2, 3].map(Num::new));
/// assert_eq!("foo = list_append(foo, [1, 2, 3])", list_append.to_string());
/// ```
///
/// Compare with when [`before`] is used:
///
/// ```
/// # use dynamodb_expression::{Num, Path};
/// # use pretty_assertions::assert_eq;
/// #
/// let list_append = Path::new_name("foo").list_append().before().list([1, 2, 3].map(Num::new));
/// assert_eq!("foo = list_append([1, 2, 3], foo)", list_append.to_string());
/// ```
///
/// The default, with the same behavior as `after`:
///
/// ```
/// # use dynamodb_expression::{Num, Path};
/// # use pretty_assertions::assert_eq;
/// #
/// let list_append = Path::new_name("foo").list_append().list([1, 2, 3].map(Num::new));
/// assert_eq!("foo = list_append(foo, [1, 2, 3])", list_append.to_string());
/// ```
///
/// [`before`]: crate::update::set::list_append::Builder::before
pub fn after(mut self) -> Self {
self.after = true;
self
}
/// The new values will be placed before the existing values.
///
/// Defaults to appending new values [`after`] existing values.
///
/// ```
/// # use dynamodb_expression::{Num, Path};
/// # use pretty_assertions::assert_eq;
/// #
/// let list_append = Path::new_name("foo").list_append().before().list([1, 2, 3].map(Num::new));
/// assert_eq!("foo = list_append([1, 2, 3], foo)", list_append.to_string());
/// ```
///
/// Compare with when [`after`] is used:
///
/// ```
/// # use dynamodb_expression::{Num, Path};
/// # use pretty_assertions::assert_eq;
/// #
/// let list_append = Path::new_name("foo").list_append().after().list([1, 2, 3].map(Num::new));
/// assert_eq!("foo = list_append(foo, [1, 2, 3])", list_append.to_string());
/// ```
///
/// The default, with the same behavior as [`after`]:
///
/// ```
/// # use dynamodb_expression::{Num, Path};
/// # use pretty_assertions::assert_eq;
/// #
/// let list_append = Path::new_name("foo").list_append().list([1, 2, 3].map(Num::new));
/// assert_eq!("foo = list_append(foo, [1, 2, 3])", list_append.to_string());
/// ```
///
/// [`after`]: crate::update::set::list_append::Builder::after
pub fn before(mut self) -> Self {
self.after = false;
self
}
/// Sets the new value(s) to concatenate with the specified field.
///
/// Consumes this [`Builder`] and creates a [`ListAppend`] instance.
pub fn list<T>(self, list: T) -> ListAppend
where
T: Into<List>,
{
let Self { dst, src, after } = self;
ListAppend {
dst,
src,
after,
list: list.into().into(),
}
}
}
#[cfg(test)]
mod test {
use pretty_assertions::assert_str_eq;
use crate::path::Name;
use super::ListAppend;
#[test]
fn display() {
let append = ListAppend::builder(Name::from("foo"))
.src(Name::from("bar"))
.after()
.list(["a", "b"]);
assert_str_eq!(r#"foo = list_append(bar, ["a", "b"])"#, append.to_string());
let append = ListAppend::builder(Name::from("foo"))
.src(Name::from("bar"))
.list(["a", "b"]);
assert_str_eq!(r#"foo = list_append(bar, ["a", "b"])"#, append.to_string());
let append = ListAppend::builder(Name::from("foo"))
.src(Name::from("bar"))
.before()
.list(["a", "b"]);
assert_str_eq!(r#"foo = list_append(["a", "b"], bar)"#, append.to_string());
let append = ListAppend::builder(Name::from("foo"))
.after()
.list(["a", "b"]);
assert_str_eq!(r#"foo = list_append(foo, ["a", "b"])"#, append.to_string());
let append = ListAppend::builder(Name::from("foo")).list(["a", "b"]);
assert_str_eq!(r#"foo = list_append(foo, ["a", "b"])"#, append.to_string());
let append = ListAppend::builder(Name::from("foo"))
.before()
.list(["a", "b"]);
assert_str_eq!(r#"foo = list_append(["a", "b"], foo)"#, append.to_string());
}
}