Ai
11 Star 11 Fork 0

Gitee 极速下载/goa

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/goadesign/goa
克隆/下载
proto.go 2.97 KB
一键复制 编辑 原始数据 按行查看 历史
package codegen
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"goa.design/goa/codegen"
"goa.design/goa/expr"
"goa.design/goa/pkg"
)
const (
// ProtoVersion is the protocol buffer version used to generate .proto files
ProtoVersion = "proto3"
)
// ProtoFiles returns a *.proto file for each gRPC service.
func ProtoFiles(genpkg string, root *expr.RootExpr) []*codegen.File {
fw := make([]*codegen.File, len(root.API.GRPC.Services))
for i, svc := range root.API.GRPC.Services {
fw[i] = protoFile(genpkg, svc)
}
return fw
}
func protoFile(genpkg string, svc *expr.GRPCServiceExpr) *codegen.File {
data := GRPCServices.Get(svc.Name())
svcName := codegen.SnakeCase(data.Service.VarName)
path := filepath.Join(codegen.Gendir, "grpc", svcName, pbPkgName, svcName+".proto")
sections := []*codegen.SectionTemplate{
// header comments
&codegen.SectionTemplate{
Name: "proto-header",
Source: protoHeaderT,
Data: map[string]interface{}{
"Title": fmt.Sprintf("%s protocol buffer definition", svc.Name()),
"ToolVersion": pkg.Version(),
},
},
// proto syntax and package
&codegen.SectionTemplate{
Name: "proto-start",
Source: protoStartT,
Data: map[string]interface{}{
"ProtoVersion": ProtoVersion,
"Pkg": codegen.SnakeCase(codegen.Goify(svcName, false)),
},
},
// service definition
&codegen.SectionTemplate{Name: "grpc-service", Source: serviceT, Data: data},
}
// message definition
for _, m := range data.Messages {
sections = append(sections, &codegen.SectionTemplate{Name: "grpc-message", Source: messageT, Data: m})
}
return &codegen.File{
Path: path,
SectionTemplates: sections,
FinalizeFunc: protoc,
}
}
func protoc(path string) error {
dir := filepath.Dir(path)
os.MkdirAll(dir, 0777)
args := []string{"--go_out=plugins=grpc:.", path, "--proto_path", dir}
cmd := exec.Command("protoc", args...)
cmd.Dir = filepath.Dir(path)
if output, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("failed to run protoc: %s: %s", err, output)
}
return nil
}
const (
protoHeaderT = `{{ if .Title -}}
// Code generated with goa {{ .ToolVersion }}, DO NOT EDIT.
//
// {{ .Title }}
//
// Command:
{{ comment commandLine }}
{{- end }}
`
protoStartT = `
syntax = {{ printf "%q" .ProtoVersion }};
package {{ .Pkg }};
option go_package = "{{ .Pkg }}pb";
`
// input: ServiceData
serviceT = `
{{ .Description | comment }}
service {{ .Name }} {
{{- range .Endpoints }}
{{ if .Method.Description }}{{ .Method.Description | comment }}{{ end }}
{{- $serverStream := or (eq .Method.StreamKind 3) (eq .Method.StreamKind 4) }}
{{- $clientStream := or (eq .Method.StreamKind 2) (eq .Method.StreamKind 4) }}
rpc {{ .Method.VarName }} ({{ if $clientStream }}stream {{ end }}{{ .Request.Message.VarName }}) returns ({{ if $serverStream }}stream {{ end }}{{ .Response.Message.VarName }});
{{- end }}
}
`
// input: service.UserTypeData
messageT = `{{ comment .Description }}
message {{ .VarName }}{{ .Def }}
`
)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/goa.git
git@gitee.com:mirrors/goa.git
mirrors
goa
goa
v2.2.5

搜索帮助