blob: ed8f15371747d7d1d806ca0fd7e4cc5fedb66b68 [file] [log] [blame]
Damien Neil0d3e8cc2019-04-01 13:31:55 -07001// 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 Tsai66500912019-05-22 15:53:35 -04005// 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 Neil0d3e8cc2019-04-01 13:31:55 -070010package protoiface
11
12import (
Damien Neile89e6242019-05-13 23:55:40 -070013 "google.golang.org/protobuf/internal/pragma"
14 "google.golang.org/protobuf/reflect/protoreflect"
Damien Neil0d3e8cc2019-04-01 13:31:55 -070015)
16
Damien Neil0d3e8cc2019-04-01 13:31:55 -070017// Methods is a set of optional fast-path implementations of various operations.
Damien Neilf12fb452020-01-21 11:27:51 -080018type Methods = struct {
Joe Tsaif8b855d2019-07-12 13:37:59 -070019 pragma.NoUnkeyedLiterals
Damien Neil0d3e8cc2019-04-01 13:31:55 -070020
Joe Tsaif8b855d2019-07-12 13:37:59 -070021 // Flags indicate support for optional features.
22 Flags SupportFlags
Damien Neil0d3e8cc2019-04-01 13:31:55 -070023
24 // Size returns the size in bytes of the wire-format encoding of m.
Joe Tsaif8b855d2019-07-12 13:37:59 -070025 // MarshalAppend must be provided if a custom Size is provided.
26 Size func(m protoreflect.Message, opts MarshalOptions) int
Damien Neil0d3e8cc2019-04-01 13:31:55 -070027
Damien Neil61781dd2020-01-21 13:29:51 -080028 // Marshal writes the wire-format encoding of m to the provided buffer.
Damien Neilb0d217f2020-01-06 11:17:07 -080029 // Size should be provided if a custom MarshalAppend is provided.
Joe Tsaif8b855d2019-07-12 13:37:59 -070030 // It must not perform required field checks.
Damien Neil61781dd2020-01-21 13:29:51 -080031 Marshal func(m protoreflect.Message, in MarshalInput) (MarshalOutput, error)
Joe Tsaif8b855d2019-07-12 13:37:59 -070032
Damien Neil61781dd2020-01-21 13:29:51 -080033 // Unmarshal parses the wire-format encoding of a message and merges the result to m.
Joe Tsaif8b855d2019-07-12 13:37:59 -070034 // It must not reset m or perform required field checks.
Damien Neil61781dd2020-01-21 13:29:51 -080035 Unmarshal func(m protoreflect.Message, in UnmarshalInput) (UnmarshalOutput, error)
Damien Neil0d3e8cc2019-04-01 13:31:55 -070036
Damien Neil4686e232019-04-05 13:31:40 -070037 // IsInitialized returns an error if any required fields in m are not set.
Joe Tsai0f81b382019-07-10 23:14:31 -070038 IsInitialized func(m protoreflect.Message) error
Damien Neil0d3e8cc2019-04-01 13:31:55 -070039}
40
Damien Neilf12fb452020-01-21 11:27:51 -080041type SupportFlags = uint64
Damien Neil0d3e8cc2019-04-01 13:31:55 -070042
43const (
Joe Tsaif8b855d2019-07-12 13:37:59 -070044 // SupportMarshalDeterministic reports whether MarshalOptions.Deterministic is supported.
45 SupportMarshalDeterministic SupportFlags = 1 << iota
46
47 // SupportUnmarshalDiscardUnknown reports whether UnmarshalOptions.DiscardUnknown is supported.
48 SupportUnmarshalDiscardUnknown
Damien Neil0d3e8cc2019-04-01 13:31:55 -070049)
50
Damien Neil61781dd2020-01-21 13:29:51 -080051// MarshalInput is input to the marshaler.
52type 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.
60type MarshalOutput = struct {
61 pragma.NoUnkeyedLiterals
62
63 Buf []byte // contains marshaled message
64}
65
Damien Neil0d3e8cc2019-04-01 13:31:55 -070066// MarshalOptions configure the marshaler.
67//
68// This type is identical to the one in package proto.
Damien Neilf12fb452020-01-21 11:27:51 -080069type MarshalOptions = struct {
Joe Tsaif8b855d2019-07-12 13:37:59 -070070 pragma.NoUnkeyedLiterals
71
72 AllowPartial bool // must be treated as true by method implementations
Damien Neil0d3e8cc2019-04-01 13:31:55 -070073 Deterministic bool
Damien Neil03e74862019-04-07 18:18:31 -070074 UseCachedSize bool
Damien Neil0d3e8cc2019-04-01 13:31:55 -070075}
76
Damien Neil61781dd2020-01-21 13:29:51 -080077// UnmarshalInput is input to the unmarshaler.
78type UnmarshalInput = struct {
79 pragma.NoUnkeyedLiterals
80
81 Buf []byte // input buffer
82 Options UnmarshalOptions
83}
84
85// UnmarshalOutput is output from the unmarshaler.
86type UnmarshalOutput = struct {
87 pragma.NoUnkeyedLiterals
88
89 // Contents available for future expansion.
90}
91
Damien Neil0d3e8cc2019-04-01 13:31:55 -070092// UnmarshalOptions configures the unmarshaler.
93//
94// This type is identical to the one in package proto.
Damien Neilf12fb452020-01-21 11:27:51 -080095type UnmarshalOptions = struct {
Joe Tsaif8b855d2019-07-12 13:37:59 -070096 pragma.NoUnkeyedLiterals
97
Joe Tsai705acad2019-09-14 18:22:59 -070098 Merge bool // must be treated as true by method implementations
Joe Tsaif8b855d2019-07-12 13:37:59 -070099 AllowPartial bool // must be treated as true by method implementations
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700100 DiscardUnknown bool
Joe Tsai1c283042019-05-14 14:28:19 -0700101 Resolver interface {
Damien Neilf12fb452020-01-21 11:27:51 -0800102 FindExtensionByName(field protoreflect.FullName) (protoreflect.ExtensionType, error)
103 FindExtensionByNumber(message protoreflect.FullName, field protoreflect.FieldNumber) (protoreflect.ExtensionType, error)
Joe Tsai1c283042019-05-14 14:28:19 -0700104 }
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700105}