Git Product home page Git Product logo

jmespath's Introduction

jmespath.lua

A pure Lua implementation of JMESPath.

local jmespath = require "jmespath"
local expression = "foo.baz"
local data = { foo = { baz = "bar" } }
local result = jmespath.search(expression, data)

jmespath.lua can be installed using LuaRocks:

luarocks install jmespath

Runtimes

jmespath.lua allows you to create a customized runtime using a hash of configuration options to control how jmespath.lua parses and evaluates expressions. Use the jmespath.runtime() function to create a new runtime.

local jmespath = require 'jmespath'
local runtime = jmespath.runtime()
runtime('foo', {foo=10}) -- outputs 10

The runtime function accepts a hash of the following configuration options:

cache

Set to false to disable caching of parsing expressions into ASTs.

local jmespath = require 'jmespath'
local runtime = jmespath.runtime{cache = false}
-- The runtime is function that accepts expression, data
runtime('[2]', {1, 2, 3}) -- outputs 3
fn_dispatcher

A function that accepts a function name as the first argument and a sequence of arguments as the second argument. This can be useful for registering new functions with the JMESPath interpreter. For example, let's say you wanted to add a custom add function that accepts a variadic number of arguments. This can be acheived by wrapping the existing default function dispatcher and adding a new function:

local jmespath = require 'jmespath'

-- Create a default dispatcher function.
local default_dispatcher = jmespath.Functions.new()

-- Create a new dispatcher function that wraps the default dispatcher.
local dispatcher = function (name, args)
  if name == 'add' then
    jmespath.Functions.reduce(args, function (carry, item, index)
      if index > 1 then return carry + item end
      return item
    end)
  end
  return default_dispatcher(name, args)
end

local runtime = jmespath.runtime{fn_dispatcher = dispatcher}
runtime('add(foo, `10`, `1`)', {foo=10}) -- outputs 21

Testing

jmespath.lua is tested using busted. You'll need to install busted and luafilesystem to run the tests:

make test-setup

After installing jmespath.lua, you can run the tests with the following command:

make test

jmespath's People

Contributors

evaera avatar mtdowling avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.