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.

57 lines
1.6 KiB

2 years ago
package protogen
import (
2 years ago
"git.diulo.com/mogfee/kit/third_party/auth"
2 years ago
"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"
1 year ago
METHOD_PUT = "PUT"
2 years ago
)
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
1 year ago
} else if vvv, ok := vv.Pattern.(*annotations.HttpRule_Put); ok {
return METHOD_PUT, vvv.Put
2 years ago
}
}
}
}
return "", ""
}
2 years ago
2 years ago
func GetAuthKey(m *protogen.Method) (authKey string, needAuth bool) {
2 years ago
if op, ok := m.Desc.Options().(*descriptorpb.MethodOptions); ok {
2 years ago
if opts, err := proto.GetExtension(op, auth.E_AuthKey); err == nil {
if vv := opts.(*string); ok && *vv != "" {
2 years ago
return *vv, true
2 years ago
}
}
}
if op, ok := m.Desc.Options().(*descriptorpb.MethodOptions); ok {
2 years ago
if opts, err := proto.GetExtension(op, auth.E_Auth); err == nil {
if v, ok := opts.(*bool); ok && *v {
2 years ago
return "", true
2 years ago
}
}
}
2 years ago
return "", false
2 years ago
}