Skip to content

Commit

Permalink
Merge pull request #10 from fuyutarow/math
Browse files Browse the repository at this point in the history
Fix tests-make/* and add options: --from --compact
  • Loading branch information
fuyutarow authored Jun 7, 2021
2 parents a1b61d1 + eb9e95e commit 1d62c0a
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 107 deletions.
16 changes: 10 additions & 6 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@ args = ["build", "--release"]
command = "cargo"
args = ["build", "--bin", "pq", "--release"]

# not working

[tasks.test]
alias = "test:all"

[tasks."test:all"]
run_task = [{ name = "test:lib" }, { name = "test:pq" }]
script = '''
makers test:lib
makers test:pq
'''

[tasks."test:lib"]
dependencies = ["build"]
command = "cargo"
args = ["test"]
dependencies = ["build"]

[tasks."test:pq"]
dependencies = ["build:pq"]
Expand Down Expand Up @@ -77,7 +83,5 @@ script = '''
alias pc="./target/debug/partiql-cli"
alias pq="./target/debug/pq"
for i in $(seq 1 18)
do
cat samples/q$i.env | pc from --to json | pq -S > samples/q$i.json
done
cat samples/q$i.env | pc from --to json | pq -S > samples/q$i.json
'''
2 changes: 2 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"
1 change: 1 addition & 0 deletions samples/mew-c.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"Mew","id":151,"fleeRate":0.1}
5 changes: 5 additions & 0 deletions samples/mew.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Mew",
"id": 151,
"fleeRate": 0.1
}
21 changes: 19 additions & 2 deletions src/bin/pq.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::convert::TryFrom;
use std::io::{self, Read};
use std::path::PathBuf;
use std::str::FromStr;
Expand Down Expand Up @@ -27,21 +28,31 @@ struct Opt {
#[structopt(short, long)]
query: Option<String>,

/// target config file
#[structopt(short, long, possible_values(&["csv", "json", "toml", "yaml", "xml"]))]
from: Option<String>,

/// target config file
#[structopt(short, long, possible_values(&["csv", "json", "toml", "yaml", "xml"]))]
to: Option<String>,

/// sort keys of objects on output. it on works when --to option is json, currently
#[structopt(short = "S", long)]
sort_keys: bool,

/// compact instead of pretty-printed output, only when outputting in JSON
#[structopt(short, long)]
compact: bool,
}

fn main() -> anyhow::Result<()> {
let Opt {
file_or_stdin,
query,
from,
to,
sort_keys,
compact,
} = Opt::from_args();
let _ = {
let input = if let Some(file) = file_or_stdin {
Expand All @@ -50,7 +61,13 @@ fn main() -> anyhow::Result<()> {
read_from_stdin()?
};

let mut lang = Lang::from_str(&input)?;
let mut lang = if let Some(s_lang_type) = from {
let lang_type = LangType::from_str(&s_lang_type)?;
Lang::from_as(&input, lang_type)?
} else {
Lang::from_str(&input)?
};

if let Some(t) = to {
match LangType::from_str(&t) {
Ok(lang_type) => lang.to = lang_type,
Expand All @@ -69,7 +86,7 @@ fn main() -> anyhow::Result<()> {
lang.sort_keys();
}

lang.print();
lang.print(compact);
};

Ok(())
Expand Down
78 changes: 67 additions & 11 deletions src/lib/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use crate::value::{BPqlValue, PqlValue, TomlValue};
#[derive(Display, FromStr, PartialEq, Clone, Debug)]
#[display(style = "snake_case")]
pub enum LangType {
Toml,
Csv,
Json,
Toml,
Yaml,
Xml,
}
Expand All @@ -32,15 +32,47 @@ impl FromStr for Lang {
type Err = anyhow::Error;

fn from_str(input: &str) -> anyhow::Result<Self> {
if let Ok(data) = csvstr_to_pqlv(&input) {
if let Ok(this) = Self::from_as_json(input) {
Ok(this)
} else if let Ok(this) = Self::from_as_toml(input) {
Ok(this)
} else if let Ok(this) = Self::from_as_xml(input) {
Ok(this)
} else if let Ok(this) = Self::from_as_xml(input) {
Ok(this)
} else {
anyhow::bail!("not supported")
}
}
}

impl Lang {
pub fn from_as(input: &str, lnag_type: LangType) -> anyhow::Result<Self> {
match lnag_type {
LangType::Csv => Self::from_as_csv(input),
LangType::Json => Self::from_as_json(input),
LangType::Toml => Self::from_as_toml(input),
LangType::Yaml => Self::from_as_yaml(input),
LangType::Xml => Self::from_as_xml(input),
}
}

pub fn from_as_csv(input: &str) -> anyhow::Result<Self> {
if let Ok(data) = csvstr_to_pqlv(input) {
Ok(Self {
data,
text: input.to_string(),
from: LangType::Csv,
to: LangType::Csv,
colnames: Vec::default(),
})
} else if let Ok(data) = serde_json::from_str::<serde_json::value::Value>(&input) {
} else {
anyhow::bail!("fail to parse input as csv");
}
}

pub fn from_as_json(input: &str) -> anyhow::Result<Self> {
if let Ok(data) = serde_json::from_str::<serde_json::value::Value>(input) {
// Json does not distinguish between Float and Int. For this reason, it it parsed once with serde_json::value::Value, not crate::value::PqlValue.
Ok(Self {
data: crate::value::json_value::to_pqlvalue(data),
Expand All @@ -49,23 +81,41 @@ impl FromStr for Lang {
to: LangType::Json,
colnames: Vec::default(),
})
} else if let Ok(data) = toml::from_str::<PqlValue>(&input) {
} else {
anyhow::bail!("fail to parse input as json");
}
}

pub fn from_as_toml(input: &str) -> anyhow::Result<Self> {
if let Ok(data) = toml::from_str::<PqlValue>(input) {
Ok(Self {
data,
text: input.to_string(),
from: LangType::Toml,
to: LangType::Toml,
colnames: Vec::default(),
})
} else if let Ok(data) = quick_xml::de::from_str::<PqlValue>(&input) {
} else {
anyhow::bail!("fail to parse input as toml");
}
}

pub fn from_as_xml(input: &str) -> anyhow::Result<Self> {
if let Ok(data) = quick_xml::de::from_str::<PqlValue>(input) {
Ok(Self {
data,
text: input.to_string(),
from: LangType::Xml,
to: LangType::Xml,
colnames: Vec::default(),
})
} else if let Ok(data) = serde_yaml::from_str::<PqlValue>(&input) {
} else {
anyhow::bail!("fail to parse input as xml");
}
}

pub fn from_as_yaml(input: &str) -> anyhow::Result<Self> {
if let Ok(data) = serde_yaml::from_str::<PqlValue>(input) {
Ok(Self {
data,
text: input.to_string(),
Expand All @@ -74,12 +124,10 @@ impl FromStr for Lang {
colnames: Vec::default(),
})
} else {
anyhow::bail!("not supported")
anyhow::bail!("fail to parse input as yaml");
}
}
}

impl Lang {
pub fn sort_keys(&mut self) {
let json = serde_json::to_string(&self.data).unwrap();
let bdata = serde_json::from_str::<BPqlValue>(&json).unwrap();
Expand All @@ -88,7 +136,7 @@ impl Lang {
self.data = data;
}

pub fn print(&self) -> anyhow::Result<()> {
pub fn print(&self, compact: bool) -> anyhow::Result<()> {
let output = match (&self.to, &self.from == &self.to) {
(LangType::Csv, _) => {
// To pad missing values with null, serialize them to json, deserialize them with polars, and write them to csv from there.
Expand All @@ -111,13 +159,21 @@ impl Lang {
let s = String::from_utf8(v)?;
s
}
(LangType::Json, _) if compact => serde_json::to_string(&self.data).unwrap(),
(LangType::Json, _) => serde_json::to_string_pretty(&self.data).unwrap(),
(_, true) => self.text.to_owned(),
(LangType::Toml, _) => {
let v = TomlValue::from(self.data.to_owned());
toml::to_string_pretty(&v).unwrap()
}
(LangType::Yaml, _) => serde_yaml::to_string(&self.data).unwrap(),
(LangType::Yaml, _) => {
dbg!("yaml");

serde_yaml::to_string(&self.data)
.unwrap()
.trim_start_matches("---\n")
.to_string()
}
(LangType::Xml, _) => quick_xml::se::to_string(&self.data).unwrap(),
};

Expand Down
9 changes: 4 additions & 5 deletions tests-make.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
includes = [
"tests-make/convert.toml",
"tests-make/float.toml",
"tests-make/package-json.toml",
"tests-make/se_csv.toml",
"tests-make/se_toml.toml",
"tests-make/convert.toml",
"tests-make/package-json.toml",
"tests-make/se_csv.toml",
"tests-make/se_toml.toml",
]
30 changes: 17 additions & 13 deletions tests-make/convert.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ INPUT = '''
}
'''

[tests.to_json]
[tests.to-json]
script = '''
alias pq ="./target/debug/pq"
echo ${INPUT} | pq -t json
echo $INPUT | ./target/debug/pq -t json
'''
tobe = '''
{
Expand All @@ -20,10 +19,18 @@ tobe = '''
}
'''

[tests.to_toml]
[tests.to-json-compact]
script = '''
alias pq ="./target/debug/pq"
echo ${INPUT} | pq -t toml
echo $INPUT | ./target/debug/pq -t json -c
'''
tobe = '''
{"name":"Mew","id":151,"fleeRate":0.1}
'''


[tests.to-toml]
script = '''
echo $INPUT | ./target/debug/pq -t toml
'''
tobe = '''
name = 'Mew'
Expand All @@ -32,23 +39,20 @@ fleeRate = 0.1
'''

[tests.to_yaml]
[tests.to-yaml]
script = '''
alias pq ="./target/debug/pq"
echo ${INPUT} | pq -t yaml
echo $INPUT | ./target/debug/pq -t yaml
'''
tobe = '''
---
name: Mew
id: 151
fleeRate: 0.1
'''

[tests.to_xml]
[tests.to-xml]
script = '''
alias pq ="./target/debug/pq"
echo ${INPUT} | pq -t xml
echo $INPUT | ./target/debug/pq -t xml
'''
tobe = '''
<name>Mew</name><id>151</id><fleeRate>0.1</fleeRate>
Expand Down
55 changes: 0 additions & 55 deletions tests-make/float.toml

This file was deleted.

Loading

0 comments on commit 1d62c0a

Please sign in to comment.