Skip to content

Commit

Permalink
fix: lua encode structural error (#209)
Browse files Browse the repository at this point in the history
Signed-off-by: acejilam <[email protected]>
  • Loading branch information
ls-2018 authored Sep 6, 2024
1 parent d261313 commit f0363f2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/util/luamanager/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (j jsonValue) MarshalJSON() (data []byte, err error) {

switch key.Type() {
case lua.LTNil: // empty table
data = []byte(`[]`)
data = []byte(`null`)
case lua.LTNumber:
arr := make([]jsonValue, 0, converted.Len())
expectedKey := lua.LNumber(1)
Expand Down
41 changes: 41 additions & 0 deletions pkg/util/luamanager/lua_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ package luamanager
import (
"encoding/json"
"fmt"
"reflect"
"testing"

rolloutv1alpha1 "github.com/openkruise/rollouts/api/v1alpha1"
"github.com/openkruise/rollouts/pkg/util"
lua "github.com/yuin/gopher-lua"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
luajson "layeh.com/gopher-json"
Expand Down Expand Up @@ -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")
}
}
}

0 comments on commit f0363f2

Please sign in to comment.