Skip to content

Commit

Permalink
chore: Shorten name of skip_serializing_if_default (vectordotdev#19591
Browse files Browse the repository at this point in the history
)

The actual code in the function merely compares the value to its type default,
so use a shorter name that reflects its actual function. This results in a serde
condition of `skip_serializing_if = "is_default"` which is a more natural
reading.  This ends up allowing `cargo fmt` to wrap more lines together,
dropping 100 LOC.
  • Loading branch information
bruceg authored Jan 11, 2024
1 parent 2448a72 commit 9dd9907
Show file tree
Hide file tree
Showing 70 changed files with 107 additions and 210 deletions.
2 changes: 1 addition & 1 deletion docs/tutorials/sinks/1_basic_sink.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub struct BasicConfig {
#[serde(
default,
deserialize_with = "crate::serde::bool_or_struct",
skip_serializing_if = "crate::serde::skip_serializing_if_default"
skip_serializing_if = "crate::serde::is_default"
)]
pub acknowledgements: AcknowledgementsConfig,
}
Expand Down
7 changes: 2 additions & 5 deletions lib/codecs/src/decoding/format/gelf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ use crate::{gelf_fields::*, VALID_FIELD_REGEX};
#[derive(Debug, Clone, Default)]
pub struct GelfDeserializerConfig {
/// GELF-specific decoding options.
#[serde(
default,
skip_serializing_if = "vector_core::serde::skip_serializing_if_default"
)]
#[serde(default, skip_serializing_if = "vector_core::serde::is_default")]
pub gelf: GelfDeserializerOptions,
}

Expand Down Expand Up @@ -90,7 +87,7 @@ pub struct GelfDeserializerOptions {
/// [U+FFFD]: https://en.wikipedia.org/wiki/Specials_(Unicode_block)#Replacement_character
#[serde(
default = "default_lossy",
skip_serializing_if = "vector_core::serde::skip_serializing_if_default"
skip_serializing_if = "vector_core::serde::is_default"
)]
#[derivative(Default(value = "default_lossy()"))]
pub lossy: bool,
Expand Down
7 changes: 2 additions & 5 deletions lib/codecs/src/decoding/format/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ use super::{default_lossy, Deserializer};
#[derive(Debug, Clone, Default)]
pub struct JsonDeserializerConfig {
/// JSON-specific decoding options.
#[serde(
default,
skip_serializing_if = "vector_core::serde::skip_serializing_if_default"
)]
#[serde(default, skip_serializing_if = "vector_core::serde::is_default")]
pub json: JsonDeserializerOptions,
}

Expand Down Expand Up @@ -77,7 +74,7 @@ pub struct JsonDeserializerOptions {
/// [U+FFFD]: https://en.wikipedia.org/wiki/Specials_(Unicode_block)#Replacement_character
#[serde(
default = "default_lossy",
skip_serializing_if = "vector_core::serde::skip_serializing_if_default"
skip_serializing_if = "vector_core::serde::is_default"
)]
#[derivative(Default(value = "default_lossy()"))]
pub lossy: bool,
Expand Down
7 changes: 2 additions & 5 deletions lib/codecs/src/decoding/format/native_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ use vector_core::config::LogNamespace;
#[derive(Debug, Clone, Default)]
pub struct NativeJsonDeserializerConfig {
/// Vector's native JSON-specific decoding options.
#[serde(
default,
skip_serializing_if = "vector_core::serde::skip_serializing_if_default"
)]
#[serde(default, skip_serializing_if = "vector_core::serde::is_default")]
pub native_json: NativeJsonDeserializerOptions,
}

Expand Down Expand Up @@ -67,7 +64,7 @@ pub struct NativeJsonDeserializerOptions {
/// [U+FFFD]: https://en.wikipedia.org/wiki/Specials_(Unicode_block)#Replacement_character
#[serde(
default = "default_lossy",
skip_serializing_if = "vector_core::serde::skip_serializing_if_default"
skip_serializing_if = "vector_core::serde::is_default"
)]
#[derivative(Default(value = "default_lossy()"))]
pub lossy: bool,
Expand Down
5 changes: 1 addition & 4 deletions lib/codecs/src/decoding/format/protobuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ use super::Deserializer;
#[derive(Debug, Clone, Default)]
pub struct ProtobufDeserializerConfig {
/// Protobuf-specific decoding options.
#[serde(
default,
skip_serializing_if = "vector_core::serde::skip_serializing_if_default"
)]
#[serde(default, skip_serializing_if = "vector_core::serde::is_default")]
pub protobuf: ProtobufDeserializerOptions,
}

Expand Down
7 changes: 2 additions & 5 deletions lib/codecs/src/decoding/format/syslog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ pub struct SyslogDeserializerConfig {
source: Option<&'static str>,

/// Syslog-specific decoding options.
#[serde(
default,
skip_serializing_if = "vector_core::serde::skip_serializing_if_default"
)]
#[serde(default, skip_serializing_if = "vector_core::serde::is_default")]
pub syslog: SyslogDeserializerOptions,
}

Expand Down Expand Up @@ -251,7 +248,7 @@ pub struct SyslogDeserializerOptions {
/// [U+FFFD]: https://en.wikipedia.org/wiki/Specials_(Unicode_block)#Replacement_character
#[serde(
default = "default_lossy",
skip_serializing_if = "vector_core::serde::skip_serializing_if_default"
skip_serializing_if = "vector_core::serde::is_default"
)]
#[derivative(Default(value = "default_lossy()"))]
pub lossy: bool,
Expand Down
2 changes: 1 addition & 1 deletion lib/codecs/src/decoding/framing/character_delimited.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub struct CharacterDelimitedDecoderOptions {
/// If there is a risk of processing malformed data, such as logs with user-controlled input,
/// consider setting the maximum length to a reasonably large value as a safety net. This
/// ensures that processing is not actually unbounded.
#[serde(skip_serializing_if = "vector_core::serde::skip_serializing_if_default")]
#[serde(skip_serializing_if = "vector_core::serde::is_default")]
pub max_length: Option<usize>,
}

Expand Down
7 changes: 2 additions & 5 deletions lib/codecs/src/decoding/framing/newline_delimited.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ use super::{BoxedFramingError, CharacterDelimitedDecoder};
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct NewlineDelimitedDecoderConfig {
/// Options for the newline delimited decoder.
#[serde(
default,
skip_serializing_if = "vector_core::serde::skip_serializing_if_default"
)]
#[serde(default, skip_serializing_if = "vector_core::serde::is_default")]
pub newline_delimited: NewlineDelimitedDecoderOptions,
}

Expand All @@ -33,7 +30,7 @@ pub struct NewlineDelimitedDecoderOptions {
/// If there is a risk of processing malformed data, such as logs with user-controlled input,
/// consider setting the maximum length to a reasonably large value as a safety net. This
/// ensures that processing is not actually unbounded.
#[serde(skip_serializing_if = "vector_core::serde::skip_serializing_if_default")]
#[serde(skip_serializing_if = "vector_core::serde::is_default")]
pub max_length: Option<usize>,
}

Expand Down
7 changes: 2 additions & 5 deletions lib/codecs/src/decoding/framing/octet_counting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ use super::BoxedFramingError;
#[configurable_component]
#[derive(Debug, Clone, Default)]
pub struct OctetCountingDecoderConfig {
#[serde(
default,
skip_serializing_if = "vector_core::serde::skip_serializing_if_default"
)]
#[serde(default, skip_serializing_if = "vector_core::serde::is_default")]
/// Options for the octet counting decoder.
pub octet_counting: OctetCountingDecoderOptions,
}
Expand All @@ -37,7 +34,7 @@ impl OctetCountingDecoderConfig {
#[derivative(Default)]
pub struct OctetCountingDecoderOptions {
/// The maximum length of the byte buffer.
#[serde(skip_serializing_if = "vector_core::serde::skip_serializing_if_default")]
#[serde(skip_serializing_if = "vector_core::serde::is_default")]
pub max_length: Option<usize>,
}

Expand Down
13 changes: 5 additions & 8 deletions lib/codecs/src/encoding/format/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub struct CsvSerializerOptions {
#[serde(
default = "default_delimiter",
with = "vector_core::serde::ascii_char",
skip_serializing_if = "vector_core::serde::skip_serializing_if_default"
skip_serializing_if = "vector_core::serde::is_default"
)]
pub delimiter: u8,

Expand All @@ -88,7 +88,7 @@ pub struct CsvSerializerOptions {
/// field data are escaped instead of doubled.
#[serde(
default = "default_double_quote",
skip_serializing_if = "vector_core::serde::skip_serializing_if_default"
skip_serializing_if = "vector_core::serde::is_default"
)]
pub double_quote: bool,

