mirror of
https://github.com/sagiegurari/duckscript
synced 2024-10-02 22:24:40 +00:00
New json_parse command #124
This commit is contained in:
parent
73fd24cf84
commit
e000613de5
|
@ -1,5 +1,9 @@
|
|||
## CHANGELOG
|
||||
|
||||
### v0.6.4
|
||||
|
||||
* New json_parse command #124
|
||||
|
||||
### v0.6.3 (2020-07-24)
|
||||
|
||||
* Reduce rustc minimal version.
|
||||
|
|
46
docs/sdk.md
46
docs/sdk.md
|
@ -105,6 +105,7 @@
|
|||
* [std::fs::TempFile (temp_file)](#std__fs__TempFile)
|
||||
* [std::fs::WriteBytes (writebinfile, write_binary_file)](#std__fs__WriteBytes)
|
||||
* [std::fs::WriteText (writefile, write_text_file)](#std__fs__WriteText)
|
||||
* [std::json::Parse (json_parse)](#std__json__Parse)
|
||||
* [std::lib::alias::Set (alias)](#std__lib__alias__Set)
|
||||
* [std::lib::alias::Unset (unalias)](#std__lib__alias__Unset)
|
||||
* [std::lib::command::Remove (remove_command)](#std__lib__command__Remove)
|
||||
|
@ -3887,6 +3888,51 @@ result = writefile ./target/tests/writefile.txt "line 1\nline 2"
|
|||
#### Aliases:
|
||||
writefile, write_text_file
|
||||
|
||||
<a name="std__json__Parse"></a>
|
||||
## std::json::Parse
|
||||
```sh
|
||||
var = json_parse string
|
||||
```
|
||||
|
||||
This function will parse the provided JSON string and will create variables based on the parsed data.<br>
|
||||
The variables will reflect the json structure.<br>
|
||||
Object keys will have name using the json path standard, for example root.child<br>
|
||||
And arrays will have the array access annotation and length variable, for example:
|
||||
|
||||
```sh
|
||||
root.child[5]
|
||||
root.child.length
|
||||
```
|
||||
|
||||
#### Parameters
|
||||
|
||||
The JSON string to parse.
|
||||
|
||||
#### Return Value
|
||||
|
||||
The root value.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
package = json_parse "{\"name\": \"my package\", \"version\": 1, \"publish\": false, \"keywords\": [\"test1\", \"test2\"], \"directories\": {\"test\": \"spec\"}}"
|
||||
|
||||
defined = is_defined package
|
||||
assert_false ${defined}
|
||||
|
||||
assert_eq ${package.name} "my package"
|
||||
assert_eq ${package.version} 1
|
||||
assert_eq ${package.publish} false
|
||||
assert_eq ${package.keywords.length} 2
|
||||
assert_eq ${package.keywords[0]} test1
|
||||
assert_eq ${package.keywords[1]} test2
|
||||
assert_eq ${package.directories.test} spec
|
||||
```
|
||||
|
||||
|
||||
#### Aliases:
|
||||
json_parse
|
||||
|
||||
<a name="std__lib__alias__Set"></a>
|
||||
## std::lib::alias::Set
|
||||
```sh
|
||||
|
|
|
@ -36,6 +36,7 @@ java-properties = "^1"
|
|||
meval = "^0.2"
|
||||
num_cpus = "^1"
|
||||
rand = "^0.7"
|
||||
serde_json = "1"
|
||||
walkdir = "^2"
|
||||
which = { version = "^4", default-features = false }
|
||||
whoami = "^0.9"
|
||||
|
|
15
duckscript_sdk/src/sdk/std/json/mod.rs
Normal file
15
duckscript_sdk/src/sdk/std/json/mod.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
mod parse;
|
||||
|
||||
use crate::utils::pckg;
|
||||
use duckscript::types::command::Commands;
|
||||
use duckscript::types::error::ScriptError;
|
||||
|
||||
static PACKAGE: &str = "json";
|
||||
|
||||
pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptError> {
|
||||
let package = pckg::concat(parent, PACKAGE);
|
||||
|
||||
commands.set(parse::create(&package))?;
|
||||
|
||||
Ok(())
|
||||
}
|
38
duckscript_sdk/src/sdk/std/json/parse/help.md
Normal file
38
duckscript_sdk/src/sdk/std/json/parse/help.md
Normal file
|
@ -0,0 +1,38 @@
|
|||
```sh
|
||||
var = json_parse string
|
||||
```
|
||||
|
||||
This function will parse the provided JSON string and will create variables based on the parsed data.<br>
|
||||
The variables will reflect the json structure.<br>
|
||||
Object keys will have name using the json path standard, for example root.child<br>
|
||||
And arrays will have the array access annotation and length variable, for example:
|
||||
|
||||
```sh
|
||||
root.child[5]
|
||||
root.child.length
|
||||
```
|
||||
|
||||
#### Parameters
|
||||
|
||||
The JSON string to parse.
|
||||
|
||||
#### Return Value
|
||||
|
||||
The root value.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
package = json_parse "{\"name\": \"my package\", \"version\": 1, \"publish\": false, \"keywords\": [\"test1\", \"test2\"], \"directories\": {\"test\": \"spec\"}}"
|
||||
|
||||
defined = is_defined package
|
||||
assert_false ${defined}
|
||||
|
||||
assert_eq ${package.name} "my package"
|
||||
assert_eq ${package.version} 1
|
||||
assert_eq ${package.publish} false
|
||||
assert_eq ${package.keywords.length} 2
|
||||
assert_eq ${package.keywords[0]} test1
|
||||
assert_eq ${package.keywords[1]} test2
|
||||
assert_eq ${package.directories.test} spec
|
||||
```
|
111
duckscript_sdk/src/sdk/std/json/parse/mod.rs
Executable file
111
duckscript_sdk/src/sdk/std/json/parse/mod.rs
Executable file
|
@ -0,0 +1,111 @@
|
|||
use crate::utils::pckg;
|
||||
use duckscript::types::command::{Command, CommandResult, Commands};
|
||||
use duckscript::types::instruction::Instruction;
|
||||
use duckscript::types::runtime::StateValue;
|
||||
use serde_json::{Result, Value};
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "./mod_test.rs"]
|
||||
mod mod_test;
|
||||
|
||||
fn parse_json(data: &str) -> Result<Value> {
|
||||
let value: Value = serde_json::from_str(data)?;
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
fn create_variables(data: Value, name: &str, variables: &mut HashMap<String, String>) {
|
||||
match data {
|
||||
Value::Null => variables.remove(name),
|
||||
Value::Bool(value) => variables.insert(name.to_string(), value.to_string()),
|
||||
Value::Number(value) => variables.insert(name.to_string(), value.to_string()),
|
||||
Value::String(value) => variables.insert(name.to_string(), value),
|
||||
Value::Array(list) => {
|
||||
let mut index = 0;
|
||||
for item in list {
|
||||
let child_name = format!("{}[{}]", name, index);
|
||||
create_variables(item, &child_name, variables);
|
||||
index = index + 1;
|
||||
}
|
||||
variables.insert(format!("{}.length", name), index.to_string());
|
||||
|
||||
None
|
||||
}
|
||||
Value::Object(map) => {
|
||||
for (key, value) in map {
|
||||
let child_name = format!("{}.{}", name, key);
|
||||
create_variables(value, &child_name, variables);
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct CommandImpl {
|
||||
package: String,
|
||||
}
|
||||
|
||||
impl Command for CommandImpl {
|
||||
fn name(&self) -> String {
|
||||
pckg::concat(&self.package, "Parse")
|
||||
}
|
||||
|
||||
fn aliases(&self) -> Vec<String> {
|
||||
vec!["json_parse".to_string()]
|
||||
}
|
||||
|
||||
fn help(&self) -> String {
|
||||
include_str!("help.md").to_string()
|
||||
}
|
||||
|
||||
fn clone_and_box(&self) -> Box<dyn Command> {
|
||||
Box::new((*self).clone())
|
||||
}
|
||||
|
||||
fn requires_context(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn run_with_context(
|
||||
&self,
|
||||
arguments: Vec<String>,
|
||||
_state: &mut HashMap<String, StateValue>,
|
||||
variables: &mut HashMap<String, String>,
|
||||
output_variable: Option<String>,
|
||||
_instructions: &Vec<Instruction>,
|
||||
_commands: &mut Commands,
|
||||
_line: usize,
|
||||
) -> CommandResult {
|
||||
if arguments.is_empty() {
|
||||
CommandResult::Error("No JSON string provided.".to_string())
|
||||
} else {
|
||||
match parse_json(&arguments[0]) {
|
||||
Ok(data) => {
|
||||
let output = match output_variable {
|
||||
Some(name) => {
|
||||
create_variables(data, &name, variables);
|
||||
|
||||
match variables.get(&name) {
|
||||
Some(value) => Some(value.to_string()),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
None => Some("true".to_string()),
|
||||
};
|
||||
|
||||
CommandResult::Continue(output)
|
||||
}
|
||||
Err(error) => CommandResult::Error(error.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn create(package: &str) -> Box<dyn Command> {
|
||||
Box::new(CommandImpl {
|
||||
package: package.to_string(),
|
||||
})
|
||||
}
|
35
duckscript_sdk/src/sdk/std/json/parse/mod_test.rs
Normal file
35
duckscript_sdk/src/sdk/std/json/parse/mod_test.rs
Normal file
|
@ -0,0 +1,35 @@
|
|||
use super::*;
|
||||
use crate::test;
|
||||
use crate::test::CommandValidation;
|
||||
|
||||
#[test]
|
||||
fn common_functions() {
|
||||
test::test_common_command_functions(create(""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_no_args() {
|
||||
test::run_script_and_error(vec![create("")], "out = json_parse", "out");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_all_types() {
|
||||
let context = test::run_script_and_validate(
|
||||
vec![create("")],
|
||||
r#"
|
||||
out = json_parse "{\"name\": \"my package\", \"version\": 1, \"publish\": false, \"keywords\": [\"test1\", \"test2\"], \"directories\": {\"test\": \"spec\"}}"
|
||||
"#,
|
||||
CommandValidation::Ignore,
|
||||
);
|
||||
|
||||
let variables = context.variables;
|
||||
|
||||
assert!(!variables.contains_key("out"));
|
||||
assert_eq!(variables.get("out.name").unwrap(), "my package");
|
||||
assert_eq!(variables.get("out.version").unwrap(), "1");
|
||||
assert_eq!(variables.get("out.publish").unwrap(), "false");
|
||||
assert_eq!(variables.get("out.keywords.length").unwrap(), "2");
|
||||
assert_eq!(variables.get("out.keywords[0]").unwrap(), "test1");
|
||||
assert_eq!(variables.get("out.keywords[1]").unwrap(), "test2");
|
||||
assert_eq!(variables.get("out.directories.test").unwrap(), "spec");
|
||||
}
|
|
@ -6,6 +6,7 @@ mod eval;
|
|||
mod flowcontrol;
|
||||
mod fs;
|
||||
mod is_defined;
|
||||
mod json;
|
||||
mod lib;
|
||||
mod man;
|
||||
mod math;
|
||||
|
@ -43,6 +44,7 @@ pub(crate) fn load(commands: &mut Commands) -> Result<(), ScriptError> {
|
|||
env::load(commands, PACKAGE)?;
|
||||
flowcontrol::load(commands, PACKAGE)?;
|
||||
fs::load(commands, PACKAGE)?;
|
||||
json::load(commands, PACKAGE)?;
|
||||
lib::load(commands, PACKAGE)?;
|
||||
math::load(commands, PACKAGE)?;
|
||||
net::load(commands, PACKAGE)?;
|
||||
|
|
16
test/std/json/json_parse_test.ds
Normal file
16
test/std/json/json_parse_test.ds
Normal file
|
@ -0,0 +1,16 @@
|
|||
|
||||
fn test_all_types
|
||||
package = json_parse "{\"name\": \"my package\", \"version\": 1, \"publish\": false, \"keywords\": [\"test1\", \"test2\"], \"directories\": {\"test\": \"spec\"}}"
|
||||
|
||||
defined = is_defined package
|
||||
assert_false ${defined}
|
||||
|
||||
assert_eq ${package.name} "my package"
|
||||
assert_eq ${package.version} 1
|
||||
assert_eq ${package.publish} false
|
||||
assert_eq ${package.keywords.length} 2
|
||||
assert_eq ${package.keywords[0]} test1
|
||||
assert_eq ${package.keywords[1]} test2
|
||||
assert_eq ${package.directories.test} spec
|
||||
end
|
||||
|
Loading…
Reference in a new issue