You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
947 B
34 lines
947 B
2 years ago
|
package protogen
|
||
|
|
||
|
import (
|
||
|
"github.com/golang/protobuf/proto"
|
||
|
"google.golang.org/genproto/googleapis/api/annotations"
|
||
|
"google.golang.org/protobuf/compiler/protogen"
|
||
|
"google.golang.org/protobuf/types/descriptorpb"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
METHOD_GET = "GET"
|
||
|
METHOD_POST = "POST"
|
||
|
METHOD_DELETE = "DELETE"
|
||
|
)
|
||
|
|
||
|
func GetProtoMethod(m *protogen.Method) (method string, path string) {
|
||
|
if op, ok := m.Desc.Options().(*descriptorpb.MethodOptions); ok {
|
||
|
if opts, err := proto.GetExtension(op, annotations.E_Http); err != nil {
|
||
|
//log.Println(err)
|
||
|
} else {
|
||
|
if vv, ok := opts.(*annotations.HttpRule); ok {
|
||
|
if vvv, ok := vv.Pattern.(*annotations.HttpRule_Get); ok {
|
||
|
return METHOD_GET, vvv.Get
|
||
|
} else if vvv, ok := vv.Pattern.(*annotations.HttpRule_Post); ok {
|
||
|
return METHOD_POST, vvv.Post
|
||
|
} else if vvv, ok := vv.Pattern.(*annotations.HttpRule_Delete); ok {
|
||
|
return METHOD_DELETE, vvv.Delete
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return "", ""
|
||
|
}
|