Expand All @@ -101,23 +101,20 @@ pub struct CsvSerializerOptions {
#[serde(
default = "default_escape",
with = "vector_core::serde::ascii_char",
skip_serializing_if = "vector_core::serde::skip_serializing_if_default"
skip_serializing_if = "vector_core::serde::is_default"
)]
pub escape: u8,

/// The quote character to use when writing CSV.
#[serde(
default = "default_escape",
with = "vector_core::serde::ascii_char",
skip_serializing_if = "vector_core::serde::skip_serializing_if_default"
skip_serializing_if = "vector_core::serde::is_default"
)]
quote: u8,

/// The quoting style to use when writing CSV data.
#[serde(
default,
skip_serializing_if = "vector_core::serde::skip_serializing_if_default"
)]
#[serde(default, skip_serializing_if = "vector_core::serde::is_default")]
pub quote_style: QuoteStyle,

/// Set the capacity (in bytes) of the internal buffer used in the CSV writer.
Expand Down
5 changes: 1 addition & 4 deletions lib/codecs/src/encoding/format/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ pub struct JsonSerializerConfig {
///
/// When set to `single`, only the last non-bare value of tags are displayed with the
/// metric. When set to `full`, all metric tags are exposed as separate assignments.
#[serde(
default,
skip_serializing_if = "vector_core::serde::skip_serializing_if_default"
)]
#[serde(default, skip_serializing_if = "vector_core::serde::is_default")]
pub metric_tag_values: MetricTagValues,
}

Expand Down
5 changes: 1 addition & 4 deletions lib/codecs/src/encoding/format/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ pub struct TextSerializerConfig {
///
/// When set to `single`, only the last non-bare value of tags are displayed with the
/// metric. When set to `full`, all metric tags are exposed as separate assignments.
#[serde(
default,
skip_serializing_if = "vector_core::serde::skip_serializing_if_default"
)]
#[serde(default, skip_serializing_if = "vector_core::serde::is_default")]
pub metric_tag_values: MetricTagValues,
}

Expand Down
32 changes: 7 additions & 25 deletions lib/vector-core/src/config/global_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,14 @@ pub struct GlobalOptions {
///
/// This is used if a component does not have its own specific log schema. All events use a log
/// schema, whether or not the default is used, to assign event fields on incoming events.
#[serde(
default,
skip_serializing_if = "crate::serde::skip_serializing_if_default"
)]
#[serde(default, skip_serializing_if = "crate::serde::is_default")]
pub log_schema: LogSchema,

/// Telemetry options.
///
/// Determines whether `source` and `service` tags should be emitted with the
/// `component_sent_*` and `component_received_*` events.
#[serde(
default,
skip_serializing_if = "crate::serde::skip_serializing_if_default"
)]
#[serde(default, skip_serializing_if = "crate::serde::is_default")]
pub telemetry: Telemetry,

/// The name of the time zone to apply to timestamp conversions that do not contain an explicit time zone.
Expand All @@ -72,17 +66,11 @@ pub struct GlobalOptions {
/// local time.
///
/// [tzdb]: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
#[serde(
default,
skip_serializing_if = "crate::serde::skip_serializing_if_default"
)]
#[serde(default, skip_serializing_if = "crate::serde::is_default")]
pub timezone: Option<TimeZone>,

#[configurable(derived)]
#[serde(
default,
skip_serializing_if = "crate::serde::skip_serializing_if_default"
)]
#[serde(default, skip_serializing_if = "crate::serde::is_default")]
pub proxy: ProxyConfig,

/// Controls how acknowledgements are handled for all sinks by default.
Expand All @@ -94,7 +82,7 @@ pub struct GlobalOptions {
#[serde(
default,
deserialize_with = "bool_or_struct",
skip_serializing_if = "crate::serde::skip_serializing_if_default"
skip_serializing_if = "crate::serde::is_default"
)]
pub acknowledgements: AcknowledgementsConfig,

Expand All @@ -107,10 +95,7 @@ pub struct GlobalOptions {
/// captured, but not so long that they continue to build up indefinitely, as this will consume
/// a small amount of memory for each metric.
#[configurable(deprecated)]
#[serde(
default,
skip_serializing_if = "crate::serde::skip_serializing_if_default"
)]
#[serde(default, skip_serializing_if = "crate::serde::is_default")]
pub expire_metrics: Option<Duration>,

