methods.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. }
  83. // UnmarshalOutput is output from the Unmarshal method.
  84. type UnmarshalOutput = struct {
  85. pragma.NoUnkeyedLiterals
  86. Flags UnmarshalOutputFlags
  87. }
  88. // UnmarshalInputFlags configure the unmarshaler.
  89. // Most flags correspond to fields in proto.UnmarshalOptions.
  90. type UnmarshalInputFlags = uint8
  91. const (
  92. UnmarshalDiscardUnknown UnmarshalInputFlags = 1 << iota
  93. )
  94. // UnmarshalOutputFlags are output from the Unmarshal method.
  95. type UnmarshalOutputFlags = uint8
  96. const (
  97. // UnmarshalInitialized may be set on return if all required fields are known to be set.
  98. // If unset, then it does not necessarily indicate that the message is uninitialized,
  99. // only that its status could not be confirmed.
  100. UnmarshalInitialized UnmarshalOutputFlags = 1 << iota
  101. )
  102. // MergeInput is input to the Merge method.
  103. type MergeInput = struct {
  104. pragma.NoUnkeyedLiterals
  105. Source protoreflect.Message
  106. Destination protoreflect.Message
  107. }
  108. // MergeOutput is output from the Merge method.
  109. type MergeOutput = struct {
  110. pragma.NoUnkeyedLiterals
  111. Flags MergeOutputFlags
  112. }
  113. // MergeOutputFlags are output from the Merge method.
  114. type MergeOutputFlags = uint8
  115. const (
  116. // MergeComplete reports whether the merge was performed.
  117. // If unset, the merger must have made no changes to the destination.
  118. MergeComplete MergeOutputFlags = 1 << iota
  119. )
  120. // CheckInitializedInput is input to the CheckInitialized method.
  121. type CheckInitializedInput = struct {
  122. pragma.NoUnkeyedLiterals
  123. Message protoreflect.Message
  124. }
  125. // CheckInitializedOutput is output from the CheckInitialized method.
  126. type CheckInitializedOutput = struct {
  127. pragma.NoUnkeyedLiterals
  128. }