Skip to content

Commit

Permalink
fix: yaml stream format convension in the kcl generator (#310)
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <xpf6677@163.com>
  • Loading branch information
Peefy authored May 21, 2024
1 parent b58231c commit 0d1338f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
6 changes: 3 additions & 3 deletions pkg/kcl/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ func ExecResultToKCLResult(o *Option, resp *gpyrpc.ExecProgram_Result, logger io
return &result, nil
}

documents, err := splitDocuments(resp.YamlResult)
documents, err := SplitDocuments(resp.YamlResult)
if err != nil {
return &result, nil
}
Expand Down Expand Up @@ -486,10 +486,10 @@ func run(pathList []string, opts ...Option) (*KCLResultList, error) {
return runWithHooks(pathList, DefaultHooks, opts...)
}

// splitDocuments returns a slice of all documents contained in a YAML string. Multiple documents can be divided by the
// SplitDocuments returns a slice of all documents contained in a YAML string. Multiple documents can be divided by the
// YAML document separator (---). It allows for white space and comments to be after the separator on the same line,
// but will return an error if anything else is on the line.
func splitDocuments(s string) ([]string, error) {
func SplitDocuments(s string) ([]string, error) {
docs := make([]string, 0)
if len(s) > 0 {
// The YAML document separator is any line that starts with ---
Expand Down
11 changes: 8 additions & 3 deletions pkg/tools/gen/genkcl_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package gen

import (
"bytes"
"github.com/goccy/go-yaml"
"io"
"strings"

"github.com/goccy/go-yaml"
"kcl-lang.io/kcl-go/pkg/kcl"
)

func (k *kclGenerator) genKclFromYaml(w io.Writer, filename string, src interface{}) error {
Expand Down Expand Up @@ -58,7 +59,11 @@ func convertKclFromYamlString(byteData []byte) ([]data, error) {
byteData = bytes.ReplaceAll(byteData, []byte("\r\n"), []byte("\n"))
var result []data
// split yaml with ‘---’
for _, item := range strings.Split(string(byteData), "---") {
items, err := kcl.SplitDocuments(string(byteData))
if err != nil {
return nil, err
}
for _, item := range items {
yamlData := &yaml.MapSlice{}
if err := yaml.UnmarshalWithOptions([]byte(item), yamlData, yaml.UseOrderedMap()); err != nil {
return nil, err
Expand Down
8 changes: 8 additions & 0 deletions pkg/tools/gen/testdata/yaml/comment/expect.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""
This file was generated by the KCL auto-gen tool. DO NOT EDIT.
Editing this file might prove futile when you re-run the KCL auto-gen generate command.
"""

{
"cluster.name" = "my-application"
}
2 changes: 2 additions & 0 deletions pkg/tools/gen/testdata/yaml/comment/input.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# ---------------------------------- Cluster -----------------------------------
cluster.name: my-application

0 comments on commit 0d1338f

Please sign in to comment.