diff --git a/lib/vector-config-macros/src/ast/container.rs b/lib/vector-config-macros/src/ast/container.rs index 78096f722828c..f4a47e71d18e7 100644 --- a/lib/vector-config-macros/src/ast/container.rs +++ b/lib/vector-config-macros/src/ast/container.rs @@ -236,7 +236,7 @@ impl<'a> Container<'a> { let original = input; let name = serde.attrs.name().deserialize_name(); - let default_value = get_serde_default_value(serde.attrs.default()); + let default_value = get_serde_default_value(&serde.ident, serde.attrs.default()); let container = Container { original, diff --git a/lib/vector-config-macros/src/ast/field.rs b/lib/vector-config-macros/src/ast/field.rs index bd0d86343bda5..790bf07b7dadd 100644 --- a/lib/vector-config-macros/src/ast/field.rs +++ b/lib/vector-config-macros/src/ast/field.rs @@ -33,7 +33,7 @@ impl<'a> Field<'a> { let original = serde.original; let name = serde.attrs.name().deserialize_name(); - let default_value = get_serde_default_value(serde.attrs.default()); + let default_value = get_serde_default_value(&serde.ty, serde.attrs.default()); Attributes::from_attributes(&original.attrs) .and_then(|attrs| { diff --git a/lib/vector-config-macros/src/ast/util.rs b/lib/vector-config-macros/src/ast/util.rs index b7276e7189691..30f66bd6a406a 100644 --- a/lib/vector-config-macros/src/ast/util.rs +++ b/lib/vector-config-macros/src/ast/util.rs @@ -1,4 +1,5 @@ use darling::error::Accumulator; +use quote::{quote, ToTokens}; use serde_derive_internals::{attr as serde_attr, Ctxt}; use syn::{spanned::Spanned, Attribute, ExprPath, Lit, Meta, MetaNameValue, NestedMeta}; @@ -137,11 +138,6 @@ fn none_if_empty(s: String) -> Option { } } -pub fn get_default_exprpath() -> ExprPath { - syn::parse_str("::std::default::Default::default") - .expect("expression path for default should never be invalid") -} - pub fn err_field_missing_description(field: &T) -> darling::Error { darling::Error::custom(ERR_FIELD_MISSING_DESCRIPTION).with_span(field) } @@ -150,10 +146,19 @@ pub fn err_field_implicit_transparent(field: &T) -> darling::Error { darling::Error::custom(ERR_FIELD_IMPLICIT_TRANSPARENT).with_span(field) } -pub fn get_serde_default_value(default: &serde_attr::Default) -> Option { +pub fn get_serde_default_value( + source: &S, + default: &serde_attr::Default, +) -> Option { match default { serde_attr::Default::None => None, - serde_attr::Default::Default => Some(get_default_exprpath()), + serde_attr::Default::Default => { + let qualified_path = syn::parse2(quote! { + <#source as ::std::default::Default>::default + }) + .expect("should not fail to parse qualified default path"); + Some(qualified_path) + } serde_attr::Default::Path(path) => Some(path.clone()), } } diff --git a/lib/vector-config/src/metadata.rs b/lib/vector-config/src/metadata.rs index 68fa535b36332..3bdda996c6fa5 100644 --- a/lib/vector-config/src/metadata.rs +++ b/lib/vector-config/src/metadata.rs @@ -2,17 +2,19 @@ use std::fmt; use vector_config_common::{attributes::CustomAttribute, validation}; +use crate::ToValue; + /// The metadata associated with a given type or field. -#[derive(Clone)] pub struct Metadata { title: Option<&'static str>, description: Option<&'static str>, - default_value: Option, + default_value: Option>, custom_attributes: Vec, deprecated: bool, deprecated_message: Option<&'static str>, transparent: bool, validations: Vec, + _dummy: std::marker::PhantomData, } impl Metadata { @@ -54,23 +56,12 @@ impl Metadata { self.description = None; } - pub fn default_value(&self) -> Option<&T> { - self.default_value.as_ref() - } - - pub fn with_default_value(default: T) -> Self { - Self { - default_value: Some(default), - ..Default::default() - } - } - - pub fn set_default_value(&mut self, default_value: T) { - self.default_value = Some(default_value); + pub fn default_value(&self) -> Option<&dyn ToValue> { + self.default_value.as_deref() } - pub fn consume_default_value(&mut self) -> Option { - self.default_value.take() + pub fn set_default_value(&mut self, default_value: impl ToValue + 'static) { + self.default_value = Some(Box::new(default_value)); } pub fn deprecated(&self) -> bool { @@ -133,6 +124,7 @@ impl Metadata { deprecated_message: other.deprecated_message.or(self.deprecated_message), transparent: other.transparent, validations: self.validations, + _dummy: Default::default(), } } @@ -149,6 +141,7 @@ impl Metadata { deprecated_message: self.deprecated_message, transparent: self.transparent, validations: self.validations.clone(), + _dummy: Default::default(), } } } @@ -164,6 +157,7 @@ impl Default for Metadata { deprecated_message: None, transparent: false, validations: Vec::new(), + _dummy: Default::default(), } } } diff --git a/src/config/mod.rs b/src/config/mod.rs index 8277731e7b0d0..559f143d17a5a 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -336,7 +336,7 @@ impl Display for Resource { #[configurable_component] #[derive(Clone, Debug)] #[serde(deny_unknown_fields)] -pub struct TestDefinition { +pub struct TestDefinition { /// The name of the unit test. pub name: String, diff --git a/src/sinks/util/buffer/compression.rs b/src/sinks/util/buffer/compression.rs index 8129bd0748e02..4c025df89c4f8 100644 --- a/src/sinks/util/buffer/compression.rs +++ b/src/sinks/util/buffer/compression.rs @@ -263,7 +263,7 @@ impl Configurable for Compression { gzip_string_subschema, zlib_string_subschema, ]); - apply_metadata(&mut all_string_oneof_subschema, string_metadata.clone()); + apply_metadata(&mut all_string_oneof_subschema, string_metadata); // Next we'll create a full schema for the given algorithms. //