standalone.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/protobuf/internal/errors"
  7. "google.golang.org/protobuf/reflect/protoreflect"
  8. )
  9. // TODO: Should the constructors take in a value rather than a pointer?
  10. // TODO: Support initializing StandaloneMessage from a google.protobuf.Type?
  11. // StandaloneMessage is a constructor for a protoreflect.MessageDescriptor
  12. // that does not have a parent and has no child declarations.
  13. type StandaloneMessage struct {
  14. Syntax protoreflect.Syntax
  15. FullName protoreflect.FullName
  16. Fields []Field
  17. Oneofs []Oneof
  18. ReservedNames []protoreflect.Name
  19. ReservedRanges [][2]protoreflect.FieldNumber
  20. ExtensionRanges [][2]protoreflect.FieldNumber
  21. ExtensionRangeOptions []protoreflect.ProtoMessage
  22. Options protoreflect.ProtoMessage
  23. IsMapEntry bool
  24. fields fieldsMeta
  25. oneofs oneofsMeta
  26. nums numbersMeta
  27. }
  28. // NewMessage creates a new protoreflect.MessageDescriptor.
  29. // The caller must relinquish full ownership of the input t and must not
  30. // access or mutate any fields.
  31. func NewMessage(t *StandaloneMessage) (protoreflect.MessageDescriptor, error) {
  32. mt := standaloneMessage{t}
  33. if err := validateMessage(mt); err != nil {
  34. return nil, err
  35. }
  36. return mt, nil
  37. }
  38. // NewMessages creates a set of new protoreflect.MessageDescriptors.
  39. //
  40. // This constructor permits the creation of cyclic message types that depend
  41. // on each other. For example, message A may have a field of type message B,
  42. // where message B may have a field of type message A. In such a case,
  43. // a placeholder message is used for these cyclic references.
  44. //
  45. // The caller must relinquish full ownership of the input ts and must not
  46. // access or mutate any fields.
  47. func NewMessages(ts []*StandaloneMessage) ([]protoreflect.MessageDescriptor, error) {
  48. // TODO: Should this be []*T or []T?
  49. // TODO: NewMessages is a superset of NewMessage. Do we need NewMessage?
  50. ms := map[protoreflect.FullName]protoreflect.MessageDescriptor{}
  51. for _, t := range ts {
  52. if _, ok := ms[t.FullName]; ok {
  53. return nil, errors.New("duplicate message %v", t.FullName)
  54. }
  55. ms[t.FullName] = standaloneMessage{t}
  56. }
  57. var mts []protoreflect.MessageDescriptor
  58. for _, t := range ts {
  59. for i, f := range t.Fields {
  60. // Resolve placeholder messages with a concrete standalone message.
  61. // If this fails, validateMessage will complain about it later.
  62. if f.MessageType != nil && f.MessageType.IsPlaceholder() && !f.IsWeak {
  63. if m, ok := ms[f.MessageType.FullName()]; ok {
  64. t.Fields[i].MessageType = m
  65. }
  66. }
  67. }
  68. mt := standaloneMessage{t}
  69. if err := validateMessage(mt); err != nil {
  70. return nil, err
  71. }
  72. mts = append(mts, mt)
  73. }
  74. return mts, nil
  75. }
  76. // StandaloneEnum is a constructor for a protoreflect.EnumDescriptor
  77. // that does not have a parent.
  78. type StandaloneEnum struct {
  79. Syntax protoreflect.Syntax
  80. FullName protoreflect.FullName
  81. Values []EnumValue
  82. ReservedNames []protoreflect.Name
  83. ReservedRanges [][2]protoreflect.EnumNumber
  84. Options protoreflect.ProtoMessage
  85. vals enumValuesMeta
  86. }
  87. // NewEnum creates a new protoreflect.EnumDescriptor.
  88. // The caller must relinquish full ownership of the input t and must not
  89. // access or mutate any fields.
  90. func NewEnum(t *StandaloneEnum) (protoreflect.EnumDescriptor, error) {
  91. et := standaloneEnum{t}
  92. if err := validateEnum(et); err != nil {
  93. return nil, err
  94. }
  95. return et, nil
  96. }
  97. // StandaloneExtension is a constructor for a protoreflect.ExtensionDescriptor
  98. // that does not have a parent.
  99. type StandaloneExtension struct {
  100. FullName protoreflect.FullName
  101. Number protoreflect.FieldNumber
  102. Cardinality protoreflect.Cardinality
  103. Kind protoreflect.Kind
  104. Default protoreflect.Value
  105. MessageType protoreflect.MessageDescriptor
  106. EnumType protoreflect.EnumDescriptor
  107. ExtendedType protoreflect.MessageDescriptor
  108. Options protoreflect.ProtoMessage
  109. IsPacked OptionalBool
  110. dv defaultValue
  111. }
  112. // NewExtension creates a new protoreflect.ExtensionDescriptor.
  113. // The caller must relinquish full ownership of the input t and must not
  114. // access or mutate any fields.
  115. func NewExtension(t *StandaloneExtension) (protoreflect.ExtensionDescriptor, error) {
  116. xt := standaloneExtension{t}
  117. if err := validateExtension(xt); err != nil {
  118. return nil, err
  119. }
  120. return xt, nil
  121. }