gobuf/gobuf.go

141 lines
3.2 KiB
Go
Raw Permalink Normal View History

2023-03-20 17:08:22 +00:00
package gobuf
import (
"bytes"
2023-03-20 17:20:24 +00:00
"fmt"
2023-03-20 17:08:22 +00:00
"io"
"os"
2023-07-23 14:22:23 +00:00
"github.com/emicklei/proto"
"gitter.top/sync/proto-contrib/pkg/protofmt"
2023-03-20 17:08:22 +00:00
)
type Parser struct {
filename string
rawData io.Reader
proto *proto.Proto
}
func NewParser(file string) (*Parser, error) {
reader, err := os.Open(file)
if err != nil {
return nil, err
}
defer reader.Close()
parser := proto.NewParser(reader)
definition, err := parser.Parse()
if err != nil {
return nil, err
}
return &Parser{
filename: file,
rawData: reader,
proto: definition,
}, nil
}
func (parser *Parser) ExistService(serviceName string) bool {
var result bool
proto.Walk(parser.proto, proto.WithService(func(service *proto.Service) {
if service.Name == serviceName {
result = true
}
}))
return result
}
func (parser *Parser) ExistMessage(messageName string) bool {
var result bool
proto.Walk(parser.proto, proto.WithMessage(func(message *proto.Message) {
if message.Name == messageName {
result = true
}
}))
return result
}
func (parser *Parser) ExistRPC(serviceName, rpcName string) bool {
var result bool
proto.Walk(parser.proto, proto.WithService(func(service *proto.Service) {
if service.Name != serviceName {
return
}
for _, element := range service.Elements {
if rpc, ok := element.(*proto.RPC); ok {
if rpc.Name == rpcName {
result = true
return
}
}
}
}))
return result
}
func (parser *Parser) AddRPC(serviceName, rpcName string) error {
proto.Walk(parser.proto, proto.WithService(func(service *proto.Service) {
if service.Name != serviceName {
return
}
service.Elements = append(service.Elements, &proto.RPC{
Comment: &proto.Comment{
Lines: []string{
2023-09-29 04:26:01 +00:00
" @desc: 描述",
" @author: 作者",
" @method: GET",
2023-03-20 17:08:22 +00:00
" @api: /" + calm2Case(rpcName),
},
},
Name: rpcName,
2023-09-29 04:26:01 +00:00
RequestType: toUpperCamlCase(serviceName) + toUpperCamlCase(rpcName) + "Req",
ReturnsType: toUpperCamlCase(serviceName) + toUpperCamlCase(rpcName) + "Resp",
2023-03-20 17:08:22 +00:00
Parent: service,
})
}))
parser.proto.Elements = append(parser.proto.Elements, &proto.Message{
2023-09-29 04:26:01 +00:00
Name: toUpperCamlCase(serviceName) + toUpperCamlCase(rpcName) + "Req",
2023-03-20 17:08:22 +00:00
Parent: parser.proto,
})
parser.proto.Elements = append(parser.proto.Elements, &proto.Message{
2023-09-29 04:26:01 +00:00
Name: toUpperCamlCase(serviceName) + toUpperCamlCase(rpcName) + "Resp",
2023-03-20 17:08:22 +00:00
Parent: parser.proto,
})
return parser.writeSync()
}
2023-03-20 17:20:24 +00:00
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",
2023-09-29 04:26:01 +00:00
" @base_url: /v1/" + calm2Case(serviceName),
2024-03-10 16:46:06 +00:00
" @gen_to: ./services/controller/v1/" + calm2Case(serviceName) + "_controller.go",
2023-03-20 17:20:24 +00:00
},
},
Name: serviceName,
Parent: parser.proto,
})
return parser.writeSync()
}
2023-03-20 17:08:22 +00:00
func (parser *Parser) writeSync() error {
var buf = new(bytes.Buffer)
protofmt.NewFormatter(buf, " ").Format(parser.proto) // 1 tab
// write back to input
if err := os.WriteFile(parser.filename, buf.Bytes(), os.ModePerm); err != nil {
return err
}
return nil
}