Skip to content

Commit

Permalink
Merge pull request #38 from bhagenbourger/feature/constant_provider
Browse files Browse the repository at this point in the history
Add string constant provider
  • Loading branch information
vianneybacoup committed Apr 14, 2024
2 parents 3209d5d + 486e2ff commit b9c5460
Show file tree
Hide file tree
Showing 12 changed files with 884 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ csv = "1.3.0"
env_logger = "0.10"
fastrand = "2.0.1"
fastrand-contrib = "0.1.0"
linked-hash-map = "0.5.6"
log = "0.4"
once_cell = "1.8"
parquet = "50.0.0"
Expand Down
36 changes: 36 additions & 0 deletions docs/columns/providers/constant.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Constant provider
-------

### string
#### unique value
```yaml
- name: constant_as_string
provider: Constant.string
data: trout
```

#### list of values
```yaml
- name: list_of_constants_as_string
provider: Constant.string
data: [trout, salmon, carp]
```

#### list of weighted values
```yaml
- name: list_of_weighted_constants_as_string
provider: Constant.string
data:
- value: trout
- value: salmon
weight: 8
- value: carp
```

Data value can be unique value, a list of values or a dictionnary.
Integer, float or string can be specify into the configuration but the result will be stored as a string.
If a unique value is specified, all lines will have this value.
If a list of values is specified, value will randomly assigned for each line.
If a weighted list of values is specified, value will weighted randomly assigned for each line: for example is useful to generate data skewing.

[Options](../options.md) are also possible.
48 changes: 48 additions & 0 deletions src/providers/constant/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use yaml_rust::Yaml;

use crate::{errors::FakeLakeError, providers::provider::Provider};

use super::string;

pub fn get_corresponding_provider(
mut provider_split: std::str::Split<'_, char>,
column: &Yaml,
) -> Result<Box<dyn Provider>, FakeLakeError> {
match provider_split.next() {
Some("string") => Ok(string::new_from_yaml(column)),
_ => Err(FakeLakeError::BadYAMLFormat("".to_string())),
}
}

#[cfg(test)]
mod tests {
use super::get_corresponding_provider;

use yaml_rust::YamlLoader;

#[test]
fn given_string_should_return_provider() {
let provider_name = "string";
let yaml_str = format!("name: is_suscribed{}provider: {}", '\n', provider_name);
let column = &YamlLoader::load_from_str(yaml_str.as_str()).unwrap()[0];

let provider_split = provider_name.split('.');
match get_corresponding_provider(provider_split, column) {
Ok(_) => (),
_ => panic!(),
}
}

#[test]
fn given_wrong_provider_should_return_error() {
let provider_name = "not_a_provider";
let yaml_str = format!("name: email{}provider: {}", '\n', provider_name);
let column = &YamlLoader::load_from_str(yaml_str.as_str()).unwrap()[0];

let provider_split = provider_name.split('.');
match get_corresponding_provider(provider_split, column) {
Err(_) => (),
_ => panic!(),
}
}
}
3 changes: 3 additions & 0 deletions src/providers/constant/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod builder;

pub mod string;
Loading

0 comments on commit b9c5460

Please sign in to comment.