Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(kclvm-tools): add json/yaml file loader for KCL-Vet. #219

Merged
merged 6 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "John Doe",
"city": "London"
invalid

Loading