influxdb-rs/influxdb-rust

Ignore a field

blasrodri opened this issue · 5 comments

Given the following struct:

 #[derive(InfluxDbWriteable)]
pub struct SomeData {
    #[tag]
    pub user_id: String,
    pub important: u64
   #[ignore_field]
    pub not_important: ShadyTypeNotWriteable
}

I'd like to be able to avoid having to implement InfluxDbWriteable on ShadyTypeNotWriteable. Is this possible? If not, I'd like to implement it. I wouldn't mind getting some pointers.

Hello. This is certainly possible.

The macro is implemented here. In similar fashion to is_tag, you'd add a new is_ignored, and then skip generation the input for this field, probably in the filter method call here.

Let me know if you have any more questions.

Hi @Empty2k12

Following your suggestion:

diff --git a/influxdb/tests/derive_integration_tests.rs b/influxdb/tests/derive_integration_tests.rs
index 9c136b2..2ed055f 100644
--- a/influxdb/tests/derive_integration_tests.rs
+++ b/influxdb/tests/derive_integration_tests.rs
@@ -20,6 +20,8 @@ struct WeatherReading {
     humidity: i32,
     #[tag]
     wind_strength: Option<u64>,
+    #[ignored]
+    temperature: u64,
 }
 
 #[test]
diff --git a/influxdb_derive/src/writeable.rs b/influxdb_derive/src/writeable.rs
index f42c333..ee563bb 100644
--- a/influxdb_derive/src/writeable.rs
+++ b/influxdb_derive/src/writeable.rs
@@ -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
@@ -20,7 +30,12 @@ impl From<Field> for WriteableField {
                 .unwrap_or_default()
                 == "tag"
         });
-        WriteableField { ident, is_tag }
+
+        WriteableField {
+            ident,
+            is_tag,
+            is_ignored,
+        }
     }
 }
 
@@ -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;

I get

❯ cargo test --features="derive,use-serde"
   Compiling influxdb v0.3.0 (/Users/bruger/projects/foss/influxdb-rust/influxdb)
error: cannot find attribute `ignored` in this scope
  --> influxdb/tests/derive_integration_tests.rs:23:7
   |
23 |     #[ignored]
   |       ^^^^^^^ help: a built-in attribute with a similar name exists: `ignore`

error: aborting due to previous error

error: could not compile `influxdb`

To learn more, run the command again with --verbose.

Could you point me to what I'm doing wrong? Thanks :)

Could you try running the command CI is using to test the crate? cargo test --package influxdb --package influxdb_derive --all-features --no-fail-fast

Sure:

error: cannot find attribute `ignored` in this scope
  --> influxdb/tests/derive_integration_tests.rs:23:7
   |
23 |     #[ignored]
   |       ^^^^^^^ help: a built-in attribute with a similar name exists: `ignore`

error: aborting due to previous error

error: could not compile `influxdb`

To learn more, run the command again with --verbose.
warning: build failed, waiting for other jobs to finish...
error: build failed
msrd0 commented

All attributes used in a derive macro need to be registered: https://github.com/Empty2k12/influxdb-rust/blob/master/influxdb_derive/src/lib.rs#L12