mirror of
https://github.com/fluencelabs/jsonpath
synced 2025-03-16 07:20:51 +00:00
53 lines
1.2 KiB
Lua
53 lines
1.2 KiB
Lua
local ffi = require('ffi')
|
|
|
|
ffi.cdef [[
|
|
const char* ffi_select(const char *json_str, const char *path);
|
|
void *ffi_path_compile(const char *path);
|
|
const char* ffi_select_with_compiled_path(void *ptr, const char *json_str);
|
|
]]
|
|
|
|
local jsonpath
|
|
local cache = {}
|
|
local module = {}
|
|
|
|
local function existsVaiable(var)
|
|
for k, _ in pairs(_G) do
|
|
if k == var then
|
|
return true
|
|
end
|
|
end
|
|
end
|
|
|
|
local _ngx
|
|
if existsVaiable('ngx') then
|
|
_ngx = ngx
|
|
else
|
|
_ngx = {}
|
|
_ngx.log = function(level, msg)
|
|
print('['..level..'] ' .. msg)
|
|
end
|
|
end
|
|
|
|
function module.compile(path)
|
|
assert(jsonpath, '"libjsonpath_lib" is not loaded')
|
|
|
|
if(cache[path] == nil) then
|
|
cache[path] = jsonpath.ffi_path_compile(path)
|
|
_ngx.log(_ngx.INFO, 'compile : [' .. path .. ']')
|
|
end
|
|
|
|
local compiledPath = cache[path]
|
|
return function(jsonStr)
|
|
local result = jsonpath.ffi_select_with_compiled_path(compiledPath, jsonStr)
|
|
return ffi.string(result);
|
|
end
|
|
end
|
|
|
|
function module.init(path)
|
|
if jsonpath == nil then
|
|
jsonpath = ffi.load(path)
|
|
_ngx.log(_ngx.INFO, '"' .. path .. '" initialized')
|
|
end
|
|
end
|
|
|
|
return module |