/// The amount of time, in seconds, that internal metrics will persist after having not been
Expand All @@ -121,10 +106,7 @@ pub struct GlobalOptions {
/// setting this to a value that ensures that metrics live long enough to be emitted and
/// captured, but not so long that they continue to build up indefinitely, as this will consume
/// a small amount of memory for each metric.
#[serde(
default,
skip_serializing_if = "crate::serde::skip_serializing_if_default"
)]
#[serde(default, skip_serializing_if = "crate::serde::is_default")]
pub expire_metrics_secs: Option<f64>,
}

Expand Down
10 changes: 5 additions & 5 deletions lib/vector-core/src/config/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use no_proxy::NoProxy;
use url::Url;
use vector_config::configurable_component;

use crate::serde::skip_serializing_if_default;
use crate::serde::is_default;

// suggestion of standardization coming from https://about.gitlab.com/blog/2021/01/27/we-need-to-talk-no-proxy/
fn from_env(key: &str) -> Option<String> {
Expand Down Expand Up @@ -54,7 +54,7 @@ pub struct ProxyConfig {
/// Enables proxying support.
#[serde(
default = "ProxyConfig::default_enabled",
skip_serializing_if = "skip_serializing_if_default"
skip_serializing_if = "is_default"
)]
pub enabled: bool,

Expand All @@ -63,14 +63,14 @@ pub struct ProxyConfig {
/// Must be a valid URI string.
#[configurable(validation(format = "uri"))]
#[configurable(metadata(docs::examples = "http://foo.bar:3128"))]
#[serde(default, skip_serializing_if = "skip_serializing_if_default")]
#[serde(default, skip_serializing_if = "is_default")]
pub http: Option<String>,

/// Proxy endpoint to use when proxying HTTPS traffic.
///
/// Must be a valid URI string.
#[configurable(validation(format = "uri"))]
#[serde(default, skip_serializing_if = "skip_serializing_if_default")]
#[serde(default, skip_serializing_if = "is_default")]
#[configurable(metadata(docs::examples = "http://foo.bar:3128"))]
pub https: Option<String>,

Expand All @@ -87,7 +87,7 @@ pub struct ProxyConfig {
/// | Splat | `*` matches all hosts |
///
/// [cidr]: https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing
#[serde(default, skip_serializing_if = "skip_serializing_if_default")]
#[serde(default, skip_serializing_if = "is_default")]
#[configurable(metadata(docs::examples = "localhost"))]
#[configurable(metadata(docs::examples = ".foo.bar"))]
#[configurable(metadata(docs::examples = "*"))]
Expand Down
5 changes: 2 additions & 3 deletions lib/vector-core/src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use std::{fmt, marker::PhantomData};

use serde::{de, Deserialize, Deserializer};

/// Answers "Is it possible to skip serializing this value, because it's the
/// default?"
/// Answers "Is this value in it's default state?" which can be used to skip serializing the value.
#[inline]
pub fn skip_serializing_if_default<E: Default + PartialEq>(e: &E) -> bool {
pub fn is_default<E: Default + PartialEq>(e: &E) -> bool {
e == &E::default()
}

Expand Down
8 changes: 4 additions & 4 deletions src/codecs/encoding/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ use vector_lib::schema::meaning;
use vrl::path::OwnedValuePath;
use vrl::value::Value;

use crate::{event::Event, serde::skip_serializing_if_default};
use crate::{event::Event, serde::is_default};

/// Transformations to prepare an event for serialization.
#[configurable_component(no_deser)]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Transformer {
/// List of fields that are included in the encoded event.
#[serde(default, skip_serializing_if = "skip_serializing_if_default")]
#[serde(default, skip_serializing_if = "is_default")]
only_fields: Option<Vec<ConfigValuePath>>,

/// List of fields that are excluded from the encoded event.
#[serde(default, skip_serializing_if = "skip_serializing_if_default")]
#[serde(default, skip_serializing_if = "is_default")]
except_fields: Option<Vec<ConfigValuePath>>,

/// Format used for timestamp fields.
#[serde(default, skip_serializing_if = "skip_serializing_if_default")]
#[serde(default, skip_serializing_if = "is_default")]
timestamp_format: Option<TimestampFormat>,
}

Expand Down
5 changes: 1 addition & 4 deletions src/config/enterprise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,7 @@ pub struct Options {
pub max_retries: u32,

#[configurable(derived)]
#[serde(
default,
skip_serializing_if = "crate::serde::skip_serializing_if_default"
)]
#[serde(default, skip_serializing_if = "crate::serde::is_default")]
proxy: ProxyConfig,

/// A map of additional tags for metrics sent to Observability Pipelines.
Expand Down
Loading

0 comments on commit 9dd9907

Please sign in to comment.