Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion influxdb/tests/derive_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ struct WeatherReading {
wind_strength: Option<u64>,
}

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "derive", derive(InfluxDbWriteable))]
#[cfg_attr(feature = "use-serde", derive(Deserialize))]
struct WeatherReadingIgnoredField {
time: DateTime<Utc>,
humidity: i32,
#[tag]
wind_strength: Option<u64>,
#[ignored]
temperature: u64,
}

#[test]
fn test_build_query() {
let weather_reading = WeatherReading {
Expand Down Expand Up @@ -80,10 +92,11 @@ async fn test_write_and_read_option() {
|| async move {
create_db(TEST_NAME).await.expect("could not setup db");
let client = create_client(TEST_NAME);
let weather_reading = WeatherReading {
let weather_reading = WeatherReadingIgnoredField {
time: Timestamp::Hours(11).into(),
humidity: 30,
wind_strength: None,
temperature: 11,
};
let write_result = client
.query(&weather_reading.into_query("weather_reading".to_string()))
Expand Down
2 changes: 1 addition & 1 deletion influxdb_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn krate() -> TokenStream2 {
quote!(::influxdb)
}

#[proc_macro_derive(InfluxDbWriteable, attributes(tag))]
#[proc_macro_derive(InfluxDbWriteable, attributes(tag, ignored))]
pub fn derive_writeable(tokens: TokenStream) -> TokenStream {
expand_writeable(tokens)
}
18 changes: 17 additions & 1 deletion influxdb_derive/src/writeable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@ use syn::{parse_macro_input, Field, Fields, Ident, ItemStruct};
struct WriteableField {
ident: Ident,
is_tag: bool,
is_ignored: bool,
}

impl From<Field> for WriteableField {
fn from(field: Field) -> WriteableField {
let ident = field.ident.expect("fields without ident are not supported");
let is_ignored = field.attrs.iter().any(|attr| {
attr.path
.segments
.iter()
.last()
.map(|seg| seg.ident.to_string())
.unwrap_or_default()
== "ignored"
});
let is_tag = field.attrs.iter().any(|attr| {
attr.path
.segments
Expand All @@ -20,7 +30,12 @@ impl From<Field> for WriteableField {
.unwrap_or_default()
== "tag"
});
WriteableField { ident, is_tag }

WriteableField {
ident,
is_tag,
is_ignored,
}
}
}

Expand All @@ -38,6 +53,7 @@ pub fn expand_writeable(tokens: TokenStream) -> TokenStream {
.named
.into_iter()
.map(WriteableField::from)
.filter(|field| !field.is_ignored)
.filter(|field| field.ident.to_string() != time_field.to_string())
.map(|field| {
let ident = field.ident;
Expand Down