Skip to content

Commit e15aec7

Browse files
authored
feat: add support for YAML and JSON to the generate command (vectordotdev#18345)
* feat: add support for YAML and JSON to the generate command * tweaks and pretty print * delete comments * more tests
1 parent 2f458f6 commit e15aec7

File tree

3 files changed

+258
-132
lines changed

3 files changed

+258
-132
lines changed

src/config/cmd.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,10 @@ mod tests {
202202
use serde_json::json;
203203
use vector_config::component::{SinkDescription, SourceDescription, TransformDescription};
204204

205+
use crate::config::Format;
205206
use crate::{
206207
config::{cmd::serialize_to_json, vars, ConfigBuilder},
208+
generate,
207209
generate::{generate_example, TransformInputsStrategy},
208210
};
209211

@@ -325,13 +327,13 @@ mod tests {
325327
.collect::<Vec<_>>()
326328
.join(","),
327329
);
328-
generate_example(
329-
false,
330-
generate_config_str.as_ref(),
331-
&None,
332-
TransformInputsStrategy::All,
333-
)
334-
.expect("invalid config generated")
330+
let opts = generate::Opts {
331+
fragment: true,
332+
expression: generate_config_str.to_string(),
333+
file: None,
334+
format: Format::Toml,
335+
};
336+
generate_example(&opts, TransformInputsStrategy::All).expect("invalid config generated")
335337
}
336338

337339
proptest! {

src/config/format.rs

+36-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![deny(missing_docs, missing_debug_implementations)]
44

55
use std::path::Path;
6+
use std::str::FromStr;
67

78
use serde::de;
89

@@ -21,6 +22,19 @@ pub enum Format {
2122
Yaml,
2223
}
2324

25+
impl FromStr for Format {
26+
type Err = String;
27+
28+
fn from_str(s: &str) -> Result<Self, Self::Err> {
29+
match s.to_lowercase().as_str() {
30+
"toml" => Ok(Format::Toml),
31+
"yaml" => Ok(Format::Yaml),
32+
"json" => Ok(Format::Json),
33+
_ => Err(format!("Invalid format: {}", s)),
34+
}
35+
}
36+
}
37+
2438
impl Format {
2539
/// Obtain the format from the file path using extension as a hint.
2640
pub fn from_path<T: AsRef<Path>>(path: T) -> Result<Self, T> {
@@ -34,8 +48,6 @@ impl Format {
3448
}
3549

3650
/// Parse the string represented in the specified format.
37-
/// If the format is unknown - fallback to the default format and attempt
38-
/// parsing using that.
3951
pub fn deserialize<T>(content: &str, format: Format) -> Result<T, Vec<String>>
4052
where
4153
T: de::DeserializeOwned,
@@ -47,9 +59,31 @@ where
4759
}
4860
}
4961

62+
/// Serialize the specified `value` into a string.
63+
pub fn serialize<T>(value: &T, format: Format) -> Result<String, String>
64+
where
65+
T: serde::ser::Serialize,
66+
{
67+
match format {
68+
Format::Toml => toml::to_string(value).map_err(|e| e.to_string()),
69+
Format::Yaml => serde_yaml::to_string(value).map_err(|e| e.to_string()),
70+
Format::Json => serde_json::to_string_pretty(value).map_err(|e| e.to_string()),
71+
}
72+
}
73+
5074
#[cfg(test)]
5175
mod tests {
5276
use super::*;
77+
use proptest::prelude::*;
78+
79+
impl Arbitrary for Format {
80+
type Parameters = ();
81+
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
82+
prop_oneof![Just(Format::Toml), Just(Format::Json), Just(Format::Yaml),].boxed()
83+
}
84+
85+
type Strategy = BoxedStrategy<Self>;
86+
}
5387

5488
/// This test ensures the logic to guess file format from the file path
5589
/// works correctly.

0 commit comments

Comments
 (0)