@@ -9,12 +9,111 @@ import (
99 lua "github.com/yuin/gopher-lua"
1010)
1111
12+ var luaHelp = `
13+ This filter runs a baker filter defined in a LUA script.
14+ It's useful to quickly write and run a Baker filter without having to recompile Baker.
15+
16+ This filter is based on [GopherLua](github.com/yuin/gopher-lua), which is an LUA5.1 virtual machine.
17+
18+ To use this filter you need to declare a function in an lua file. This function serves the same purpose
19+ of the equivalent ` + ticks ("baker.Filter.Process" ) + ` method one would write for a Go filter.
20+
21+ This is a simple filter which writes "hey" to the field "f0" of every record:
22+ ` + startBlock ("lua" ) + `
23+ -- rec is a record object to be processed
24+ -- next is the function next(record) to forward, if you want to, the record into the filter chain.
25+ function myFilter(rec, next)
26+ rec:set(fieldNames["f0"], "hey")
27+ next(rec)
28+ end
29+ ` + endBlock () + `
30+
31+ ### The ` + ticks ("Record" ) + ` table
32+
33+ The first argument received by your lua filter function is a Record table. The Record table is an surrogate
34+ for its Go counterpart, ` + ticks ("baker.Record" ) + `. The Lua Record table defines the following methods:
35+
36+ #### get
37+
38+ - Go: ` + ticks ("Record.Get(FieldIndex) []byte" ) + `
39+ - lua: ` + ticks ("record:get(idx) -> string" ) + `
40+
41+ Same as its Go counterpart ` + ticks ("record::get" ) + ` takes a integer representing the field index and returns its value.
42+ But, unlike in Go where the value is a ` + ticks ("[]byte]" ) + `, in lua it's returned as a string, to allow for fast protoyping.
43+
44+ #### "set"
45+
46+ - Go: ` + ticks ("Record.Set(FieldIndex, []byte)" ) + `
47+ - lua: ` + ticks ("record:set(idx, string)" ) + `
48+
49+ Same as its Go counterpart ` + ticks ("record::set" ) + ` takes a integer representing the field index and the value to set.
50+ But, unlike in Go where the value is a ` + ticks ("[]byte]" ) + `, in lua it's a string, to allow for fast protoyping.
51+
52+ #### "copy"
53+
54+ - Go: ` + ticks ("Record.Copy() Record" ) + `
55+ - lua: ` + ticks ("record:copy() -> record" ) + `
56+
57+ Calling ` + ticks ("record::copy" ) + ` returns a new record, a deep-copy of the original.
58+
59+ #### "clear"
60+
61+ - Go: ` + ticks ("Record.Clear()" ) + `
62+ - lua: ` + ticks ("lua: record:clear()" ) + `
63+
64+ Calling ` + ticks ("record::clear" ) + ` clears the records internal state, making all its fields empty.
65+
66+
67+ ### Global functions
68+
69+ #### createRecord
70+
71+ - Go: ` + ticks ("Components.CreateRecord() Record" ) + `
72+ - lua: ` + ticks ("createRecord -> record" ) + `
73+
74+ ` + ticks ("createRecord" ) + ` is the lua equivalent of the ` + ticks ("CreateRecord" ) + ` function passed to your filter during construction.
75+ It allows to create a new Record instance.
76+
77+ #### validateRecord
78+
79+ - Go: ` + ticks ("Components.ValidateRecord(Record) (bool, FieldIndex)" ) + `
80+ - lua: ` + ticks ("validateRecord(record) -> (bool, int)" ) + `
81+
82+ ` + ticks ("validateRecord" ) + ` is the lua equivalent of the ` + ticks ("ValidateRecord" ) + ` function passed to your filter during construction.
83+ It validates a given record with respect to the validation function, returning a boolean indicating whether
84+ the record is a valid one, if false, the returned integer indicates the index of the first invalid field it met.
85+
86+ #### fieldByName
87+
88+ - Go: ` + ticks ("Components.FieldByName(string) (FieldIndex, bool)" ) + `
89+ - lua: ` + ticks ("fieldByName(string) -> int|nil" ) + `
90+
91+ ` + ticks ("fieldByName" ) + ` is the lua equivalent of the ` + ticks ("FieldByName" ) + ` function passed to your filter during construction.
92+ It allows to lookup a field index by its name, returning the index or nil if no field exists with this index.
93+
94+ ### Global tables
95+
96+ #### fieldNames
97+
98+ - Go: ` + ticks ("Components.FieldNames []string" ) + `
99+ - lua: ` + ticks ("fieldNames" ) + `
100+
101+ ` + ticks ("fieldNames" ) + ` is an integed-indexed table, in other words an array, containing all field names, as ` + ticks ("FieldNames" ) + ` in Go.
102+ `
103+
104+ // TODO(arl): ideally this functions should not be required, but writing
105+ // markdown documentation in Go strings is tedious and error-prone.
106+
107+ func ticks (s string ) string { return "`" + s + "`" }
108+ func startBlock (lang string ) string { return "```" + lang }
109+ func endBlock () string { return "```" }
110+
12111// LUADesc describes the LUA filter
13112var LUADesc = baker.FilterDesc {
14113 Name : "LUA" ,
15114 New : NewLUA ,
16115 Config : & LUAConfig {},
17- Help : `Run a baker filter defined in a lua script` ,
116+ Help : luaHelp ,
18117}
19118
20119// LUAConfig holds the configuration for the LUA filter.
0 commit comments