feat: add rpc

This commit is contained in:
xuthus5 2023-09-29 12:26:01 +08:00
parent 1775a7b444
commit dda09f2e32
Signed by: xuthus5
GPG Key ID: A23CF9620CBB55F9
4 changed files with 19 additions and 10 deletions

View File

@ -10,7 +10,7 @@ package main
import "gitter.top/common/gobuf"
func main() {
parser, err := NewParser("example.proto")
parser, err := gobuf.NewParser("example.proto")
if err != nil {
// do something
}

View File

@ -81,25 +81,25 @@ func (parser *Parser) AddRPC(serviceName, rpcName string) error {
service.Elements = append(service.Elements, &proto.RPC{
Comment: &proto.Comment{
Lines: []string{
" @desc: ",
" @author: ",
" @method: ",
" @desc: 描述",
" @author: 作者",
" @method: GET",
" @api: /" + calm2Case(rpcName),
},
},
Name: rpcName,
RequestType: rpcName + "Req",
ReturnsType: rpcName + "Resp",
RequestType: toUpperCamlCase(serviceName) + toUpperCamlCase(rpcName) + "Req",
ReturnsType: toUpperCamlCase(serviceName) + toUpperCamlCase(rpcName) + "Resp",
Parent: service,
})
}))
parser.proto.Elements = append(parser.proto.Elements, &proto.Message{
Name: rpcName + "Req",
Name: toUpperCamlCase(serviceName) + toUpperCamlCase(rpcName) + "Req",
Parent: parser.proto,
})
parser.proto.Elements = append(parser.proto.Elements, &proto.Message{
Name: rpcName + "Resp",
Name: toUpperCamlCase(serviceName) + toUpperCamlCase(rpcName) + "Resp",
Parent: parser.proto,
})
@ -115,8 +115,8 @@ func (parser *Parser) AddService(serviceName string) error {
Comment: &proto.Comment{
Lines: []string{
" @route_group: true",
" @base_url: /api/" + calm2Case(serviceName),
" @gen_to: ./internal/controller/" + calm2Case(serviceName) + "_controller.go",
" @base_url: /v1/" + calm2Case(serviceName),
" @gen_to: ./api_services/v1/" + calm2Case(serviceName) + "_controller.go",
},
},
Name: serviceName,

View File

@ -26,4 +26,6 @@ func TestParser_AddService(t *testing.T) {
assert.Nil(t, err)
err = parser.AddService("UserInfo")
assert.Nil(t, err)
err = parser.AddRPC("UserInfo", "Detail")
assert.Nil(t, err)
}

View File

@ -28,6 +28,13 @@ func toLowerCamelCase(str string) string {
return ""
}
func toUpperCamlCase(str string) string {
if len(str) != 0 {
return string(unicode.ToUpper(rune(str[0]))) + str[1:]
}
return ""
}
func getFieldValue(fieldName string, cases TagValueStyle) string {
switch cases {
case Underline: