GLU (glucose) - gopher-lua module extensions
- go 1.17 (as gopher-lua required):
under branch v1 with uri "github.com/ZenLiuCN/glu" - go 1.18 (with Generic):
under branch v2 with "github.com/ZenLiuCN/glu/v2" - go 1.18 (simplify API):
under branch v3 with "github.com/ZenLiuCN/glu/v3"
- √
gluthe core module:- Define helper
ModuleandTypefor easier register user library and user type; - Define global LState pool for reuse;
- Define Registry for
Modulars, with optional auto-injection; - Support
Help(string?)for help information;
- Define helper
- √
jsondynamic json library base on Jeffail/gabs - √
httphttp server and client library base on gorilla/mux, depends onjson - √
sqlxsqlx base on jmoiron/sqlx, depends onjson, new in versionv2.0.2
- usage
package sample
import (
"fmt"
"github.com/ZenLiuCN/glu/v3"
lua "github.com/yuin/gopher-lua"
)
func main() {
fmt.Println(DoSomeScript("1+2") == 3.0)
}
func DoSomeScript(script string) float64 {
vm := glu.Get()
defer glu.Put(vm)
if err := vm.DoString(script); err != nil {
panic(err)
}
return float64(vm.CheckNumber(1))
}- print help
local http=require('http')
local json=require('json')
print(json.help()) --will print comma split keyword list
print(http.help('?')) --will print module help
print(http.Server.help('?')) --will print type constructor help
for word in string.gmatch(http.Server.help(), '([^,]+)') do
print(http.Server.help(word)) --will print method constructor help
end
print(http.CTX.help('?'))
for word in string.gmatch(http.CTX.help(), '([^,]+)') do
print(http.CTX.Help(word))
end- http server
local http=require('http')
local server=http.Server.new(':8081') --new Server with listen address
function handle(c)
c:sendString(c:query('p'))
end
server:get('/',handle)
server:start(false)
while (true) do end- http client
local res,err=require('http').Client.new(5):get('http://github.com') print(err) if res:size()>0 then local txt=res:body() print(txt) end
-
offer your ideas
-
fork and pull
MIT as gopher-lua did
Those are record start at version 2.0.2
v2.0.2:- add module
sqlxwithsqlx.DB,sqlx.Result - add function
of(jsonString):Jsonin modulejson
- add module
v2.0.3:- adding
sqlx.Tx,sqlx.Stmt,sqlx.NamedStmtto modulesqlx
- adding
v2.0.4:- add
Json:get(string|number)to modulejson, which will replaceJson:at - add
x:execManyandx:queryManyto modulesqlx - add
sqlx.encB64andsqlx.decB64to modulesqlxfor numeric issue- when use pgx for postgresql there will no such needs.
- add
v2.0.5:- add
sqlx:to_numandsqlx:from_numto modulesqlx, which convert json array of objects numeric fields from|to binary
- add
Those are record start at version 3.0.1
v3.0.1:- simplify core api
Get: get a pooled VMPut: return a pooled VMRegister: register a modularNewModule: create a ModuleNewSimpleType: create a Simple TypeNewType: create a TypeNewTypeCast: create a Type with auto castmodular.AddFunc: add a function to Module or Typemodular.AddField: add a field to Module or Typemodular.AddFieldSupplier: add a field by supplier to Module or Typemodular.AddModule: add a sub-moduleType.New: create new instance and push to stackType.NewValue: create new instance onlyType.Cast: check if is auto cast TypeType.Check: check stack value is Type, must a Cast TypeType.CheckSelf: check top stack value is Type, must a Cast TypeType.CheckUserData: check user-data value is Type, must a Cast TypeType.Caster: the caster or nilType.AddMethod: add a MethodType.AddMethodUserData: add a Method use UserData as receiverType.AddMethodCast: add a Method use specific Type as receiver, must a Cast TypeType.Override: override a meta functionType.OverrideUserData: override a meta function use UserData as receiverType.OverrideCast: override a meta function use specific Type as receiver, must a Cast Type
jsongabs module to process JSONjson.stringify: convert JSON to json stringjson.parse: create JSON from json stringjson.of: create JSON from lua valueJSON.new: create new JSON from a json stringJSON:json: convert JSON to json stringJSON:path: fetch JSON element at pathJSON:exists: check JSON path existsJSON:get: fetch JSON element at path or index of arrayJSON:set: set JSON element at path or index of array, nil value will delete.JSON:type: get JSON element type at path.JSON:append: append JSON element at path, returns error message.JSON:isArray: check if JSON element at path is JSON array.JSON:isObject: check if JSON element at path is JSON Object.JSON:bool: fetch JSON element at path, which should be a boolean.JSON:string: fetch JSON element at path, which should be a string.JSON:number: fetch JSON element at path, which should be a number.JSON:size: fetch JSON size at path,if not array or object, returns nil.JSON:raw: fetch JSON element at path, and convert to lua value.tostring(JSON): convert JSON to json string.
http: mux module to access and serve httphttp.Server: the http serverhttp.Client: the http clienthttp.CTX: the http request contexthttp.Response: the http responseCTX:vars: path variables by nameCTX:header: request header by nameCTX:query: request query by nameCTX:method: request methodCTX:body: request body as JSONCTX:setHeader: set response headerCTX:status: set response statusCTX:sendJson: send JSON as response body and end processCTX:sendString: send string as response body and end processCTX:sendFile: send File as response body and end processServer.new: create new http.Server listen at addressServer:stop: shutdown http serverServer:running: check if server is runningServer:start: start server to listenServer:route: declare route without limit request methodServer:get: declare route for GET methodServer:post: declare route for POST methodServer:put: declare route for PUT methodServer:head: declare route for HEAD methodServer:patch: declare route for PATCH methodServer:delete: declare route for DELETE methodServer:connect: declare route for CONNECT methodServer:options: declare route for OPTIONS methodServer:trace: declare route for TRACE methodServer:files: declare route for serve with filesServer:release: free server resourcesServer.pool: server pool sizeServer.poolKeys: server pool keysServer.pooled: fetch server from pool by keyResponse:statusCode: response status codeResponse:status: response status textResponse:size: response content sizeResponse:header: response headersResponse:body: response body as stringResponse:bodyJson: response body as JSONClient.new: create new http clientClient:get: send GET requestClient:post: send POST requestClient:head: send HEAD requestClient:form: send POST request with formClient:request: send request with string dataClient:requestJson: send request with JSON dataClient:release: free client resourcesClient.pool: client pool sizeClient.poolKeys: client pool keysClient.pooled: get client from pool by key
sqlx: sqlx module to access databasesqlx.DB: sqlx databasesqlx.Tx: sqlx transactionsqlx.Stmt: sqlx prepared statementsqlx.NamedStmt: sqlx prepared named statementsqlx.Result: sql execute resultsqlx.connect: connect to databasesqlx.encB64: encode string to base64sqlx.decB64: decode base64 to stringsqlx.from_num: encode string of decimal to base64sqlx.to_num: decode base64 to string of decimalDB.new: connect to databaseDB:query: query SQL data as JSONDB:exec: execute SQL fetch ResultDB:queryMany: query SQL with batch of parametersDB:execMany: execute SQL with batch of parametersDB:begin: begin transactionDB:prepare: prepare statementDB:prepareNamed: prepare named statementDB:close: close databaseTx:query: query SQL data as JSONTx:exec: execute SQL fetch ResultTx:queryMany: query SQL with batch of parametersTx:execMany: execute SQL with batch of parametersTx:prepare: prepare statementTx:prepareNamed: prepare named statementTx:commit:commit transactionTx:rollback:rollback transactionStmt:query: query data as JSONStmt:exec: execute fetch ResultStmt:queryMany: query with batch of parametersStmt:execMany: execute with batch of parametersStmt:close: close statementNamedStmt:query: query data as JSONNamedStmt:exec: execute fetch ResultNamedStmt:queryMany: query with batch of parametersNamedStmt:execMany: execute with batch of parametersNamedStmt:close: close statementResult:lastID: last inserted IDResult:rows: affected rows
- simplify core api