Ensure Record IDs are parsed as integers consistently

Closes #149
This commit is contained in:
Tobie Morgan Hitchcock 2022-09-14 23:40:19 +01:00
parent 74c3b6d708
commit 6a3b1d99b5
4 changed files with 48 additions and 13 deletions

View file

@ -101,19 +101,16 @@ pub fn table(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
pub fn thing(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
match args.len() {
2 => {
let tb = args.remove(0);
match args.remove(0) {
Value::Thing(id) => Ok(Value::Thing(Thing {
tb: tb.as_string(),
id: id.id,
2 => Ok(Value::Thing(Thing {
tb: args.remove(0).as_string(),
id: match args.remove(0) {
Value::Thing(v) => v.id,
Value::Array(v) => v.into(),
Value::Object(v) => v.into(),
Value::Number(Number::Int(v)) => v.into(),
v => v.as_string().into(),
},
})),
id => Ok(Value::Thing(Thing {
tb: tb.as_string(),
id: id.as_string().into(),
})),
}
}
1 => match args.remove(0) {
Value::Thing(v) => Ok(v.into()),
_ => Ok(Value::None),

View file

@ -18,6 +18,7 @@ pub fn number(i: &str) -> IResult<&str, ()> {
map(char(')'), |_| ()),
map(char(']'), |_| ()),
map(char('}'), |_| ()),
map(char('"'), |_| ()),
map(char(';'), |_| ()),
map(char(','), |_| ()),
map(tag(".."), |_| ()),

View file

@ -37,6 +37,18 @@ impl From<u64> for Id {
}
}
impl From<Array> for Id {
fn from(v: Array) -> Self {
Id::Array(v)
}
}
impl From<Object> for Id {
fn from(v: Object) -> Self {
Id::Object(v)
}
}
impl From<String> for Id {
fn from(v: String) -> Self {
Id::String(v)
@ -88,6 +100,15 @@ mod tests {
use super::*;
#[test]
fn id_int() {
let sql = "001";
let res = id(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!(Id::from(1), out);
}
#[test]
fn id_number() {
let sql = "100";

View file

@ -166,6 +166,22 @@ mod tests {
);
}
#[test]
fn thing_integer() {
let sql = "test:001";
let res = thing(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!("test:1", format!("{}", out));
assert_eq!(
out,
Thing {
tb: String::from("test"),
id: Id::from(1),
}
);
}
#[test]
fn thing_quoted_backtick() {
let sql = "`test`:`id`";