Skip to content

Commit

Permalink
Add option: --from
Browse files Browse the repository at this point in the history
  • Loading branch information
fuyutarow committed Jun 7, 2021
1 parent ea422fc commit 194763f
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 24 deletions.
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
}
14 changes: 13 additions & 1 deletion 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,6 +28,10 @@ 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>,
Expand All @@ -40,6 +45,7 @@ fn main() -> anyhow::Result<()> {
let Opt {
file_or_stdin,
query,
from,
to,
sort_keys,
} = Opt::from_args();
Expand All @@ -50,7 +56,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 Down
81 changes: 64 additions & 17 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,16 +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) {
// 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) {
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 {
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 @@ -50,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 @@ -75,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 Down
3 changes: 1 addition & 2 deletions tests-make/se_csv.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

[tests.as-name]
script = '''
alias pq ="./target/debug/pq"
cat samples/ip_addr.json | pq -q "SELECT address, ifname, addr_info.family" -t csv
cat samples/ip_addr.json | ./target/debug/pq -q "SELECT address, ifname, addr_info.family" -t csv
'''
tobe = '''
address,ifname,family
Expand Down
6 changes: 2 additions & 4 deletions tests-make/se_toml.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

[tests.nonamed-table]
script = '''
alias pq ="./target/debug/pq"
cat<<EOS | pq -t toml
cat<<EOS | ./target/debug/pq -t toml
[
{
"id": 3,
Expand Down Expand Up @@ -32,8 +31,7 @@ title = 'Software Eng 2'

[tests.value-after-table]
script = '''
alias pq ="./target/debug/pq"
cat<<EOS | pq -t toml
cat<<EOS | ./target/debug/pq -t toml
{
"name": "partiql-pokemon",
"private": true,
Expand Down

0 comments on commit 194763f

Please sign in to comment.