Last active
August 29, 2015 22:17
-
-
Save nathwill/0d28a63e7ff371303bf7 to your computer and use it in GitHub Desktop.
POC for generic kv pairs decoder for heka
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local l = require "lpeg" | |
require "cjson" | |
l.locale(l) | |
local sp = l.space^0 | |
local unreserved = l.digit + l.alnum + l.S"/=-.,_~ " | |
local name = l.C(unreserved^1) * sp | |
local comment = l.P"#" * (1 - l.P"\n")^1 * l.P"\n" | |
local sep = l.P(read_config("pair_separator")) | |
local pair = comment^0 * l.Cg(name * read_config("kv_separator") * sp * name) * comment^0 * sep^-1 | |
local rig = l.Cf(l.Ct("") * pair^0, rawset) | |
local ok, numeric_fields = pcall(cjson.decode, read_config("numeric_fields")) | |
local msg = { | |
Type = read_config('type') or 'kvpairs', | |
Payload = nil, | |
Fields = nil | |
} | |
function process_message() | |
local data = read_message("Payload") | |
msg.Fields = rig:match(data) | |
if not msg.Fields then return -1 end | |
-- keep payload, or not | |
if payload_keep then | |
msg.Payload = read_message("Payload") | |
end | |
for i, name in ipairs(numeric_fields) do | |
msg.Fields[name] = tonumber(msg.Fields[name]) | |
end | |
inject_message(msg) | |
return 0 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example config: