Skip to content

Commit

Permalink
refactor: yaml stream format
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Feb 22, 2024
1 parent e4a1e43 commit e44d567
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 38 deletions.
13 changes: 11 additions & 2 deletions kclvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,23 @@ 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)
}
result, err := kcl.Run(file, kcl.WithSortKeys(true))
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())
}

Expand Down Expand Up @@ -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())
}

Expand Down
47 changes: 11 additions & 36 deletions pkg/kcl/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package kcl

import (
"bytes"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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
}

Expand Down
5 changes: 5 additions & 0 deletions testdata/stream/one_stream.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import manifests

manifests.yaml_stream([
{a = 1}
])
File renamed without changes.

0 comments on commit e44d567

Please sign in to comment.