proto: add MarshalState, UnmarshalState
Add functions to the proto package which plumb through the fast-path state.
As a sample use case: A followup CL adds an Initialized field to
protoiface.UnmarshalOutput, permitting the unmarshaller to report back
when it can confirm that a message is fully initialized. We want to
preserve that information when an unmarshal operation threads through
the proto package (such as when unmarshaling extensions).
To allow these functions to be added as methods of MarshalOptions and
UnmarshalOptions rather than top-level functions, separate the options
from the input structs.
Also update options passed to fast-path methods to set AllowPartial and
Merge to reflect the expected behavior of those methods. (Always allow
partial, never merge.)
Change-Id: I482477b0c9340793be533e75a86d0bb88708716a
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/215877
Reviewed-by: Joe Tsai <joetsai@google.com>
diff --git a/runtime/protoiface/methods.go b/runtime/protoiface/methods.go
index ed8f153..a79ed31 100644
--- a/runtime/protoiface/methods.go
+++ b/runtime/protoiface/methods.go
@@ -27,12 +27,10 @@
// Marshal writes the wire-format encoding of m to the provided buffer.
// Size should be provided if a custom MarshalAppend is provided.
- // It must not perform required field checks.
- Marshal func(m protoreflect.Message, in MarshalInput) (MarshalOutput, error)
+ Marshal func(m protoreflect.Message, in MarshalInput, opts MarshalOptions) (MarshalOutput, error)
// Unmarshal parses the wire-format encoding of a message and merges the result to m.
- // It must not reset m or perform required field checks.
- Unmarshal func(m protoreflect.Message, in UnmarshalInput) (UnmarshalOutput, error)
+ Unmarshal func(m protoreflect.Message, in UnmarshalInput, opts UnmarshalOptions) (UnmarshalOutput, error)
// IsInitialized returns an error if any required fields in m are not set.
IsInitialized func(m protoreflect.Message) error
@@ -52,8 +50,7 @@
type MarshalInput = struct {
pragma.NoUnkeyedLiterals
- Buf []byte // output is appended to this buffer
- Options MarshalOptions
+ Buf []byte // output is appended to this buffer
}
// MarshalOutput is output from the marshaler.
@@ -69,7 +66,7 @@
type MarshalOptions = struct {
pragma.NoUnkeyedLiterals
- AllowPartial bool // must be treated as true by method implementations
+ AllowPartial bool // may be treated as true by method implementations
Deterministic bool
UseCachedSize bool
}
@@ -78,8 +75,7 @@
type UnmarshalInput = struct {
pragma.NoUnkeyedLiterals
- Buf []byte // input buffer
- Options UnmarshalOptions
+ Buf []byte // input buffer
}
// UnmarshalOutput is output from the unmarshaler.
@@ -95,8 +91,8 @@
type UnmarshalOptions = struct {
pragma.NoUnkeyedLiterals
- Merge bool // must be treated as true by method implementations
- AllowPartial bool // must be treated as true by method implementations
+ Merge bool // may be treated as true by method implementations
+ AllowPartial bool // may be treated as true by method implementations
DiscardUnknown bool
Resolver interface {
FindExtensionByName(field protoreflect.FullName) (protoreflect.ExtensionType, error)