fexpr

Package Version Hex Docs

A filter query executor for Gleam.

This library was heavily inspired by fexpr for Go.

gleam add fexpr@1
import fexpr

pub fn main() -> Nil {
  let user_json =
    json.object([
      #("id", json.string("user_123456")),
      #("username", json.string("Remy")),
      #("balance", json.float(250.5)),
      #("last_connection", birl.now() |> birl.to_iso8601() |> json.string),
      #("is_admin", json.bool(True)),
      #(
        "settings",
        json.object([
          #("theme", json.string("dark")),
          #("notifications", json.bool(False)),
        ]),
      ),
    ])

  let res =
    fexpr.verify_fexpr(
      fexpr.fexpr("is_admin", fexpr.Equal, fexpr.Bool(True))
        |> fexpr.and("settings.theme", fexpr.Equal, fexpr.String("dark")),
      json.to_string(user_json),
    )
  assert res == True

  let res =
    fexpr.verify_fexpr(
      fexpr.fexpr("balance", fexpr.Greater, fexpr.Float(100.0))
        |> fexpr.or("settings.notifications", fexpr.Equal, fexpr.Bool(True)),
      json.to_string(user_json),
    )
  assert res == True
}

Further documentation can be found at https://hexdocs.pm/fexpr.

Development

gleam run   # Run the project
gleam test  # Run the tests
Search Document