fix: lua encode structural error (#209)
Signed-off-by: acejilam <acejilam@gmail.com>
This commit is contained in:
parent
d2613132aa
commit
f0363f28c0
|
|
@ -108,7 +108,7 @@ func (j jsonValue) MarshalJSON() (data []byte, err error) {
|
||||||
|
|
||||||
switch key.Type() {
|
switch key.Type() {
|
||||||
case lua.LTNil: // empty table
|
case lua.LTNil: // empty table
|
||||||
data = []byte(`[]`)
|
data = []byte(`null`)
|
||||||
case lua.LTNumber:
|
case lua.LTNumber:
|
||||||
arr := make([]jsonValue, 0, converted.Len())
|
arr := make([]jsonValue, 0, converted.Len())
|
||||||
expectedKey := lua.LNumber(1)
|
expectedKey := lua.LNumber(1)
|
||||||
|
|
|
||||||
|
|
@ -19,11 +19,13 @@ package luamanager
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
rolloutv1alpha1 "github.com/openkruise/rollouts/api/v1alpha1"
|
rolloutv1alpha1 "github.com/openkruise/rollouts/api/v1alpha1"
|
||||||
"github.com/openkruise/rollouts/pkg/util"
|
"github.com/openkruise/rollouts/pkg/util"
|
||||||
lua "github.com/yuin/gopher-lua"
|
lua "github.com/yuin/gopher-lua"
|
||||||
|
v1 "k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
luajson "layeh.com/gopher-json"
|
luajson "layeh.com/gopher-json"
|
||||||
|
|
@ -152,3 +154,42 @@ func TestRunLuaScript(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func TestEmptyMetadata(t *testing.T) {
|
||||||
|
script := `
|
||||||
|
local x = obj
|
||||||
|
return x `
|
||||||
|
pod := v1.Pod{
|
||||||
|
Spec: v1.PodSpec{
|
||||||
|
Containers: []v1.Container{
|
||||||
|
{
|
||||||
|
Name: "centos",
|
||||||
|
Image: "centos:7",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
unObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&pod)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
u := &unstructured.Unstructured{Object: unObj}
|
||||||
|
l, err := new(LuaManager).RunLuaScript(u, script)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
returnValue := l.Get(-1)
|
||||||
|
if returnValue.Type() == lua.LTTable {
|
||||||
|
jsonBytes, err := Encode(returnValue)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
p := v1.Pod{}
|
||||||
|
err = json.Unmarshal(jsonBytes, &p)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(pod, p) {
|
||||||
|
t.Fatal("return not equal before call")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue