fix: Use proto::PartitionTemplate's serde_json support directly

Rather than defining new types and implementing serialization on them
pull/24376/head
Carol (Nichols || Goulding) 2023-07-26 13:02:44 -04:00
parent 1ddc64d68d
commit 14815435b8
No known key found for this signature in database
GPG Key ID: E907EE5A736F87D4
2 changed files with 19 additions and 56 deletions

View File

@ -1,6 +1,4 @@
use generated_types::influxdata::iox::partition_template::v1 as proto;
use influxdb_iox_client::table::generated_types::PartitionTemplate;
use serde::{Deserialize, Serialize};
use snafu::{ResultExt, Snafu};
#[allow(clippy::enum_variant_names)]
@ -27,13 +25,13 @@ pub struct PartitionTemplateConfig {
long = "partition-template",
short = 'p',
default_value = None,
value_parser = parse_part_template,
value_parser = parse_partition_template,
)]
pub parts: Option<PartTemplate>,
pub partition_template: Option<proto::PartitionTemplate>,
}
fn parse_part_template(s: &str) -> Result<PartTemplate, Error> {
let part_template: PartTemplate =
fn parse_partition_template(s: &str) -> Result<proto::PartitionTemplate, Error> {
let part_template: proto::PartitionTemplate =
serde_json::from_str(s).context(InvalidPartitionTemplateSnafu)?;
// Error if empty parts
@ -44,46 +42,6 @@ fn parse_part_template(s: &str) -> Result<PartTemplate, Error> {
Ok(part_template)
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct PartTemplate {
pub parts: Vec<Part>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Part {
TagValue(String),
TimeFormat(String),
}
impl PartitionTemplateConfig {
pub fn partition_template(&self) -> Option<PartitionTemplate> {
self.parts.as_ref()?;
let part_template = self.parts.as_ref().unwrap().clone();
let template_parts = part_template
.parts
.into_iter()
.map(|part| match part {
Part::TagValue(tag_value) => proto::TemplatePart {
part: Some(proto::template_part::Part::TagValue(tag_value.clone())),
},
Part::TimeFormat(time_format) => proto::TemplatePart {
part: Some(proto::template_part::Part::TimeFormat(time_format.clone())),
},
})
.collect::<Vec<_>>();
if template_parts.is_empty() {
return None;
}
Some(PartitionTemplate {
parts: template_parts,
})
}
}
#[cfg(test)]
mod tests {
use clap::Parser;
@ -179,7 +137,7 @@ mod tests {
])
.unwrap();
let part_template = actual.partition_template().unwrap();
let part_template = actual.partition_template.unwrap();
assert_eq!(part_template.parts.len(), 1);
assert_eq!(
part_template.parts[0].part,
@ -198,7 +156,7 @@ mod tests {
])
.unwrap();
let part_template = actual.partition_template().unwrap();
let part_template = actual.partition_template.unwrap();
assert_eq!(part_template.parts.len(), 1);
assert_eq!(
part_template.parts[0].part,
@ -217,7 +175,7 @@ mod tests {
])
.unwrap();
let part_template = actual.partition_template().unwrap();
let part_template = actual.partition_template.unwrap();
assert_eq!(part_template.parts.len(), 3);
assert_eq!(
part_template.parts[0].part,
@ -248,7 +206,7 @@ mod tests {
])
.unwrap();
let part_template = actual.partition_template().unwrap();
let part_template = actual.partition_template.unwrap();
assert_eq!(part_template.parts.len(), 3);
assert_eq!(
part_template.parts[0].part,

View File

@ -14,22 +14,24 @@ pub struct Config {
/// Partition template
#[clap(flatten)]
partition_template: PartitionTemplateConfig,
partition_template_config: PartitionTemplateConfig,
}
pub async fn command(connection: Connection, config: Config) -> Result<()> {
let Config {
database,
table,
partition_template,
partition_template_config,
} = config;
let partition_template = partition_template.partition_template();
let mut client = influxdb_iox_client::table::Client::new(connection);
let table = client
.create_table(&database, &table, partition_template)
.create_table(
&database,
&table,
partition_template_config.partition_template,
)
.await?;
println!("{}", serde_json::to_string_pretty(&table)?);
@ -80,6 +82,9 @@ mod tests {
assert_eq!(config.database, "database");
assert_eq!(config.table, "table");
assert_eq!(config.partition_template.partition_template(), expected);
assert_eq!(
config.partition_template_config.partition_template,
expected
);
}
}