Skip to content

Commit

Permalink
error dont panic, time
Browse files Browse the repository at this point in the history
  • Loading branch information
serprex committed May 5, 2024
1 parent 5a6959f commit d69953b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
63 changes: 43 additions & 20 deletions flow/pua/peerdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pua
import (
"bytes"
"fmt"
"math"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -137,6 +138,20 @@ func LuaRowIndex(ls *lua.LState) int {
return 1
}

func LVAsTime(ls *lua.LState, lv lua.LValue) time.Time {
switch v := lv.(type) {
case lua.LNumber:
ipart, fpart := math.Modf(float64(v))
return time.Unix(int64(ipart), int64(fpart*1e9))
case *lua.LUserData:
if tm, ok := v.Value.(time.Time); ok {
return tm
}
}
ls.RaiseError("Cannot convert %T to time.Time", lv)
return time.Time{}
}

func LuaRowNewIndex(ls *lua.LState) int {
_, row := LuaRow.Check(ls, 1)
key := ls.CheckString(2)
Expand Down Expand Up @@ -188,18 +203,16 @@ func LuaRowNewIndex(ls *lua.LState) int {
}
case qvalue.QValueKindString:
newqv = qvalue.QValueString{Val: lua.LVAsString(val)}
/* TODO time
case qvalue.QValueKindTimestamp:
newqv = qvalue.QValueTimestamp{Val:}
case qvalue.QValueKindTimestampTZ:
newqv = qvalue.QValueTimestampTZ{Val:}
case qvalue.QValueKindDate:
newqv = qvalue.QValueDate{Val:}
case qvalue.QValueKindTime:
newqv = qvalue.QValueTime{Val:}
case qvalue.QValueKindTimeTZ:
newqv = qvalue.QValueTimeTZ{Val:}
*/
case qvalue.QValueKindTimestamp:
newqv = qvalue.QValueTimestamp{Val: LVAsTime(ls, val)}
case qvalue.QValueKindTimestampTZ:
newqv = qvalue.QValueTimestampTZ{Val: LVAsTime(ls, val)}
case qvalue.QValueKindDate:
newqv = qvalue.QValueDate{Val: LVAsTime(ls, val)}
case qvalue.QValueKindTime:
newqv = qvalue.QValueTime{Val: LVAsTime(ls, val)}
case qvalue.QValueKindTimeTZ:
newqv = qvalue.QValueTimeTZ{Val: LVAsTime(ls, val)}
case qvalue.QValueKindNumeric:
if ud, ok := val.(*lua.LUserData); ok {
if num, ok := ud.Value.(decimal.Decimal); ok {
Expand Down Expand Up @@ -266,14 +279,24 @@ func LuaRowNewIndex(ls *lua.LState) int {
}),
}
}
/* TODO TIME
case qvalue.QValueKindArrayDate:
newqv = qvalue.QValueArrayDate{Val:}
case qvalue.QValueKindArrayTimestamp:
newqv = qvalue.QValueArrayTimestamp{Val:}
case qvalue.QValueKindArrayTimestampTZ:
newqv = qvalue.QValueArrayTimestampTZ{Val:}
*/
case qvalue.QValueKindArrayDate:
if tbl, ok := val.(*lua.LTable); ok {
newqv = qvalue.QValueArrayDate{
Val: shared.LTableToSlice(ls, tbl, LVAsTime),
}
}
case qvalue.QValueKindArrayTimestamp:
if tbl, ok := val.(*lua.LTable); ok {
newqv = qvalue.QValueArrayDate{
Val: shared.LTableToSlice(ls, tbl, LVAsTime),
}
}
case qvalue.QValueKindArrayTimestampTZ:
if tbl, ok := val.(*lua.LTable); ok {
newqv = qvalue.QValueArrayDate{
Val: shared.LTableToSlice(ls, tbl, LVAsTime),
}
}
case qvalue.QValueKindArrayBoolean:
if tbl, ok := val.(*lua.LTable); ok {
newqv = qvalue.QValueArrayBoolean{
Expand Down
3 changes: 2 additions & 1 deletion flow/pua/stream_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func AttachToStream(ls *lua.LState, lfn *lua.LFunction, stream *model.QRecordStr
ls.Push(lfn)
ls.Push(LuaRow.New(ls, row))
if err := ls.PCall(1, 0, nil); err != nil {
panic(err.Error()) // TODO error handling
output.Close(err)
return
}
for i, field := range schema.Fields {
record[i] = row.GetColumnValue(field.Name)
Expand Down

0 comments on commit d69953b

Please sign in to comment.