standalone.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package prototype
  5. import (
  6. "google.golang.org/proto/reflect/protoreflect"
  7. )
  8. // TODO: Should the constructors take in a value rather than a pointer?
  9. // TODO: Support initializing StandaloneMessage from a google.protobuf.Type?
  10. // StandaloneMessage is a constructor for a protoreflect.MessageDescriptor
  11. // that does not have a parent and has no child declarations.
  12. type StandaloneMessage struct {
  13. Syntax protoreflect.Syntax
  14. FullName protoreflect.FullName
  15. IsMapEntry bool
  16. Fields []Field
  17. Oneofs []Oneof
  18. ExtensionRanges [][2]protoreflect.FieldNumber
  19. fields fieldsMeta
  20. oneofs oneofsMeta
  21. nums numbersMeta
  22. }
  23. // NewMessage creates a new protoreflect.MessageDescriptor.
  24. // The caller must relinquish full ownership of the input t and must not
  25. // access or mutate any fields.
  26. func NewMessage(t *StandaloneMessage) (protoreflect.MessageDescriptor, error) {
  27. mt := standaloneMessage{t}
  28. if err := validateMessage(mt); err != nil {
  29. return nil, err
  30. }
  31. return mt, nil
  32. }
  33. // StandaloneEnum is a constructor for a protoreflect.EnumDescriptor
  34. // that does not have a parent.
  35. type StandaloneEnum struct {
  36. Syntax protoreflect.Syntax
  37. FullName protoreflect.FullName
  38. Values []EnumValue
  39. vals enumValuesMeta
  40. }
  41. // NewEnum creates a new protoreflect.EnumDescriptor.
  42. // The caller must relinquish full ownership of the input t and must not
  43. // access or mutate any fields.
  44. func NewEnum(t *StandaloneEnum) (protoreflect.EnumDescriptor, error) {
  45. et := standaloneEnum{t}
  46. if err := validateEnum(et); err != nil {
  47. return nil, err
  48. }
  49. return et, nil
  50. }
  51. // StandaloneExtension is a constructor for a protoreflect.ExtensionDescriptor
  52. // that does not have a parent.
  53. type StandaloneExtension struct {
  54. Syntax protoreflect.Syntax
  55. FullName protoreflect.FullName
  56. Number protoreflect.FieldNumber
  57. Cardinality protoreflect.Cardinality
  58. Kind protoreflect.Kind
  59. IsPacked bool
  60. Default protoreflect.Value
  61. MessageType protoreflect.MessageDescriptor
  62. EnumType protoreflect.EnumDescriptor
  63. ExtendedType protoreflect.MessageDescriptor
  64. }
  65. // NewExtension creates a new protoreflect.ExtensionDescriptor.
  66. // The caller must relinquish full ownership of the input t and must not
  67. // access or mutate any fields.
  68. func NewExtension(t *StandaloneExtension) (protoreflect.ExtensionDescriptor, error) {
  69. xt := standaloneExtension{t}
  70. if err := validateExtension(xt); err != nil {
  71. return nil, err
  72. }
  73. return xt, nil
  74. }