blob: 5984a2686c96ebc00c183ed25a38be744bb8e2ba [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"
15 "google.golang.org/protobuf/reflect/protoregistry"
Damien Neil0d3e8cc2019-04-01 13:31:55 -070016)
17
Joe Tsai0f81b382019-07-10 23:14:31 -070018// Methoder is an optional interface implemented by protoreflect.Message to
Damien Neil0d3e8cc2019-04-01 13:31:55 -070019// provide fast-path implementations of various operations.
Joe Tsaif8b855d2019-07-12 13:37:59 -070020// The returned Methods struct must not be mutated.
Damien Neil0d3e8cc2019-04-01 13:31:55 -070021type Methoder interface {
Joe Tsai0f81b382019-07-10 23:14:31 -070022 ProtoMethods() *Methods // may return nil
Damien Neil0d3e8cc2019-04-01 13:31:55 -070023}
24
25// Methods is a set of optional fast-path implementations of various operations.
26type Methods struct {
Joe Tsaif8b855d2019-07-12 13:37:59 -070027 pragma.NoUnkeyedLiterals
Damien Neil0d3e8cc2019-04-01 13:31:55 -070028
Joe Tsaif8b855d2019-07-12 13:37:59 -070029 // Flags indicate support for optional features.
30 Flags SupportFlags
Damien Neil0d3e8cc2019-04-01 13:31:55 -070031
32 // Size returns the size in bytes of the wire-format encoding of m.
Joe Tsaif8b855d2019-07-12 13:37:59 -070033 // MarshalAppend must be provided if a custom Size is provided.
34 Size func(m protoreflect.Message, opts MarshalOptions) int
Damien Neil0d3e8cc2019-04-01 13:31:55 -070035
Joe Tsaif8b855d2019-07-12 13:37:59 -070036 // MarshalAppend appends the wire-format encoding of m to b, returning the result.
37 // Size must be provided if a custom MarshalAppend is provided.
38 // It must not perform required field checks.
39 MarshalAppend func(b []byte, m protoreflect.Message, opts MarshalOptions) ([]byte, error)
40
41 // Unmarshal parses the wire-format message in b and merges the result in m.
42 // It must not reset m or perform required field checks.
Joe Tsai0f81b382019-07-10 23:14:31 -070043 Unmarshal func(b []byte, m protoreflect.Message, opts UnmarshalOptions) error
Damien Neil0d3e8cc2019-04-01 13:31:55 -070044
Damien Neil4686e232019-04-05 13:31:40 -070045 // IsInitialized returns an error if any required fields in m are not set.
Joe Tsai0f81b382019-07-10 23:14:31 -070046 IsInitialized func(m protoreflect.Message) error
Damien Neil0d3e8cc2019-04-01 13:31:55 -070047}
48
Joe Tsaif8b855d2019-07-12 13:37:59 -070049type SupportFlags uint64
Damien Neil0d3e8cc2019-04-01 13:31:55 -070050
51const (
Joe Tsaif8b855d2019-07-12 13:37:59 -070052 // SupportMarshalDeterministic reports whether MarshalOptions.Deterministic is supported.
53 SupportMarshalDeterministic SupportFlags = 1 << iota
54
55 // SupportUnmarshalDiscardUnknown reports whether UnmarshalOptions.DiscardUnknown is supported.
56 SupportUnmarshalDiscardUnknown
Damien Neil0d3e8cc2019-04-01 13:31:55 -070057)
58
59// MarshalOptions configure the marshaler.
60//
61// This type is identical to the one in package proto.
62type MarshalOptions struct {
Joe Tsaif8b855d2019-07-12 13:37:59 -070063 pragma.NoUnkeyedLiterals
64
65 AllowPartial bool // must be treated as true by method implementations
Damien Neil0d3e8cc2019-04-01 13:31:55 -070066 Deterministic bool
Damien Neil03e74862019-04-07 18:18:31 -070067 UseCachedSize bool
Damien Neil0d3e8cc2019-04-01 13:31:55 -070068}
69
70// UnmarshalOptions configures the unmarshaler.
71//
72// This type is identical to the one in package proto.
73type UnmarshalOptions struct {
Joe Tsaif8b855d2019-07-12 13:37:59 -070074 pragma.NoUnkeyedLiterals
75
76 AllowPartial bool // must be treated as true by method implementations
Damien Neil0d3e8cc2019-04-01 13:31:55 -070077 DiscardUnknown bool
Joe Tsai1c283042019-05-14 14:28:19 -070078 Resolver interface {
79 protoregistry.ExtensionTypeResolver
80 }
Damien Neil0d3e8cc2019-04-01 13:31:55 -070081}