Skip to content

Commit e3c853d

Browse files
committed
filter: lua: improve filter documentation
1 parent fc9d1b5 commit e3c853d

File tree

1 file changed

+97
-1
lines changed

1 file changed

+97
-1
lines changed

filter/lua.go

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,108 @@ import (
99
lua "github.com/yuin/gopher-lua"
1010
)
1111

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

20116
// LUAConfig holds the configuration for the LUA filter.

0 commit comments

Comments
 (0)