Skip to content

Replace use of Any by an enum #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 9, 2019
Merged
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
86 changes: 41 additions & 45 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ use reqwest::{StatusCode, Url};
use std::mem;

use crate::error::InfluxDbError;
use crate::query::read_query::InfluxDbReadQuery;
use crate::query::write_query::InfluxDbWriteQuery;
use crate::query::InfluxDbQuery;

use std::any::Any;
use crate::query::{InfluxDbQuery, InfluxDbQueryTypes};

#[derive(Clone, Debug)]
/// Internal Authentication representation
Expand Down Expand Up @@ -184,9 +180,10 @@ impl InfluxDbClient {
/// a [`InfluxDbError`] variant will be returned.
///
/// [`InfluxDbError`]: enum.InfluxDbError.html
pub fn query<Q>(&self, q: &Q) -> Box<dyn Future<Item = String, Error = InfluxDbError>>
pub fn query<'q, Q>(&self, q: &'q Q) -> Box<dyn Future<Item = String, Error = InfluxDbError>>
where
Q: Any + InfluxDbQuery,
Q: InfluxDbQuery,
&'q Q: Into<InfluxDbQueryTypes<'q>>,
{
use futures::future;

Expand All @@ -200,49 +197,48 @@ impl InfluxDbClient {
Ok(query) => query,
};

let any_value = q as &dyn Any;
let basic_parameters: Vec<(String, String)> = self.into();

let client = if let Some(_) = any_value.downcast_ref::<InfluxDbReadQuery>() {
let read_query = query.get();
let client = match q.into() {
InfluxDbQueryTypes::Read(_) => {
let read_query = query.get();
let mut url = match Url::parse_with_params(
format!("{url}/query", url = self.database_url()).as_str(),
basic_parameters,
) {
Ok(url) => url,
Err(err) => {
let error = InfluxDbError::UrlConstructionError {
error: format!("{}", err),
};
return Box::new(future::err::<String, InfluxDbError>(error));
}
};
url.query_pairs_mut().append_pair("q", &read_query.clone());

let mut url = match Url::parse_with_params(
format!("{url}/query", url = self.database_url()).as_str(),
basic_parameters,
) {
Ok(url) => url,
Err(err) => {
let error = InfluxDbError::UrlConstructionError {
error: format!("{}", err),
};
return Box::new(future::err::<String, InfluxDbError>(error));
if read_query.contains("SELECT") || read_query.contains("SHOW") {
Client::new().get(url)
} else {
Client::new().post(url)
}
};
url.query_pairs_mut().append_pair("q", &read_query.clone());

if read_query.contains("SELECT") || read_query.contains("SHOW") {
Client::new().get(url)
} else {
Client::new().post(url)
}
} else if let Some(write_query) = any_value.downcast_ref::<InfluxDbWriteQuery>() {
let mut url = match Url::parse_with_params(
format!("{url}/write", url = self.database_url()).as_str(),
basic_parameters,
) {
Ok(url) => url,
Err(err) => {
let error = InfluxDbError::InvalidQueryError {
error: format!("{}", err),
};
return Box::new(future::err::<String, InfluxDbError>(error));
}
};
url.query_pairs_mut()
.append_pair("precision", &write_query.get_precision());
Client::new().post(url).body(query.get())
} else {
unreachable!()
InfluxDbQueryTypes::Write(write_query) => {
let mut url = match Url::parse_with_params(
format!("{url}/write", url = self.database_url()).as_str(),
basic_parameters,
) {
Ok(url) => url,
Err(err) => {
let error = InfluxDbError::InvalidQueryError {
error: format!("{}", err),
};
return Box::new(future::err::<String, InfluxDbError>(error));
}
};
url.query_pairs_mut()
.append_pair("precision", &write_query.get_precision());
Client::new().post(url).body(query.get())
}
};
Box::new(
client
Expand Down
20 changes: 19 additions & 1 deletion src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@ impl fmt::Display for Timestamp {
}
}

/// Internal enum used to represent either type of query.
pub enum InfluxDbQueryTypes<'a> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some rustdoc comments so it's clear what this is used for for users of this library.

Read(&'a InfluxDbReadQuery),
Write(&'a InfluxDbWriteQuery),
}

impl<'a> From<&'a InfluxDbReadQuery> for InfluxDbQueryTypes<'a> {
fn from(query: &'a InfluxDbReadQuery) -> Self {
Self::Read(query)
}
}

impl<'a> From<&'a InfluxDbWriteQuery> for InfluxDbQueryTypes<'a> {
fn from(query: &'a InfluxDbWriteQuery) -> Self {
Self::Write(query)
}
}

pub trait InfluxDbQuery {
/// Builds valid InfluxSQL which can be run against the Database.
/// In case no fields have been specified, it will return an error,
Expand All @@ -71,7 +89,7 @@ pub trait InfluxDbQuery {
fn get_type(&self) -> QueryType;
}

impl InfluxDbQuery {
impl dyn InfluxDbQuery {
/// Returns a [`InfluxDbWriteQuery`](crate::query::write_query::InfluxDbWriteQuery) builder.
///
/// # Examples
Expand Down