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