diff --git a/kclvm_test.go b/kclvm_test.go index e1216b50..07152ea3 100644 --- a/kclvm_test.go +++ b/kclvm_test.go @@ -68,7 +68,7 @@ func TestRunFiles(t *testing.T) { } func TestStreamResult(t *testing.T) { - file, err := filepath.Abs("./testdata/stream/main.k") + file, err := filepath.Abs("./testdata/stream/one_stream.k") if err != nil { t.Fatal(err) } @@ -76,6 +76,15 @@ func TestStreamResult(t *testing.T) { if err != nil { t.Fatal(err) } + assert2.Equal(t, "a: 1", result.GetRawYamlResult()) + file, err = filepath.Abs("./testdata/stream/two_stream.k") + if err != nil { + t.Fatal(err) + } + result, err = kcl.Run(file, kcl.WithSortKeys(true)) + if err != nil { + t.Fatal(err) + } assert2.Equal(t, "a: 1\n---\nb: 2", result.GetRawYamlResult()) } @@ -546,7 +555,7 @@ func TestWithExternalpkg(t *testing.T) { if err != nil { t.Fatal(err) } - assert2.Equal(t, "{\"a\": \"Hello External_1 World!\", \"b\": \"Hello External_2 World!\"}", result.GetRawJsonResult()) + assert2.Equal(t, "{\"a\":\"Hello External_1 World!\",\"b\":\"Hello External_2 World!\"}\n", result.GetRawJsonResult()) assert2.Equal(t, "a: Hello External_1 World!\nb: Hello External_2 World!", result.GetRawYamlResult()) } diff --git a/pkg/kcl/api.go b/pkg/kcl/api.go index 75ca8f71..a85e3e72 100644 --- a/pkg/kcl/api.go +++ b/pkg/kcl/api.go @@ -4,6 +4,7 @@ package kcl import ( + "bytes" "encoding/json" "errors" "fmt" @@ -350,14 +351,12 @@ func ExecResultToKCLResult(o *Option, resp *gpyrpc.ExecProgram_Result, logger io return &result, nil } - result.raw_yaml_result = resp.YamlResult - var mList []map[string]interface{} - if len(documents) == 1 { - if err := json.Unmarshal([]byte(resp.JsonResult), &mList); err != nil { + for _, d := range documents { + if err := yaml.Unmarshal([]byte(d), &mList); err != nil { err = nil - if err := json.Unmarshal([]byte(resp.JsonResult), &result.result); err != nil { + if err := yaml.Unmarshal([]byte(d), &result.result); err != nil { return nil, err } } @@ -377,38 +376,14 @@ func ExecResultToKCLResult(o *Option, resp *gpyrpc.ExecProgram_Result, logger io } } } - result.raw_json_result = resp.JsonResult - } else { - for _, d := range documents { - if err := yaml.Unmarshal([]byte(d), &mList); err != nil { - err = nil - if err := yaml.Unmarshal([]byte(d), &result.result); err != nil { - return nil, err - } - } - // Store raw result to KCLResult - if len(mList) == 0 && result.result != nil { - // Scalar or map result - m, err := result.ToMap() - if err == nil { - result.list = append(result.list, m) - } - } else { - // Stream result - result.list = make([]KCLResult, 0, len(mList)) - for _, m := range mList { - if len(m) != 0 { - result.list = append(result.list, m) - } - } - } - } - data, err := json.Marshal(result.list) - if err != nil { - return &result, nil - } - result.raw_json_result = string(data) } + buffer := bytes.NewBuffer(nil) + encoder := json.NewEncoder(buffer) + for _, m := range result.list { + encoder.Encode(m) + } + result.raw_json_result = buffer.String() + result.raw_yaml_result = resp.YamlResult return &result, nil } diff --git a/testdata/stream/one_stream.k b/testdata/stream/one_stream.k new file mode 100644 index 00000000..e3e97b32 --- /dev/null +++ b/testdata/stream/one_stream.k @@ -0,0 +1,5 @@ +import manifests + +manifests.yaml_stream([ + {a = 1} +]) diff --git a/testdata/stream/main.k b/testdata/stream/two_stream.k similarity index 100% rename from testdata/stream/main.k rename to testdata/stream/two_stream.k