methods.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // Package protoiface contains types referenced or implemented by messages.
  5. //
  6. // WARNING: This package should only be imported by message implementations.
  7. // The functionality found in this package should be accessed through
  8. // higher-level abstractions provided by the proto package.
  9. package protoiface
  10. import (
  11. "google.golang.org/protobuf/internal/pragma"
  12. "google.golang.org/protobuf/reflect/protoreflect"
  13. "google.golang.org/protobuf/reflect/protoregistry"
  14. )
  15. // Methoder is an optional interface implemented by protoreflect.Message to
  16. // provide fast-path implementations of various operations.
  17. // The returned Methods struct must not be mutated.
  18. type Methoder interface {
  19. ProtoMethods() *Methods // may return nil
  20. }
  21. // Methods is a set of optional fast-path implementations of various operations.
  22. type Methods struct {
  23. pragma.NoUnkeyedLiterals
  24. // Flags indicate support for optional features.
  25. Flags SupportFlags
  26. // Size returns the size in bytes of the wire-format encoding of m.
  27. // MarshalAppend must be provided if a custom Size is provided.
  28. Size func(m protoreflect.Message, opts MarshalOptions) int
  29. // MarshalAppend appends the wire-format encoding of m to b, returning the result.
  30. // Size must be provided if a custom MarshalAppend is provided.
  31. // It must not perform required field checks.
  32. MarshalAppend func(b []byte, m protoreflect.Message, opts MarshalOptions) ([]byte, error)
  33. // Unmarshal parses the wire-format message in b and merges the result in m.
  34. // It must not reset m or perform required field checks.
  35. Unmarshal func(b []byte, m protoreflect.Message, opts UnmarshalOptions) error
  36. // IsInitialized returns an error if any required fields in m are not set.
  37. IsInitialized func(m protoreflect.Message) error
  38. }
  39. type SupportFlags uint64
  40. const (
  41. // SupportMarshalDeterministic reports whether MarshalOptions.Deterministic is supported.
  42. SupportMarshalDeterministic SupportFlags = 1 << iota
  43. // SupportUnmarshalDiscardUnknown reports whether UnmarshalOptions.DiscardUnknown is supported.
  44. SupportUnmarshalDiscardUnknown
  45. )
  46. // MarshalOptions configure the marshaler.
  47. //
  48. // This type is identical to the one in package proto.
  49. type MarshalOptions struct {
  50. pragma.NoUnkeyedLiterals
  51. AllowPartial bool // must be treated as true by method implementations
  52. Deterministic bool
  53. UseCachedSize bool
  54. }
  55. // UnmarshalOptions configures the unmarshaler.
  56. //
  57. // This type is identical to the one in package proto.
  58. type UnmarshalOptions struct {
  59. pragma.NoUnkeyedLiterals
  60. Merge bool // must be treated as true by method implementations
  61. AllowPartial bool // must be treated as true by method implementations
  62. DiscardUnknown bool
  63. Resolver interface {
  64. protoregistry.ExtensionTypeResolver
  65. }
  66. }