-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c1e5cc6
commit 260ee39
Showing
7 changed files
with
195 additions
and
2 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
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 | ||
""" | ||
} | ||
} |
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,13 @@ | ||
package v1 | ||
|
||
import ( | ||
template "github.com/GuanceCloud/iacker/pkg/template/v1" | ||
) | ||
|
||
name: "proto" | ||
|
||
inputs: template.#Inputs | ||
|
||
outputs: template.#Outputs | ||
|
||
diagnostics: [...template.#Diagnostic] |
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,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 | ||
} | ||
} |
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,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) | ||
""" | ||
} | ||
} |
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,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" | ||
}, | ||
] |
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