Skip to main content

to_attribute_value

Function to_attribute_value 

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

Converts a serde::Serialize value into a DynamoDB AttributeValue using serde_dynamo.

This is a convenience wrapper around try_to_attribute_value that panics on failure. Use it when you are confident the serialization cannot fail (e.g. for well-known types like &[&str] or simple structs).

Prefer try_to_attribute_value in contexts where you want to propagate errors rather than panic.

§Panics

Panics if serde_dynamo::to_attribute_value returns an error. This should be rare in practice — it can happen for types that serialize to formats unsupported by DynamoDB (e.g. maps with non-string keys).

§Examples

Updating the platform_config field of a MainPlatformConfig item via Update::set:

use serde::{Serialize, Deserialize};
use dynamodb_facade::{
    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 — to_attribute_value bridges it
// to an AttributeValue for use in Update::set.
MainPlatformConfig::update_by_id(
    client,
    KeyId::NONE,
    Update::set("platform_config", to_attribute_value(&new_config)),
)
.await?;