2023-10-03 05:22:23 +00:00
|
|
|
//go:build !yq_nolua
|
|
|
|
|
2023-10-03 05:00:51 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math"
|
|
|
|
|
|
|
|
lua "github.com/yuin/gopher-lua"
|
|
|
|
)
|
|
|
|
|
|
|
|
type luaDecoder struct {
|
|
|
|
reader io.Reader
|
|
|
|
finished bool
|
|
|
|
prefs LuaPreferences
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLuaDecoder(prefs LuaPreferences) Decoder {
|
|
|
|
return &luaDecoder{
|
|
|
|
prefs: prefs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dec *luaDecoder) Init(reader io.Reader) error {
|
|
|
|
dec.reader = reader
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func (dec *luaDecoder) convertToYamlNode(ls *lua.LState, lv lua.LValue) *CandidateNode {
|
2023-10-03 05:00:51 +00:00
|
|
|
switch lv.Type() {
|
|
|
|
case lua.LTNil:
|
2023-10-18 01:11:53 +00:00
|
|
|
return &CandidateNode{
|
|
|
|
Kind: ScalarNode,
|
2023-10-03 05:00:51 +00:00
|
|
|
Tag: "!!null",
|
|
|
|
Value: "",
|
|
|
|
}
|
|
|
|
case lua.LTBool:
|
2023-10-18 01:11:53 +00:00
|
|
|
return &CandidateNode{
|
|
|
|
Kind: ScalarNode,
|
2023-10-03 05:00:51 +00:00
|
|
|
Tag: "!!bool",
|
|
|
|
Value: lv.String(),
|
|
|
|
}
|
|
|
|
case lua.LTNumber:
|
|
|
|
n := float64(lua.LVAsNumber(lv))
|
|
|
|
// various special case floats
|
|
|
|
if math.IsNaN(n) {
|
2023-10-18 01:11:53 +00:00
|
|
|
return &CandidateNode{
|
|
|
|
Kind: ScalarNode,
|
2023-10-03 05:00:51 +00:00
|
|
|
Tag: "!!float",
|
|
|
|
Value: ".nan",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if math.IsInf(n, 1) {
|
2023-10-18 01:11:53 +00:00
|
|
|
return &CandidateNode{
|
|
|
|
Kind: ScalarNode,
|
2023-10-03 05:00:51 +00:00
|
|
|
Tag: "!!float",
|
|
|
|
Value: ".inf",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if math.IsInf(n, -1) {
|
2023-10-18 01:11:53 +00:00
|
|
|
return &CandidateNode{
|
|
|
|
Kind: ScalarNode,
|
2023-10-03 05:00:51 +00:00
|
|
|
Tag: "!!float",
|
|
|
|
Value: "-.inf",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// does it look like an integer?
|
|
|
|
if n == float64(int(n)) {
|
2023-10-18 01:11:53 +00:00
|
|
|
return &CandidateNode{
|
|
|
|
Kind: ScalarNode,
|
2023-10-03 05:00:51 +00:00
|
|
|
Tag: "!!int",
|
|
|
|
Value: lv.String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
return &CandidateNode{
|
|
|
|
Kind: ScalarNode,
|
2023-10-03 05:00:51 +00:00
|
|
|
Tag: "!!float",
|
|
|
|
Value: lv.String(),
|
|
|
|
}
|
|
|
|
case lua.LTString:
|
2023-10-18 01:11:53 +00:00
|
|
|
return &CandidateNode{
|
|
|
|
Kind: ScalarNode,
|
2023-10-03 05:00:51 +00:00
|
|
|
Tag: "!!str",
|
|
|
|
Value: lv.String(),
|
|
|
|
}
|
|
|
|
case lua.LTFunction:
|
2023-10-18 01:11:53 +00:00
|
|
|
return &CandidateNode{
|
|
|
|
Kind: ScalarNode,
|
2023-10-03 05:00:51 +00:00
|
|
|
Tag: "tag:lua.org,2006,function",
|
|
|
|
Value: lv.String(),
|
|
|
|
}
|
|
|
|
case lua.LTTable:
|
|
|
|
// Simultaneously create a sequence and a map, pick which one to return
|
|
|
|
// based on whether all keys were consecutive integers
|
|
|
|
i := 1
|
2023-10-18 01:11:53 +00:00
|
|
|
yaml_sequence := &CandidateNode{
|
|
|
|
Kind: SequenceNode,
|
2023-10-03 05:00:51 +00:00
|
|
|
Tag: "!!seq",
|
|
|
|
}
|
2023-10-18 01:11:53 +00:00
|
|
|
yaml_map := &CandidateNode{
|
|
|
|
Kind: MappingNode,
|
2023-10-03 05:00:51 +00:00
|
|
|
Tag: "!!map",
|
|
|
|
}
|
|
|
|
t := lv.(*lua.LTable)
|
|
|
|
k, v := ls.Next(t, lua.LNil)
|
|
|
|
for k != lua.LNil {
|
|
|
|
if ki, ok := k.(lua.LNumber); i != 0 && ok && math.Mod(float64(ki), 1) == 0 && int(ki) == i {
|
|
|
|
i++
|
|
|
|
} else {
|
|
|
|
i = 0
|
|
|
|
}
|
2023-10-18 01:11:53 +00:00
|
|
|
newKey := dec.convertToYamlNode(ls, k)
|
|
|
|
|
2023-10-03 05:00:51 +00:00
|
|
|
yv := dec.convertToYamlNode(ls, v)
|
2023-10-18 01:11:53 +00:00
|
|
|
yaml_map.AddKeyValueChild(newKey, yv)
|
|
|
|
|
2023-10-03 05:00:51 +00:00
|
|
|
if i != 0 {
|
2023-10-18 01:11:53 +00:00
|
|
|
yaml_sequence.AddChild(yv)
|
2023-10-03 05:00:51 +00:00
|
|
|
}
|
|
|
|
k, v = ls.Next(t, k)
|
|
|
|
}
|
|
|
|
if i != 0 {
|
|
|
|
return yaml_sequence
|
|
|
|
}
|
|
|
|
return yaml_map
|
|
|
|
default:
|
2023-10-18 01:11:53 +00:00
|
|
|
return &CandidateNode{
|
|
|
|
Kind: ScalarNode,
|
2023-10-03 05:00:51 +00:00
|
|
|
LineComment: fmt.Sprintf("Unhandled Lua type: %s", lv.Type().String()),
|
|
|
|
Tag: "!!null",
|
|
|
|
Value: lv.String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func (dec *luaDecoder) decideTopLevelNode(ls *lua.LState) *CandidateNode {
|
2023-10-03 05:00:51 +00:00
|
|
|
if ls.GetTop() == 0 {
|
|
|
|
// no items were explicitly returned, encode the globals table instead
|
|
|
|
return dec.convertToYamlNode(ls, ls.Get(lua.GlobalsIndex))
|
|
|
|
}
|
|
|
|
return dec.convertToYamlNode(ls, ls.Get(1))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dec *luaDecoder) Decode() (*CandidateNode, error) {
|
|
|
|
if dec.finished {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
ls := lua.NewState(lua.Options{SkipOpenLibs: true})
|
|
|
|
defer ls.Close()
|
|
|
|
fn, err := ls.Load(dec.reader, "@input")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ls.Push(fn)
|
|
|
|
err = ls.PCall(0, lua.MultRet, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
firstNode := dec.decideTopLevelNode(ls)
|
|
|
|
dec.finished = true
|
2023-10-18 01:11:53 +00:00
|
|
|
return firstNode, nil
|
2023-10-03 05:00:51 +00:00
|
|
|
}
|