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