-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
start of work to match cql2 against json #55
base: main
Are you sure you want to change the base?
Conversation
So, trying to set this up so that the reduce function can actually be used for any CQL2 expression to collapse things that don't need to go through to the base data. So say you had a CQL2 query like |
Cargo.toml
Outdated
@@ -24,9 +24,11 @@ keywords = ["cql2"] | |||
|
|||
[dependencies] | |||
boon = "0.6.0" | |||
derive_is_enum_variant = "0.1.1" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW I use https://crates.io/crates/enum-as-inner for this functionality. It looks to be much more popular and more recently maintained.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really know CQL so I didn't look too closely at the body of reduce
(yet)
src/expr.rs
Outdated
@@ -35,7 +37,222 @@ pub enum Expr { | |||
Geometry(Geometry), | |||
} | |||
|
|||
impl From<Value> for Expr { | |||
fn from(v: Value) -> Expr { | |||
let e: Expr = serde_json::from_value(v).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: If there's an unwrap
in there, you usually should be implementing TryFrom
instead of From
.
src/expr.rs
Outdated
} | ||
|
||
|
||
impl TryInto<f64> for Expr { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should implement TryFrom
instead of TryInto
: https://doc.rust-lang.org/std/convert/trait.TryInto.html
src/expr.rs
Outdated
/// ); | ||
/// | ||
/// let mut expr: Expr = serde_json::from_value(expr_json).unwrap(); | ||
/// println!("Initial {:?}", expr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know we're WIP testing, but I find that println
debugging is quicker+better when it's in library tests, not in doctests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also when it's a library test and not a doctest you can just use dbg!()
instead of having to use string formatting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh. I am well known for copious use of print statements when in WIP mode. Will definitely clear when marking for merge.
src/expr.rs
Outdated
/// | ||
/// | ||
/// ``` | ||
pub fn reduce(&mut self, j: &Value) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this might be more ergonomic as a consume-then-return:
pub fn reduce(&mut self, j: &Value) { | |
pub fn reduce(self, j: &Value) -> Expr { |
That way you can just return directly from those match branches below rather than assigning to *self
.
src/expr.rs
Outdated
let b: Result<bool, _> = arg.as_ref().clone().try_into(); | ||
match b { | ||
Ok(true) => anytrue = true, | ||
Ok(false) => { | ||
alltrue = false; | ||
} | ||
_ => { | ||
alltrue = false; | ||
allbool = false; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about the .as_ref().clone()
removal, but I'd guess there'd be a way to avoid that? I'm working in the browser right now so don't have IDE powers to make me less dumb 😄:
let b: Result<bool, _> = arg.as_ref().clone().try_into(); | |
match b { | |
Ok(true) => anytrue = true, | |
Ok(false) => { | |
alltrue = false; | |
} | |
_ => { | |
alltrue = false; | |
allbool = false; | |
} | |
} | |
if let Ok(bool) = arg.try_into() { | |
if bool { | |
anytrue = true; | |
} else { | |
alltrue = false; | |
} | |
} else { | |
alltrue = false; | |
allbool = false; | |
// Can you break here? | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't break there as the main thing that loop is doing is to run reduce against each arg. We are just doing the bool check as an addition so we only have to loop through the args once.
src/expr.rs
Outdated
let mut e = self.clone(); | ||
e.reduce(j); | ||
match e { | ||
Expr::Bool(v) => Ok(v), | ||
_ => Err(()), | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How often will we re-use matches
? Can we make this consuming (taking self
) and let the caller do a clone
themselves? Cloning inside a simple method like this feels a bit off.
Co-authored-by: Pete Gadomski <[email protected]>
Co-authored-by: Pete Gadomski <[email protected]>
WORK IN PROGRESS
TODO: