From 260ee39a068dbb02479cbe36751efa7d0924e896 Mon Sep 17 00:00:00 2001 From: Yufei Li Date: Tue, 17 Oct 2023 20:06:35 +0800 Subject: [PATCH] feat: Add proto generator (#23) --- templates/proto/v1/godoc.cue | 25 +++++ templates/proto/v1/manifest.cue | 13 +++ templates/proto/v1/proto.cue | 117 ++++++++++++++++++++++ templates/proto/v1/readme.cue | 19 ++++ templates/proto/v1/tests/manifest.cue | 19 ++++ templates/terraform/v1/builder/model.cue | 2 +- templates/terraform/v1/tests/manifest.cue | 2 +- 7 files changed, 195 insertions(+), 2 deletions(-) create mode 100644 templates/proto/v1/godoc.cue create mode 100644 templates/proto/v1/manifest.cue create mode 100644 templates/proto/v1/proto.cue create mode 100644 templates/proto/v1/readme.cue create mode 100644 templates/proto/v1/tests/manifest.cue diff --git a/templates/proto/v1/godoc.cue b/templates/proto/v1/godoc.cue new file mode 100644 index 0000000..c0fb966 --- /dev/null +++ b/templates/proto/v1/godoc.cue @@ -0,0 +1,25 @@ +package v1 + +import ( + "strings" + + template "github.com/GuanceCloud/iacker/pkg/template/v1" +) + +// Generate godoc files +for rsname, rsinfo in *inputs.resources | {} { + outputs: files: "resources/\(strings.ToLower(rsname))/v1/doc.go": template.#File & { + _lowername: strings.ToLower(rsname) + + content: """ + /* + Package v1 + + # \(rsinfo.title.en) + + \(rsinfo.description.en) + */ + package v1 + """ + } +} diff --git a/templates/proto/v1/manifest.cue b/templates/proto/v1/manifest.cue new file mode 100644 index 0000000..135f7ce --- /dev/null +++ b/templates/proto/v1/manifest.cue @@ -0,0 +1,13 @@ +package v1 + +import ( + template "github.com/GuanceCloud/iacker/pkg/template/v1" +) + +name: "proto" + +inputs: template.#Inputs + +outputs: template.#Outputs + +diagnostics: [...template.#Diagnostic] diff --git a/templates/proto/v1/proto.cue b/templates/proto/v1/proto.cue new file mode 100644 index 0000000..3adb97b --- /dev/null +++ b/templates/proto/v1/proto.cue @@ -0,0 +1,117 @@ +package v1 + +import ( + "list" + "strings" + gotemplate "text/template" + + template "github.com/GuanceCloud/iacker/pkg/template/v1" + resource "github.com/GuanceCloud/iacker/pkg/resource/v1" +) + +#Resource: { + name: string + + rs: resource.#Resource + + output: string + + _lowername: strings.ToLower(name) + + _template: """ + syntax = "proto3"; + + package pkg.resources.\( _lowername ).v1; + + option go_package = "github.com/GuanceCloud/openapi/pkg/resources/\( _lowername )/v1;v1"; + + """ + + _model_template: """ + /* + {{- if .v.title.en }} + {{ .v.title.en }} + {{- end }} + */ + message {{ .name }} { + {{- range $v := .properties }} + + {{ $v }} + {{- end }} + } + """ + + _prop_template: """ + /* + {{ .v.title.en }} + + {{- if .v.description.en }} + {{ .v.description.en }} + {{- end }} + */ + {{ template "type" .v.schema }} {{ .v.name }} = {{ .index }}; + + {{- define "type" }} + {{- if eq .type "array" -}} + repeated {{ template "elem" .elem }} + {{- else if eq .type "object" -}} + {{ .model }} + {{- else if eq .type "ref" -}} + {{ template "optional" . }}string + {{- else if eq .type "integer" -}} + {{ template "optional" . }}int64 + {{- else if eq .type "boolean" -}} + {{ template "optional" . }}bool + {{- else -}} + {{ template "optional" . }}{{ .type }} + {{- end -}} + {{- end }} + + {{- define "elem" }} + {{- if eq .type "object" -}} + {{ .model }} + {{- else if eq .type "ref" -}} + string + {{- else if eq .type "integer" -}} + int64 + {{- else if eq .type "boolean" -}} + bool + {{- else -}} + {{ .type }} + {{- end -}} + {{- end }} + + {{- define "optional" }} + {{- if .required }}{{ else }}optional {{ end -}} + {{- end }} + """ + + _models: [ + for mname, minfo in rs.models { + gotemplate.Execute(_model_template, { + "name": mname + "v": minfo + properties: [ + for i, pinfo in minfo.properties { + gotemplate.Execute(_prop_template, { + "v": pinfo + "index": i + 1 + }) + }, + ] + }) + }, + ] + + output: strings.Join(list.Concat([[_template], _models]), "\n") +} + +for rsname, rsinfo in *inputs.resources | {} { + outputs: files: "resources/\(strings.ToLower(rsname))/v1/\(strings.ToLower(rsname)).proto": template.#File & { + _gen: #Resource & { + "name": rsname + "rs": rsinfo + } + content: _gen.output + } +} diff --git a/templates/proto/v1/readme.cue b/templates/proto/v1/readme.cue new file mode 100644 index 0000000..482968b --- /dev/null +++ b/templates/proto/v1/readme.cue @@ -0,0 +1,19 @@ +package v1 + +import ( + "strings" + + template "github.com/GuanceCloud/iacker/pkg/template/v1" +) + +for rsname, rsinfo in *inputs.resources | {} { + outputs: files: "resources/\(strings.ToLower(rsname))/v1/README.md": template.#File & { + _lowername: strings.ToLower(rsname) + + content: """ + # \(rsinfo.title.en) + + \(rsinfo.description.en) + """ + } +} diff --git a/templates/proto/v1/tests/manifest.cue b/templates/proto/v1/tests/manifest.cue new file mode 100644 index 0000000..69614f6 --- /dev/null +++ b/templates/proto/v1/tests/manifest.cue @@ -0,0 +1,19 @@ +package tests + +import ( + "github.com/GuanceCloud/iacker/examples/petstore" + proto "github.com/GuanceCloud/iacker/templates/proto/v1" +) + +"resources": petstore.resources + +"templates": { + "proto": proto +} + +"options": "templates": [ + { + template: "proto" + outdir: ".build" + }, +] diff --git a/templates/terraform/v1/builder/model.cue b/templates/terraform/v1/builder/model.cue index 473bc82..3eec034 100644 --- a/templates/terraform/v1/builder/model.cue +++ b/templates/terraform/v1/builder/model.cue @@ -148,7 +148,7 @@ import ( }) _models: [ - for mname, minfo in (rs.models | *{}) if mname != name { + for mname, minfo in (*rs.models | {}) if mname != name { gotemplate.Execute(_model_template, { "name": naming.#UpperCamel & {"name": mname} "v": minfo diff --git a/templates/terraform/v1/tests/manifest.cue b/templates/terraform/v1/tests/manifest.cue index cc48987..333174c 100644 --- a/templates/terraform/v1/tests/manifest.cue +++ b/templates/terraform/v1/tests/manifest.cue @@ -2,7 +2,7 @@ package tests import ( "github.com/GuanceCloud/iacker/examples/petstore" - "github.com/GuanceCloud/iacker/templates/terraform/v1" + terraform "github.com/GuanceCloud/iacker/templates/terraform/v1" ) resources: petstore.resources