Files
masonry/lang_templates/proto/application.proto.tmpl

288 lines
7.9 KiB
Cheetah

// Code generated by Masonry DSL CLI tool. DO NOT EDIT.
syntax = "proto3";
{{- $serverName := "MyService" }}
{{- $serverHost := "localhost:8080" }}
{{- range .AST.Definitions }}
{{- if .Server }}
{{- $serverName = .Server.Name }}
{{- $serverHost = getServerHostPort .Server.Settings }}
{{- end }}
{{- end }}
package {{ $serverName | title }};
import "gorm/options/gorm.proto";
import "gorm/types/types.proto";
import "google/api/annotations.proto";
import "google/protobuf/timestamp.proto";
option go_package = "./;pb";
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger) = {
info: {
title: "{{ $serverName | title }} API"
version: "v1.0"
description: "Generated API for {{ $serverName }}"
}
host: "{{ $serverHost }}"
};
service {{ $serverName | title }}Service {
option (gorm.server).autogen = true;
{{- range .AST.Definitions }}
{{- if .Entity }}
{{- $entityName := .Entity.Name }}
// CRUD operations for {{ $entityName }}
rpc Create{{ $entityName }} (Create{{ $entityName }}Request) returns (Create{{ $entityName }}Response) {
option (google.api.http) = {
post: "/v1/{{ $entityName | title }}"
body: "*"
};
}
rpc Read{{ $entityName }} (Read{{ $entityName }}Request) returns (Read{{ $entityName }}Response) {
option (google.api.http) = {
get: "/v1/{{ $entityName | title }}/{id}"
};
}
rpc List{{ $entityName }}s (List{{ $entityName }}sRequest) returns (List{{ $entityName }}sResponse) {
option (google.api.http) = {
get: "/v1/{{ $entityName | title }}"
};
}
rpc Update{{ $entityName }} (Update{{ $entityName }}Request) returns (Update{{ $entityName }}Response) {
option (google.api.http) = {
put: "/v1/{{ $entityName | title }}"
body: "*"
};
}
rpc Delete{{ $entityName }} (Delete{{ $entityName }}Request) returns (Delete{{ $entityName }}Response) {
option (gorm.method).object_type = "{{ $entityName }}";
option (google.api.http) = {
delete: "/v1/{{ $entityName | title }}/{id}"
};
}
{{- end }}
{{- end }}
{{- range .AST.Definitions }}
{{- if .Endpoint }}
{{- $method := .Endpoint.Method }}
{{- $path := .Endpoint.Path }}
{{- $entity := .Endpoint.Entity }}
{{- $handlerName := pathToHandlerName $path }}
// Custom endpoint: {{ $method }} {{ $path }}
rpc {{ $handlerName }}{{ $method }} ({{ $handlerName }}{{ $method }}Request) returns ({{ $handlerName }}{{ $method }}Response) {
option (google.api.http) = {
{{- if eq $method "GET" }}
get: "{{ $path }}"
{{- else if eq $method "POST" }}
post: "{{ $path }}"
body: "*"
{{- else if eq $method "PUT" }}
put: "{{ $path }}"
body: "*"
{{- else if eq $method "PATCH" }}
patch: "{{ $path }}"
body: "*"
{{- else if eq $method "DELETE" }}
delete: "{{ $path }}"
{{- end }}
};
}
{{- end }}
{{- end }}
}
{{- range .AST.Definitions }}
{{- if .Entity }}
{{- $entityName := .Entity.Name }}
// CRUD Request/Response messages for {{ $entityName }}
message Create{{ $entityName }}Request {
{{ $entityName }} payload = 1;
}
message Create{{ $entityName }}Response {
{{ $entityName }} result = 1;
}
message Read{{ $entityName }}Request {
{{- $idField := "" }}
{{- $idType := "uint64" }}
{{- range .Entity.Fields }}
{{- if eq .Name "id" }}
{{- $idField = .Name }}
{{- if eq .Type "uuid" }}
{{- $idType = "string" }}
{{- else if eq .Type "string" }}
{{- $idType = "string" }}
{{- else }}
{{- $idType = "uint64" }}
{{- end }}
{{- end }}
{{- end }}
{{ $idType }} id = 1;
}
message Read{{ $entityName }}Response {
{{ $entityName }} result = 1;
}
message List{{ $entityName }}sRequest {
int32 page_size = 1;
string page_token = 2;
}
message List{{ $entityName }}sResponse {
repeated {{ $entityName }} results = 1;
string next_page_token = 2;
}
message Update{{ $entityName }}Request {
{{ $entityName }} payload = 1;
}
message Update{{ $entityName }}Response {
{{ $entityName }} result = 1;
}
message Delete{{ $entityName }}Request {
{{- $idType := "uint64" }}
{{- range .Entity.Fields }}
{{- if eq .Name "id" }}
{{- if eq .Type "uuid" }}
{{- $idType = "string" }}
{{- else if eq .Type "string" }}
{{- $idType = "string" }}
{{- else }}
{{- $idType = "uint64" }}
{{- end }}
{{- end }}
{{- end }}
{{ $idType }} id = 1;
}
message Delete{{ $entityName }}Response {}
{{- end }}
{{- end }}
{{- range .AST.Definitions }}
{{- if .Endpoint }}
{{- $method := .Endpoint.Method }}
{{- $path := .Endpoint.Path }}
{{- $handlerName := pathToHandlerName $path }}
// Custom endpoint messages for {{ $method }} {{ $path }}
message {{ $handlerName }}{{ $method }}Request {
{{- if .Endpoint.Params }}
{{- $fieldNum := 1 }}
{{- range .Endpoint.Params }}
{{ goType .Type }} {{ .Name }} = {{ $fieldNum }};
{{- $fieldNum = add $fieldNum 1 }}
{{- end }}
{{- end }}
}
message {{ $handlerName }}{{ $method }}Response {
{{- if .Endpoint.Response }}
{{- if eq .Endpoint.Response.Type "list" }}
repeated {{ .Endpoint.Entity }} results = 1;
{{- else }}
{{ .Endpoint.Response.Type }} result = 1;
{{- end }}
{{- else }}
bool success = 1;
{{- end }}
}
{{- end }}
{{- end }}
{{- range .AST.Definitions }}
{{- if .Entity }}
{{- $entityName := .Entity.Name }}
// {{ $entityName }} entity message
message {{ $entityName }} {
option (gorm.opts).ormable = true;
{{- if .Entity.Description }}
// {{ .Entity.Description }}
{{- end }}
{{- $fieldNum := 1 }}
{{- range .Entity.Fields }}
{{- $protoType := "string" }}
{{- $gormTags := slice }}
{{- if eq .Type "string" }}
{{- $protoType = "string" }}
{{- else if eq .Type "int" }}
{{- $protoType = "int64" }}
{{- else if eq .Type "uuid" }}
{{- $protoType = "string" }}
{{- $gormTags = append $gormTags "type:uuid" }}
{{- else if eq .Type "boolean" }}
{{- $protoType = "bool" }}
{{- else if eq .Type "timestamp" }}
{{- $protoType = "google.protobuf.Timestamp" }}
{{- else if eq .Type "text" }}
{{- $protoType = "string" }}
{{- $gormTags = append $gormTags "type:text" }}
{{- else }}
{{- $protoType = "string" }}
{{- end }}
{{- if .Required }}
{{- $gormTags = append $gormTags "not null" }}
{{- end }}
{{- if .Unique }}
{{- $gormTags = append $gormTags "unique" }}
{{- end }}
{{- if .Index }}
{{- $gormTags = append $gormTags "index" }}
{{- end }}
{{- if .Default }}
{{- $gormTags = append $gormTags (printf "default:%s" (derefString .Default)) }}
{{- end }}
{{- if .Relationship }}
{{- if eq .Relationship.Cardinality "one" }}
{{- if .Relationship.ForeignKey }}
{{- $gormTags = append $gormTags (printf "foreignKey:%s" (derefString .Relationship.ForeignKey)) }}
{{- else }}
{{- $gormTags = append $gormTags (printf "foreignKey:%sID" .Name) }}
{{- end }}
{{- else if eq .Relationship.Cardinality "many" }}
{{- $protoType = printf "repeated %s" .Relationship.Type }}
{{- if .Relationship.Through }}
{{- $gormTags = append $gormTags (printf "many2many:%s" (derefString .Relationship.Through)) }}
{{- else }}
{{- $gormTags = append $gormTags (printf "foreignKey:%sID" $entityName) }}
{{- end }}
{{- end }}
{{- end }}
{{ $protoType }} {{ .Name }} = {{ $fieldNum }}
{{- if $gormTags }}
[(gorm.field).tag = {
{{- range $i, $tag := $gormTags }}
{{- if $i }}, {{ end }}{{ $tag }}
{{- end }}
}]
{{- end }};
{{- $fieldNum = add $fieldNum 1 }}
{{- end }}
}
{{- end }}
{{- end }}