PageRenderTime 77ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor/types.go

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 322 lines | 211 code | 34 blank | 77 comment | 27 complexity | ea51215d24467d19da3745c1b0213255 MD5 | raw file
  1. package descriptor
  2. import (
  3. "fmt"
  4. "strings"
  5. descriptor "github.com/golang/protobuf/protoc-gen-go/descriptor"
  6. gogen "github.com/golang/protobuf/protoc-gen-go/generator"
  7. "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/httprule"
  8. )
  9. // GoPackage represents a golang package
  10. type GoPackage struct {
  11. // Path is the package path to the package.
  12. Path string
  13. // Name is the package name of the package
  14. Name string
  15. // Alias is an alias of the package unique within the current invokation of grpc-gateway generator.
  16. Alias string
  17. }
  18. // Standard returns whether the import is a golang standard package.
  19. func (p GoPackage) Standard() bool {
  20. return !strings.Contains(p.Path, ".")
  21. }
  22. // String returns a string representation of this package in the form of import line in golang.
  23. func (p GoPackage) String() string {
  24. if p.Alias == "" {
  25. return fmt.Sprintf("%q", p.Path)
  26. }
  27. return fmt.Sprintf("%s %q", p.Alias, p.Path)
  28. }
  29. // File wraps descriptor.FileDescriptorProto for richer features.
  30. type File struct {
  31. *descriptor.FileDescriptorProto
  32. // GoPkg is the go package of the go file generated from this file..
  33. GoPkg GoPackage
  34. // Messages is the list of messages defined in this file.
  35. Messages []*Message
  36. // Enums is the list of enums defined in this file.
  37. Enums []*Enum
  38. // Services is the list of services defined in this file.
  39. Services []*Service
  40. }
  41. // proto2 determines if the syntax of the file is proto2.
  42. func (f *File) proto2() bool {
  43. return f.Syntax == nil || f.GetSyntax() == "proto2"
  44. }
  45. // Message describes a protocol buffer message types
  46. type Message struct {
  47. // File is the file where the message is defined
  48. File *File
  49. // Outers is a list of outer messages if this message is a nested type.
  50. Outers []string
  51. *descriptor.DescriptorProto
  52. Fields []*Field
  53. // Index is proto path index of this message in File.
  54. Index int
  55. }
  56. // FQMN returns a fully qualified message name of this message.
  57. func (m *Message) FQMN() string {
  58. components := []string{""}
  59. if m.File.Package != nil {
  60. components = append(components, m.File.GetPackage())
  61. }
  62. components = append(components, m.Outers...)
  63. components = append(components, m.GetName())
  64. return strings.Join(components, ".")
  65. }
  66. // GoType returns a go type name for the message type.
  67. // It prefixes the type name with the package alias if
  68. // its belonging package is not "currentPackage".
  69. func (m *Message) GoType(currentPackage string) string {
  70. var components []string
  71. components = append(components, m.Outers...)
  72. components = append(components, m.GetName())
  73. name := strings.Join(components, "_")
  74. if m.File.GoPkg.Path == currentPackage {
  75. return name
  76. }
  77. pkg := m.File.GoPkg.Name
  78. if alias := m.File.GoPkg.Alias; alias != "" {
  79. pkg = alias
  80. }
  81. return fmt.Sprintf("%s.%s", pkg, name)
  82. }
  83. // Enum describes a protocol buffer enum types
  84. type Enum struct {
  85. // File is the file where the enum is defined
  86. File *File
  87. // Outers is a list of outer messages if this enum is a nested type.
  88. Outers []string
  89. *descriptor.EnumDescriptorProto
  90. Index int
  91. }
  92. // FQEN returns a fully qualified enum name of this enum.
  93. func (e *Enum) FQEN() string {
  94. components := []string{""}
  95. if e.File.Package != nil {
  96. components = append(components, e.File.GetPackage())
  97. }
  98. components = append(components, e.Outers...)
  99. components = append(components, e.GetName())
  100. return strings.Join(components, ".")
  101. }
  102. // Service wraps descriptor.ServiceDescriptorProto for richer features.
  103. type Service struct {
  104. // File is the file where this service is defined.
  105. File *File
  106. *descriptor.ServiceDescriptorProto
  107. // Methods is the list of methods defined in this service.
  108. Methods []*Method
  109. }
  110. // Method wraps descriptor.MethodDescriptorProto for richer features.
  111. type Method struct {
  112. // Service is the service which this method belongs to.
  113. Service *Service
  114. *descriptor.MethodDescriptorProto
  115. // RequestType is the message type of requests to this method.
  116. RequestType *Message
  117. // ResponseType is the message type of responses from this method.
  118. ResponseType *Message
  119. Bindings []*Binding
  120. }
  121. // Binding describes how an HTTP endpoint is bound to a gRPC method.
  122. type Binding struct {
  123. // Method is the method which the endpoint is bound to.
  124. Method *Method
  125. // Index is a zero-origin index of the binding in the target method
  126. Index int
  127. // PathTmpl is path template where this method is mapped to.
  128. PathTmpl httprule.Template
  129. // HTTPMethod is the HTTP method which this method is mapped to.
  130. HTTPMethod string
  131. // PathParams is the list of parameters provided in HTTP request paths.
  132. PathParams []Parameter
  133. // Body describes parameters provided in HTTP request body.
  134. Body *Body
  135. }
  136. // ExplicitParams returns a list of explicitly bound parameters of "b",
  137. // i.e. a union of field path for body and field paths for path parameters.
  138. func (b *Binding) ExplicitParams() []string {
  139. var result []string
  140. if b.Body != nil {
  141. result = append(result, b.Body.FieldPath.String())
  142. }
  143. for _, p := range b.PathParams {
  144. result = append(result, p.FieldPath.String())
  145. }
  146. return result
  147. }
  148. // Field wraps descriptor.FieldDescriptorProto for richer features.
  149. type Field struct {
  150. // Message is the message type which this field belongs to.
  151. Message *Message
  152. // FieldMessage is the message type of the field.
  153. FieldMessage *Message
  154. *descriptor.FieldDescriptorProto
  155. }
  156. // Parameter is a parameter provided in http requests
  157. type Parameter struct {
  158. // FieldPath is a path to a proto field which this parameter is mapped to.
  159. FieldPath
  160. // Target is the proto field which this parameter is mapped to.
  161. Target *Field
  162. // Method is the method which this parameter is used for.
  163. Method *Method
  164. }
  165. // ConvertFuncExpr returns a go expression of a converter function.
  166. // The converter function converts a string into a value for the parameter.
  167. func (p Parameter) ConvertFuncExpr() (string, error) {
  168. tbl := proto3ConvertFuncs
  169. if p.Target.Message.File.proto2() {
  170. tbl = proto2ConvertFuncs
  171. }
  172. typ := p.Target.GetType()
  173. conv, ok := tbl[typ]
  174. if !ok {
  175. return "", fmt.Errorf("unsupported field type %s of parameter %s in %s.%s", typ, p.FieldPath, p.Method.Service.GetName(), p.Method.GetName())
  176. }
  177. return conv, nil
  178. }
  179. // Body describes a http requtest body to be sent to the method.
  180. type Body struct {
  181. // FieldPath is a path to a proto field which the request body is mapped to.
  182. // The request body is mapped to the request type itself if FieldPath is empty.
  183. FieldPath FieldPath
  184. }
  185. // RHS returns a right-hand-side expression in go to be used to initialize method request object.
  186. // It starts with "msgExpr", which is the go expression of the method request object.
  187. func (b Body) RHS(msgExpr string) string {
  188. return b.FieldPath.RHS(msgExpr)
  189. }
  190. // FieldPath is a path to a field from a request message.
  191. type FieldPath []FieldPathComponent
  192. // String returns a string representation of the field path.
  193. func (p FieldPath) String() string {
  194. var components []string
  195. for _, c := range p {
  196. components = append(components, c.Name)
  197. }
  198. return strings.Join(components, ".")
  199. }
  200. // IsNestedProto3 indicates whether the FieldPath is a nested Proto3 path.
  201. func (p FieldPath) IsNestedProto3() bool {
  202. if len(p) > 1 && !p[0].Target.Message.File.proto2() {
  203. return true
  204. }
  205. return false
  206. }
  207. // RHS is a right-hand-side expression in go to be used to assign a value to the target field.
  208. // It starts with "msgExpr", which is the go expression of the method request object.
  209. func (p FieldPath) RHS(msgExpr string) string {
  210. l := len(p)
  211. if l == 0 {
  212. return msgExpr
  213. }
  214. components := []string{msgExpr}
  215. for i, c := range p {
  216. if i == l-1 {
  217. components = append(components, c.RHS())
  218. continue
  219. }
  220. components = append(components, c.LHS())
  221. }
  222. return strings.Join(components, ".")
  223. }
  224. // FieldPathComponent is a path component in FieldPath
  225. type FieldPathComponent struct {
  226. // Name is a name of the proto field which this component corresponds to.
  227. // TODO(yugui) is this necessary?
  228. Name string
  229. // Target is the proto field which this component corresponds to.
  230. Target *Field
  231. }
  232. // RHS returns a right-hand-side expression in go for this field.
  233. func (c FieldPathComponent) RHS() string {
  234. return gogen.CamelCase(c.Name)
  235. }
  236. // LHS returns a left-hand-side expression in go for this field.
  237. func (c FieldPathComponent) LHS() string {
  238. if c.Target.Message.File.proto2() {
  239. return fmt.Sprintf("Get%s()", gogen.CamelCase(c.Name))
  240. }
  241. return gogen.CamelCase(c.Name)
  242. }
  243. var (
  244. proto3ConvertFuncs = map[descriptor.FieldDescriptorProto_Type]string{
  245. descriptor.FieldDescriptorProto_TYPE_DOUBLE: "runtime.Float64",
  246. descriptor.FieldDescriptorProto_TYPE_FLOAT: "runtime.Float32",
  247. descriptor.FieldDescriptorProto_TYPE_INT64: "runtime.Int64",
  248. descriptor.FieldDescriptorProto_TYPE_UINT64: "runtime.Uint64",
  249. descriptor.FieldDescriptorProto_TYPE_INT32: "runtime.Int32",
  250. descriptor.FieldDescriptorProto_TYPE_FIXED64: "runtime.Uint64",
  251. descriptor.FieldDescriptorProto_TYPE_FIXED32: "runtime.Uint32",
  252. descriptor.FieldDescriptorProto_TYPE_BOOL: "runtime.Bool",
  253. descriptor.FieldDescriptorProto_TYPE_STRING: "runtime.String",
  254. // FieldDescriptorProto_TYPE_GROUP
  255. // FieldDescriptorProto_TYPE_MESSAGE
  256. // FieldDescriptorProto_TYPE_BYTES
  257. // TODO(yugui) Handle bytes
  258. descriptor.FieldDescriptorProto_TYPE_UINT32: "runtime.Uint32",
  259. // FieldDescriptorProto_TYPE_ENUM
  260. // TODO(yugui) Handle Enum
  261. descriptor.FieldDescriptorProto_TYPE_SFIXED32: "runtime.Int32",
  262. descriptor.FieldDescriptorProto_TYPE_SFIXED64: "runtime.Int64",
  263. descriptor.FieldDescriptorProto_TYPE_SINT32: "runtime.Int32",
  264. descriptor.FieldDescriptorProto_TYPE_SINT64: "runtime.Int64",
  265. }
  266. proto2ConvertFuncs = map[descriptor.FieldDescriptorProto_Type]string{
  267. descriptor.FieldDescriptorProto_TYPE_DOUBLE: "runtime.Float64P",
  268. descriptor.FieldDescriptorProto_TYPE_FLOAT: "runtime.Float32P",
  269. descriptor.FieldDescriptorProto_TYPE_INT64: "runtime.Int64P",
  270. descriptor.FieldDescriptorProto_TYPE_UINT64: "runtime.Uint64P",
  271. descriptor.FieldDescriptorProto_TYPE_INT32: "runtime.Int32P",
  272. descriptor.FieldDescriptorProto_TYPE_FIXED64: "runtime.Uint64P",
  273. descriptor.FieldDescriptorProto_TYPE_FIXED32: "runtime.Uint32P",
  274. descriptor.FieldDescriptorProto_TYPE_BOOL: "runtime.BoolP",
  275. descriptor.FieldDescriptorProto_TYPE_STRING: "runtime.StringP",
  276. // FieldDescriptorProto_TYPE_GROUP
  277. // FieldDescriptorProto_TYPE_MESSAGE
  278. // FieldDescriptorProto_TYPE_BYTES
  279. // TODO(yugui) Handle bytes
  280. descriptor.FieldDescriptorProto_TYPE_UINT32: "runtime.Uint32P",
  281. // FieldDescriptorProto_TYPE_ENUM
  282. // TODO(yugui) Handle Enum
  283. descriptor.FieldDescriptorProto_TYPE_SFIXED32: "runtime.Int32P",
  284. descriptor.FieldDescriptorProto_TYPE_SFIXED64: "runtime.Int64P",
  285. descriptor.FieldDescriptorProto_TYPE_SINT32: "runtime.Int32P",
  286. descriptor.FieldDescriptorProto_TYPE_SINT64: "runtime.Int64P",
  287. }
  288. )