feat: proto file format

This commit is contained in:
Young Xu 2023-03-19 00:46:31 +08:00
parent a51a9fe3bf
commit 0a81c9ad68
Signed by: xuthus5
GPG Key ID: A23CF9620CBB55F9
4 changed files with 88 additions and 37 deletions

View File

@ -1,45 +1,61 @@
syntax = "proto3";
package v1;
package controller;
option go_package = "./;core";
option go_package = "./;main";
// @route_group: true
// @base_url: /api/v1
// @gen_to: ./example_controller.go
service ExampleService { // tail
// @author: xuthus
// @desc: test rpc
// @method: GET
rpc ExampleCall1(ExampleMessage1) returns(ReturnType) {}
rpc ExampleCall2(ExampleMessage2) returns(ReturnType) {}
// @route_api: /api/file
// @gen_to: ./core/file_controller.go
service File {
// @desc:
// @author: Young Xu
// @method: GET
// @api: /list
rpc List (ListReq) returns (ListResp);
// @desc:
// @author: Young Xu
// @method: POST
// @api: /upload
rpc Upload (UploadReq) returns (UploadResp);
// @desc:
// @author: Young Xu
// @method: POST
// @api: /delete
rpc Delete (DeleteReq) returns (DeleteResp);
// @desc:
// @author: Young Xu
// @method: GET
// @api: /download
rpc Download (DownloadReq) returns (DownloadResp);
}
// @route_group: true
// @base_url: /api/v2
service Example1Service { // tail
rpc ExampleCall1(ExampleMessage1) returns(ReturnType) {}
rpc ExampleCall2(ExampleMessage2) returns(ReturnType) {}
message ListReq {}
message ListResp {
message Item {
string filename = 1; //
string file_size = 2; //
string created_at = 3; //
}
repeated Item items = 1; //
}
// ExampleMessage1 - Example Leading Comment for ExampleMessage1
message ExampleMessage1 {
string MyString = 1;
message UploadReq {}
message UploadResp {}
message DeleteReq {
string filename = 1; //
}
/*
ExampleMessage2 - Example Leading Comment for ExampleMessage2
*/
message ExampleMessage2 {
int32 MyInt = 1;
// MyInt - Example trailing Comment
message ExampleNested {
bytes data = 1;
}
ExampleNested nested = 2;
message DeleteResp {}
message DownloadReq {
// @v: required
string f = 1; //
}
/*
ReturnType - Empty Structure Placeholder
*/
message ReturnType {}
message DownloadResp {}

4
go.mod
View File

@ -3,7 +3,9 @@ module gitter.top/coco/protoc-gen-coco
go 1.19
require (
github.com/emicklei/proto v1.11.1
gitter.top/coco/coco v0.0.0-20230312134419-0dd59eb0e955
gitter.top/coco/goast v0.0.0-20230318151709-875a73a463aa
google.golang.org/protobuf v1.28.1
)
@ -25,7 +27,7 @@ require (
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.9 // indirect
gitter.top/coco/goast v0.0.0-20230318151709-875a73a463aa // indirect
gitter.top/sync/proto-contrib v0.15.0 // indirect
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
golang.org/x/crypto v0.5.0 // indirect
golang.org/x/net v0.7.0 // indirect

10
go.sum
View File

@ -7,6 +7,8 @@ github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583j
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emicklei/proto v1.11.1 h1:CBZwNVwPJvkdevxvsoCuFedF9ENiBz0saen3L9y0OTA=
github.com/emicklei/proto v1.11.1/go.mod h1:rn1FgRS/FANiZdD2djyH7TMA9jdRDcYQ9IEN9yvjX0A=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.9.0 h1:OjyFBKICoexlu99ctXNR2gg+c5pKrKMuyjgARg9qeY8=
@ -59,12 +61,12 @@ github.com/ugorji/go/codec v1.2.9 h1:rmenucSohSTiyL09Y+l2OCk+FrMxGMzho2+tjr5ticU
github.com/ugorji/go/codec v1.2.9/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
gitter.top/coco/coco v0.0.0-20230312134419-0dd59eb0e955 h1:dLnS34ByD6KgTiUHBPQSqhvh1yk9pR10P3So/Gz9Zo0=
gitter.top/coco/coco v0.0.0-20230312134419-0dd59eb0e955/go.mod h1:IptVJiCXf87Xywfw26zrvXQjTjF8ZsIHUTJ8yPWSpWE=
gitter.top/coco/goast v0.0.0-20230303150326-1f614fadb952 h1:6nsHIjVKo9vHEG3sIIZcVv/1o9VZOxkgZvsGDB2zsag=
gitter.top/coco/goast v0.0.0-20230303150326-1f614fadb952/go.mod h1:irSK6CvEKqxRIu45LbT1GTXvwu9D94h51hK9bR6Zcoo=
gitter.top/coco/goast v0.0.0-20230318150308-b74a5040b338 h1:JWoLRyJRupqsbb/HEqPuhhoFrfwVThZD5trIgU4EMeQ=
gitter.top/coco/goast v0.0.0-20230318150308-b74a5040b338/go.mod h1:irSK6CvEKqxRIu45LbT1GTXvwu9D94h51hK9bR6Zcoo=
gitter.top/coco/goast v0.0.0-20230318151709-875a73a463aa h1:qSlQttmvI3fRWJUdZ5Ra5Prg+5OCfzrn/BIN8q+lItE=
gitter.top/coco/goast v0.0.0-20230318151709-875a73a463aa/go.mod h1:irSK6CvEKqxRIu45LbT1GTXvwu9D94h51hK9bR6Zcoo=
gitter.top/sync/proto-contrib v0.14.0 h1:kFnFolvQZjksvbg/XAMgHjyGWNoHHcKT/MdKJcN9KGQ=
gitter.top/sync/proto-contrib v0.14.0/go.mod h1:ZnQxhRQHLM3Y8lKwvRZwDe2CZ7bkVH6dZz2yEVpU5zg=
gitter.top/sync/proto-contrib v0.15.0 h1:V2vOTT1Rgw37jxLM8+IBHDPP8UtdCZAj4CqQIM4owFM=
gitter.top/sync/proto-contrib v0.15.0/go.mod h1:ZnQxhRQHLM3Y8lKwvRZwDe2CZ7bkVH6dZz2yEVpU5zg=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 h1:18EFjUmQOcUvxNYSkA6jO9VAiXCnxFY6NyDX0bHDmkU=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.5.0 h1:U/0M97KRkSFvyD/3FSmdP5W5swImpNgle/EHFhOsQPE=

31
main.go
View File

@ -1,9 +1,13 @@
package main
import (
"bytes"
"fmt"
"github.com/emicklei/proto"
"gitter.top/sync/proto-contrib/pkg/protofmt"
"google.golang.org/protobuf/compiler/protogen"
"os"
"time"
)
type Coco struct{}
@ -13,12 +17,37 @@ func (c *Coco) Generate(plugin *protogen.Plugin) error {
if len(plugin.Files) == 0 {
return nil
}
c.format(plugin)
c.generateRouterMap(plugin)
c.generateRouterImpl(plugin)
return nil
}
func (c *Coco) format(plugin *protogen.Plugin) {
for _, pbFile := range plugin.Files {
filename := pbFile.Desc.Path()
file, err := os.Open(filename)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "format pbFile %s failed: %v\n", filename, err)
continue
}
buf := new(bytes.Buffer)
parser := proto.NewParser(file)
parser.Filename(filename)
def, err := parser.Parse()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "parse pbFile %s failed: %v\n", filename, err)
continue
}
protofmt.NewFormatter(buf, " ").Format(def)
if err := os.WriteFile(filename, buf.Bytes(), os.ModePerm); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "rewrite pbFile %s failed: %v\n", filename, err)
continue
}
}
}
func (c *Coco) generateRouterMap(plugin *protogen.Plugin) {
for _, pbFile := range plugin.Files {
// service empty
@ -29,6 +58,8 @@ func (c *Coco) generateRouterMap(plugin *protogen.Plugin) {
filename := fmt.Sprintf("autogen_router_%s.go", pbFile.GeneratedFilenamePrefix)
g := plugin.NewGeneratedFile(filename, pbFile.GoImportPath)
g.P("// Code generated by protoc-gen-coco. DO NOT EDIT.")
g.P("// source: ", pbFile.GeneratedFilenamePrefix, ".proto")
g.P("// generate at: ", time.Now().Format("2006-01-02 15:04:05"))
g.P()
g.P("package ", pbFile.GoPackageName)
g.P()