Skip to content

Commit

Permalink
fix distinction between global/environment, use raw api
Browse files Browse the repository at this point in the history
  • Loading branch information
serprex committed Mar 12, 2024
1 parent 10906e9 commit dae2af6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
4 changes: 3 additions & 1 deletion flow/connectors/kafka/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,11 @@ func (c *KafkaConnector) SyncRecords(ctx context.Context, req *model.SyncRecords
if err != nil {
return nil, fmt.Errorf("error executing script %s: %w", req.Script, err)
}
// discard results, for now rely on globals instead of script returning anything
ls.SetTop(0)

var ok bool
fn, ok = ls.GetGlobal("onRecord").(*lua.LFunction)
fn, ok = ls.Env.RawGetString("onRecord").(*lua.LFunction)
if !ok {
return nil, errors.New("script should define `onRecord` function")
}
Expand Down
24 changes: 12 additions & 12 deletions flow/pua/peerdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,25 @@ var (
func RegisterTypes(ls *lua.LState) {
// gopher-lua provides 2 loaders {preload, file}
// overwrite file loader with one retrieving scripts from database
loaders := ls.GetField(ls.Get(lua.RegistryIndex), "_LOADERS").(*lua.LTable)
ls.RawSetInt(loaders, 2, ls.NewFunction(LoadPeerdbScript))
loaders := ls.G.Registry.RawGetString("_LOADERS").(*lua.LTable)
loaders.RawSetInt(2, ls.NewFunction(LoadPeerdbScript))

mt := LuaRecord.NewMetatable(ls)
ls.SetField(mt, "__index", ls.NewFunction(LuaRecordIndex))
mt.RawSetString("__index", ls.NewFunction(LuaRecordIndex))

mt = LuaRow.NewMetatable(ls)
ls.SetField(mt, "__index", ls.NewFunction(LuaRowIndex))
ls.SetField(mt, "__len", ls.NewFunction(LuaRowLen))
mt.RawSetString("__index", ls.NewFunction(LuaRowIndex))
mt.RawSetString("__len", ls.NewFunction(LuaRowLen))

mt = LuaQValue.NewMetatable(ls)
ls.SetField(mt, "__index", ls.NewFunction(LuaQValueIndex))
ls.SetField(mt, "__len", ls.NewFunction(LuaQValueLen))
mt.RawSetString("__index", ls.NewFunction(LuaQValueIndex))
mt.RawSetString("__len", ls.NewFunction(LuaQValueLen))

peerdb := ls.NewTable()
ls.SetField(peerdb, "RowToJSON", ls.NewFunction(LuaRowToJSON))
ls.SetField(peerdb, "RowColumns", ls.NewFunction(LuaRowColumns))
ls.SetField(peerdb, "UnixNow", ls.NewFunction(LuaUnixNow))
ls.SetGlobal("peerdb", peerdb)
peerdb.RawSetString("RowToJSON", ls.NewFunction(LuaRowToJSON))
peerdb.RawSetString("RowColumns", ls.NewFunction(LuaRowColumns))
peerdb.RawSetString("UnixNow", ls.NewFunction(LuaUnixNow))
ls.Env.RawSetString("peerdb", peerdb)
}

func LoadPeerdbScript(ls *lua.LState) int {
Expand Down Expand Up @@ -107,7 +107,7 @@ func LuaRowColumns(ls *lua.LState) int {
_, row := LuaRow.Check(ls, 1)
tbl := ls.CreateTable(len(row.ColToValIdx), 0)
for col, idx := range row.ColToValIdx {
ls.RawSetInt(tbl, idx, lua.LString(col))
tbl.RawSetInt(idx, lua.LString(col))
}
ls.Push(tbl)
return 1
Expand Down

0 comments on commit dae2af6

Please sign in to comment.