standalone.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. "github.com/golang/protobuf/v2/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. dv defaultValue
  65. }
  66. // NewExtension creates a new protoreflect.ExtensionDescriptor.
  67. // The caller must relinquish full ownership of the input t and must not
  68. // access or mutate any fields.
  69. func NewExtension(t *StandaloneExtension) (protoreflect.ExtensionDescriptor, error) {
  70. xt := standaloneExtension{t}
  71. if err := validateExtension(xt); err != nil {
  72. return nil, err
  73. }
  74. return xt, nil
  75. }