Expand description
§Derive macros for mapping between Value and Rust structs
Converting structures from/to hash maps manually is tedious. This module defines a few derive macros that can generate the conversion code automatically for you.
§Converting a custom Rust struct to a Value
use stargate_grpc::Value;
use stargate_grpc_derive::IntoValue;
#[derive(IntoValue)]
struct User {
id: i64,
login: &'static str
}
let user = User { id: 1, login: "user" };
let value = Value::from(user);
assert_eq!(value, Value::udt(vec![("id", Value::bigint(1)), ("login", Value::string("user"))]))§Converting a Value to a custom Rust struct
use stargate_grpc::Value;
use stargate_grpc_derive::TryFromValue;
#[derive(TryFromValue)]
struct User {
id: i64,
login: String
}
let value = Value::udt(vec![("id", Value::bigint(1)), ("login", Value::string("user"))]);
let user: User = value.try_into().unwrap();
assert_eq!(user.id, 1);
assert_eq!(user.login, "user".to_string());§Using custom structs as arguments in queries
It is possible to unpack struct fields in such a way that each field value
gets bound to a named argument of a query. For that to work, the struct must implement
std::convert::Into<Values> trait. You can derive such trait automatically:
use stargate_grpc::Query;
use stargate_grpc_derive::IntoValues;
#[derive(IntoValues)]
struct User {
id: i64,
login: &'static str
}
let user = User { id: 1, login: "user" };
let query = Query::builder()
.query("INSERT INTO users(id, login) VALUES (:id, :login)")
.bind(user) // bind user.id to :id and user.login to :login
.build();§Converting result set rows to custom struct values
You can convert a Row to a value of your custom type by deriving
TryFromRow and then passing the rows to a mapper:
use stargate_grpc::*;
use stargate_grpc_derive::*;
#[derive(Debug, TryFromRow)]
struct User {
id: i64,
login: String,
}
let result_set: ResultSet = unimplemented!(); // replace with actual code to run a query
let mapper = result_set.mapper().unwrap();
for row in result_set.rows {
let user: User = mapper.try_unpack(row).unwrap();
println!("{:?}", user)
}
§Options
All macros defined in this module accept a #[stargate] attribute that you can set
on struct fields to control the details of how the conversion should be made.
§#[stargate(skip)]
Skips the field when doing the conversion to Value. This is useful when the structure
needs to store some data that are not mapped to the database schema.
However, the field is included in the conversion from Value, and the conversion would fail
if it was missing, hence you likely need to set #[stargate(default)] as well.
§#[stargate(default)]
Uses the default value for the field type provided by std::default::Default,
if the source Value doesn’t contain the field, or if the field is set to Value::null
or Value::unset.
§#[stargate(default = "expression")]
Obtains the default value by evaluating given Rust expression given as a string.
use stargate_grpc_derive::TryFromValue;
fn default_file_name() -> String {
"file.txt".to_string()
}
#[derive(TryFromValue)]
struct File {
#[stargate(default = "default_file_name()")]
path: String,
}§#[stargate(cql_type = "type")]
Sets the target CQL type the field should be converted into, useful when there are multiple possibilities.
use stargate_grpc::types;
use stargate_grpc_derive::IntoValue;
#[derive(IntoValue)]
struct InetAndUuid {
#[stargate(cql_type = "types::Inet")]
inet: [u8; 16],
#[stargate(cql_type = "types::Uuid")]
uuid: [u8; 16],
}§#[stargate(name = "column")]
Sets the CQL field, column or query argument name associated with the field. If not given, it is assumed to be the same as struct field name.
Derive Macros§
- Into
Value - Derives the
IntoValueandDefaultCqlTypeimplementations for a struct. - Into
Values - Derives the
IntoValuesimpl that allows to use struct inQueryBuilder::bind - TryFrom
Row - Derives the
TryFromRowimplementation for a struct. - TryFrom
Value - Derives the
TryFromValueimplementation for a struct.