methods.go raw

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