Procedural macro for google-cloud-spanner.
[dependencies]
google-cloud-spanner-derive = <version>
#[derive(Table)]
generates the implementation for following traits.
* TryFromStruct
* ToStruct
* TryFrom<Row>
```rust use chrono::{DateTime, Utc}; use googlecloudspanner::client::Client; use googlecloudspanner::mutation::insertstruct; use googlecloudspanner::reader::AsyncIterator; use googlecloudspanner::statement::Statement; use googlecloudspannerderive::Table;
pub struct UserCharacter {
pub userid: String,
pub characterid: i64,
// #[spanner(name=...) is used when the column name does not appear in camel case of the field name
#[spanner(name="LevelX")]
pub level: i64,
#[spanner(commitTimestamp)]
pub updated_at: DateTime
async fn run(client: &Client) -> Result
let mut tx = client.read_only_transaction().await?;
let stmt = Statement::new("SELECT * From UserCharacter Limit 10");
let mut reader = tx.query(stmt).await?;
let mut result = vec![];
while let Some(row) = reader.next().await? {
result.push(row.try_into()?);
}
Ok(result)
} ```
Here is the generated implementation. ```rust impl ToStruct for UserCharacter { fn tokinds(&self) -> Kinds { vec![ ("UserId", self.userid.tokind()), ("CharacterId", self.characterid.tokind()), ("LevelX", self.level.tokind()), ("UpdatedAt", self.updatedat.tokind()), ] }
fn get_types() -> Types {
vec![
("UserId", String::get_type()),
("CharacterId", i64::get_type()),
("LevelX", i64::get_type()),
("UpdatedAt", CommitTimestamp::get_type()),
]
}
}
impl TryFromStruct for UserCharacter {
fn tryfromstruct(s: Struct<'>) -> Result
impl TryFrom
#[derive(Query)]
generates the implementation for following traits.
* TryFrom<Row>
```rust use chrono::{DateTime, Utc}; use googlecloudspanner::transaction::Transaction; use googlecloudspanner::reader::AsyncIterator; use googlecloudspanner::statement::Statement; use googlecloudspanner_derive::{Table, Query};
pub struct UserCharacter { pub userid: String, pub characterid: i64, }
pub struct UserItem { pub userid: String, pub itemid: i64, }
pub struct UserBundle {
pub userid: String,
pub usercharacters: Vec
async fn run(userid: &str, tx: &mut Transaction) -> Resultparam("UserID", &userid); let mut reader = tx.query(stmt).await?; match reader.next().await? { Some(row) => Ok(row.tryinto()?), None => Ok(None) } } ```