Skip to content

Commit

Permalink
feat: Add schema at proto template
Browse files Browse the repository at this point in the history
  • Loading branch information
yufeiminds committed Oct 25, 2023
1 parent 8a38a2a commit fe9b426
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
package pkg.resources.\( _lowername ).v1;
option go_package = "github.com/GuanceCloud/openapi/pkg/resources/\( _lowername )/v1;v1";
option go_package = "github.com/GuanceCloud/iakcer-server/pkg/resources/\( _lowername )/v1;v1";
"""

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package v1

import (
"strings"
"encoding/json"
gotemplate "text/template"

naming "github.com/GuanceCloud/iacker/pkg/helpers/naming"
template "github.com/GuanceCloud/iacker/pkg/template/v1"
)

resources: *inputs.resources | {}

// Generate JSON files
for rsname, rsinfo in resources {
outputs: files: "resources/\(strings.ToLower(rsname))/v1/\(strings.ToLower(rsname)).schema.json": template.#File & {
content: json.Indent(json.Marshal(rsinfo), "", " ")
}
}

_schema_template: """
// Code generated by Iacker. DO NOT EDIT IT.
package v1
import (
_ "embed"
)
// ResourceType is the unique type name of {{ .name }}
const ResourceType = "{{ .name }}"
//go:embed {{ .lowername }}.schema.json
var schemaRaw []byte
{{ range $varname, $enum := .consts }}
const (
{{- range $_, $item := $enum }}
// {{ $varname }}{{ $item.name }} is the value of {{ $item.title }}
{{ $varname }}{{ $item.name }} = {{ $item.value }}
{{- end }}
)
{{ end }}
// resourceSchema is the schema of {{ .name }}
type resourceSchema struct {
}
// Raw returns the raw schema bytes of {{ .name }}
func (s *resourceSchema) Raw() []byte {
return schemaRaw
}
// ReferenceTypes returns the related resource types of {{ .name }}
func (s *resourceSchema) ReferenceTypes() []string {
return []string{
{{- range $ref, $pkg := .refs }}
"{{ $ref }}",
{{- end }}
}
}
"""

// Generate schema definitions
for rsname, rsinfo in resources {
outputs: files: "resources/\(strings.ToLower(rsname))/v1/\(strings.ToLower(rsname)).schema.go": template.#File & {
content: gotemplate.Execute(_schema_template, {
name: rsname
lowername: strings.ToLower(rsname)
consts: {
for mname, minfo in rsinfo.models {
for pinfo in minfo.properties {
if pinfo.schema.enum != _|_ {
"\(mname)\((naming.#Snake & {name: pinfo.name}).uppercamel)": [
for item in pinfo.schema.enum {
{
"name": (naming.#Snake & {"name": item.name}).uppercamel
if pinfo.schema.type == "string" {
value: "\"\(item.value)\""
}
if pinfo.schema.type != "string" {
value: item.value
}
title: item.title.en
}
},
]
}

if pinfo.schema.type == "array" && pinfo.schema.elem.enum != _|_ {
"\(mname)\((naming.#Snake & {name: pinfo.name}).uppercamel)": [
for item in pinfo.schema.elem.enum {
{
"name": (naming.#Snake & {"name": item.name}).uppercamel
if pinfo.schema.elem.type == "string" {
value: "\"\(item.value)\""
}
if pinfo.schema.elem.type != "string" {
value: item.value
}
title: item.title.en
}
},
]
}
}
}
}

refs: {
for mname, minfo in rsinfo.models {
for pinfo in minfo.properties {
if pinfo.schema.ref != _|_ {
"\(pinfo.schema.ref)": pinfo.schema.ref == rsname
}

if pinfo.schema.type == "array" && pinfo.schema.elem.ref != _|_ {
"\(pinfo.schema.elem.ref)": pinfo.schema.elem.ref == rsname
}
}
}
}
})
}
}

0 comments on commit fe9b426

Please sign in to comment.