Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use yaml.v2 to keep indent to 2 spaces #341

Merged
merged 1 commit into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/cmd/compile/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"path/filepath"

yamlv3 "gopkg.in/yaml.v3"
yamlv2 "gopkg.in/yaml.v2"

"kusionstack.io/kusion/pkg/cmd/spec"
"kusionstack.io/kusion/pkg/generator"
Expand Down Expand Up @@ -85,7 +85,7 @@ func (o *CompileOptions) Run() error {
}
}

yaml, err := yamlv3.Marshal(sp.Resources)
yaml, err := yamlv2.Marshal(sp.Resources)
if err != nil {
return err
}
Expand Down
38 changes: 16 additions & 22 deletions pkg/engine/kcl.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,39 @@
package engine

import (
"fmt"

yamlv2 "gopkg.in/yaml.v2"
yamlv3 "gopkg.in/yaml.v3"
"encoding/json"

kcl "kusionstack.io/kclvm-go"

"kusionstack.io/kusion/pkg/engine/models"
"kusionstack.io/kusion/pkg/log"
jsonUtil "kusionstack.io/kusion/pkg/util/json"
)

const MaxLogLength = 3751

func ResourcesYAML2Spec(resourcesYAML []kcl.KCLResult) (*models.Spec, error) {
resources := []models.Resource{}
func KCLResult2Spec(kclResults []kcl.KCLResult) (*models.Spec, error) {
SparkYuan marked this conversation as resolved.
Show resolved Hide resolved
resources := make([]models.Resource, len(kclResults))

for i, result := range kclResults {
// Marshal kcl result to bytes
bytes, err := json.Marshal(result)
if err != nil {
return nil, err
}

for _, resourcesYamlMap := range resourcesYAML {
// Convert kcl result to yaml string
msg := jsonUtil.MustMarshal2String(resourcesYamlMap)
msg := string(bytes)
if len(msg) > MaxLogLength {
msg = msg[0:MaxLogLength]
}

log.Infof("convertKCLResult2Resources resource:%v", msg)
// Using yamlv2.Marshal and yamlv3.Unmarshal is a workaround for the error "did not find expected '-' indicator" in unmarshalling yaml
yamlByte, err := yamlv2.Marshal(resourcesYamlMap)
if err != nil {
return nil, fmt.Errorf("yaml marshal failed. %v,%w", jsonUtil.MustMarshal2String(resourcesYamlMap), err)
}
log.Infof("convert kcl result to resource: %s", msg)

// Parse yaml string as Resource
item := &models.Resource{}
err = yamlv3.Unmarshal(yamlByte, item)
if err != nil {
// Parse json data as models.Resource
var item models.Resource
if err = json.Unmarshal(bytes, &item); err != nil {
return nil, err
}

resources = append(resources, *item)
resources[i] = item
}

return &models.Spec{Resources: resources}, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/generator/kcl/kcl_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (g *Generator) GenerateSpec(o *generator.Options, stack *projectstack.Stack
}

// convert Run result to spec
spec, err := engine.ResourcesYAML2Spec(compileResult.Documents)
spec, err := engine.KCLResult2Spec(compileResult.Documents)
if err != nil {
return nil, err
}
Expand Down