Skip to content

Commit

Permalink
feat: add support for YAML and JSON to the generate command
Browse files Browse the repository at this point in the history
  • Loading branch information
pront committed Aug 22, 2023
1 parent 61c0ae8 commit 5e7620d
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 103 deletions.
16 changes: 9 additions & 7 deletions src/config/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,10 @@ mod tests {
use serde_json::json;
use vector_config::component::{SinkDescription, SourceDescription, TransformDescription};

use crate::config::Format;
use crate::{
config::{cmd::serialize_to_json, vars, ConfigBuilder},
generate,
generate::{generate_example, TransformInputsStrategy},
};

Expand Down Expand Up @@ -325,13 +327,13 @@ mod tests {
.collect::<Vec<_>>()
.join(","),
);
generate_example(
false,
generate_config_str.as_ref(),
&None,
TransformInputsStrategy::All,
)
.expect("invalid config generated")
let opts = generate::Opts {
fragment: true,
expression: generate_config_str.to_string(),
file: None,
format: Format::Toml,
};
generate_example(&opts, TransformInputsStrategy::All).expect("invalid config generated")
}

proptest! {
Expand Down
25 changes: 25 additions & 0 deletions src/config/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![deny(missing_docs, missing_debug_implementations)]

use std::path::Path;
use std::str::FromStr;

use serde::de;

Expand All @@ -21,6 +22,19 @@ pub enum Format {
Yaml,
}

impl FromStr for Format {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"toml" => Ok(Format::Toml),
"json" => Ok(Format::Json),
"yaml" => Ok(Format::Yaml),
_ => Err(format!("Invalid format: {}", s)),
}
}
}

impl Format {
/// Obtain the format from the file path using extension as a hint.
pub fn from_path<T: AsRef<Path>>(path: T) -> Result<Self, T> {
Expand All @@ -47,6 +61,17 @@ where
}
}

pub fn serialize<T>(value: &T, format: Format) -> Result<String, String>

Check failure on line 64 in src/config/format.rs

View workflow job for this annotation

GitHub Actions / Checks

missing documentation for a function
where
T: serde::ser::Serialize,
{
match format {
Format::Toml => toml::to_string(value).map_err(|e| e.to_string()),
Format::Yaml => serde_yaml::to_string(value).map_err(|e| e.to_string()),
Format::Json => serde_json::to_string(value).map_err(|e| e.to_string()),
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading

0 comments on commit 5e7620d

Please sign in to comment.