fix: add service and add rpc api

This commit is contained in:
Young Xu 2023-03-21 01:20:24 +08:00
parent 696184f41a
commit 67ae76c9b9
Signed by: xuthus5
GPG Key ID: A23CF9620CBB55F9
3 changed files with 35 additions and 0 deletions

View File

@ -68,3 +68,10 @@ message DownloadResp {}
message UpdateUserInfoReq {}
message UpdateUserInfoResp {}
// @route_group: true
// @route_api: /
// @gen_to: ./
service UserInfo {}

View File

@ -2,6 +2,7 @@ package gobuf
import (
"bytes"
"fmt"
"github.com/emicklei/proto"
"gitter.top/sync/proto-contrib/pkg/protofmt"
"io"
@ -104,6 +105,26 @@ func (parser *Parser) AddRPC(serviceName, rpcName string) error {
return parser.writeSync()
}
func (parser *Parser) AddService(serviceName string) error {
if parser.ExistService(serviceName) {
return fmt.Errorf("service name exist")
}
parser.proto.Elements = append(parser.proto.Elements, &proto.Service{
Comment: &proto.Comment{
Lines: []string{
" @route_group: true",
" @base_url: /api/" + calm2Case(serviceName),
" @gen_to: ./internal/controller/" + calm2Case(serviceName) + ".go",
},
},
Name: serviceName,
Parent: parser.proto,
})
return parser.writeSync()
}
func (parser *Parser) writeSync() error {
var buf = new(bytes.Buffer)

View File

@ -20,3 +20,10 @@ func TestParser_AddRPC(t *testing.T) {
err = parser.AddRPC("File", "UpdateUserInfo")
assert.Nil(t, err)
}
func TestParser_AddService(t *testing.T) {
parser, err := NewParser("example.proto")
assert.Nil(t, err)
err = parser.AddService("UserInfo")
assert.Nil(t, err)
}