Skip to content

Commit

Permalink
feat: impl kcl config generation and add indent block for JSON/YAML i…
Browse files Browse the repository at this point in the history
…nputs (#209)

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

Signed-off-by: peefy <[email protected]>

* feat: use data template in the config template

Signed-off-by: peefy <[email protected]>

---------

Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy authored Jan 14, 2024
1 parent de70a20 commit 11a8a42
Show file tree
Hide file tree
Showing 14 changed files with 643 additions and 598 deletions.
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

0 comments on commit 11a8a42

Please sign in to comment.