-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generate kcl data from yaml (#150)
- Loading branch information
Showing
9 changed files
with
166 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,26 @@ | ||
package gen | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/goccy/go-yaml" | ||
"io" | ||
"sort" | ||
) | ||
|
||
func (k *kclGenerator) genKclFromJsonData(w io.Writer, filename string, src interface{}) error { | ||
code, err := readSource(filename, src) | ||
if err != nil { | ||
return err | ||
} | ||
jsonData := &map[string]interface{}{} | ||
if err = json.Unmarshal(code, jsonData); err != nil { | ||
|
||
// as yaml can be viewed as a superset of json, | ||
// we can handle json data like yaml. | ||
yamlData := &yaml.MapSlice{} | ||
if err = yaml.UnmarshalWithOptions(code, yamlData, yaml.UseOrderedMap(), yaml.UseJSONUnmarshaler()); err != nil { | ||
return err | ||
} | ||
|
||
// convert json data to kcl | ||
result := convertKclFromJson(jsonData) | ||
// convert to kcl | ||
result := convertKclFromYaml(yamlData) | ||
|
||
// generate kcl code | ||
return k.genKcl(w, kclFile{Data: result}) | ||
} | ||
|
||
func convertKclFromJson(jsonData *map[string]interface{}) []data { | ||
var result []data | ||
for key, value := range *jsonData { | ||
switch value := value.(type) { | ||
case map[string]interface{}: | ||
result = append(result, data{ | ||
Key: key, | ||
Value: convertKclFromJson(&value), | ||
}) | ||
case []interface{}: | ||
var vals []interface{} | ||
for _, v := range value { | ||
switch v := v.(type) { | ||
case map[string]interface{}: | ||
vals = append(vals, convertKclFromJson(&v)) | ||
default: | ||
vals = append(vals, v) | ||
} | ||
} | ||
result = append(result, data{Key: key, Value: vals}) | ||
default: | ||
result = append(result, data{Key: key, Value: value}) | ||
} | ||
} | ||
sort.Slice(result, func(i, j int) bool { | ||
return result[i].Key < result[j].Key | ||
}) | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package gen | ||
|
||
import ( | ||
"github.com/goccy/go-yaml" | ||
"io" | ||
) | ||
|
||
func (k *kclGenerator) genKclFromYaml(w io.Writer, filename string, src interface{}) error { | ||
code, err := readSource(filename, src) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
yamlData := &yaml.MapSlice{} | ||
if err = yaml.UnmarshalWithOptions(code, yamlData, yaml.UseOrderedMap()); err != nil { | ||
return err | ||
} | ||
|
||
// convert yaml data to kcl | ||
result := convertKclFromYaml(yamlData) | ||
|
||
// generate kcl code | ||
return k.genKcl(w, kclFile{Data: result}) | ||
} | ||
|
||
func convertKclFromYaml(yamlData *yaml.MapSlice) []data { | ||
var result []data | ||
for _, item := range *yamlData { | ||
key, ok := item.Key.(string) | ||
if !ok { | ||
continue | ||
} | ||
switch value := item.Value.(type) { | ||
case yaml.MapSlice: | ||
result = append(result, data{ | ||
Key: key, | ||
Value: convertKclFromYaml(&value), | ||
}) | ||
case []interface{}: | ||
var vals []interface{} | ||
for _, v := range value { | ||
switch v := v.(type) { | ||
case yaml.MapSlice: | ||
vals = append(vals, convertKclFromYaml(&v)) | ||
default: | ||
vals = append(vals, v) | ||
} | ||
} | ||
result = append(result, data{Key: key, Value: vals}) | ||
default: | ||
result = append(result, data{Key: key, Value: value}) | ||
} | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 = { | ||
app = "nginx" | ||
} | ||
} | ||
template = { | ||
metadata = { | ||
labels = { | ||
app = "nginx" | ||
} | ||
} | ||
spec = { | ||
containers = [ | ||
{ | ||
name = "nginx" | ||
image = "nginx:latest" | ||
ports = [ | ||
{ | ||
containerPort = 80 | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: nginx-deployment | ||
labels: | ||
app: nginx | ||
spec: | ||
replicas: 3 | ||
selector: | ||
matchLabels: | ||
app: nginx | ||
template: | ||
metadata: | ||
labels: | ||
app: nginx | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: 'nginx:latest' | ||
ports: | ||
- containerPort: 80 |