methods.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright 2019 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 protoiface contains types referenced or implemented by messages.
  5. //
  6. // WARNING: This package should only be imported by message implementations.
  7. // The functionality found in this package should be accessed through
  8. // higher-level abstractions provided by the proto package.
  9. package protoiface
  10. import (
  11. "google.golang.org/protobuf/internal/pragma"
  12. "google.golang.org/protobuf/reflect/protoreflect"
  13. )
  14. // Methods is a set of optional fast-path implementations of various operations.
  15. type Methods = struct {
  16. pragma.NoUnkeyedLiterals
  17. // Flags indicate support for optional features.
  18. Flags SupportFlags
  19. // Size returns the size in bytes of the wire-format encoding of a message.
  20. // Marshal must be provided if a custom Size is provided.
  21. Size func(SizeInput) SizeOutput
  22. // Marshal formats a message in the wire-format encoding to the provided buffer.
  23. // Size should be provided if a custom Marshal is provided.
  24. // It must not return an error for a partial message.
  25. Marshal func(MarshalInput) (MarshalOutput, error)
  26. // Unmarshal parses the wire-format encoding and merges the result into a message.
  27. // It must not reset the target message or return an error for a partial message.
  28. Unmarshal func(UnmarshalInput) (UnmarshalOutput, error)
  29. // Merge merges the contents of a source message into a destination message.
  30. Merge func(MergeInput) MergeOutput
  31. // CheckInitialized returns an error if any required fields in the message are not set.
  32. CheckInitialized func(CheckInitializedInput) (CheckInitializedOutput, error)
  33. // Equal compares two messages and returns EqualOutput.Equal == true if they are equal.
  34. Equal func(EqualInput) EqualOutput
  35. }
  36. // SupportFlags indicate support for optional features.
  37. type SupportFlags = uint64
  38. const (
  39. // SupportMarshalDeterministic reports whether MarshalOptions.Deterministic is supported.
  40. SupportMarshalDeterministic SupportFlags = 1 << iota
  41. // SupportUnmarshalDiscardUnknown reports whether UnmarshalOptions.DiscardUnknown is supported.
  42. SupportUnmarshalDiscardUnknown
  43. )
  44. // SizeInput is input to the Size method.
  45. type SizeInput = struct {
  46. pragma.NoUnkeyedLiterals
  47. Message protoreflect.Message
  48. Flags MarshalInputFlags
  49. }
  50. // SizeOutput is output from the Size method.
  51. type SizeOutput = struct {
  52. pragma.NoUnkeyedLiterals
  53. Size int
  54. }
  55. // MarshalInput is input to the Marshal method.
  56. type MarshalInput = struct {
  57. pragma.NoUnkeyedLiterals
  58. Message protoreflect.Message
  59. Buf []byte // output is appended to this buffer
  60. Flags MarshalInputFlags
  61. }
  62. // MarshalOutput is output from the Marshal method.
  63. type MarshalOutput = struct {
  64. pragma.NoUnkeyedLiterals
  65. Buf []byte // contains marshaled message
  66. }
  67. // MarshalInputFlags configure the marshaler.
  68. // Most flags correspond to fields in proto.MarshalOptions.
  69. type MarshalInputFlags = uint8
  70. const (
  71. MarshalDeterministic MarshalInputFlags = 1 << iota
  72. MarshalUseCachedSize
  73. )
  74. // UnmarshalInput is input to the Unmarshal method.
  75. type UnmarshalInput = struct {
  76. pragma.NoUnkeyedLiterals
  77. Message protoreflect.Message
  78. Buf []byte // input buffer
  79. Flags UnmarshalInputFlags
  80. Resolver interface {
  81. FindExtensionByName(field protoreflect.FullName) (protoreflect.ExtensionType, error)
  82. FindExtensionByNumber(message protoreflect.FullName, field protoreflect.FieldNumber) (protoreflect.ExtensionType, error)
  83. }
  84. Depth int
  85. }
  86. // UnmarshalOutput is output from the Unmarshal method.
  87. type UnmarshalOutput = struct {
  88. pragma.NoUnkeyedLiterals
  89. Flags UnmarshalOutputFlags
  90. }
  91. // UnmarshalInputFlags configure the unmarshaler.
  92. // Most flags correspond to fields in proto.UnmarshalOptions.
  93. type UnmarshalInputFlags = uint8
  94. const (
  95. UnmarshalDiscardUnknown UnmarshalInputFlags = 1 << iota
  96. // UnmarshalAliasBuffer permits unmarshal operations to alias the input buffer.
  97. // The unmarshaller must not modify the contents of the buffer.
  98. UnmarshalAliasBuffer
  99. // UnmarshalValidated indicates that validation has already been
  100. // performed on the input buffer.
  101. UnmarshalValidated
  102. // UnmarshalCheckRequired is set if this unmarshal operation ultimately will care if required fields are
  103. // initialized.
  104. UnmarshalCheckRequired
  105. // UnmarshalNoLazyDecoding is set if this unmarshal operation should not use
  106. // lazy decoding, even when otherwise available.
  107. UnmarshalNoLazyDecoding
  108. )
  109. // UnmarshalOutputFlags are output from the Unmarshal method.
  110. type UnmarshalOutputFlags = uint8
  111. const (
  112. // UnmarshalInitialized may be set on return if all required fields are known to be set.
  113. // If unset, then it does not necessarily indicate that the message is uninitialized,
  114. // only that its status could not be confirmed.
  115. UnmarshalInitialized UnmarshalOutputFlags = 1 << iota
  116. )
  117. // MergeInput is input to the Merge method.
  118. type MergeInput = struct {
  119. pragma.NoUnkeyedLiterals
  120. Source protoreflect.Message
  121. Destination protoreflect.Message
  122. }
  123. // MergeOutput is output from the Merge method.
  124. type MergeOutput = struct {
  125. pragma.NoUnkeyedLiterals
  126. Flags MergeOutputFlags
  127. }
  128. // MergeOutputFlags are output from the Merge method.
  129. type MergeOutputFlags = uint8
  130. const (
  131. // MergeComplete reports whether the merge was performed.
  132. // If unset, the merger must have made no changes to the destination.
  133. MergeComplete MergeOutputFlags = 1 << iota
  134. )
  135. // CheckInitializedInput is input to the CheckInitialized method.
  136. type CheckInitializedInput = struct {
  137. pragma.NoUnkeyedLiterals
  138. Message protoreflect.Message
  139. }
  140. // CheckInitializedOutput is output from the CheckInitialized method.
  141. type CheckInitializedOutput = struct {
  142. pragma.NoUnkeyedLiterals
  143. }
  144. // EqualInput is input to the Equal method.
  145. type EqualInput = struct {
  146. pragma.NoUnkeyedLiterals
  147. MessageA protoreflect.Message
  148. MessageB protoreflect.Message
  149. }
  150. // EqualOutput is output from the Equal method.
  151. type EqualOutput = struct {
  152. pragma.NoUnkeyedLiterals
  153. Equal bool
  154. }