Skip to content

Commit 60074a1

Browse files
authored
feat: Trim release and environment (#184)
* feat: Trim release and environment * fix: Linting
1 parent d59164f commit 60074a1

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

general/derive/src/lib.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@ struct FieldAttrs {
677677
field_name: String,
678678
required: Option<bool>,
679679
nonempty: Option<bool>,
680+
trim_whitespace: Option<bool>,
680681
pii: Option<bool>,
681682
retain: bool,
682683
match_regex: Option<String>,
@@ -711,6 +712,14 @@ impl FieldAttrs {
711712
quote!(false)
712713
};
713714

715+
let trim_whitespace = if let Some(trim_whitespace) = self.trim_whitespace {
716+
quote!(#trim_whitespace)
717+
} else if let Some(ref parent_attrs) = inherit_from_field_attrs {
718+
quote!(#parent_attrs.trim_whitespace)
719+
} else {
720+
quote!(false)
721+
};
722+
714723
let pii = if let Some(pii) = self.pii {
715724
quote!(#pii)
716725
} else if let Some(ref parent_attrs) = inherit_from_field_attrs {
@@ -753,6 +762,7 @@ impl FieldAttrs {
753762
name: Some(#field_name),
754763
required: #required,
755764
nonempty: #nonempty,
765+
trim_whitespace: #trim_whitespace,
756766
match_regex: #match_regex,
757767
max_chars: #max_chars,
758768
bag_size: #bag_size,
@@ -872,7 +882,18 @@ fn parse_field_attributes(
872882
other => panic!("Unknown value {}", other),
873883
},
874884
_ => {
875-
panic!("Got non string literal for required");
885+
panic!("Got non string literal for nonempty");
886+
}
887+
}
888+
} else if ident == "trim_whitespace" {
889+
match lit {
890+
Lit::Str(litstr) => match litstr.value().as_str() {
891+
"true" => rv.trim_whitespace = Some(true),
892+
"false" => rv.trim_whitespace = Some(false),
893+
other => panic!("Unknown value {}", other),
894+
},
895+
_ => {
896+
panic!("Got non string literal for trim_whitespace");
876897
}
877898
}
878899
} else if ident == "match_regex" {
@@ -943,6 +964,8 @@ fn parse_field_attributes(
943964
panic!("Got non string literal for legacy_alias");
944965
}
945966
}
967+
} else {
968+
panic!("Unknown argument to metastructure: {}", ident);
946969
}
947970
}
948971
other => {

general/src/processor/attrs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ pub struct FieldAttrs {
330330
pub required: bool,
331331
/// If the field should be non-empty.
332332
pub nonempty: bool,
333+
/// Whether to trim whitespace from this string.
334+
pub trim_whitespace: bool,
333335
/// A regex to validate the (string) value against.
334336
pub match_regex: Option<Regex>,
335337
/// The maximum char length of this field.
@@ -347,6 +349,7 @@ lazy_static::lazy_static! {
347349
name: None,
348350
required: false,
349351
nonempty: false,
352+
trim_whitespace: false,
350353
match_regex: None,
351354
max_chars: None,
352355
bag_size: None,
@@ -360,6 +363,7 @@ lazy_static::lazy_static! {
360363
name: None,
361364
required: false,
362365
nonempty: false,
366+
trim_whitespace: false,
363367
match_regex: None,
364368
max_chars: None,
365369
bag_size: None,
@@ -373,6 +377,7 @@ lazy_static::lazy_static! {
373377
name: None,
374378
required: false,
375379
nonempty: false,
380+
trim_whitespace: false,
376381
match_regex: None,
377382
max_chars: None,
378383
bag_size: None,

general/src/protocol/event.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ pub struct Event {
241241
max_chars = "tag_value", // release ends in tag
242242
match_regex = r"^[^\r\n]*\z",
243243
required = "false",
244+
trim_whitespace = "true",
244245
nonempty = "true",
245246
skip_serialization = "empty"
246247
)]
@@ -257,7 +258,11 @@ pub struct Event {
257258
pub dist: Annotated<String>,
258259

259260
/// Environment the environment was generated in ("production" or "development").
260-
#[metastructure(max_chars = "environment", match_regex = r"^[^\r\n\x0C/]+$")]
261+
#[metastructure(
262+
max_chars = "environment",
263+
match_regex = r"^[^\r\n\x0C/]+$",
264+
trim_whitespace = "true"
265+
)]
261266
pub environment: Annotated<String>,
262267

263268
/// Deprecated in favor of tags.

general/src/store/schema.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ impl Processor for SchemaProcessor {
1010
meta: &mut Meta,
1111
state: &ProcessingState<'_>,
1212
) -> ValueAction {
13-
verify_value_nonempty(value, meta, &state)
13+
value_trim_whitespace(value, meta, &state)
14+
.and_then(|| verify_value_nonempty(value, meta, &state))
1415
.and_then(|| verify_value_pattern(value, meta, &state))
1516
}
1617

@@ -54,6 +55,20 @@ impl Processor for SchemaProcessor {
5455
}
5556
}
5657

58+
fn value_trim_whitespace(
59+
value: &mut String,
60+
_meta: &mut Meta,
61+
state: &ProcessingState<'_>,
62+
) -> ValueAction {
63+
if state.attrs().trim_whitespace {
64+
let new_value = value.trim().to_owned();
65+
value.clear();
66+
value.push_str(&new_value);
67+
}
68+
69+
ValueAction::Keep
70+
}
71+
5772
fn verify_value_nonempty<T>(
5873
value: &mut T,
5974
meta: &mut Meta,
@@ -133,7 +148,7 @@ mod tests {
133148
}
134149

135150
#[test]
136-
fn test_release_newlines() {
151+
fn test_release_invalid_newlines() {
137152
use crate::protocol::Event;
138153

139154
let mut event = Annotated::new(Event {
@@ -265,4 +280,22 @@ mod tests {
265280

266281
assert_eq_dbg!(stack, expected);
267282
}
283+
284+
#[test]
285+
fn test_newlines_release() {
286+
use crate::protocol::Event;
287+
let mut event = Annotated::new(Event {
288+
release: Annotated::new("42\n".to_string().into()),
289+
..Default::default()
290+
});
291+
292+
process_value(&mut event, &mut SchemaProcessor, ProcessingState::root());
293+
294+
let expected = Annotated::new(Event {
295+
release: Annotated::new("42".to_string().into()),
296+
..Default::default()
297+
});
298+
299+
assert_eq_dbg!(expected, event);
300+
}
268301
}

0 commit comments

Comments
 (0)