mirror of
https://github.com/sagiegurari/duckscript
synced 2024-10-06 08:02:06 +00:00
The read_properties command now support **--prefix** flag
This commit is contained in:
parent
74bd5eeade
commit
dfefb7ff00
|
@ -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.
|
||||
|
|
15
docs/sdk.md
15
docs/sdk.md
|
@ -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
|
||||
```
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
```
|
||||
|
|
|
@ -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()))
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue