protoc-gen-coco/comment_parse.go

234 lines
5.1 KiB
Go

package main
import (
"regexp"
"strings"
"google.golang.org/protobuf/compiler/protogen"
)
func IsCommentRouterGroup(comments protogen.CommentSet) bool {
raw := comments.Leading.String()
split := strings.Split(raw, "\n")
if len(split) == 0 {
return false
}
re := regexp.MustCompile("@route_group:\\s*(true|false)")
for _, s := range split {
match := re.FindAllStringSubmatch(s, -1)
if len(match) == 0 {
continue
}
if len(match[0]) != 2 {
continue
}
if match[0][1] != "true" {
return false
}
return true
}
return false
}
func GetCommentGenerateTo(comments protogen.CommentSet) string {
raw := comments.Leading.String()
split := strings.Split(raw, "\n")
re := regexp.MustCompile("@gen_to:\\s*([\\w|/|\\.]*)")
for _, s := range split {
match := re.FindAllStringSubmatch(s, -1)
if len(match) == 0 {
continue
}
if len(match[0]) != 2 {
continue
}
return trim(match[0][1])
}
return ""
}
func GetCommentRpcGenerateTo(comments protogen.CommentSet) string {
raw := comments.Leading.String()
split := strings.Split(raw, "\n")
re := regexp.MustCompile("@rpc_to:\\s*([\\w|/|\\.]*)")
for _, s := range split {
match := re.FindAllStringSubmatch(s, -1)
if len(match) == 0 {
continue
}
if len(match[0]) != 2 {
continue
}
return trim(match[0][1])
}
return ""
}
func GetCommentBaseURL(comments protogen.CommentSet) string {
raw := comments.Leading.String()
split := strings.Split(raw, "\n")
re := regexp.MustCompile("@base_url:\\s*([\\w|/]*)")
for _, s := range split {
match := re.FindAllStringSubmatch(s, -1)
if len(match) == 0 {
continue
}
if len(match[0]) != 2 {
continue
}
return trim(match[0][1])
}
return "/"
}
func GetCommentHttpMethod(comments protogen.CommentSet) string {
raw := comments.Leading.String()
split := strings.Split(raw, "\n")
re := regexp.MustCompile("@method:\\s*([\\w]*)")
for _, s := range split {
match := re.FindAllStringSubmatch(s, -1)
if len(match) == 0 {
continue
}
if len(match[0]) != 2 {
continue
}
return trim(match[0][1])
}
return "GET"
}
func GetCommentAuthor(comments protogen.CommentSet) string {
raw := comments.Leading.String()
split := strings.Split(raw, "\n")
re := regexp.MustCompile("@author:\\s*(.*)")
for _, s := range split {
match := re.FindAllStringSubmatch(s, -1)
if len(match) == 0 {
continue
}
if len(match[0]) != 2 {
continue
}
return trim(match[0][1])
}
return ""
}
func GetCommentDescribe(comments protogen.CommentSet) string {
raw := comments.Leading.String()
split := strings.Split(raw, "\n")
re := regexp.MustCompile("@desc:\\s*(.*)")
for _, s := range split {
match := re.FindAllStringSubmatch(s, -1)
if len(match) == 0 {
continue
}
if len(match[0]) != 2 {
continue
}
return trim(match[0][1])
}
return ""
}
func GetCommentApiURL(method *protogen.Method) string {
raw := method.Comments.Leading.String()
split := strings.Split(raw, "\n")
re := regexp.MustCompile("@api:\\s*([\\w|/]*)")
for _, s := range split {
match := re.FindAllStringSubmatch(s, -1)
if len(match) == 0 {
continue
}
if len(match[0]) != 2 {
continue
}
return match[0][1]
}
return "/" + CamelCaseToJavascriptCase(method.GoName)
}
func GetCommentTableName(message *protogen.Message) string {
raw := message.Comments.Leading.String()
split := strings.Split(raw, "\n")
re := regexp.MustCompile("@table_name:\\s*(.*)")
for _, s := range split {
match := re.FindAllStringSubmatch(s, -1)
if len(match) == 0 {
continue
}
if len(match[0]) != 2 {
continue
}
return trim(match[0][1])
}
return CamelCaseToUnderscore(message.GoIdent.GoName)
}
func GetCommentBsonName(field *protogen.Field) string {
raw := field.Comments.Leading.String()
split := strings.Split(raw, "\n")
re := regexp.MustCompile("@bson:\\s*(.*)")
var value string
for _, s := range split {
match := re.FindAllStringSubmatch(s, -1)
if len(match) == 0 {
continue
}
if len(match[0]) != 2 {
continue
}
value = trim(match[0][1])
}
if value == "" {
value = CamelCaseToUnderscore(field.GoName)
}
if strings.EqualFold(value, "id") {
value = "_id"
}
return value
}
func GetRPCInfoList(services *protogen.Service) []*rpcInfo {
if len(services.Methods) == 0 {
return nil
}
var list []*rpcInfo
for _, method := range services.Methods {
node := &rpcInfo{
FuncName: method.GoName,
RouterPath: GetCommentApiURL(method),
Method: GetCommentHttpMethod(method.Comments),
Author: GetCommentAuthor(method.Comments),
Describe: GetCommentDescribe(method.Comments),
ReqName: method.Input.GoIdent.GoName,
RespName: method.Output.GoIdent.GoName,
}
list = append(list, node)
}
return list
}
func IsCommentModel(message *protogen.Message) bool {
raw := message.Comments.Leading.String()
split := strings.Split(raw, "\n")
re := regexp.MustCompile("@model:\\s*(true|false)")
for _, s := range split {
match := re.FindAllStringSubmatch(s, -1)
if len(match) == 0 {
continue
}
if len(match[0]) != 2 {
continue
}
if match[0][1] == "true" {
return true
}
}
if strings.HasPrefix(message.GoIdent.GoName, "Model") {
return true
}
return false
}