Skip to content

Commit

Permalink
chore(config): Store default value in a box (#16487)
Browse files Browse the repository at this point in the history
* Drop unused functions

* Add dummy phantom data to hold T

* Drop the `Clone` derive on `struct Metadata`

* Make Metadata store a Boxed default value

* generate a qualified expression path for Default::default

---------

Co-authored-by: Toby Lawrence <toby@nuclearfurnace.com>
  • Loading branch information
bruceg and tobz authored Feb 17, 2023
1 parent faa96f8 commit 59ab0da
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/vector-config-macros/src/ast/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion lib/vector-config-macros/src/ast/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
19 changes: 12 additions & 7 deletions lib/vector-config-macros/src/ast/util.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -137,11 +138,6 @@ fn none_if_empty(s: String) -> Option<String> {
}
}

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<T: Spanned>(field: &T) -> darling::Error {
darling::Error::custom(ERR_FIELD_MISSING_DESCRIPTION).with_span(field)
}
Expand All @@ -150,10 +146,19 @@ pub fn err_field_implicit_transparent<T: Spanned>(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<ExprPath> {
pub fn get_serde_default_value<S: ToTokens>(
source: &S,
default: &serde_attr::Default,
) -> Option<ExprPath> {
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()),
}
}
Expand Down
28 changes: 11 additions & 17 deletions lib/vector-config/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
title: Option<&'static str>,
description: Option<&'static str>,
default_value: Option<T>,
default_value: Option<Box<dyn ToValue>>,
custom_attributes: Vec<CustomAttribute>,
deprecated: bool,
deprecated_message: Option<&'static str>,
transparent: bool,
validations: Vec<validation::Validation>,
_dummy: std::marker::PhantomData<T>,
}

impl<T> Metadata<T> {
Expand Down Expand Up @@ -54,23 +56,12 @@ impl<T> Metadata<T> {
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<T> {
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 {
Expand Down Expand Up @@ -133,6 +124,7 @@ impl<T> Metadata<T> {
deprecated_message: other.deprecated_message.or(self.deprecated_message),
transparent: other.transparent,
validations: self.validations,
_dummy: Default::default(),
}
}

Expand All @@ -149,6 +141,7 @@ impl<T> Metadata<T> {
deprecated_message: self.deprecated_message,
transparent: self.transparent,
validations: self.validations.clone(),
_dummy: Default::default(),
}
}
}
Expand All @@ -164,6 +157,7 @@ impl<T> Default for Metadata<T> {
deprecated_message: None,
transparent: false,
validations: Vec::new(),
_dummy: Default::default(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ impl Display for Resource {
#[configurable_component]
#[derive(Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct TestDefinition<T = OutputId> {
pub struct TestDefinition<T: 'static = OutputId> {
/// The name of the unit test.
pub name: String,

Expand Down
2 changes: 1 addition & 1 deletion src/sinks/util/buffer/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down

0 comments on commit 59ab0da

Please sign in to comment.