Skip to content

Commit

Permalink
feat: Add proto generator (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
yufeiminds committed Oct 17, 2023
1 parent c1e5cc6 commit 260ee39
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 2 deletions.
25 changes: 25 additions & 0 deletions templates/proto/v1/godoc.cue
Original file line number Diff line number Diff line change
@@ -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
"""
}
}
13 changes: 13 additions & 0 deletions templates/proto/v1/manifest.cue
Original file line number Diff line number Diff line change
@@ -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]
117 changes: 117 additions & 0 deletions templates/proto/v1/proto.cue
Original file line number Diff line number Diff line change
@@ -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
}
}
19 changes: 19 additions & 0 deletions templates/proto/v1/readme.cue
Original file line number Diff line number Diff line change
@@ -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)
"""
}
}
19 changes: 19 additions & 0 deletions templates/proto/v1/tests/manifest.cue
Original file line number Diff line number Diff line change
@@ -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"
},
]
2 changes: 1 addition & 1 deletion templates/terraform/v1/builder/model.cue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion templates/terraform/v1/tests/manifest.cue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 260ee39

Please sign in to comment.