shopify / goluago Goto Github PK
View Code? Open in Web Editor NEWLua wrappers for the Go standard library
License: MIT License
Lua wrappers for the Go standard library
License: MIT License
Lua's table
type models both "arrays" like { 1, 2, 3 }
(accessed by index) and "dictionaries" like { a = 1, b = 2, c = 3}
(accessed by key).
json.mashal can handle decoding a table
like a dictionary, but it fails to decode a table like an array, printing the error invalid key to 'next'
.
Here's a simple reproducible example:
payload = {
a_key = { 1, 2, 3 } -- the value is a table, but it's meant to be json-encoded as an array.
}
json.marshal(payload) -- ๐ฅ `invalid key to 'next'`
Teach json.marshal
to detect tables that are intended to model arrays, and decode them into the appropriate [1, 2, 3]
JSON form.
It ain't pretty:
local changeset = '['
.. table.concat({
json.marshal(config.add_variant_change),
json.marshal(config.add_shipping_line_change),
}, ",")
.. ']'
Add a sticker in the README.
See: https://coveralls.io/ as an example
To start off, I know I am doing something wrong. My usage of PullTable
is based off the work done with encoding JSON support.
The value PullTable
is trying to get would be the last return
in the execution of the script.
When I try, I get the following error:
panic: interface conversion: lua.value is nil, not *lua.luaClosure
goroutine 1 [running]:
github.com/Shopify/go-lua.(*State).prototype(...)
/Users/pivotal/go/pkg/mod/github.com/!shopify/[email protected]/debug.go:14
github.com/Shopify/go-lua.(*State).currentLine(...)
/Users/pivotal/go/pkg/mod/github.com/!shopify/[email protected]/debug.go:17
github.com/Shopify/go-lua.(*State).runtimeError(0xc0000d8000, 0x116fafe, 0x15)
/Users/pivotal/go/pkg/mod/github.com/!shopify/[email protected]/debug.go:43 +0x2bc
github.com/Shopify/go-lua.(*State).next(0xc0000d8000, 0xc0000a23c0, 0x4, 0xc0000e2360)
/Users/pivotal/go/pkg/mod/github.com/!shopify/[email protected]/tables.go:230 +0x6eb
github.com/Shopify/go-lua.(*State).Next(0xc0000d8000, 0x3, 0x12821b1)
/Users/pivotal/go/pkg/mod/github.com/!shopify/[email protected]/lua.go:1193 +0x81
github.com/Shopify/goluago/util.pullTableRec(0xc0000d8000, 0xffffffffffffffff, 0x5, 0xc00009e320, 0xf, 0xc000094500)
/Users/pivotal/go/pkg/mod/github.com/!shopify/[email protected]/util/deep_pull.go:63 +0x11d
github.com/Shopify/goluago/util.toGoValue(0xc0000d8000, 0xffffffffffffffff, 0xc00009e320, 0xf, 0x10cdb01, 0xc0000d8000)
/Users/pivotal/go/pkg/mod/github.com/!shopify/[email protected]/util/deep_pull.go:143 +0x246
github.com/Shopify/goluago/util.pullTableRec(0xc0000d8000, 0x1, 0x5, 0xc0000d8000, 0x1, 0xffffffffffffffff)
/Users/pivotal/go/pkg/mod/github.com/!shopify/[email protected]/util/deep_pull.go:72 +0x17b
github.com/Shopify/goluago/util.PullTable(0xc0000d8000, 0x1, 0x5, 0x0, 0x0, 0xc00009e258)
/Users/pivotal/go/pkg/mod/github.com/!shopify/[email protected]/util/deep_pull.go:47 +0x1ef
main.main()
The source to reproduce this problem:
package main
import (
"github.com/Shopify/go-lua"
"github.com/Shopify/goluago/util"
"log"
"os"
)
func main() {
l := lua.NewState()
source := `
local my_map = {
key1="value1",
key2="value2",
}
local some_list = {my_map, "item2"}
return({list_with_a_map=some_list})
`
if err := lua.DoString(l, source); err != nil {
panic(err)
}
index := 1
if !l.IsTable(index) {
log.Println("the last return value of the script must be a table")
os.Exit(1)
}
_, err := util.PullTable(l, index)
if err != nil {
log.Panicln("getting table: %s", err)
}
}
Any insight to what I am doing wrong?
Should be able to push arbitrary types on Lua, such as structs with fields and methods. For instances:
func (l *lua.State) int {
u, err := url.Parse(...)
if err != nil { ... }
return lua.DeepPush(l, u)
}
Would expose all the fields and methods of var u url.URL
. Changing the fields from lua would change the fields in the actual u
on the Go side, so that u.String()
would reflect the changes made in lua:
u = url.parse("http://bing.com/search?q=dotnet")
u.scheme = "https"
u.host = "google.com"
q = url.query()
q.set("q", "golang")
url.rawQuery = q.encode()
equals("can change string", "https://google.com/search?q=golang", u.string())
Hi
I try to use goluago library, but I got the error:
runtime error: scripts/gointeroperate.lua:3: attempt to index trim 'local' (a nil value)
The lua script is:
require("goluago/strings")
strings.trim("loll ")
Invoking the script like this:
package main
import (
"gitlab.meitu.com/pl_storage/atlas/atlas-devops/data"
"github.com/Shopify/go-lua"
"github.com/Shopify/goluago"
"fmt"
)
type LuaProcessor struct {
L *lua.State
Script string
}
// script: the lua script file name
func NewLuaProcessor(script string) *LuaProcessor {
l := lua.NewState()
lua.OpenLibraries(l)
goluago.Open(l)
return &LuaProcessor{L:l, Script:script}
}
func (processor *LuaProcessor) Process(taskIndex int, record data.Record) (size int32, err error) {
if err := lua.DoFile(processor.L, processor.Script); err != nil {
fmt.Print(err)
}
return 0, nil
}
func ExampleLuaProcessor_GoInteroperate() {
processor := NewLuaProcessor("scripts/gointeroperate.lua")
processor.Process(1, nil)
//Output:Hello World from Lua file!
}
Any helps? thanks.
Currently we only support importing goluago modules by doing the following:
local hmac2 = require("goluago/crypto/hmac")
We also want to support this by using dot syntax as shown below:
local hmac2 = require("goluago.crypto.hmac")
We can support both by doing the following in the goluago Open function body:
goluago/pkg/crypto/hmac/hmac.go
Line 17 in 976d1ef
@camilo do it, DO IT!
A feature was requested to be able to generate a zip from a specific file. We should add a method that takes in a string based path and a file system. From those two params, we should create an in-memory zip that's returned back into the Lua runtime.
-- Example call, nil returned if it failed
data = zip("path")
// Sample golang based method to do this (include lua runtime somewhere)
func zip(path string, http.FileSystem) []byte, error {
...
}
To reproduce -
local json = require('goluago/encoding/json')
print(json.marshal({
foo = 1.01
}))
The output will be {"foo":1}
when it should be {"foo":1.01}
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.