diff options
| author | unwox <me@unwox.com> | 2025-01-27 17:44:25 +0600 |
|---|---|---|
| committer | unwox <me@unwox.com> | 2025-01-27 17:44:25 +0600 |
| commit | ef75533bc54fdaa84dbb1e4a3a213af57fad3915 (patch) | |
| tree | 2132179ba3f5271eb063286b8099c22eadd872ff | |
| parent | c71a1b5ab5d7fbbf4613f0e130a205134875092f (diff) | |
add both camelCase and kebab-case versions of API functions
| -rw-r--r-- | README.md | 28 | ||||
| -rw-r--r-- | example/main.lua | 6 | ||||
| -rw-r--r-- | main.go | 8 |
3 files changed, 24 insertions, 18 deletions
@@ -51,7 +51,7 @@ then exit. This behavior is useful when you want to have a script file with an access to the luna API. If luna is started with -D flag it will accept user input into its stdio. The -input is executed in Lua state as is or (if `luna.evalfn` was called in +input is executed in Lua state as is or (if `luna.onEval` was called in LUAFILE) is passed to a custom eval handler. ## Lua API @@ -60,9 +60,11 @@ All API functions return 2 values: the first one is a boolean indicating if the call was successful (true for successful and false if there was an error) and the second one is the value (or an error text if the call was unsuccessful). -API names are hyphenated since I am mostly calling the functions from -[fennel language](https://fennel-lang.org/) and for lisps hyphenation is the -norm. +All API names have hyphenated alternatives since I am mostly calling the +functions from [fennel language](https://fennel-lang.org/) and for lisps +hyphenation is the norm. So for example you can call `luna.db.execTx` also +as `luna.db["exec-tx"]`, `luna.http.encodeURL` as `luna.http["encode-url"]` +and so on. Every argument in every API function is required: luna does not support optional arguments yet. Pass an empty value for corresponding type instead if you do not @@ -99,7 +101,7 @@ luna.http.request("GET", "https://git.sr.ht", {Accept = "text/html"}, "") ``` -__luna.http\["encode-url"\](string)__: +__luna.http.encodeURL(string)__: encodes a STRING for a safe usage in URLs. @@ -126,7 +128,7 @@ commits a transaction TX. ```lua local ok, tx = luna.db.begin(db) -local ok, err = luna.db["exec-tx"](tx, "DELETE FROM foobar;") +local ok, err = luna.db.execTx(tx, "DELETE FROM foobar;") local ok, err = luna.db.commit(tx) ``` @@ -136,18 +138,18 @@ rolls back a transaction TX. ```lua local ok, tx = luna.db.begin(db) -local ok, err = luna.db["exec-tx"](tx, "DELETE FROM foobar;", {}) +local ok, err = luna.db.execTx(tx, "DELETE FROM foobar;", {}) local ok, err = luna.db.rollback(tx) -- the tx was rolled back so "DELETE FROM foobar;" isn't executed ``` -__luna.db\["exec-tx"\](tx, query, args)__: +__luna.db.execTx(tx, query, args)__: executes a QUERY with ARGS in the context of a given transaction TX. ```lua local ok, tx = luna.db.begin(db) -local ok, err = luna.db["exec-tx"]( +local ok, err = luna.db.execTx( tx, "INSERT INTO foobar VALUES (?, ?, ?);" {1, "hello!", "2024-12-26 12:00:00"} @@ -185,13 +187,13 @@ local ok, res = luna.db.query( ``` -__luna.db.\["query\*"\](db, query, args)__: +__luna.db.queryAssoc(db, query, args)__: executes a QUERY with ARGS in a given DB and returns result as an array of tables where keys are column names and values are associated value: ```lua local ok, db = luna.db.open("file:var/db.sqlite?_journal=WAL&_sync=NORMAL") -local ok, res = luna.db["query*"]( +local ok, res = luna.db.queryAssoc( db, "SELECT * FROM foobar WHERE name = '?'", {"hello!"} @@ -232,7 +234,7 @@ __luna.crypto.sha1(string)__: returns hex-encoded SHA1 hash of a STRING. -__luna.evalfn(handler)__: +__luna.onEval(handler)__: sets an eval handler for server commands. Handler accepts one argument TEXT. If the server is started with -D flag it starts listening for input from stdio. In this case if eval handler is set the handler will receive the user input and @@ -241,7 +243,7 @@ executed in Lua state as is. ``` local fennel = require("fennel") -luna.evalfn(function (text) fennel.eval(text, {env = _G}) end) +luna.onEval(function (text) fennel.eval(text, {env = _G}) end) ``` diff --git a/example/main.lua b/example/main.lua index 98e3ea8..335eaad 100644 --- a/example/main.lua +++ b/example/main.lua @@ -97,7 +97,7 @@ luna.router.static("GET /static/", "static/") -- luna stdio when it's run with -D flag. in this case we print the result of -- evaluation. eval handler is evaluated once for each worker so the result -- may be printed more than once -luna.evalfn(function (text) - local res = loadstring(text) - print(res) +luna.onEval(function (text) + local res = (_G.loadstring or _G.load)(text) + res() end) @@ -172,6 +172,7 @@ func main() { } return luaOk(l, url.QueryEscape(str)) } + httpModule["encodeURL"] = httpModule["encode-url"] // define luna.db module dbModule := make(map[string]any) @@ -261,6 +262,7 @@ func main() { } return luaOk(l, nil) } + dbModule["execTx"] = dbModule["exec-tx"] dbModule["exec"] = func (l *Lua) int { var file, query string var params []any @@ -319,7 +321,7 @@ func main() { l.yield() return luaOk(l, ares) } - dbModule["query*"] = func (l *Lua) int { + dbModule["query-assoc"] = func (l *Lua) int { var file, query string var params []any err := l.Scan(&file, &query, ¶ms) @@ -359,6 +361,7 @@ func main() { l.yield() return luaOk(l, ares) } + dbModule["queryAssoc"] = dbModule["query-assoc"] dbModule["close"] = func (l *Lua) int { var file string err := l.Scan(&file) @@ -424,7 +427,7 @@ func main() { module["db"] = dbModule module["utf8"] = utf8Module module["crypto"] = cryptoModule - module["evalfn"] = func (l *Lua) int { + module["on-eval"] = func (l *Lua) int { var evalFn LuaRef err := l.Scan(&evalFn) if err != nil { @@ -438,6 +441,7 @@ func main() { } return luaOk(l, nil) } + module["onEval"] = module["on-eval"] module["debug"] = debug wg := sync.WaitGroup{} |
