protoc-gen-coco/comment_parse.go
2023-03-12 23:37:02 +08:00

133 lines
2.8 KiB
Go

package main
import (
"google.golang.org/protobuf/compiler/protogen"
"regexp"
"strings"
)
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 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 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 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 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 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)
}