From 6a3b1d99b5bac13f80b99c4177f6739307802729 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Wed, 14 Sep 2022 23:40:19 +0100 Subject: [PATCH] Ensure Record IDs are parsed as integers consistently Closes #149 --- lib/src/fnc/type.rs | 23 ++++++++++------------- lib/src/sql/ending.rs | 1 + lib/src/sql/id.rs | 21 +++++++++++++++++++++ lib/src/sql/thing.rs | 16 ++++++++++++++++ 4 files changed, 48 insertions(+), 13 deletions(-) diff --git a/lib/src/fnc/type.rs b/lib/src/fnc/type.rs index f52bd1d0..eb322757 100644 --- a/lib/src/fnc/type.rs +++ b/lib/src/fnc/type.rs @@ -101,19 +101,16 @@ pub fn table(_: &Context, mut args: Vec) -> Result { pub fn thing(_: &Context, mut args: Vec) -> Result { 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, - })), - id => Ok(Value::Thing(Thing { - tb: tb.as_string(), - id: id.as_string().into(), - })), - } - } + 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(), + }, + })), 1 => match args.remove(0) { Value::Thing(v) => Ok(v.into()), _ => Ok(Value::None), diff --git a/lib/src/sql/ending.rs b/lib/src/sql/ending.rs index 8ee62061..62469bfc 100644 --- a/lib/src/sql/ending.rs +++ b/lib/src/sql/ending.rs @@ -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(".."), |_| ()), diff --git a/lib/src/sql/id.rs b/lib/src/sql/id.rs index 26a8790b..0be88adc 100644 --- a/lib/src/sql/id.rs +++ b/lib/src/sql/id.rs @@ -37,6 +37,18 @@ impl From for Id { } } +impl From for Id { + fn from(v: Array) -> Self { + Id::Array(v) + } +} + +impl From for Id { + fn from(v: Object) -> Self { + Id::Object(v) + } +} + impl From 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"; diff --git a/lib/src/sql/thing.rs b/lib/src/sql/thing.rs index b5f66510..419a123a 100644 --- a/lib/src/sql/thing.rs +++ b/lib/src/sql/thing.rs @@ -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`";