Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 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 | |
Joe Tsai | 6650091 | 2019-05-22 15:53:35 -0400 | [diff] [blame] | 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. |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 10 | package protoiface |
| 11 | |
| 12 | import ( |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 13 | "google.golang.org/protobuf/internal/pragma" |
| 14 | "google.golang.org/protobuf/reflect/protoreflect" |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 15 | ) |
| 16 | |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 17 | // Methods is a set of optional fast-path implementations of various operations. |
Damien Neil | f12fb45 | 2020-01-21 11:27:51 -0800 | [diff] [blame] | 18 | type Methods = struct { |
Joe Tsai | f8b855d | 2019-07-12 13:37:59 -0700 | [diff] [blame] | 19 | pragma.NoUnkeyedLiterals |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 20 | |
Joe Tsai | f8b855d | 2019-07-12 13:37:59 -0700 | [diff] [blame] | 21 | // Flags indicate support for optional features. |
| 22 | Flags SupportFlags |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 23 | |
| 24 | // Size returns the size in bytes of the wire-format encoding of m. |
Joe Tsai | f8b855d | 2019-07-12 13:37:59 -0700 | [diff] [blame] | 25 | // MarshalAppend must be provided if a custom Size is provided. |
| 26 | Size func(m protoreflect.Message, opts MarshalOptions) int |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 27 | |
Damien Neil | 61781dd | 2020-01-21 13:29:51 -0800 | [diff] [blame^] | 28 | // Marshal writes the wire-format encoding of m to the provided buffer. |
Damien Neil | b0d217f | 2020-01-06 11:17:07 -0800 | [diff] [blame] | 29 | // Size should be provided if a custom MarshalAppend is provided. |
Joe Tsai | f8b855d | 2019-07-12 13:37:59 -0700 | [diff] [blame] | 30 | // It must not perform required field checks. |
Damien Neil | 61781dd | 2020-01-21 13:29:51 -0800 | [diff] [blame^] | 31 | Marshal func(m protoreflect.Message, in MarshalInput) (MarshalOutput, error) |
Joe Tsai | f8b855d | 2019-07-12 13:37:59 -0700 | [diff] [blame] | 32 | |
Damien Neil | 61781dd | 2020-01-21 13:29:51 -0800 | [diff] [blame^] | 33 | // Unmarshal parses the wire-format encoding of a message and merges the result to m. |
Joe Tsai | f8b855d | 2019-07-12 13:37:59 -0700 | [diff] [blame] | 34 | // It must not reset m or perform required field checks. |
Damien Neil | 61781dd | 2020-01-21 13:29:51 -0800 | [diff] [blame^] | 35 | Unmarshal func(m protoreflect.Message, in UnmarshalInput) (UnmarshalOutput, error) |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 36 | |
Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 37 | // IsInitialized returns an error if any required fields in m are not set. |
Joe Tsai | 0f81b38 | 2019-07-10 23:14:31 -0700 | [diff] [blame] | 38 | IsInitialized func(m protoreflect.Message) error |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Damien Neil | f12fb45 | 2020-01-21 11:27:51 -0800 | [diff] [blame] | 41 | type SupportFlags = uint64 |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 42 | |
| 43 | const ( |
Joe Tsai | f8b855d | 2019-07-12 13:37:59 -0700 | [diff] [blame] | 44 | // SupportMarshalDeterministic reports whether MarshalOptions.Deterministic is supported. |
| 45 | SupportMarshalDeterministic SupportFlags = 1 << iota |
| 46 | |
| 47 | // SupportUnmarshalDiscardUnknown reports whether UnmarshalOptions.DiscardUnknown is supported. |
| 48 | SupportUnmarshalDiscardUnknown |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 49 | ) |
| 50 | |
Damien Neil | 61781dd | 2020-01-21 13:29:51 -0800 | [diff] [blame^] | 51 | // MarshalInput is input to the marshaler. |
| 52 | type MarshalInput = struct { |
| 53 | pragma.NoUnkeyedLiterals |
| 54 | |
| 55 | Buf []byte // output is appended to this buffer |
| 56 | Options MarshalOptions |
| 57 | } |
| 58 | |
| 59 | // MarshalOutput is output from the marshaler. |
| 60 | type MarshalOutput = struct { |
| 61 | pragma.NoUnkeyedLiterals |
| 62 | |
| 63 | Buf []byte // contains marshaled message |
| 64 | } |
| 65 | |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 66 | // MarshalOptions configure the marshaler. |
| 67 | // |
| 68 | // This type is identical to the one in package proto. |
Damien Neil | f12fb45 | 2020-01-21 11:27:51 -0800 | [diff] [blame] | 69 | type MarshalOptions = struct { |
Joe Tsai | f8b855d | 2019-07-12 13:37:59 -0700 | [diff] [blame] | 70 | pragma.NoUnkeyedLiterals |
| 71 | |
| 72 | AllowPartial bool // must be treated as true by method implementations |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 73 | Deterministic bool |
Damien Neil | 03e7486 | 2019-04-07 18:18:31 -0700 | [diff] [blame] | 74 | UseCachedSize bool |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Damien Neil | 61781dd | 2020-01-21 13:29:51 -0800 | [diff] [blame^] | 77 | // UnmarshalInput is input to the unmarshaler. |
| 78 | type UnmarshalInput = struct { |
| 79 | pragma.NoUnkeyedLiterals |
| 80 | |
| 81 | Buf []byte // input buffer |
| 82 | Options UnmarshalOptions |
| 83 | } |
| 84 | |
| 85 | // UnmarshalOutput is output from the unmarshaler. |
| 86 | type UnmarshalOutput = struct { |
| 87 | pragma.NoUnkeyedLiterals |
| 88 | |
| 89 | // Contents available for future expansion. |
| 90 | } |
| 91 | |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 92 | // UnmarshalOptions configures the unmarshaler. |
| 93 | // |
| 94 | // This type is identical to the one in package proto. |
Damien Neil | f12fb45 | 2020-01-21 11:27:51 -0800 | [diff] [blame] | 95 | type UnmarshalOptions = struct { |
Joe Tsai | f8b855d | 2019-07-12 13:37:59 -0700 | [diff] [blame] | 96 | pragma.NoUnkeyedLiterals |
| 97 | |
Joe Tsai | 705acad | 2019-09-14 18:22:59 -0700 | [diff] [blame] | 98 | Merge bool // must be treated as true by method implementations |
Joe Tsai | f8b855d | 2019-07-12 13:37:59 -0700 | [diff] [blame] | 99 | AllowPartial bool // must be treated as true by method implementations |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 100 | DiscardUnknown bool |
Joe Tsai | 1c28304 | 2019-05-14 14:28:19 -0700 | [diff] [blame] | 101 | Resolver interface { |
Damien Neil | f12fb45 | 2020-01-21 11:27:51 -0800 | [diff] [blame] | 102 | FindExtensionByName(field protoreflect.FullName) (protoreflect.ExtensionType, error) |
| 103 | FindExtensionByNumber(message protoreflect.FullName, field protoreflect.FieldNumber) (protoreflect.ExtensionType, error) |
Joe Tsai | 1c28304 | 2019-05-14 14:28:19 -0700 | [diff] [blame] | 104 | } |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 105 | } |