The read_properties command now support **--prefix** flag

This commit is contained in:
sagie gur ari 2020-01-25 15:57:52 +00:00
parent 74bd5eeade
commit dfefb7ff00
6 changed files with 63 additions and 8 deletions

View file

@ -2,6 +2,7 @@
### v0.1.9
* The read_properties command now support **--prefix** flag.
* New array_concat command.
* New trigger_error command.
* New array_push command.

View file

@ -988,15 +988,17 @@ range
<a name="std__collections__ReadProperties"></a>
## std::collections::ReadProperties
```sh
count = read_properties text
count = read_properties [--prefix key] text
```
Parses the properties (based on java properties format) text and sets them as variables.<br>
This command will also return the count of properties read.
This command will also return the count of properties read.<br>
If prefix is provided, all properties read, will be stored as variables with the **prefix.** as their prefix.
#### Parameters
The text to parse.
* Optional --prefix and the prefix value
* The text to parse.
#### Return Value
@ -1011,6 +1013,13 @@ assert_eq ${count} 3
assert_eq ${a} 1
assert_eq ${b} 2
assert_eq ${a.b.c} 3
count = read_properties --prefix config a=1\nb=2\na.b.c=3
assert_eq ${count} 3
assert_eq ${config.a} 1
assert_eq ${config.b} 2
assert_eq ${config.a.b.c} 3
```

View file

@ -1,13 +1,15 @@
```sh
count = read_properties text
count = read_properties [--prefix key] text
```
Parses the properties (based on java properties format) text and sets them as variables.<br>
This command will also return the count of properties read.
This command will also return the count of properties read.<br>
If prefix is provided, all properties read, will be stored as variables with the **prefix.** as their prefix.
#### Parameters
The text to parse.
* Optional --prefix and the prefix value
* The text to parse.
#### Return Value
@ -22,4 +24,11 @@ assert_eq ${count} 3
assert_eq ${a} 1
assert_eq ${b} 2
assert_eq ${a.b.c} 3
count = read_properties --prefix config a=1\nb=2\na.b.c=3
assert_eq ${count} 3
assert_eq ${config.a} 1
assert_eq ${config.b} 2
assert_eq ${config.a.b.c} 3
```

View file

@ -48,10 +48,22 @@ impl Command for CommandImpl {
if arguments.len() < 1 {
CommandResult::Error("Missing properties text argument.".to_string())
} else {
match read(arguments[0].as_bytes()) {
let (prefix, text) = if arguments.len() >= 3 && arguments[0] == "--prefix" {
(arguments[1].to_string(), arguments[2].to_string())
} else {
("".to_string(), arguments[0].to_string())
};
match read(text.as_bytes()) {
Ok(data) => {
for (key, value) in &data {
variables.insert(key.to_string(), value.to_string());
let mut var_key = key.to_string();
if !prefix.is_empty() {
var_key.insert(0, '.');
var_key.insert_str(0, &prefix);
}
variables.insert(var_key, value.to_string());
}
CommandResult::Continue(Some(data.len().to_string()))

View file

@ -26,3 +26,18 @@ fn run_valid() {
assert_eq!(context.variables.get("a").unwrap(), "1");
assert_eq!(context.variables.get("b").unwrap(), "2");
}
#[test]
fn run_with_prefix() {
let context = test::run_script_and_validate(
vec![create(""), Box::new(SetCommand {})],
r#"
props = test_set "a=1\nb=2"
out = read_properties --prefix config ${props}
"#,
CommandValidation::Match("out".to_string(), "2".to_string()),
);
assert_eq!(context.variables.get("config.a").unwrap(), "1");
assert_eq!(context.variables.get("config.b").unwrap(), "2");
}

View file

@ -7,3 +7,12 @@ function test_read_properties
assert_eq ${b} 2
assert_eq ${a.b.c} 3
end
function test_read_properties_with_prefix
count = read_properties --prefix config a=1\nb=2\na.b.c=3
assert_eq ${count} 3
assert_eq ${config.a} 1
assert_eq ${config.b} 2
assert_eq ${config.a.b.c} 3
end