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