Skip to content

Commit a799f54

Browse files
msrd0Empty2k12
authored andcommitted
Allow writing Option<T> (#37)
1 parent da95fa0 commit a799f54

File tree

2 files changed

+82
-5
lines changed

2 files changed

+82
-5
lines changed

src/query/write_query.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@ use std::fmt::{Display, Formatter};
88

99
// todo: batch write queries
1010

11+
pub trait WriteField {
12+
fn add_to_fields(self, tag: String, fields: &mut Vec<(String, String)>);
13+
}
14+
15+
impl<T: Into<Type>> WriteField for T {
16+
fn add_to_fields(self, tag: String, fields: &mut Vec<(String, String)>) {
17+
let val: Type = self.into();
18+
fields.push((tag, val.to_string()));
19+
}
20+
}
21+
22+
impl<T: Into<Type>> WriteField for Option<T> {
23+
fn add_to_fields(self, tag: String, fields: &mut Vec<(String, String)>) {
24+
if let Some(val) = self {
25+
val.add_to_fields(tag, fields);
26+
}
27+
}
28+
}
29+
1130
/// Internal Representation of a Write query that has not yet been built
1231
pub struct WriteQuery {
1332
fields: Vec<(String, String)>,
@@ -39,13 +58,12 @@ impl WriteQuery {
3958
///
4059
/// Query::write_query(Timestamp::NOW, "measurement").add_field("field1", 5).build();
4160
/// ```
42-
pub fn add_field<S, I>(mut self, tag: S, value: I) -> Self
61+
pub fn add_field<S, F>(mut self, tag: S, value: F) -> Self
4362
where
4463
S: Into<String>,
45-
I: Into<Type>,
64+
F: WriteField,
4665
{
47-
let val: Type = value.into();
48-
self.fields.push((tag.into(), val.to_string()));
66+
value.add_to_fields(tag.into(), &mut self.fields);
4967
self
5068
}
5169

@@ -206,6 +224,17 @@ mod tests {
206224
);
207225
}
208226

227+
#[test]
228+
fn test_write_builder_optional_fields() {
229+
let query = Query::write_query(Timestamp::HOURS(11), "weather")
230+
.add_field("temperature", Some(82u64))
231+
.add_field("wind_strength", <Option<u64>>::None)
232+
.build();
233+
234+
assert!(query.is_ok(), "Query was empty");
235+
assert_eq!(query.unwrap(), "weather temperature=82 11");
236+
}
237+
209238
#[test]
210239
fn test_write_builder_only_tags() {
211240
let query = Query::write_query(Timestamp::HOURS(11), "weather")

tests/integration_tests.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,55 @@ fn test_write_and_read_field() {
273273
#[cfg(feature = "use-serde")]
274274
/// INTEGRATION TEST
275275
///
276-
/// This test case tests whether JSON can be decoded from a InfluxDB response and wether that JSON
276+
/// This integration tests that writing data and retrieving the data again is working
277+
fn test_write_and_read_option() {
278+
use serde::Deserialize;
279+
let test_name = "test_write_and_read_option";
280+
create_db(test_name).expect("could not setup db");
281+
let _run_on_drop = RunOnDrop {
282+
closure: Box::new(|| {
283+
delete_db("test_write_and_read_option").expect("could not clean up db");
284+
}),
285+
};
286+
287+
let client = create_client(test_name);
288+
// Todo: Convert this to derive based insert for easier comparison of structs
289+
let write_query = Query::write_query(Timestamp::HOURS(11), "weather")
290+
.add_field("temperature", 82)
291+
.add_field("wind_strength", <Option<u64>>::None);
292+
let write_result = get_runtime().block_on(client.query(&write_query));
293+
assert_result_ok(&write_result);
294+
295+
#[derive(Deserialize, Debug, PartialEq)]
296+
struct Weather {
297+
time: String,
298+
temperature: i32,
299+
wind_strength: Option<u64>,
300+
}
301+
302+
let query = Query::raw_read_query("SELECT time, temperature, wind_strength FROM weather");
303+
let future = client
304+
.json_query(query)
305+
.and_then(|mut db_result| db_result.deserialize_next::<Weather>());
306+
let result = get_runtime().block_on(future);
307+
assert_result_ok(&result);
308+
309+
assert_eq!(
310+
result.unwrap().series[0].values[0],
311+
Weather {
312+
time: "1970-01-01T11:00:00Z".to_string(),
313+
temperature: 82,
314+
wind_strength: None,
315+
}
316+
);
317+
delete_db(test_name).expect("could not clean up db");
318+
}
319+
320+
#[test]
321+
#[cfg(feature = "use-serde")]
322+
/// INTEGRATION TEST
323+
///
324+
/// This test case tests whether JSON can be decoded from a InfluxDB response and whether that JSON
277325
/// is equal to the data which was written to the database
278326
fn test_json_query() {
279327
use serde::Deserialize;

0 commit comments

Comments
 (0)