methods.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. }
  34. // SupportFlags indicate support for optional features.
  35. type SupportFlags = uint64
  36. const (
  37. // SupportMarshalDeterministic reports whether MarshalOptions.Deterministic is supported.
  38. SupportMarshalDeterministic SupportFlags = 1 << iota
  39. // SupportUnmarshalDiscardUnknown reports whether UnmarshalOptions.DiscardUnknown is supported.
  40. SupportUnmarshalDiscardUnknown
  41. )
  42. // SizeInput is input to the Size method.
  43. type SizeInput = struct {
  44. pragma.NoUnkeyedLiterals
  45. Message protoreflect.Message
  46. Flags MarshalInputFlags
  47. }
  48. // SizeOutput is output from the Size method.
  49. type SizeOutput = struct {
  50. pragma.NoUnkeyedLiterals
  51. Size int
  52. }
  53. // MarshalInput is input to the Marshal method.
  54. type MarshalInput = struct {
  55. pragma.NoUnkeyedLiterals
  56. Message protoreflect.Message
  57. Buf []byte // output is appended to this buffer
  58. Flags MarshalInputFlags
  59. }
  60. // MarshalOutput is output from the Marshal method.
  61. type MarshalOutput = struct {
  62. pragma.NoUnkeyedLiterals
  63. Buf []byte // contains marshaled message
  64. }
  65. // MarshalInputFlags configure the marshaler.
  66. // Most flags correspond to fields in proto.MarshalOptions.
  67. type MarshalInputFlags = uint8
  68. const (
  69. MarshalDeterministic MarshalInputFlags = 1 << iota
  70. MarshalUseCachedSize
  71. )
  72. // UnmarshalInput is input to the Unmarshal method.
  73. type UnmarshalInput = struct {
  74. pragma.NoUnkeyedLiterals
  75. Message protoreflect.Message
  76. Buf []byte // input buffer
  77. Flags UnmarshalInputFlags
  78. Resolver interface {
  79. FindExtensionByName(field protoreflect.FullName) (protoreflect.ExtensionType, error)
  80. FindExtensionByNumber(message protoreflect.FullName, field protoreflect.FieldNumber) (protoreflect.ExtensionType, error)
  81. }
  82. Depth int
  83. }
  84. // UnmarshalOutput is output from the Unmarshal method.
  85. type UnmarshalOutput = struct {
  86. pragma.NoUnkeyedLiterals
  87. Flags UnmarshalOutputFlags
  88. }
  89. // UnmarshalInputFlags configure the unmarshaler.
  90. // Most flags correspond to fields in proto.UnmarshalOptions.
  91. type UnmarshalInputFlags = uint8
  92. const (
  93. UnmarshalDiscardUnknown UnmarshalInputFlags = 1 << iota
  94. )
  95. // UnmarshalOutputFlags are output from the Unmarshal method.
  96. type UnmarshalOutputFlags = uint8
  97. const (
  98. // UnmarshalInitialized may be set on return if all required fields are known to be set.
  99. // If unset, then it does not necessarily indicate that the message is uninitialized,
  100. // only that its status could not be confirmed.
  101. UnmarshalInitialized UnmarshalOutputFlags = 1 << iota
  102. )
  103. // MergeInput is input to the Merge method.
  104. type MergeInput = struct {
  105. pragma.NoUnkeyedLiterals
  106. Source protoreflect.Message
  107. Destination protoreflect.Message
  108. }
  109. // MergeOutput is output from the Merge method.
  110. type MergeOutput = struct {
  111. pragma.NoUnkeyedLiterals
  112. Flags MergeOutputFlags
  113. }
  114. // MergeOutputFlags are output from the Merge method.
  115. type MergeOutputFlags = uint8
  116. const (
  117. // MergeComplete reports whether the merge was performed.
  118. // If unset, the merger must have made no changes to the destination.
  119. MergeComplete MergeOutputFlags = 1 << iota
  120. )
  121. // CheckInitializedInput is input to the CheckInitialized method.
  122. type CheckInitializedInput = struct {
  123. pragma.NoUnkeyedLiterals
  124. Message protoreflect.Message
  125. }
  126. // CheckInitializedOutput is output from the CheckInitialized method.
  127. type CheckInitializedOutput = struct {
  128. pragma.NoUnkeyedLiterals
  129. }