Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lua package for yaml support #6545

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions pkg/actions/lua/encoding/yaml/yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package yaml

import (
"bytes"

"github.com/Shopify/go-lua"
"github.com/treeverse/lakefs/pkg/actions/lua/util"
"gopkg.in/yaml.v3"
)

func Open(l *lua.State) {
yamlOpen := func(l *lua.State) int {
lua.NewLibrary(l, yamlLibrary)
return 1
}
lua.Require(l, "encoding/yaml", yamlOpen, false)
l.Pop(1)
}

var yamlLibrary = []lua.RegistryFunction{
{Name: "marshal", Function: yamlMarshal},
{Name: "unmarshal", Function: yamlUnmarshal},
}

func check(l *lua.State, err error) {
if err != nil {
lua.Errorf(l, err.Error())
panic("unreachable")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How expensive is this? And... are we sure we are exception-safe and defer x.Close() for all our Closers along the stack?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lua implementation we are using implemented the error handling using raise (panic), as described https://pkg.go.dev/github.com/Shopify/go-lua#Errorf.

Can go over the current provided files and lookup for resource management issues.

}
}

func yamlMarshal(l *lua.State) int {
var t interface{}
var err error
if !l.IsNil(1) {
t, err = util.PullTable(l, 1)
check(l, err)
}
var buf bytes.Buffer
e := yaml.NewEncoder(&buf)
err = e.Encode(t)
check(l, err)
l.PushString(buf.String())
return 1
}

func yamlUnmarshal(l *lua.State) int {
payload := lua.CheckString(l, 1)
var output interface{}
check(l, yaml.Unmarshal([]byte(payload), &output))
return util.DeepPush(l, output)
}
2 changes: 2 additions & 0 deletions pkg/actions/lua/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/treeverse/lakefs/pkg/actions/lua/encoding/hex"
"github.com/treeverse/lakefs/pkg/actions/lua/encoding/json"
"github.com/treeverse/lakefs/pkg/actions/lua/encoding/parquet"
"github.com/treeverse/lakefs/pkg/actions/lua/encoding/yaml"
"github.com/treeverse/lakefs/pkg/actions/lua/net/http"
"github.com/treeverse/lakefs/pkg/actions/lua/path"
"github.com/treeverse/lakefs/pkg/actions/lua/regexp"
Expand All @@ -28,6 +29,7 @@ func Open(l *lua.State, ctx context.Context, cfg OpenSafeConfig) {
regexp.Open(l)
strings.Open(l)
json.Open(l)
yaml.Open(l)
time.Open(l)
hmac.Open(l)
base64.Open(l)
Expand Down