http_descriptor.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package genbm
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/golang/glog"
  6. "github.com/golang/protobuf/proto"
  7. "github.com/golang/protobuf/protoc-gen-go/descriptor"
  8. "google.golang.org/genproto/googleapis/api/annotations"
  9. )
  10. // BMServerDescriptor descriptor for BM server
  11. type BMServerDescriptor struct {
  12. Name string
  13. ProtoService *descriptor.ServiceDescriptorProto
  14. Methods []*BMMethodDescriptor
  15. }
  16. // BMMethodDescriptor descriptor for BM http method
  17. type BMMethodDescriptor struct {
  18. Name string
  19. Method string
  20. PathPattern string
  21. RequestType string
  22. ReplyType string
  23. ProtoMethod *descriptor.MethodDescriptorProto
  24. HTTPRule *annotations.HttpRule
  25. }
  26. // ParseBMServer parse BMServerDescriptor form service descriptor proto
  27. func ParseBMServer(service *descriptor.ServiceDescriptorProto) (*BMServerDescriptor, error) {
  28. glog.V(1).Infof("parse bmserver from service %s", service.GetName())
  29. serverDesc := &BMServerDescriptor{
  30. Name: service.GetName(),
  31. ProtoService: service,
  32. }
  33. for _, method := range service.GetMethod() {
  34. if !HasHTTPRuleOptions(method) {
  35. glog.V(5).Infof("method %s not include http rule, skipped", method.GetName())
  36. continue
  37. }
  38. bmMethod, err := ParseBMMethod(method)
  39. if err != nil {
  40. return nil, err
  41. }
  42. serverDesc.Methods = append(serverDesc.Methods, bmMethod)
  43. }
  44. return serverDesc, nil
  45. }
  46. // ParseBMMethod parse BMMethodDescriptor form method descriptor proto
  47. func ParseBMMethod(method *descriptor.MethodDescriptorProto) (*BMMethodDescriptor, error) {
  48. glog.V(1).Infof("parse bmmethod from method %s", method.GetName())
  49. ext, err := proto.GetExtension(method.GetOptions(), annotations.E_Http)
  50. if err != nil {
  51. return nil, fmt.Errorf("get extension error: %s", err)
  52. }
  53. rule := ext.(*annotations.HttpRule)
  54. var httpMethod string
  55. var pathPattern string
  56. switch pattern := rule.Pattern.(type) {
  57. case *annotations.HttpRule_Get:
  58. pathPattern = pattern.Get
  59. httpMethod = http.MethodGet
  60. case *annotations.HttpRule_Put:
  61. pathPattern = pattern.Put
  62. httpMethod = http.MethodPut
  63. case *annotations.HttpRule_Post:
  64. pathPattern = pattern.Post
  65. httpMethod = http.MethodPost
  66. case *annotations.HttpRule_Patch:
  67. pathPattern = pattern.Patch
  68. httpMethod = http.MethodPatch
  69. case *annotations.HttpRule_Delete:
  70. pathPattern = pattern.Delete
  71. httpMethod = http.MethodDelete
  72. default:
  73. return nil, fmt.Errorf("unsupport http pattern %s", rule.Pattern)
  74. }
  75. if len(rule.AdditionalBindings) != 0 {
  76. glog.Warningf("unsupport additional binding, additional binding will be ignored")
  77. }
  78. // TODO: support use type from other package
  79. requestType := splitLastElem(method.GetInputType(), ".")
  80. replyType := splitLastElem(method.GetOutputType(), ".")
  81. bmMethod := &BMMethodDescriptor{
  82. Name: method.GetName(),
  83. Method: httpMethod,
  84. PathPattern: pathPattern,
  85. RequestType: requestType,
  86. ReplyType: replyType,
  87. ProtoMethod: method,
  88. HTTPRule: rule,
  89. }
  90. glog.V(5).Infof("bmMethod %s: %s %s, Request:%s Reply: %s", bmMethod.Name, bmMethod.Method, bmMethod.PathPattern, bmMethod.RequestType, bmMethod.ReplyType)
  91. return bmMethod, nil
  92. }
  93. // HasHTTPRuleOptions check method has httprule extension
  94. func HasHTTPRuleOptions(method *descriptor.MethodDescriptorProto) bool {
  95. options := method.GetOptions()
  96. if options == nil {
  97. return false
  98. }
  99. return proto.HasExtension(options, annotations.E_Http)
  100. }