Skip to main content

try_to_attribute_value

Function try_to_attribute_value 

Source
pub fn try_to_attribute_value<T: Serialize>(value: T) -> Result<AttributeValue>
Expand description

Converts a serde::Serialize value into a DynamoDB AttributeValue using serde_dynamo, returning a Result on failure.

Use this when you need to handle serialization errors gracefully. For infallible cases, to_attribute_value is more ergonomic.

§Errors

Returns Error::Serde if serde_dynamo cannot convert the value — for example, if the type serializes to a map with non-string keys, which DynamoDB does not support.

§Examples

Updating the platform_config field of a MainPlatformConfig item via Update::set, propagating any serialization error with ?:

use serde::{Serialize, Deserialize};
use dynamodb_facade::{
    try_to_attribute_value, DynamoDBItemOp, KeyId, Update,
    StringAttribute, dynamodb_item,
};

#[derive(Debug, Clone, Serialize, Deserialize)]
struct MainPlatformConfig {
    platform_config: PlatformConfig,
    main_since_ts: u64,
}

dynamodb_item! {
    #[table = PlatformTable]
    MainPlatformConfig {
        #[partition_key]
        PK { const VALUE: &'static str = "MAIN_PLATFORM_CONFIG"; }
        #[sort_key]
        SK { const VALUE: &'static str = "MAIN_PLATFORM_CONFIG"; }
    }
}

let new_config = PlatformConfig {
    max_enrollments: 50,
    maintenance_mode: false,
};

// PlatformConfig is a Serialize type — try_to_attribute_value bridges it
// to an AttributeValue for use in Update::set.
MainPlatformConfig::update_by_id(
    client,
    KeyId::NONE,
    Update::set("platform_config", try_to_attribute_value(&new_config)?),
)
.await?;