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

Plain text transformer #177

Merged
merged 3 commits into from
Jun 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### 🚀 Added
- Add the plain text transformer [#177](https://github.com/datanymizer/datanymizer/pull/177)
([@evgeniy-r](https://github.com/evgeniy-r))
- Add bcrypt filter to generate hashes [#164](https://github.com/datanymizer/datanymizer/pull/164)
([@akirill0v](https://github.com/akirill0v))
- Add wildcards support in the filter section [#151](https://github.com/datanymizer/datanymizer/pull/151)
Expand Down
3 changes: 1 addition & 2 deletions cli/pg_datanymizer/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ use url::Url;

use crate::options::{Options, TransactionConfig};

use datanymizer_dumper::indicator::Indicator;
use datanymizer_dumper::{
indicator::{ConsoleIndicator, SilentIndicator},
indicator::{ConsoleIndicator, Indicator, SilentIndicator},
postgres::{connector::Connector, dumper::PgDumper, IsolationLevel},
Dumper,
};
Expand Down
3 changes: 1 addition & 2 deletions datanymizer_engine/src/transformer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ use std::{
collections::HashMap,
error,
fmt::{self, Display, Formatter},
result,
sync::{Arc, RwLock},
};

use crate::{settings::TemplatesCollection, LocaleConfig};

pub type TransformResult = result::Result<Option<String>, TransformError>;
pub type TransformResult = Result<Option<String>, TransformError>;
pub type Globals = HashMap<String, Value>;
type TemplateStore = Arc<RwLock<HashMap<String, tera::Value>>>;

Expand Down
4 changes: 4 additions & 0 deletions datanymizer_engine/src/transformers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ pub use datetime::RandomDateTimeTransformer;
mod token;
pub use token::{Base64TokenTransformer, Base64UrlTokenTransformer, HexTokenTransformer};

mod plain;
pub use plain::PlainTransformer;

mod fk;
pub use fk::sql_value::AsSqlValue;
pub use fk::*;
Expand Down Expand Up @@ -78,6 +81,7 @@ define_transformers_enum![
("random_num", RandomNum, RandomNumberTransformer),
("password", Password, PasswordTransformer),
("datetime", DateTime, RandomDateTimeTransformer),
("plain", Plain, PlainTransformer),

("hex_token", HexToken, HexTokenTransformer),
("base64_token", Base64Token, Base64TokenTransformer),
Expand Down
40 changes: 40 additions & 0 deletions datanymizer_engine/src/transformers/plain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use crate::{transformer::TransformContext, TransformResult, Transformer};
use serde::{Deserialize, Serialize};

/// Just outputs a fixed string (plain text).
///
/// # Example:
///
/// #...
/// rules:
/// field_name:
/// plain: "some text"
/// ```
#[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Debug)]
pub struct PlainTransformer(String);

impl Transformer for PlainTransformer {
fn transform(
&self,
_field_name: &str,
_field_value: &str,
_ctx: &Option<TransformContext>,
) -> TransformResult {
Ok(Some(self.0.clone()))
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::Transformers;

#[test]
fn parse_and_transform() {
let cfg = "plain: 'some text'";
let transformer: Transformers = serde_yaml::from_str(cfg).unwrap();
let result = transformer.transform("field", "", &None).unwrap().unwrap();

assert_eq!(result, "some text");
}
}
10 changes: 10 additions & 0 deletions docs/transformers.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ These are due to the fact that we removed the dependency on the [chrono](https:/
the [time](https://crates.io/crates/time) crate directly (because of
[security issue](https://github.com/chronotope/chrono/pull/578) in `chrono`).

#### plain

Generates a fixed text (a plain text).

Example:

```yaml
plain: "some text"
```

#### random_num

Gets a random number.
Expand Down