presence.go 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. // Copyright 2025 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 filedesc
  5. import "google.golang.org/protobuf/reflect/protoreflect"
  6. // UsePresenceForField reports whether the presence bitmap should be used for
  7. // the specified field.
  8. func UsePresenceForField(fd protoreflect.FieldDescriptor) (usePresence, canBeLazy bool) {
  9. switch {
  10. case fd.ContainingOneof() != nil && !fd.ContainingOneof().IsSynthetic():
  11. // Oneof fields never use the presence bitmap.
  12. //
  13. // Synthetic oneofs are an exception: Those are used to implement proto3
  14. // optional fields and hence should follow non-oneof field semantics.
  15. return false, false
  16. case fd.IsMap():
  17. // Map-typed fields never use the presence bitmap.
  18. return false, false
  19. case fd.Kind() == protoreflect.MessageKind || fd.Kind() == protoreflect.GroupKind:
  20. // Lazy fields always use the presence bitmap (only messages can be lazy).
  21. isLazy := fd.(interface{ IsLazy() bool }).IsLazy()
  22. return isLazy, isLazy
  23. default:
  24. // If the field has presence, use the presence bitmap.
  25. return fd.HasPresence(), false
  26. }
  27. }