diff --git a/pkg/actions/lua/encoding/yaml/yaml.go b/pkg/actions/lua/encoding/yaml/yaml.go new file mode 100644 index 00000000000..f6bf11aaa68 --- /dev/null +++ b/pkg/actions/lua/encoding/yaml/yaml.go @@ -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") + } +} + +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) +} diff --git a/pkg/actions/lua/open.go b/pkg/actions/lua/open.go index 3ac85d6f88e..2a40bd4ec1d 100644 --- a/pkg/actions/lua/open.go +++ b/pkg/actions/lua/open.go @@ -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" @@ -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)