GithubHelp home page GithubHelp logo

goluago's Introduction

GoDoc

Lua wrappers for the Go standard library

Wraps Go's standard libraries so they can be used with the go-lua Lua VM implementation.

Most of the packages under pkg expose a single function Open(l *lua.State) that makes the corresponding Go package available to Lua scripts. For example:

import "github.com/Shopify/goluago/pkg/strings"
...
strings.Open(l)
...

allows Lua scripts loaded by l to:

require("goluago/strings")
strings.trim("loll ")
strings.split("cat,dog,elephant,walrus", ",")
strings.replace("oink oink oink", "k", "ky", 2)

To make all supported APIs available to Lua:

import "github.com/Shopify/goluago"
...
goluago.Open(l)
...

The "github.com/Shopify/goluago/util" package provides helper functions to push Go values onto the Lua stack, pull tables of string->string or string->value from the Lua stack to Go, and support variadic arguments.

License

goluago is licensed under the MIT license.

goluago's People

Contributors

abecevello avatar aleksi avatar aybabtme avatar cursedcoder avatar dianadevasia avatar etiennemartin avatar fbogsany avatar graemej avatar jpcaissy avatar maximebedard avatar pierluc-codes avatar simon-shopify avatar tmlayton avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

goluago's Issues

`json.marshal` converts floats to ints

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}.

attempt to index trim 'local' (a nil value)

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.

Create capability of an in-memory zip from a file

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 {
   ...
}

interface conversion error when trying to `PullTable`

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?

json.Marshal can't decode arrays: "invalid key to 'next'"

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).

Issue

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'`

Goal

Teach json.marshal to detect tables that are intended to model arrays, and decode them into the appropriate [1, 2, 3] JSON form.

Workaround

It ain't pretty:

local changeset = '['
      .. table.concat({
        json.marshal(config.add_variant_change),
        json.marshal(config.add_shipping_line_change),
      }, ",")
  .. ']'

Push arbitrary Go types on Lua stack

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())

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.