Repository: datanymizer/datanymizer (language: rust)

Issue:
Plain text transformer
A transformer with some fixed content (a template transformer can solve this, but a special one will be simpler and faster).


Discussion / hints:

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 96b23c0..cc58e89 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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)
diff --git a/cli/pg_datanymizer/src/app.rs b/cli/pg_datanymizer/src/app.rs
index 4db2efb..dbc6590 100644
--- a/cli/pg_datanymizer/src/app.rs
+++ b/cli/pg_datanymizer/src/app.rs
@@ -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,
 };
diff --git a/datanymizer_engine/src/transformer/mod.rs b/datanymizer_engine/src/transformer/mod.rs
index 37bdd79..ce9d19c 100644
--- a/datanymizer_engine/src/transformer/mod.rs
+++ b/datanymizer_engine/src/transformer/mod.rs
@@ -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>>>;
 
diff --git a/datanymizer_engine/src/transformers/mod.rs b/datanymizer_engine/src/transformers/mod.rs
index b690b51..7385bc7 100644
--- a/datanymizer_engine/src/transformers/mod.rs
+++ b/datanymizer_engine/src/transformers/mod.rs
@@ -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::*;
@@ -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),
diff --git a/datanymizer_engine/src/transformers/plain.rs b/datanymizer_engine/src/transformers/plain.rs
new file mode 100644
index 0000000..c285e92
--- /dev/null
+++ b/datanymizer_engine/src/transformers/plain.rs
@@ -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");
+    }
+}
diff --git a/docs/transformers.md b/docs/transformers.md
index a1aab64..fbaaca3 100644
--- a/docs/transformers.md
+++ b/docs/transformers.md
@@ -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.


Test patch: