package generator
import (
"google.golang.org/genproto/googleapis/api/annotations"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"log"
)
const (
ginPkg = protogen.GoImportPath("github.com/gin-gonic/gin")
httpPkg = protogen.GoImportPath("net/http")
)
// test
var methodSets = make(map[string]int)
func GenerateFile(gen *protogen.Plugin, file *protogen.File) *protogen.GeneratedFile {
if len(file.Services) == 0 {
return nil
}
//设置生成的文件名,文件名会被protoc使用,生成的文件会被放在响应的目录下
filename := file.GeneratedFilenamePrefix + "_gin.pb.go"
log.Println("filename", filename)
g := gen.NewGeneratedFile(filename, file.GoImportPath)
//该注释会被go的ide识别到, 表示该文件是自动生成的,尽量不要修改
g.P("// Code generated by protoc-gen-gin. DO NOT EDIT.")
g.P()
g.P("package ", file.GoPackageName)
//该函数是注册全局的packge 的内容,但是此时不会写入 import
g.QualifiedGoIdent(ginPkg.Ident(""))
g.QualifiedGoIdent(httpPkg.Ident(""))
for _, service := range file.Services {
genService(file, g, service)
}
//自己写文件看结果
//f, err := os.Create("api_gin.pb.go")
//
//if err != nil {
// log.Fatal(err)
//}
//
//defer f.Close()
//
//contentStr, _ := g.Content()
//_, _ = f.WriteString(string(contentStr))
return g
}
func genService(file *protogen.File, g *protogen.GeneratedFile, s *protogen.Service) {
// HTTP Server
sd := &service{
Name: s.GoName,
FullName: string(s.Desc.FullName()),
}
log.Println("chengpeng3,", sd)
for _, method := range s.Methods {
sd.Methods = append(sd.Methods, genMethod(method)...)
}
text := sd.execute()
g.P(text)
}
func genMethod(m *protogen.Method) []*method {
var methods []*method
// 存在 http rule 配置
// options
rule, ok := proto.GetExtension(m.Desc.Options(), annotations.E_Http).(*annotations.HttpRule)
if rule != nil && ok {
//for _, bind := range rule.AdditionalBindings {
// methods = append(methods, buildHTTPRule(m, bind))
//}
methods = append(methods, buildHTTPRule(m, rule))
return methods
}
methods = append(methods, defaultMethod(m))
log.Println(methods)
return methods
}
func defaultMethod(m *protogen.Method) *method {
// TODO path
// $prefix + / + ${package}.${service} + / + ${method}
// /api/demo.v0.Demo/GetName
md := buildMethodDesc(m, "POST", "")
log.Println("chengpeng2123,", md)
md.Body = "*"
return md
}
func buildHTTPRule(m *protogen.Method, rule *annotations.HttpRule) *method {
var path, method string
switch pattern := rule.Pattern.(type) {
case *annotations.HttpRule_Get:
path = pattern.Get
method = "GET"
case *annotations.HttpRule_Put:
path = pattern.Put
method = "PUT"
case *annotations.HttpRule_Post:
path = pattern.Post
method = "POST"
case *annotations.HttpRule_Delete:
path = pattern.Delete
method = "DELETE"
case *annotations.HttpRule_Patch:
path = pattern.Patch
method = "PATCH"
case *annotations.HttpRule_Custom:
path = pattern.Custom.Path
method = pattern.Custom.Kind
}
md := buildMethodDesc(m, method, path)
return md
}
func buildMethodDesc(m *protogen.Method, httpMethod string, path string) *method {
defer func() { methodSets[m.GoName]++ }()
md := &method{
Name: m.GoName,
Num: methodSets[m.GoName],
Request: m.Input.GoIdent.GoName,
Reply: m.Output.GoIdent.GoName,
Path: path,
Method: httpMethod,
}
log.Println("chengpeng1,", md)
md.initPathParams()
return md
}
没有合适的资源?快使用搜索试试~ 我知道了~
protoc生成gin的插件
共3个文件
go:2个
tpl:1个
需积分: 2 0 下载量 102 浏览量
2024-01-24
07:01:40
上传
评论
收藏 4KB ZIP 举报
温馨提示
protoc生成gin的插件
资源推荐
资源详情
资源评论
收起资源包目录
generator.zip (3个子文件)
generator
template.go 3KB
template.go.tpl 1KB
generate.go 3KB
共 3 条
- 1
资源评论
小乞丐程序员
- 粉丝: 195
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Picasso_v3.1 2.ipa
- chromedriver-mac-arm64.zip
- 蓝zapro.apk
- chromedriver-linux64.zip
- UCAS研一深度学习实验-MNIST手写数字识别python源码+详细注释(高分项目)
- 基于Python和PyTorch框架完成的一个手写数字识别实验源码(带MINIST手写数字数据集)+详细注释(高分项目)
- 基于Matlab在MNIST数据集上利用CNN完成手写体数字识别任务,并实现单层CNN反向传播算法+源代码+文档说明(高分项目)
- NVIDIA驱动、CUDA和Pytorch及其依赖
- 基于SVM多特征融合的微表情识别python源码+项目说明+详细注释(高分课程设计)
- html动态爱心代码一(附源码)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功