qhwa
Formular - A tiny DSL engine / code evaluator (configuration as code)
Hi, all,
I just published Formular package. It is a tiny library that evaluates a piece of Elixir code.
On the shoulder of Elixir’s Code module
Given a piece of Elixir code (as a string, or AST), Formular runs it with Elixir’s Code module under some security limitations.
So far, the limitations are:
- No calling module functions;
- No calling exit;
- No sending messages.
Indeed, the whole library consists of only one thin module, thanks to the power Elixir has already shipped out of the box.
Motivation
Formular was developed to support some dynamic configuration scenarios. For example, in a scene of an online book store, the discount of a book can be dynamically configured as a piece of code, then evaluated by Formular:
iex> discount_formula = ~s"
...> case order do
...> # old books get a big promotion
...> %{book: %{year: year}} when year < 2000 ->
...> 0.5
...>
...> %{book: %{tags: tags}} ->
...> # Elixir books!
...> if ~s{elixir} in tags do
...> 0.9
...> else
...> 1.0
...> end
...>
...> _ ->
...> 1.0
...> end
...> "
...>
...> book_order = %{
...> book: %{
...> title: "Elixir in Action", year: 2019, tags: ["elixir"]
...> }
...> }
...>
...> Formular.eval(discount_formula, [order: book_order])
{:ok, 0.9}
In such a way, the discount calculation code, which changes frequently, is separated away from the stable business flow, and the primary code is probably more generic and flexible.
I’ve been using it in production for a while so I publish it today in case others may find it useful too.
Cheers!
Trending in Announcing
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #performance
- #security










Most Liked
hauleth
DoS (atom exhaustion):
I needed to create range manually, as you do not export
../2operator.qhwa
Two new versions have been released:
v0.2.2
0.2.xv0.3.0
Formula.used_vars/2to extract the used variable names in the formula.Highlights:
Performance improvements:
A simple benchmark on a simple formula results in this:
The compilation approach is about 80x faster than the original eval approach in v0.2.2, and 300x faster than versions prior to v0.2.2.
Restricted evaluation:
limited execution time:
limited max heap size by word:
If limited, the evaluation will be run in a separate process.
Extract used vars API:
This can be helpful if you need to build some UI arround the formula.
mat-hek
Nice, though I’m curious if you tried using Sand?