-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(kclvm-tools): add json/yaml file loader for KCL-Vet. (#219)
* 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
Showing
9 changed files
with
351 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "John Doe", | ||
"city": "London" | ||
invalid | ||
|
Oops, something went wrong.