standalone.go 4.2 KB

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