improve derive macro by not making it so reliant on how the type is written (with or without :: in an option)

This commit is contained in:
OMGeeky
2023-06-04 11:28:20 +02:00
parent a6b301fd32
commit d92139785e
4 changed files with 24 additions and 8 deletions

View File

@@ -1,6 +1,6 @@
[package]
name = "google_bigquery_v2"
version = "0.2.4"
version = "0.2.5"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -9,7 +9,7 @@ edition = "2021"
google-bigquery2 = "5.0.2"
serde_json = "1.0.95"
tokio = "1.0.2"
google_bigquery_v2_derive = { version = "0.0.1", path = "./google_bigquery_v2_derive" }
google_bigquery_v2_derive = { version = "0.0.2", path = "./google_bigquery_v2_derive" }
chrono = "0.4.24"
nameof = "1.2.2"
env_logger = "0.10.0"

View File

@@ -217,9 +217,8 @@ fn implement_set_field_value(ast: &DeriveInput) -> TokenStream {
fn write_set_field_value(f: Field) -> TokenStream {
let field_ident = f.field_ident;
let local_name = f.local_name;
let field_type = f.ty;
quote::quote! {
#local_name => self.#field_ident = #field_type::from_param(value)?,
#local_name => self.#field_ident = Self::from_param(value)?,
}
}
let fields = get_fields_without_client(&ast.data);
@@ -263,10 +262,9 @@ fn implement_get_field_value(ast: &DeriveInput) -> TokenStream {
fn implement_from_query_result_row(ast: &DeriveInput) -> TokenStream {
fn set_field_value(f: Field) -> TokenStream {
let field_ident = f.field_ident;
let field_type = f.ty;
let db_name = f.db_name;
quote::quote! {
#field_ident: #field_type::from_param(&row[#db_name])?,
#field_ident: Self::from_param(&row[#db_name])?,
}
}
let client_ident = get_client_field(&ast.data).field_ident;

View File

@@ -8,7 +8,9 @@ pub use google_bigquery2::api::{QueryParameterType, QueryParameterValue};
use serde_json::Value;
use crate::client::BigqueryClient;
use crate::data::param_conversion::{convert_value_to_string, BigDataValueType};
use crate::data::param_conversion::{
convert_value_to_string, BigDataValueType, ConvertBigQueryParams,
};
use crate::data::query_builder::{
NoClient, NoStartingData, QueryBuilder, QueryResultType, QueryTypeDelete, QueryTypeInsert,
QueryTypeNoType, QueryTypeSelect, QueryTypeUpdate, QueryWasNotBuilt,
@@ -248,6 +250,23 @@ pub trait BigQueryTable: BigQueryTableBase {
}
Ok(())
}
/// converts the value to Result<T> by using T::from_param(value)
///
/// this is basically just a wrapper so we don't need to know the type
/// of the field in the derive macro (or at least we don't need to write it)
/// like this:
///
/// self.info1 = Option::<String>::from_param(value)?
///
/// instead we can just call:
///
/// self.info1 = Self::from_param(value)?;
///
/// and the types will be inferred
fn from_param<T: ConvertBigQueryParams>(value: &Value) -> Result<T> {
T::from_param(value)
}
}
impl<T> BigQueryTable for T where T: BigQueryTableBase {}

View File

@@ -1,4 +1,3 @@
use crate::prelude::*;
use nameof::name_of;
use google_bigquery_v2::data::query_builder::QueryResultType;