Skip to content

Commit

Permalink
Feat(kclvm-tools): add json/yaml file loader for KCL-Vet. (#219)
Browse files Browse the repository at this point in the history
* Feat(kclvm-tools): add json/yaml file loader for KCL-Vet.

add json/yaml file loader for KCL-Vet in kclvm/tools/util.

issue #67.

* add comments and remove useless struct

* add test_case for load()

* add invalid test cases

* add test case for invalid json/yaml

* fix test failed
  • Loading branch information
zong-zhe committed Sep 27, 2022
1 parent ef3f63b commit 8ec986b
Show file tree
Hide file tree
Showing 9 changed files with 351 additions and 5 deletions.
12 changes: 7 additions & 5 deletions kclvm/Cargo.lock

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

2 changes: 2 additions & 0 deletions kclvm/tools/Cargo.toml
Expand Up @@ -16,6 +16,8 @@ kclvm-error = {path = "../error", version = "0.1.0"}
kclvm-parser = {path = "../parser", version = "0.1.0"}
kclvm-sema = {path = "../sema", version = "0.1.0"}
kclvm-config = {path = "../config", version = "0.1.0"}
serde_json = "1.0.85"
serde_yaml = "0.9.13"

[dev-dependencies]
pretty_assertions = "1.2.1"
Expand Down
74 changes: 74 additions & 0 deletions kclvm/tools/src/util/loader.rs
@@ -0,0 +1,74 @@
use std::fs;

use anyhow::{bail, Context, Result};

pub(crate) trait Loader<T> {
fn load(&self) -> Result<T>;
}
pub(crate) enum LoaderKind {
YAML,
JSON,
}

/// DataLoader for Json or Yaml
/// If `DataLoader` is constructed using a file path, then `content` is the content of the file.
/// If `DataLoader` is constructed using a Json/Yaml string, then `content` is the string
pub(crate) struct DataLoader {
kind: LoaderKind,
content: String,
}

impl DataLoader {
/// If `DataLoader` is constructed using a file path, then `content` is the content of the file.
pub(crate) fn new_with_file_path(loader_kind: LoaderKind, file_path: &str) -> Result<Self> {
let content = fs::read_to_string(file_path)
.with_context(|| format!("Failed to Load '{}'", file_path))?;

Ok(Self {
kind: loader_kind,
content,
})
}

/// If `DataLoader` is constructed using a Json/Yaml string, then `content` is the string
pub(crate) fn new_with_str(loader_kind: LoaderKind, content: &str) -> Result<Self> {
Ok(Self {
kind: loader_kind,
content: content.to_string(),
})
}

pub(crate) fn get_data(&self) -> &str {
&self.content
}
}

impl Loader<serde_json::Value> for DataLoader {
/// Load data into Json value.
fn load(&self) -> Result<serde_json::Value> {
let v = match self.kind {
LoaderKind::JSON => serde_json::from_str(&self.get_data())
.with_context(|| format!("Failed to String '{}' to Json", self.get_data()))?,
_ => {
bail!("Failed to String to Json Value")
}
};

Ok(v)
}
}

impl Loader<serde_yaml::Value> for DataLoader {
/// Load data into Yaml value.
fn load(&self) -> Result<serde_yaml::Value> {
let v = match self.kind {
LoaderKind::YAML => serde_yaml::from_str(&self.get_data())
.with_context(|| format!("Failed to String '{}' to Yaml", self.get_data()))?,
_ => {
bail!("Failed to String to Yaml Value")
}
};

Ok(v)
}
}
4 changes: 4 additions & 0 deletions kclvm/tools/src/util/mod.rs
Expand Up @@ -3,6 +3,10 @@ use kclvm_config::modfile::KCL_FILE_SUFFIX;
use std::path::Path;
use walkdir::WalkDir;

pub mod loader;
#[cfg(test)]
mod tests;

/// Get kcl files from path.
pub(crate) fn get_kcl_files<P: AsRef<Path>>(path: P, recursively: bool) -> Result<Vec<String>> {
let mut files = vec![];
Expand Down
12 changes: 12 additions & 0 deletions kclvm/tools/src/util/test_datas/test.json
@@ -0,0 +1,12 @@
{
"name": "John Doe",
"age": 43,
"address": {
"street": "10 Downing Street",
"city": "London"
},
"phones": [
"+44 1234567",
"+44 2345678"
]
}
9 changes: 9 additions & 0 deletions kclvm/tools/src/util/test_datas/test.yaml
@@ -0,0 +1,9 @@
languages:
- Ruby
- Perl
- Python
websites:
YAML: yaml.org
Ruby: ruby-lang.org
Python: python.org
Perl: use.perl.org
9 changes: 9 additions & 0 deletions kclvm/tools/src/util/test_datas/test_invalid.json
@@ -0,0 +1,9 @@
languages:
- Ruby
- Perl
- Python
websites:
YAML: yaml.org
Ruby: ruby-lang.org
Python: python.org
Perl: use.perl.org
5 changes: 5 additions & 0 deletions kclvm/tools/src/util/test_datas/test_invalid.yaml
@@ -0,0 +1,5 @@
{
"name": "John Doe",
"city": "London"
invalid

0 comments on commit 8ec986b

Please sign in to comment.