Replies: 1 comment 7 replies
-
That depends a bit on how the structs are passed there. Are they using struct Query { ... }
impl Object for Query { ... }
/// Utility function to extract a [`Query`] out of a [`Value`].
fn value_as_query(obj: &Value) -> Result<&Query, Error> {
obj.downcast_object_ref::<Query>().ok_or_else(|| {
Error::new(
ErrorKind::InvalidOperation,
"filter must be applied to query",
)
})
}
/// Filters a query by some keyword arguments as filter function.
fn filter_filter(obj: &Value, kwargs: Kwargs) -> Result<Value, Error> {
Ok(Value::from_object(value_as_query(obj)?.filter(kwargs)))
} See this example for the full code: https://github.com/mitsuhiko/minijinja/blob/main/examples/dsl/src/main.rs |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm using minijinja as the templating library for a small web app. I make a bunch of sqlx calls and instantiate them as custom serializable structs which are passed to minijinja. (I would love to just pass the anonymous struct objects from calls to the query! macro, but they are not serializable.) Since different places need different fields, these structs are often local to an endpoint and contain different subsets of fields from the same tables.
I would like to write a filter that takes a struct object that contains certain required fields and returns a String. Since these structs are all dynamic Value objects once inside a template, I thought I could do this fairly simply, but I cannot figure it out. I tried using ViaDeserialize, but that only works with one specific concrete type.
What should the argument type for my filter be? Can I just use Value and deserialize it into a custom struct that just contains the fields that I want to use?
Beta Was this translation helpful? Give feedback.
All reactions