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

feat: impl kcl config generation and add indent block for JSON/YAML inputs #209

Merged
merged 2 commits into from
Jan 14, 2024
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
7 changes: 5 additions & 2 deletions pkg/tools/gen/genkcl_json.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package gen

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

"github.com/goccy/go-yaml"
)

func (k *kclGenerator) genKclFromJsonData(w io.Writer, filename string, src interface{}) error {
Expand All @@ -22,5 +23,7 @@ func (k *kclGenerator) genKclFromJsonData(w io.Writer, filename string, src inte
result := convertKclFromYaml(yamlData)

// generate kcl code
return k.genKcl(w, kclFile{Data: result})
return k.genKcl(w, kclFile{Config: []config{
{Data: result},
}})
}
4 changes: 3 additions & 1 deletion pkg/tools/gen/genkcl_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ func (k *kclGenerator) genKclFromYaml(w io.Writer, filename string, src interfac
result := convertKclFromYaml(yamlData)

// generate kcl code
return k.genKcl(w, kclFile{Data: result})
return k.genKcl(w, kclFile{Config: []config{
{Data: result},
}})
}

func convertKclFromYaml(yamlData *yaml.MapSlice) []data {
Expand Down
3 changes: 3 additions & 0 deletions pkg/tools/gen/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
)

var (
//go:embed templates/kcl/config.gotmpl
configTmpl string
//go:embed templates/kcl/data.gotmpl
dataTmpl string
//go:embed templates/kcl/document.gotmpl
Expand Down Expand Up @@ -53,6 +55,7 @@ func (k *kclGenerator) genKcl(w io.Writer, s kclFile) error {
return buf.String(), nil
}

tmpl = addTemplate(tmpl, "config", configTmpl)
tmpl = addTemplate(tmpl, "data", dataTmpl)
tmpl = addTemplate(tmpl, "document", documentTmpl)
tmpl = addTemplate(tmpl, "header", headerTmpl)
Expand Down
5 changes: 5 additions & 0 deletions pkg/tools/gen/templates/kcl/config.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{- if .Var }}{{ formatName .Var }}{{- if .IsUnion }}{{ " : " }}{{- else }}{{ " = " }}{{- end }}{{- end }}{{- if .Name }}{{ formatName .Name }}{{- end }}{{- if .Data }}{{- "{\n" }}
{{- range .Data -}}
{{- indentLines (include "data" .) " " }}
{{- end -}}
{{- "}" }}{{- else }}{}{{- end }}
4 changes: 4 additions & 0 deletions pkg/tools/gen/templates/kcl/index.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
{{ range .Data -}}
{{ template "data" . -}}
{{ end -}}

{{ range .Config -}}
{{ template "config" . -}}
{{ end -}}
54 changes: 28 additions & 26 deletions pkg/tools/gen/testdata/json/expect.k
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,41 @@ 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.
"""

apiVersion = "apps/v1"
kind = "Deployment"
metadata = {
name = "nginx-deployment"
labels = {
app = "nginx"
}
}
spec = {
replicas = 3
selector = {
matchLabels = {
{
apiVersion = "apps/v1"
kind = "Deployment"
metadata = {
name = "nginx-deployment"
labels = {
app = "nginx"
}
}
template = {
metadata = {
labels = {
spec = {
replicas = 3
selector = {
matchLabels = {
app = "nginx"
}
}
spec = {
containers = [
{
name = "nginx-container"
image = "nginx:latest"
ports = [
{
containerPort = 80
}
]
template = {
metadata = {
labels = {
app = "nginx"
}
]
}
spec = {
containers = [
{
name = "nginx-container"
image = "nginx:latest"
ports = [
{
containerPort = 80
}
]
}
]
}
}
}
}
54 changes: 28 additions & 26 deletions pkg/tools/gen/testdata/yaml/basic/expect.k
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,41 @@ 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.
"""

apiVersion = "apps/v1"
kind = "Deployment"
metadata = {
name = "nginx-deployment"
labels = {
app = "nginx"
}
}
spec = {
replicas = 3
selector = {
matchLabels = {
{
apiVersion = "apps/v1"
kind = "Deployment"
metadata = {
name = "nginx-deployment"
labels = {
app = "nginx"
}
}
template = {
metadata = {
labels = {
spec = {
replicas = 3
selector = {
matchLabels = {
app = "nginx"
}
}
spec = {
containers = [
{
name = "nginx"
image = "nginx:latest"
ports = [
{
containerPort = 80
}
]
template = {
metadata = {
labels = {
app = "nginx"
}
]
}
spec = {
containers = [
{
name = "nginx"
image = "nginx:latest"
ports = [
{
containerPort = 80
}
]
}
]
}
}
}
}
Loading
Loading