| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 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 | |
| 5 | package filedesc |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "fmt" |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 10 | "strings" |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 11 | "sync" |
| 12 | "sync/atomic" |
| 13 | |
| 14 | "google.golang.org/protobuf/internal/descfmt" |
| 15 | "google.golang.org/protobuf/internal/descopts" |
| 16 | "google.golang.org/protobuf/internal/encoding/defval" |
| 17 | "google.golang.org/protobuf/internal/encoding/messageset" |
| 18 | "google.golang.org/protobuf/internal/genid" |
| 19 | "google.golang.org/protobuf/internal/pragma" |
| 20 | "google.golang.org/protobuf/internal/strs" |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 21 | "google.golang.org/protobuf/reflect/protoreflect" |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 22 | ) |
| 23 | |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 24 | // Edition is an Enum for proto2.Edition |
| 25 | type Edition int32 |
| 26 | |
| 27 | // These values align with the value of Enum in descriptor.proto which allows |
| 28 | // direct conversion between the proto enum and this enum. |
| 29 | const ( |
| 30 | EditionUnknown Edition = 0 |
| 31 | EditionProto2 Edition = 998 |
| 32 | EditionProto3 Edition = 999 |
| 33 | Edition2023 Edition = 1000 |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 34 | Edition2024 Edition = 1001 |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 35 | EditionUnsupported Edition = 100000 |
| 36 | ) |
| 37 | |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 38 | // The types in this file may have a suffix: |
| 39 | // • L0: Contains fields common to all descriptors (except File) and |
| 40 | // must be initialized up front. |
| 41 | // • L1: Contains fields specific to a descriptor and |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 42 | // must be initialized up front. If the associated proto uses Editions, the |
| 43 | // Editions features must always be resolved. If not explicitly set, the |
| 44 | // appropriate default must be resolved and set. |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 45 | // • L2: Contains fields that are lazily initialized when constructing |
| 46 | // from the raw file descriptor. When constructing as a literal, the L2 |
| 47 | // fields must be initialized up front. |
| 48 | // |
| 49 | // The types are exported so that packages like reflect/protodesc can |
| 50 | // directly construct descriptors. |
| 51 | |
| 52 | type ( |
| 53 | File struct { |
| 54 | fileRaw |
| 55 | L1 FileL1 |
| 56 | |
| 57 | once uint32 // atomically set if L2 is valid |
| 58 | mu sync.Mutex // protects L2 |
| 59 | L2 *FileL2 |
| 60 | } |
| 61 | FileL1 struct { |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 62 | Syntax protoreflect.Syntax |
| 63 | Edition Edition // Only used if Syntax == Editions |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 64 | Path string |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 65 | Package protoreflect.FullName |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 66 | |
| 67 | Enums Enums |
| 68 | Messages Messages |
| 69 | Extensions Extensions |
| 70 | Services Services |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 71 | |
| 72 | EditionFeatures EditionFeatures |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 73 | } |
| 74 | FileL2 struct { |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 75 | Options func() protoreflect.ProtoMessage |
| 76 | Imports FileImports |
| 77 | OptionImports func() protoreflect.FileImports |
| 78 | Locations SourceLocations |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 79 | } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 80 | |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 81 | // EditionFeatures is a frequently-instantiated struct, so please take care |
| 82 | // to minimize padding when adding new fields to this struct (add them in |
| 83 | // the right place/order). |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 84 | EditionFeatures struct { |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 85 | // StripEnumPrefix determines if the plugin generates enum value |
| 86 | // constants as-is, with their prefix stripped, or both variants. |
| 87 | StripEnumPrefix int |
| 88 | |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 89 | // IsFieldPresence is true if field_presence is EXPLICIT |
| 90 | // https://protobuf.dev/editions/features/#field_presence |
| 91 | IsFieldPresence bool |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 92 | |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 93 | // IsFieldPresence is true if field_presence is LEGACY_REQUIRED |
| 94 | // https://protobuf.dev/editions/features/#field_presence |
| 95 | IsLegacyRequired bool |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 96 | |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 97 | // IsOpenEnum is true if enum_type is OPEN |
| 98 | // https://protobuf.dev/editions/features/#enum_type |
| 99 | IsOpenEnum bool |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 100 | |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 101 | // IsPacked is true if repeated_field_encoding is PACKED |
| 102 | // https://protobuf.dev/editions/features/#repeated_field_encoding |
| 103 | IsPacked bool |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 104 | |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 105 | // IsUTF8Validated is true if utf_validation is VERIFY |
| 106 | // https://protobuf.dev/editions/features/#utf8_validation |
| 107 | IsUTF8Validated bool |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 108 | |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 109 | // IsDelimitedEncoded is true if message_encoding is DELIMITED |
| 110 | // https://protobuf.dev/editions/features/#message_encoding |
| 111 | IsDelimitedEncoded bool |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 112 | |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 113 | // IsJSONCompliant is true if json_format is ALLOW |
| 114 | // https://protobuf.dev/editions/features/#json_format |
| 115 | IsJSONCompliant bool |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 116 | |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 117 | // GenerateLegacyUnmarshalJSON determines if the plugin generates the |
| 118 | // UnmarshalJSON([]byte) error method for enums. |
| 119 | GenerateLegacyUnmarshalJSON bool |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 120 | // APILevel controls which API (Open, Hybrid or Opaque) should be used |
| 121 | // for generated code (.pb.go files). |
| 122 | APILevel int |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 123 | } |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 124 | ) |
| 125 | |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 126 | func (fd *File) ParentFile() protoreflect.FileDescriptor { return fd } |
| 127 | func (fd *File) Parent() protoreflect.Descriptor { return nil } |
| 128 | func (fd *File) Index() int { return 0 } |
| 129 | func (fd *File) Syntax() protoreflect.Syntax { return fd.L1.Syntax } |
| 130 | func (fd *File) Name() protoreflect.Name { return fd.L1.Package.Name() } |
| 131 | func (fd *File) FullName() protoreflect.FullName { return fd.L1.Package } |
| 132 | func (fd *File) IsPlaceholder() bool { return false } |
| 133 | func (fd *File) Options() protoreflect.ProtoMessage { |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 134 | if f := fd.lazyInit().Options; f != nil { |
| 135 | return f() |
| 136 | } |
| 137 | return descopts.File |
| 138 | } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 139 | func (fd *File) Path() string { return fd.L1.Path } |
| 140 | func (fd *File) Package() protoreflect.FullName { return fd.L1.Package } |
| 141 | func (fd *File) Imports() protoreflect.FileImports { return &fd.lazyInit().Imports } |
| 142 | func (fd *File) Enums() protoreflect.EnumDescriptors { return &fd.L1.Enums } |
| 143 | func (fd *File) Messages() protoreflect.MessageDescriptors { return &fd.L1.Messages } |
| 144 | func (fd *File) Extensions() protoreflect.ExtensionDescriptors { return &fd.L1.Extensions } |
| 145 | func (fd *File) Services() protoreflect.ServiceDescriptors { return &fd.L1.Services } |
| 146 | func (fd *File) SourceLocations() protoreflect.SourceLocations { return &fd.lazyInit().Locations } |
| 147 | func (fd *File) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, fd) } |
| 148 | func (fd *File) ProtoType(protoreflect.FileDescriptor) {} |
| 149 | func (fd *File) ProtoInternal(pragma.DoNotImplement) {} |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 150 | |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 151 | // The next two are not part of the FileDescriptor interface. They are just used to reconstruct |
| 152 | // the original FileDescriptor proto. |
| 153 | func (fd *File) Edition() int32 { return int32(fd.L1.Edition) } |
| 154 | func (fd *File) OptionImports() protoreflect.FileImports { |
| 155 | if f := fd.lazyInit().OptionImports; f != nil { |
| 156 | return f() |
| 157 | } |
| 158 | return emptyFiles |
| 159 | } |
| 160 | |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 161 | func (fd *File) lazyInit() *FileL2 { |
| 162 | if atomic.LoadUint32(&fd.once) == 0 { |
| 163 | fd.lazyInitOnce() |
| 164 | } |
| 165 | return fd.L2 |
| 166 | } |
| 167 | |
| 168 | func (fd *File) lazyInitOnce() { |
| 169 | fd.mu.Lock() |
| 170 | if fd.L2 == nil { |
| 171 | fd.lazyRawInit() // recursively initializes all L2 structures |
| 172 | } |
| 173 | atomic.StoreUint32(&fd.once, 1) |
| 174 | fd.mu.Unlock() |
| 175 | } |
| 176 | |
| 177 | // GoPackagePath is a pseudo-internal API for determining the Go package path |
| 178 | // that this file descriptor is declared in. |
| 179 | // |
| 180 | // WARNING: This method is exempt from the compatibility promise and may be |
| 181 | // removed in the future without warning. |
| 182 | func (fd *File) GoPackagePath() string { |
| 183 | return fd.builder.GoPackagePath |
| 184 | } |
| 185 | |
| 186 | type ( |
| 187 | Enum struct { |
| 188 | Base |
| 189 | L1 EnumL1 |
| 190 | L2 *EnumL2 // protected by fileDesc.once |
| 191 | } |
| 192 | EnumL1 struct { |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 193 | EditionFeatures EditionFeatures |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 194 | Visibility int32 |
| 195 | eagerValues bool // controls whether EnumL2.Values is already populated |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 196 | } |
| 197 | EnumL2 struct { |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 198 | Options func() protoreflect.ProtoMessage |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 199 | Values EnumValues |
| 200 | ReservedNames Names |
| 201 | ReservedRanges EnumRanges |
| 202 | } |
| 203 | |
| 204 | EnumValue struct { |
| 205 | Base |
| 206 | L1 EnumValueL1 |
| 207 | } |
| 208 | EnumValueL1 struct { |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 209 | Options func() protoreflect.ProtoMessage |
| 210 | Number protoreflect.EnumNumber |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 211 | } |
| 212 | ) |
| 213 | |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 214 | func (ed *Enum) Options() protoreflect.ProtoMessage { |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 215 | if f := ed.lazyInit().Options; f != nil { |
| 216 | return f() |
| 217 | } |
| 218 | return descopts.Enum |
| 219 | } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 220 | func (ed *Enum) Values() protoreflect.EnumValueDescriptors { |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 221 | if ed.L1.eagerValues { |
| 222 | return &ed.L2.Values |
| 223 | } |
| 224 | return &ed.lazyInit().Values |
| 225 | } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 226 | func (ed *Enum) ReservedNames() protoreflect.Names { return &ed.lazyInit().ReservedNames } |
| 227 | func (ed *Enum) ReservedRanges() protoreflect.EnumRanges { return &ed.lazyInit().ReservedRanges } |
| 228 | func (ed *Enum) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, ed) } |
| 229 | func (ed *Enum) ProtoType(protoreflect.EnumDescriptor) {} |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 230 | |
| 231 | // This is not part of the EnumDescriptor interface. It is just used to reconstruct |
| 232 | // the original FileDescriptor proto. |
| 233 | func (ed *Enum) Visibility() int32 { return ed.L1.Visibility } |
| 234 | |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 235 | func (ed *Enum) lazyInit() *EnumL2 { |
| 236 | ed.L0.ParentFile.lazyInit() // implicitly initializes L2 |
| 237 | return ed.L2 |
| 238 | } |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 239 | func (ed *Enum) IsClosed() bool { |
| 240 | return !ed.L1.EditionFeatures.IsOpenEnum |
| 241 | } |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 242 | |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 243 | func (ed *EnumValue) Options() protoreflect.ProtoMessage { |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 244 | if f := ed.L1.Options; f != nil { |
| 245 | return f() |
| 246 | } |
| 247 | return descopts.EnumValue |
| 248 | } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 249 | func (ed *EnumValue) Number() protoreflect.EnumNumber { return ed.L1.Number } |
| 250 | func (ed *EnumValue) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, ed) } |
| 251 | func (ed *EnumValue) ProtoType(protoreflect.EnumValueDescriptor) {} |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 252 | |
| 253 | type ( |
| 254 | Message struct { |
| 255 | Base |
| 256 | L1 MessageL1 |
| 257 | L2 *MessageL2 // protected by fileDesc.once |
| 258 | } |
| 259 | MessageL1 struct { |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 260 | Enums Enums |
| 261 | Messages Messages |
| 262 | Extensions Extensions |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 263 | EditionFeatures EditionFeatures |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 264 | Visibility int32 |
| 265 | IsMapEntry bool // promoted from google.protobuf.MessageOptions |
| 266 | IsMessageSet bool // promoted from google.protobuf.MessageOptions |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 267 | } |
| 268 | MessageL2 struct { |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 269 | Options func() protoreflect.ProtoMessage |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 270 | Fields Fields |
| 271 | Oneofs Oneofs |
| 272 | ReservedNames Names |
| 273 | ReservedRanges FieldRanges |
| 274 | RequiredNumbers FieldNumbers // must be consistent with Fields.Cardinality |
| 275 | ExtensionRanges FieldRanges |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 276 | ExtensionRangeOptions []func() protoreflect.ProtoMessage // must be same length as ExtensionRanges |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | Field struct { |
| 280 | Base |
| 281 | L1 FieldL1 |
| 282 | } |
| 283 | FieldL1 struct { |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 284 | Options func() protoreflect.ProtoMessage |
| 285 | Number protoreflect.FieldNumber |
| 286 | Cardinality protoreflect.Cardinality // must be consistent with Message.RequiredNumbers |
| 287 | Kind protoreflect.Kind |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 288 | StringName stringName |
| 289 | IsProto3Optional bool // promoted from google.protobuf.FieldDescriptorProto |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 290 | IsLazy bool // promoted from google.protobuf.FieldOptions |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 291 | Default defaultValue |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 292 | ContainingOneof protoreflect.OneofDescriptor // must be consistent with Message.Oneofs.Fields |
| 293 | Enum protoreflect.EnumDescriptor |
| 294 | Message protoreflect.MessageDescriptor |
| 295 | |
| 296 | EditionFeatures EditionFeatures |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | Oneof struct { |
| 300 | Base |
| 301 | L1 OneofL1 |
| 302 | } |
| 303 | OneofL1 struct { |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 304 | Options func() protoreflect.ProtoMessage |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 305 | Fields OneofFields // must be consistent with Message.Fields.ContainingOneof |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 306 | |
| 307 | EditionFeatures EditionFeatures |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 308 | } |
| 309 | ) |
| 310 | |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 311 | func (md *Message) Options() protoreflect.ProtoMessage { |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 312 | if f := md.lazyInit().Options; f != nil { |
| 313 | return f() |
| 314 | } |
| 315 | return descopts.Message |
| 316 | } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 317 | func (md *Message) IsMapEntry() bool { return md.L1.IsMapEntry } |
| 318 | func (md *Message) Fields() protoreflect.FieldDescriptors { return &md.lazyInit().Fields } |
| 319 | func (md *Message) Oneofs() protoreflect.OneofDescriptors { return &md.lazyInit().Oneofs } |
| 320 | func (md *Message) ReservedNames() protoreflect.Names { return &md.lazyInit().ReservedNames } |
| 321 | func (md *Message) ReservedRanges() protoreflect.FieldRanges { return &md.lazyInit().ReservedRanges } |
| 322 | func (md *Message) RequiredNumbers() protoreflect.FieldNumbers { return &md.lazyInit().RequiredNumbers } |
| 323 | func (md *Message) ExtensionRanges() protoreflect.FieldRanges { return &md.lazyInit().ExtensionRanges } |
| 324 | func (md *Message) ExtensionRangeOptions(i int) protoreflect.ProtoMessage { |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 325 | if f := md.lazyInit().ExtensionRangeOptions[i]; f != nil { |
| 326 | return f() |
| 327 | } |
| 328 | return descopts.ExtensionRange |
| 329 | } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 330 | func (md *Message) Enums() protoreflect.EnumDescriptors { return &md.L1.Enums } |
| 331 | func (md *Message) Messages() protoreflect.MessageDescriptors { return &md.L1.Messages } |
| 332 | func (md *Message) Extensions() protoreflect.ExtensionDescriptors { return &md.L1.Extensions } |
| 333 | func (md *Message) ProtoType(protoreflect.MessageDescriptor) {} |
| 334 | func (md *Message) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, md) } |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 335 | |
| 336 | // This is not part of the MessageDescriptor interface. It is just used to reconstruct |
| 337 | // the original FileDescriptor proto. |
| 338 | func (md *Message) Visibility() int32 { return md.L1.Visibility } |
| 339 | |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 340 | func (md *Message) lazyInit() *MessageL2 { |
| 341 | md.L0.ParentFile.lazyInit() // implicitly initializes L2 |
| 342 | return md.L2 |
| 343 | } |
| 344 | |
| 345 | // IsMessageSet is a pseudo-internal API for checking whether a message |
| 346 | // should serialize in the proto1 message format. |
| 347 | // |
| 348 | // WARNING: This method is exempt from the compatibility promise and may be |
| 349 | // removed in the future without warning. |
| 350 | func (md *Message) IsMessageSet() bool { |
| 351 | return md.L1.IsMessageSet |
| 352 | } |
| 353 | |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 354 | func (fd *Field) Options() protoreflect.ProtoMessage { |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 355 | if f := fd.L1.Options; f != nil { |
| 356 | return f() |
| 357 | } |
| 358 | return descopts.Field |
| 359 | } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 360 | func (fd *Field) Number() protoreflect.FieldNumber { return fd.L1.Number } |
| 361 | func (fd *Field) Cardinality() protoreflect.Cardinality { return fd.L1.Cardinality } |
| 362 | func (fd *Field) Kind() protoreflect.Kind { |
| 363 | return fd.L1.Kind |
| 364 | } |
| 365 | func (fd *Field) HasJSONName() bool { return fd.L1.StringName.hasJSON } |
| 366 | func (fd *Field) JSONName() string { return fd.L1.StringName.getJSON(fd) } |
| 367 | func (fd *Field) TextName() string { return fd.L1.StringName.getText(fd) } |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 368 | func (fd *Field) HasPresence() bool { |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 369 | if fd.L1.Cardinality == protoreflect.Repeated { |
| 370 | return false |
| 371 | } |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 372 | return fd.IsExtension() || fd.L1.EditionFeatures.IsFieldPresence || fd.L1.Message != nil || fd.L1.ContainingOneof != nil |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 373 | } |
| 374 | func (fd *Field) HasOptionalKeyword() bool { |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 375 | return (fd.L0.ParentFile.L1.Syntax == protoreflect.Proto2 && fd.L1.Cardinality == protoreflect.Optional && fd.L1.ContainingOneof == nil) || fd.L1.IsProto3Optional |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 376 | } |
| 377 | func (fd *Field) IsPacked() bool { |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 378 | if fd.L1.Cardinality != protoreflect.Repeated { |
| 379 | return false |
| 380 | } |
| 381 | switch fd.L1.Kind { |
| 382 | case protoreflect.StringKind, protoreflect.BytesKind, protoreflect.MessageKind, protoreflect.GroupKind: |
| 383 | return false |
| 384 | } |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 385 | return fd.L1.EditionFeatures.IsPacked |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 386 | } |
| 387 | func (fd *Field) IsExtension() bool { return false } |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 388 | func (fd *Field) IsWeak() bool { return false } |
| 389 | func (fd *Field) IsLazy() bool { return fd.L1.IsLazy } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 390 | func (fd *Field) IsList() bool { return fd.Cardinality() == protoreflect.Repeated && !fd.IsMap() } |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 391 | func (fd *Field) IsMap() bool { return fd.Message() != nil && fd.Message().IsMapEntry() } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 392 | func (fd *Field) MapKey() protoreflect.FieldDescriptor { |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 393 | if !fd.IsMap() { |
| 394 | return nil |
| 395 | } |
| 396 | return fd.Message().Fields().ByNumber(genid.MapEntry_Key_field_number) |
| 397 | } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 398 | func (fd *Field) MapValue() protoreflect.FieldDescriptor { |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 399 | if !fd.IsMap() { |
| 400 | return nil |
| 401 | } |
| 402 | return fd.Message().Fields().ByNumber(genid.MapEntry_Value_field_number) |
| 403 | } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 404 | func (fd *Field) HasDefault() bool { return fd.L1.Default.has } |
| 405 | func (fd *Field) Default() protoreflect.Value { return fd.L1.Default.get(fd) } |
| 406 | func (fd *Field) DefaultEnumValue() protoreflect.EnumValueDescriptor { return fd.L1.Default.enum } |
| 407 | func (fd *Field) ContainingOneof() protoreflect.OneofDescriptor { return fd.L1.ContainingOneof } |
| 408 | func (fd *Field) ContainingMessage() protoreflect.MessageDescriptor { |
| 409 | return fd.L0.Parent.(protoreflect.MessageDescriptor) |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 410 | } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 411 | func (fd *Field) Enum() protoreflect.EnumDescriptor { |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 412 | return fd.L1.Enum |
| 413 | } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 414 | func (fd *Field) Message() protoreflect.MessageDescriptor { |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 415 | return fd.L1.Message |
| 416 | } |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 417 | func (fd *Field) IsMapEntry() bool { |
| 418 | parent, ok := fd.L0.Parent.(protoreflect.MessageDescriptor) |
| 419 | return ok && parent.IsMapEntry() |
| 420 | } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 421 | func (fd *Field) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, fd) } |
| 422 | func (fd *Field) ProtoType(protoreflect.FieldDescriptor) {} |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 423 | |
| 424 | // EnforceUTF8 is a pseudo-internal API to determine whether to enforce UTF-8 |
| 425 | // validation for the string field. This exists for Google-internal use only |
| 426 | // since proto3 did not enforce UTF-8 validity prior to the open-source release. |
| 427 | // If this method does not exist, the default is to enforce valid UTF-8. |
| 428 | // |
| 429 | // WARNING: This method is exempt from the compatibility promise and may be |
| 430 | // removed in the future without warning. |
| 431 | func (fd *Field) EnforceUTF8() bool { |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 432 | return fd.L1.EditionFeatures.IsUTF8Validated |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | func (od *Oneof) IsSynthetic() bool { |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 436 | return od.L0.ParentFile.L1.Syntax == protoreflect.Proto3 && len(od.L1.Fields.List) == 1 && od.L1.Fields.List[0].HasOptionalKeyword() |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 437 | } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 438 | func (od *Oneof) Options() protoreflect.ProtoMessage { |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 439 | if f := od.L1.Options; f != nil { |
| 440 | return f() |
| 441 | } |
| 442 | return descopts.Oneof |
| 443 | } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 444 | func (od *Oneof) Fields() protoreflect.FieldDescriptors { return &od.L1.Fields } |
| 445 | func (od *Oneof) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, od) } |
| 446 | func (od *Oneof) ProtoType(protoreflect.OneofDescriptor) {} |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 447 | |
| 448 | type ( |
| 449 | Extension struct { |
| 450 | Base |
| 451 | L1 ExtensionL1 |
| 452 | L2 *ExtensionL2 // protected by fileDesc.once |
| 453 | } |
| 454 | ExtensionL1 struct { |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 455 | Number protoreflect.FieldNumber |
| 456 | Extendee protoreflect.MessageDescriptor |
| 457 | Cardinality protoreflect.Cardinality |
| 458 | Kind protoreflect.Kind |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 459 | IsLazy bool |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 460 | EditionFeatures EditionFeatures |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 461 | } |
| 462 | ExtensionL2 struct { |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 463 | Options func() protoreflect.ProtoMessage |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 464 | StringName stringName |
| 465 | IsProto3Optional bool // promoted from google.protobuf.FieldDescriptorProto |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 466 | Default defaultValue |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 467 | Enum protoreflect.EnumDescriptor |
| 468 | Message protoreflect.MessageDescriptor |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 469 | } |
| 470 | ) |
| 471 | |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 472 | func (xd *Extension) Options() protoreflect.ProtoMessage { |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 473 | if f := xd.lazyInit().Options; f != nil { |
| 474 | return f() |
| 475 | } |
| 476 | return descopts.Field |
| 477 | } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 478 | func (xd *Extension) Number() protoreflect.FieldNumber { return xd.L1.Number } |
| 479 | func (xd *Extension) Cardinality() protoreflect.Cardinality { return xd.L1.Cardinality } |
| 480 | func (xd *Extension) Kind() protoreflect.Kind { return xd.L1.Kind } |
| 481 | func (xd *Extension) HasJSONName() bool { return xd.lazyInit().StringName.hasJSON } |
| 482 | func (xd *Extension) JSONName() string { return xd.lazyInit().StringName.getJSON(xd) } |
| 483 | func (xd *Extension) TextName() string { return xd.lazyInit().StringName.getText(xd) } |
| 484 | func (xd *Extension) HasPresence() bool { return xd.L1.Cardinality != protoreflect.Repeated } |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 485 | func (xd *Extension) HasOptionalKeyword() bool { |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 486 | return (xd.L0.ParentFile.L1.Syntax == protoreflect.Proto2 && xd.L1.Cardinality == protoreflect.Optional) || xd.lazyInit().IsProto3Optional |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 487 | } |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 488 | func (xd *Extension) IsPacked() bool { |
| 489 | if xd.L1.Cardinality != protoreflect.Repeated { |
| 490 | return false |
| 491 | } |
| 492 | switch xd.L1.Kind { |
| 493 | case protoreflect.StringKind, protoreflect.BytesKind, protoreflect.MessageKind, protoreflect.GroupKind: |
| 494 | return false |
| 495 | } |
| 496 | return xd.L1.EditionFeatures.IsPacked |
| 497 | } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 498 | func (xd *Extension) IsExtension() bool { return true } |
| 499 | func (xd *Extension) IsWeak() bool { return false } |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 500 | func (xd *Extension) IsLazy() bool { return xd.L1.IsLazy } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 501 | func (xd *Extension) IsList() bool { return xd.Cardinality() == protoreflect.Repeated } |
| 502 | func (xd *Extension) IsMap() bool { return false } |
| 503 | func (xd *Extension) MapKey() protoreflect.FieldDescriptor { return nil } |
| 504 | func (xd *Extension) MapValue() protoreflect.FieldDescriptor { return nil } |
| 505 | func (xd *Extension) HasDefault() bool { return xd.lazyInit().Default.has } |
| 506 | func (xd *Extension) Default() protoreflect.Value { return xd.lazyInit().Default.get(xd) } |
| 507 | func (xd *Extension) DefaultEnumValue() protoreflect.EnumValueDescriptor { |
| 508 | return xd.lazyInit().Default.enum |
| 509 | } |
| 510 | func (xd *Extension) ContainingOneof() protoreflect.OneofDescriptor { return nil } |
| 511 | func (xd *Extension) ContainingMessage() protoreflect.MessageDescriptor { return xd.L1.Extendee } |
| 512 | func (xd *Extension) Enum() protoreflect.EnumDescriptor { return xd.lazyInit().Enum } |
| 513 | func (xd *Extension) Message() protoreflect.MessageDescriptor { return xd.lazyInit().Message } |
| 514 | func (xd *Extension) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, xd) } |
| 515 | func (xd *Extension) ProtoType(protoreflect.FieldDescriptor) {} |
| 516 | func (xd *Extension) ProtoInternal(pragma.DoNotImplement) {} |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 517 | func (xd *Extension) lazyInit() *ExtensionL2 { |
| 518 | xd.L0.ParentFile.lazyInit() // implicitly initializes L2 |
| 519 | return xd.L2 |
| 520 | } |
| 521 | |
| 522 | type ( |
| 523 | Service struct { |
| 524 | Base |
| 525 | L1 ServiceL1 |
| 526 | L2 *ServiceL2 // protected by fileDesc.once |
| 527 | } |
| 528 | ServiceL1 struct{} |
| 529 | ServiceL2 struct { |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 530 | Options func() protoreflect.ProtoMessage |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 531 | Methods Methods |
| 532 | } |
| 533 | |
| 534 | Method struct { |
| 535 | Base |
| 536 | L1 MethodL1 |
| 537 | } |
| 538 | MethodL1 struct { |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 539 | Options func() protoreflect.ProtoMessage |
| 540 | Input protoreflect.MessageDescriptor |
| 541 | Output protoreflect.MessageDescriptor |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 542 | IsStreamingClient bool |
| 543 | IsStreamingServer bool |
| 544 | } |
| 545 | ) |
| 546 | |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 547 | func (sd *Service) Options() protoreflect.ProtoMessage { |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 548 | if f := sd.lazyInit().Options; f != nil { |
| 549 | return f() |
| 550 | } |
| 551 | return descopts.Service |
| 552 | } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 553 | func (sd *Service) Methods() protoreflect.MethodDescriptors { return &sd.lazyInit().Methods } |
| 554 | func (sd *Service) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, sd) } |
| 555 | func (sd *Service) ProtoType(protoreflect.ServiceDescriptor) {} |
| 556 | func (sd *Service) ProtoInternal(pragma.DoNotImplement) {} |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 557 | func (sd *Service) lazyInit() *ServiceL2 { |
| 558 | sd.L0.ParentFile.lazyInit() // implicitly initializes L2 |
| 559 | return sd.L2 |
| 560 | } |
| 561 | |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 562 | func (md *Method) Options() protoreflect.ProtoMessage { |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 563 | if f := md.L1.Options; f != nil { |
| 564 | return f() |
| 565 | } |
| 566 | return descopts.Method |
| 567 | } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 568 | func (md *Method) Input() protoreflect.MessageDescriptor { return md.L1.Input } |
| 569 | func (md *Method) Output() protoreflect.MessageDescriptor { return md.L1.Output } |
| 570 | func (md *Method) IsStreamingClient() bool { return md.L1.IsStreamingClient } |
| 571 | func (md *Method) IsStreamingServer() bool { return md.L1.IsStreamingServer } |
| 572 | func (md *Method) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, md) } |
| 573 | func (md *Method) ProtoType(protoreflect.MethodDescriptor) {} |
| 574 | func (md *Method) ProtoInternal(pragma.DoNotImplement) {} |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 575 | |
| 576 | // Surrogate files are can be used to create standalone descriptors |
| 577 | // where the syntax is only information derived from the parent file. |
| 578 | var ( |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 579 | SurrogateProto2 = &File{L1: FileL1{Syntax: protoreflect.Proto2}, L2: &FileL2{}} |
| 580 | SurrogateProto3 = &File{L1: FileL1{Syntax: protoreflect.Proto3}, L2: &FileL2{}} |
| 581 | SurrogateEdition2023 = &File{L1: FileL1{Syntax: protoreflect.Editions, Edition: Edition2023}, L2: &FileL2{}} |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 582 | ) |
| 583 | |
| 584 | type ( |
| 585 | Base struct { |
| 586 | L0 BaseL0 |
| 587 | } |
| 588 | BaseL0 struct { |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 589 | FullName protoreflect.FullName // must be populated |
| 590 | ParentFile *File // must be populated |
| 591 | Parent protoreflect.Descriptor |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 592 | Index int |
| 593 | } |
| 594 | ) |
| 595 | |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 596 | func (d *Base) Name() protoreflect.Name { return d.L0.FullName.Name() } |
| 597 | func (d *Base) FullName() protoreflect.FullName { return d.L0.FullName } |
| 598 | func (d *Base) ParentFile() protoreflect.FileDescriptor { |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 599 | if d.L0.ParentFile == SurrogateProto2 || d.L0.ParentFile == SurrogateProto3 { |
| 600 | return nil // surrogate files are not real parents |
| 601 | } |
| 602 | return d.L0.ParentFile |
| 603 | } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 604 | func (d *Base) Parent() protoreflect.Descriptor { return d.L0.Parent } |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 605 | func (d *Base) Index() int { return d.L0.Index } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 606 | func (d *Base) Syntax() protoreflect.Syntax { return d.L0.ParentFile.Syntax() } |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 607 | func (d *Base) IsPlaceholder() bool { return false } |
| 608 | func (d *Base) ProtoInternal(pragma.DoNotImplement) {} |
| 609 | |
| 610 | type stringName struct { |
| 611 | hasJSON bool |
| 612 | once sync.Once |
| 613 | nameJSON string |
| 614 | nameText string |
| 615 | } |
| 616 | |
| 617 | // InitJSON initializes the name. It is exported for use by other internal packages. |
| 618 | func (s *stringName) InitJSON(name string) { |
| 619 | s.hasJSON = true |
| 620 | s.nameJSON = name |
| 621 | } |
| 622 | |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 623 | // Returns true if this field is structured like the synthetic field of a proto2 |
| 624 | // group. This allows us to expand our treatment of delimited fields without |
| 625 | // breaking proto2 files that have been upgraded to editions. |
| 626 | func isGroupLike(fd protoreflect.FieldDescriptor) bool { |
| 627 | // Groups are always group types. |
| 628 | if fd.Kind() != protoreflect.GroupKind { |
| 629 | return false |
| 630 | } |
| 631 | |
| 632 | // Group fields are always the lowercase type name. |
| 633 | if strings.ToLower(string(fd.Message().Name())) != string(fd.Name()) { |
| 634 | return false |
| 635 | } |
| 636 | |
| 637 | // Groups could only be defined in the same file they're used. |
| 638 | if fd.Message().ParentFile() != fd.ParentFile() { |
| 639 | return false |
| 640 | } |
| 641 | |
| 642 | // Group messages are always defined in the same scope as the field. File |
| 643 | // level extensions will compare NULL == NULL here, which is why the file |
| 644 | // comparison above is necessary to ensure both come from the same file. |
| 645 | if fd.IsExtension() { |
| 646 | return fd.Parent() == fd.Message().Parent() |
| 647 | } |
| 648 | return fd.ContainingMessage() == fd.Message().Parent() |
| 649 | } |
| 650 | |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 651 | func (s *stringName) lazyInit(fd protoreflect.FieldDescriptor) *stringName { |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 652 | s.once.Do(func() { |
| 653 | if fd.IsExtension() { |
| 654 | // For extensions, JSON and text are formatted the same way. |
| 655 | var name string |
| 656 | if messageset.IsMessageSetExtension(fd) { |
| 657 | name = string("[" + fd.FullName().Parent() + "]") |
| 658 | } else { |
| 659 | name = string("[" + fd.FullName() + "]") |
| 660 | } |
| 661 | s.nameJSON = name |
| 662 | s.nameText = name |
| 663 | } else { |
| 664 | // Format the JSON name. |
| 665 | if !s.hasJSON { |
| 666 | s.nameJSON = strs.JSONCamelCase(string(fd.Name())) |
| 667 | } |
| 668 | |
| 669 | // Format the text name. |
| 670 | s.nameText = string(fd.Name()) |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 671 | if isGroupLike(fd) { |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 672 | s.nameText = string(fd.Message().Name()) |
| 673 | } |
| 674 | } |
| 675 | }) |
| 676 | return s |
| 677 | } |
| 678 | |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 679 | func (s *stringName) getJSON(fd protoreflect.FieldDescriptor) string { return s.lazyInit(fd).nameJSON } |
| 680 | func (s *stringName) getText(fd protoreflect.FieldDescriptor) string { return s.lazyInit(fd).nameText } |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 681 | |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 682 | func DefaultValue(v protoreflect.Value, ev protoreflect.EnumValueDescriptor) defaultValue { |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 683 | dv := defaultValue{has: v.IsValid(), val: v, enum: ev} |
| 684 | if b, ok := v.Interface().([]byte); ok { |
| 685 | // Store a copy of the default bytes, so that we can detect |
| 686 | // accidental mutations of the original value. |
| 687 | dv.bytes = append([]byte(nil), b...) |
| 688 | } |
| 689 | return dv |
| 690 | } |
| 691 | |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 692 | func unmarshalDefault(b []byte, k protoreflect.Kind, pf *File, ed protoreflect.EnumDescriptor) defaultValue { |
| 693 | var evs protoreflect.EnumValueDescriptors |
| 694 | if k == protoreflect.EnumKind { |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 695 | // If the enum is declared within the same file, be careful not to |
| 696 | // blindly call the Values method, lest we bind ourselves in a deadlock. |
| 697 | if e, ok := ed.(*Enum); ok && e.L0.ParentFile == pf { |
| 698 | evs = &e.L2.Values |
| 699 | } else { |
| 700 | evs = ed.Values() |
| 701 | } |
| 702 | |
| 703 | // If we are unable to resolve the enum dependency, use a placeholder |
| 704 | // enum value since we will not be able to parse the default value. |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 705 | if ed.IsPlaceholder() && protoreflect.Name(b).IsValid() { |
| 706 | v := protoreflect.ValueOfEnum(0) |
| 707 | ev := PlaceholderEnumValue(ed.FullName().Parent().Append(protoreflect.Name(b))) |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 708 | return DefaultValue(v, ev) |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | v, ev, err := defval.Unmarshal(string(b), k, evs, defval.Descriptor) |
| 713 | if err != nil { |
| 714 | panic(err) |
| 715 | } |
| 716 | return DefaultValue(v, ev) |
| 717 | } |
| 718 | |
| 719 | type defaultValue struct { |
| 720 | has bool |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 721 | val protoreflect.Value |
| 722 | enum protoreflect.EnumValueDescriptor |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 723 | bytes []byte |
| 724 | } |
| 725 | |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 726 | func (dv *defaultValue) get(fd protoreflect.FieldDescriptor) protoreflect.Value { |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 727 | // Return the zero value as the default if unpopulated. |
| 728 | if !dv.has { |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 729 | if fd.Cardinality() == protoreflect.Repeated { |
| 730 | return protoreflect.Value{} |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 731 | } |
| 732 | switch fd.Kind() { |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 733 | case protoreflect.BoolKind: |
| 734 | return protoreflect.ValueOfBool(false) |
| 735 | case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind: |
| 736 | return protoreflect.ValueOfInt32(0) |
| 737 | case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind: |
| 738 | return protoreflect.ValueOfInt64(0) |
| 739 | case protoreflect.Uint32Kind, protoreflect.Fixed32Kind: |
| 740 | return protoreflect.ValueOfUint32(0) |
| 741 | case protoreflect.Uint64Kind, protoreflect.Fixed64Kind: |
| 742 | return protoreflect.ValueOfUint64(0) |
| 743 | case protoreflect.FloatKind: |
| 744 | return protoreflect.ValueOfFloat32(0) |
| 745 | case protoreflect.DoubleKind: |
| 746 | return protoreflect.ValueOfFloat64(0) |
| 747 | case protoreflect.StringKind: |
| 748 | return protoreflect.ValueOfString("") |
| 749 | case protoreflect.BytesKind: |
| 750 | return protoreflect.ValueOfBytes(nil) |
| 751 | case protoreflect.EnumKind: |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 752 | if evs := fd.Enum().Values(); evs.Len() > 0 { |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 753 | return protoreflect.ValueOfEnum(evs.Get(0).Number()) |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 754 | } |
| mgouda | 64be882 | 2025-05-30 10:44:00 +0530 | [diff] [blame] | 755 | return protoreflect.ValueOfEnum(0) |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | |
| 759 | if len(dv.bytes) > 0 && !bytes.Equal(dv.bytes, dv.val.Bytes()) { |
| 760 | // TODO: Avoid panic if we're running with the race detector |
| 761 | // and instead spawn a goroutine that periodically resets |
| 762 | // this value back to the original to induce a race. |
| 763 | panic(fmt.Sprintf("detected mutation on the default bytes for %v", fd.FullName())) |
| 764 | } |
| 765 | return dv.val |
| 766 | } |