Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5/n] [test-utils] turn redactor into the builder pattern #6786

Conversation

sunshowers
Copy link
Contributor

In an upcoming PR I want to redact everything except UUIDs. The most convenient
way to do that is to use the builder pattern for the redactor.

Created using spr 1.3.6-beta.1
Created using spr 1.3.6-beta.1

[skip ci]
Created using spr 1.3.6-beta.1
@sunshowers sunshowers changed the base branch from main to sunshowers/spr/main.5n-test-utils-turn-redactor-into-the-builder-pattern October 6, 2024 00:22
Comment on lines 221 to 222
let mut redactor = Redactor::new();
redactor.extra(redactions);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have the context for where else you'll be using the redactor in your future PRs but it feels like having the builder methods take self instead of &mut self would be more ergonomic, at least in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah so an issue is that we have places where we do conditional redactions, and setting redactor = redactor.xyz() feels a bit weird to me. In general whenever there are conditional things I tend to prefer &mut self builders. But 🤷🏽

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I feel like neither is ever the "right" answer. (The one unfortunate thing about the builder pattern.)

Comment on lines 482 to 485
redactor.map_or_else(
|| r.to_string(),
|redactions| redact_extra(r, redactions),
|redactor| redactor.do_redact(r),
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something that comes to mind as I read this is that I think we can simplify usage quite a bit in this file if Option<Redactor> could be replaced with a Redactor that does nothing by default. I think #[derive(Default)] on Redactor would do the right thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's reasonable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I think the default should be that redactions are enabled, so I added a manual Default impl as well as a noop constructor.

test-utils/src/dev/test_cmds.rs Outdated Show resolved Hide resolved
Comment on lines +152 to +155
pub fn do_redact(&self, input: &str) -> String {
// Perform extra redactions at the beginning, not the end. This is because
// some of the built-in redactions in redact_variable might match a
// substring of something that should be handled by extra_redactions (e.g.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[bikeshed 2/2]:

Suggested change
pub fn do_redact(&self, input: &str) -> String {
// Perform extra redactions at the beginning, not the end. This is because
// some of the built-in redactions in redact_variable might match a
// substring of something that should be handled by extra_redactions (e.g.
pub fn redact(&self, input: &str) -> String {
// Perform extra redactions at the beginning, not the end. This is because
// some of the built-in redactions in redact_variable might match a
// substring of something that should be handled by extra_redactions (e.g.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had redact at first, but especially now that the config knobs are shorter (e.g. uuids, basic) I wanted to draw a distinction between the knobs and the actual process of redaction.

test-utils/src/dev/test_cmds.rs Outdated Show resolved Hide resolved
Created using spr 1.3.6-beta.1

[skip ci]
Created using spr 1.3.6-beta.1
@sunshowers sunshowers changed the base branch from sunshowers/spr/main.5n-test-utils-turn-redactor-into-the-builder-pattern to main October 9, 2024 02:05
Created using spr 1.3.6-beta.1
@sunshowers sunshowers requested a review from iliana October 9, 2024 04:43
@sunshowers
Copy link
Contributor Author

Thanks iliana!

@sunshowers sunshowers merged commit beaba46 into main Oct 9, 2024
16 checks passed
@sunshowers sunshowers deleted the sunshowers/spr/5n-test-utils-turn-redactor-into-the-builder-pattern branch October 9, 2024 04:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants