-
Notifications
You must be signed in to change notification settings - Fork 81
Description
Hello!
Thank you for making this implementation of liquid for Rust, we've been using it at Meilisearch to let our users tell us how we should turn their JSON documents into text, and it has been good for us!
There's a currently missing capability that would help us validate the templates written by our users ahead of time: listing (global) variables in use in a template, so that we can check that rendering that template with a given object will not fail because of a missing field.
A concrete application of this for us would be to validate that any template written by our users would only reference the following globals:
doc
with type object- for all
x
,doc.x
with any type fields
with type array- for each element of
fields
, an object with keysname
(with type string) andvalue
(with any type)
I made an attempt at this kind of validation with an implementation of ObjectView
that I called TemplateChecker
such that rendering the template would either succeed (without producing a significant string) or return a meaningful error. However, the implementation is limited:
- Some code paths in the liquid runtime turn a
ValueView
into a concreteValue
, preventing me from being able to run my custom code that allows to infinitely dereferencedoc
with any type (e.g.doc.foo.bar.example
). - More fundamentally, this evaluation is lazy and might not consider some unreachable code paths (if it takes the
else
branch in anif
construct, it will not evaluate the other branch). I need some kind of static analysis here.
Such a hypothetical feature could add a method to Template
to list the referenced global variables in the template.
- It would exclude globals created through assignment
- Optionally, it would include the type of each variable
- Optionally, it would also include subvariables accessed through a global variable
Thank you for reading and maybe considering that feature request. I would understand if it were out of scope for that project.
I can help with the implementation but I'm not super sure where would be the most sensible location to gather that list of variables so I might need some instructions here.