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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
use core::fmt::{self, LowerExp, UpperExp};
use aws_sdk_dynamodb::{primitives::Blob, types::AttributeValue};
use super::base64;
use super::Num;
/// <https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes>
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Scalar {
/// DynamoDB [string](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_AttributeValue.html#DDB-Type-AttributeValue-S)
/// value
String(String),
/// DynamoDB [numeric](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_AttributeValue.html#DDB-Type-AttributeValue-N)
/// value
Num(Num),
/// DynamoDB [boolean](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_AttributeValue.html#DDB-Type-AttributeValue-BOOL)
/// value
Bool(bool),
/// DynamoDB [binary](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_AttributeValue.html#DDB-Type-AttributeValue-B)
/// value
Binary(Vec<u8>),
/// DynamoDB [null](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_AttributeValue.html#DDB-Type-AttributeValue-NULL)
/// value
Null,
}
impl Scalar {
/// Use when you need a [string][1] value for DynamoDB.
///
/// See also: [`Value::new_string`]
///
/// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_AttributeValue.html#DDB-Type-AttributeValue-S
/// [`Value::new_string`]: crate::value::Value::new_string
pub fn new_string<T>(value: T) -> Self
where
T: Into<String>,
{
Self::String(value.into())
}
/// Use when you need a [numeric][1] value for DynamoDB.
///
/// See also: [`Scalar::new_num_lower_exp`], [`Scalar::new_num_upper_exp`],
/// [`Value::new_num`], [`Num`]
///
/// # Examples
///
/// ```
/// use dynamodb_expression::Scalar;
/// # use pretty_assertions::assert_eq;
///
/// let value = Scalar::new_num(2600);
/// assert_eq!("2600", value.to_string());
///
/// let value = Scalar::new_num(2600.0);
/// assert_eq!("2600", value.to_string());
/// ```
///
/// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_AttributeValue.html#DDB-Type-AttributeValue-N
/// [`Value::new_num`]: crate::value::Value::new_num
pub fn new_num<N>(value: N) -> Self
where
N: ToString + num::Num,
{
Self::Num(Num::new(value))
}
/// Use when you need a [numeric][1] value for DynamoDB in exponent form
/// (with a lowercase `e`).
///
/// See also: [`Scalar::new_num`], [`Scalar::new_num_upper_exp`],
/// [`Value::new_num_lower_exp`], [`Num`]
///
/// # Examples
///
/// ```
/// use dynamodb_expression::Scalar;
/// # use pretty_assertions::assert_eq;
///
/// let value = Scalar::new_num_lower_exp(2600);
/// assert_eq!("2.6e3", value.to_string());
///
/// let value = Scalar::new_num_lower_exp(2600.0);
/// assert_eq!("2.6e3", value.to_string());
/// ```
///
/// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_AttributeValue.html#DDB-Type-AttributeValue-N
/// [`Value::new_num_lower_exp`]: crate::value::Value::new_num_lower_exp
pub fn new_num_lower_exp<N>(value: N) -> Self
where
N: LowerExp + num::Num,
{
Self::Num(Num::new_lower_exp(value))
}
/// Use when you need a [numeric][1] value for DynamoDB in exponent form
/// (with an uppercase `e`).
///
/// See also: [`Scalar::new_num`], [`Scalar::new_num_lower_exp`],
/// [`Value::new_num_upper_exp`], [`Num`]
///
/// # Examples
///
/// ```
/// use dynamodb_expression::Scalar;
/// # use pretty_assertions::assert_eq;
///
/// let value = Scalar::new_num_upper_exp(2600);
/// assert_eq!("2.6E3", value.to_string());
///
/// let value = Scalar::new_num_upper_exp(2600.0);
/// assert_eq!("2.6E3", value.to_string());
/// ```
///
/// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_AttributeValue.html#DDB-Type-AttributeValue-N
/// [`Value::new_num_upper_exp`]: crate::value::Value::new_num_upper_exp
pub fn new_num_upper_exp<N>(value: N) -> Self
where
N: UpperExp + num::Num,
{
Self::Num(Num::new_upper_exp(value))
}
/// Use when you need a [boolean][1] value for DynamoDB.
///
/// See also: [`Value::new_bool`]
///
/// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_AttributeValue.html#DDB-Type-AttributeValue-BOOL
/// [`Value::new_bool`]: crate::value::Value::new_bool
pub fn new_bool(b: bool) -> Self {
Self::Bool(b)
}
/// Use when you need a [binary][1] value for DynamoDB.
///
/// See also: [`Value::new_binary`]
///
/// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_AttributeValue.html#DDB-Type-AttributeValue-B
/// [`Value::new_binary`]: crate::value::Value::new_binary
pub fn new_binary<B>(binary: B) -> Self
where
B: Into<Vec<u8>>,
{
Self::Binary(binary.into())
}
/// Use when you need a [null][1] value for DynamoDB.
///
/// See also: [`Value::new_null`]
///
/// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_AttributeValue.html#DDB-Type-AttributeValue-NULL
/// [`Value::new_null`]: crate::value::Value::new_null
pub fn new_null() -> Self {
Self::Null
}
// Intentionally not using `impl From<Scalar> for AttributeValue` because
// I don't want to make this a public API people rely on. The purpose of this
// crate is not to make creating `AttributeValues` easier. They should try
// `serde_dynamo`.
pub(super) fn into_attribute_value(self) -> AttributeValue {
match self {
Scalar::String(s) => AttributeValue::S(s),
Scalar::Num(n) => n.into_attribute_value(),
Scalar::Bool(b) => AttributeValue::Bool(b),
Scalar::Binary(b) => AttributeValue::B(Blob::new(b)),
Scalar::Null => AttributeValue::Null(true),
}
}
}
impl fmt::Display for Scalar {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::String(s) => serde_json::to_string(s).unwrap().fmt(f),
Self::Num(n) => n.fmt(f),
Self::Bool(b) => serde_json::Value::Bool(*b).to_string().fmt(f),
Self::Binary(b) => serde_json::Value::String(base64(b)).to_string().fmt(f),
// TODO: I'm pretty sure this isn't right.
// https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_AttributeValue.html#DDB-Type-AttributeValue-NULL
Self::Null => f.write_str("NULL"),
}
}
}
impl From<String> for Scalar {
fn from(value: String) -> Self {
Self::String(value)
}
}
impl From<&String> for Scalar {
fn from(value: &String) -> Self {
Self::String(value.to_owned())
}
}
impl From<&str> for Scalar {
fn from(value: &str) -> Self {
Self::String(value.to_owned())
}
}
impl From<&&str> for Scalar {
fn from(value: &&str) -> Self {
Self::String((*value).to_owned())
}
}
impl From<Num> for Scalar {
fn from(value: Num) -> Self {
Self::Num(value)
}
}
impl From<bool> for Scalar {
fn from(value: bool) -> Self {
Self::Bool(value)
}
}
impl From<Vec<u8>> for Scalar {
fn from(value: Vec<u8>) -> Self {
Self::Binary(value)
}
}
impl<const N: usize> From<[u8; N]> for Scalar {
fn from(value: [u8; N]) -> Self {
Self::Binary(value.into())
}
}
impl From<()> for Scalar {
fn from(_: ()) -> Self {
Self::Null
}
}
impl FromIterator<u8> for Scalar {
fn from_iter<T>(iter: T) -> Self
where
T: IntoIterator<Item = u8>,
{
Self::Binary(iter.into_iter().collect())
}
}
#[cfg(test)]
mod test {
use pretty_assertions::assert_str_eq;
use super::Scalar;
#[test]
fn string() {
let actual = Scalar::new_string("fish");
assert_str_eq!("\"fish\"", actual.to_string());
}
#[test]
fn numeric() {
let actual = Scalar::new_num(42);
assert_str_eq!("42", actual.to_string());
}
#[test]
fn boolean() {
assert_str_eq!("true", Scalar::new_bool(true).to_string());
assert_str_eq!("false", Scalar::new_bool(false).to_string());
}
#[test]
fn binary_vec() {
let bytes: Vec<u8> = b"fish".to_vec();
let actual = Scalar::new_binary(bytes);
assert_str_eq!(r#""ZmlzaA==""#, actual.to_string());
}
#[test]
fn binary_array() {
let bytes: [u8; 4] = [b'f', b'i', b's', b'h'];
let actual = Scalar::new_binary(bytes);
assert_str_eq!(r#""ZmlzaA==""#, actual.to_string());
}
#[test]
fn binary_slice() {
let bytes: &[u8] = &b"fish"[..];
let actual = Scalar::new_binary(bytes);
assert_str_eq!(r#""ZmlzaA==""#, actual.to_string());
}
#[test]
fn null() {
let actual = Scalar::new_null();
assert_str_eq!("NULL", actual.to_string());
}
}