blob: 28e9e9f03971ae7eda83a5f31852f3ac0b2e6600 [file] [log] [blame]
khenaidoo7d3c5582021-08-11 18:09:44 -04001// 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.
10package protoiface
11
12import (
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.
18type 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)
Akash Reddy Kankanala92dfdf82025-03-23 22:07:09 +053042
43 // Equal compares two messages and returns EqualOutput.Equal == true if they are equal.
44 Equal func(EqualInput) EqualOutput
khenaidoo7d3c5582021-08-11 18:09:44 -040045}
46
47// SupportFlags indicate support for optional features.
48type SupportFlags = uint64
49
50const (
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.
59type SizeInput = struct {
60 pragma.NoUnkeyedLiterals
61
62 Message protoreflect.Message
63 Flags MarshalInputFlags
64}
65
66// SizeOutput is output from the Size method.
67type SizeOutput = struct {
68 pragma.NoUnkeyedLiterals
69
70 Size int
71}
72
73// MarshalInput is input to the Marshal method.
74type 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.
83type 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.
91type MarshalInputFlags = uint8
92
93const (
94 MarshalDeterministic MarshalInputFlags = 1 << iota
95 MarshalUseCachedSize
96)
97
98// UnmarshalInput is input to the Unmarshal method.
99type 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 }
Akash Reddy Kankanala92dfdf82025-03-23 22:07:09 +0530109 Depth int
khenaidoo7d3c5582021-08-11 18:09:44 -0400110}
111
112// UnmarshalOutput is output from the Unmarshal method.
113type 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.
121type UnmarshalInputFlags = uint8
122
123const (
124 UnmarshalDiscardUnknown UnmarshalInputFlags = 1 << iota
Akash Reddy Kankanala92dfdf82025-03-23 22:07:09 +0530125
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
khenaidoo7d3c5582021-08-11 18:09:44 -0400141)
142
143// UnmarshalOutputFlags are output from the Unmarshal method.
144type UnmarshalOutputFlags = uint8
145
146const (
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.
154type MergeInput = struct {
155 pragma.NoUnkeyedLiterals
156
157 Source protoreflect.Message
158 Destination protoreflect.Message
159}
160
161// MergeOutput is output from the Merge method.
162type MergeOutput = struct {
163 pragma.NoUnkeyedLiterals
164
165 Flags MergeOutputFlags
166}
167
168// MergeOutputFlags are output from the Merge method.
169type MergeOutputFlags = uint8
170
171const (
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.
178type CheckInitializedInput = struct {
179 pragma.NoUnkeyedLiterals
180
181 Message protoreflect.Message
182}
183
184// CheckInitializedOutput is output from the CheckInitialized method.
185type CheckInitializedOutput = struct {
186 pragma.NoUnkeyedLiterals
187}
Akash Reddy Kankanala92dfdf82025-03-23 22:07:09 +0530188
189// EqualInput is input to the Equal method.
190type EqualInput = struct {
191 pragma.NoUnkeyedLiterals
192
193 MessageA protoreflect.Message
194 MessageB protoreflect.Message
195}
196
197// EqualOutput is output from the Equal method.
198type EqualOutput = struct {
199 pragma.NoUnkeyedLiterals
200
201 Equal bool
202}