Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 1 | // Copyright 2018 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 fileinit constructs protoreflect.FileDescriptors from the encoded |
| 6 | // file descriptor proto messages. This package uses a custom proto unmarshaler |
| 7 | // 1) to avoid a dependency on the descriptor proto 2) for performance to keep |
| 8 | // the initialization cost as low as possible. |
| 9 | package fileinit |
| 10 | |
| 11 | import ( |
| 12 | "fmt" |
| 13 | "reflect" |
| 14 | "sync" |
| 15 | |
Joe Tsai | a2dd228 | 2019-04-10 16:45:04 -0700 | [diff] [blame] | 16 | descopts "github.com/golang/protobuf/v2/internal/descopts" |
Joe Tsai | 35ec98f | 2019-03-25 14:41:32 -0700 | [diff] [blame] | 17 | pimpl "github.com/golang/protobuf/v2/internal/impl" |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 18 | pragma "github.com/golang/protobuf/v2/internal/pragma" |
| 19 | pfmt "github.com/golang/protobuf/v2/internal/typefmt" |
| 20 | "github.com/golang/protobuf/v2/proto" |
| 21 | pref "github.com/golang/protobuf/v2/reflect/protoreflect" |
Joe Tsai | 08cd884 | 2019-03-18 13:46:39 -0700 | [diff] [blame] | 22 | preg "github.com/golang/protobuf/v2/reflect/protoregistry" |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame] | 23 | piface "github.com/golang/protobuf/v2/runtime/protoiface" |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | // FileBuilder construct a protoreflect.FileDescriptor from the |
| 27 | // raw file descriptor and the Go types for declarations and dependencies. |
| 28 | // |
| 29 | // |
| 30 | // Flattened Ordering |
| 31 | // |
| 32 | // The protobuf type system represents declarations as a tree. Certain nodes in |
| 33 | // the tree require us to either associate it with a concrete Go type or to |
| 34 | // resolve a dependency, which is information that must be provided separately |
| 35 | // since it cannot be derived from the file descriptor alone. |
| 36 | // |
| 37 | // However, representing a tree as Go literals is difficult to simply do in a |
| 38 | // space and time efficient way. Thus, we store them as a flattened list of |
| 39 | // objects where the serialization order from the tree-based form is important. |
| 40 | // |
| 41 | // The "flattened ordering" is defined as a tree traversal of all enum, message, |
| 42 | // extension, and service declarations using the following algorithm: |
| 43 | // |
| 44 | // def VisitFileDecls(fd): |
| 45 | // for e in fd.Enums: yield e |
| 46 | // for m in fd.Messages: yield m |
| 47 | // for x in fd.Extensions: yield x |
| 48 | // for s in fd.Services: yield s |
| 49 | // for m in fd.Messages: yield from VisitMessageDecls(m) |
| 50 | // |
| 51 | // def VisitMessageDecls(md): |
| 52 | // for e in md.Enums: yield e |
| 53 | // for m in md.Messages: yield m |
| 54 | // for x in md.Extensions: yield x |
| 55 | // for m in md.Messages: yield from VisitMessageDecls(m) |
| 56 | // |
| 57 | // The traversal starts at the root file descriptor and yields each direct |
| 58 | // declaration within each node before traversing into sub-declarations |
| 59 | // that children themselves may have. |
| 60 | type FileBuilder struct { |
| 61 | // RawDescriptor is the wire-encoded bytes of FileDescriptorProto. |
| 62 | RawDescriptor []byte |
| 63 | |
| 64 | // GoTypes is a unique set of the Go types for all declarations and |
| 65 | // dependencies. Each type is represented as a zero value of the Go type. |
| 66 | // |
| 67 | // Declarations are Go types generated for enums and messages directly |
| 68 | // declared (not publicly imported) in the proto source file. |
| 69 | // Messages for map entries are included, but represented by nil. |
| 70 | // Enum declarations in "flattened ordering" come first, followed by |
| 71 | // message declarations in "flattened ordering". The length of each sub-list |
| 72 | // is len(EnumOutputTypes) and len(MessageOutputTypes), respectively. |
| 73 | // |
| 74 | // Dependencies are Go types for enums or messages referenced by |
| 75 | // message fields (excluding weak fields), for parent extended messages of |
| 76 | // extension fields, for enums or messages referenced by extension fields, |
| 77 | // and for input and output messages referenced by service methods. |
| 78 | // Dependencies must come after declarations, but the ordering of |
| 79 | // dependencies themselves is unspecified. |
| 80 | GoTypes []interface{} |
| 81 | |
| 82 | // DependencyIndexes is an ordered list of indexes into GoTypes for the |
| 83 | // dependencies of messages, extensions, or services. There are 4 sub-lists |
| 84 | // each in "flattened ordering" concatenated back-to-back: |
| 85 | // * Extension field targets: list of the extended parent message of |
| 86 | // every extension. Length is len(ExtensionOutputTypes). |
| 87 | // * Message field dependencies: list of the enum or message type |
| 88 | // referred to by every message field. |
| 89 | // * Extension field dependencies: list of the enum or message type |
| 90 | // referred to by every extension field. |
| 91 | // * Service method dependencies: list of the input and output message type |
| 92 | // referred to by every service method. |
| 93 | DependencyIndexes []int32 |
| 94 | |
| 95 | // TODO: Provide a list of imported files. |
| 96 | // FileDependencies []pref.FileDescriptor |
| 97 | |
| 98 | // TODO: Provide a list of extension types for options extensions. |
| 99 | // OptionDependencies []pref.ExtensionType |
| 100 | |
Joe Tsai | afb455e | 2019-03-14 16:08:22 -0700 | [diff] [blame] | 101 | // LegacyExtensions are a list of legacy extension descriptors. |
| 102 | // If provided, the pointer to the v1 ExtensionDesc will be stored into the |
| 103 | // associated v2 ExtensionType and accessible via a pseudo-internal API. |
| 104 | // Also, the v2 ExtensionType will be stored into each v1 ExtensionDesc. |
| 105 | // If non-nil, len(LegacyExtensions) must equal len(ExtensionOutputTypes). |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame] | 106 | LegacyExtensions []piface.ExtensionDescV1 |
Joe Tsai | afb455e | 2019-03-14 16:08:22 -0700 | [diff] [blame] | 107 | |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 108 | // EnumOutputTypes is where Init stores all initialized enum types |
| 109 | // in "flattened ordering". |
| 110 | EnumOutputTypes []pref.EnumType |
| 111 | // MessageOutputTypes is where Init stores all initialized message types |
Joe Tsai | 4532dd7 | 2019-03-19 17:04:06 -0700 | [diff] [blame] | 112 | // in "flattened ordering". This includes slots for map entry messages, |
| 113 | // which are skipped over. |
Joe Tsai | 35ec98f | 2019-03-25 14:41:32 -0700 | [diff] [blame] | 114 | MessageOutputTypes []pimpl.MessageType |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 115 | // ExtensionOutputTypes is where Init stores all initialized extension types |
| 116 | // in "flattened ordering". |
| 117 | ExtensionOutputTypes []pref.ExtensionType |
| 118 | |
Joe Tsai | 08cd884 | 2019-03-18 13:46:39 -0700 | [diff] [blame] | 119 | // FilesRegistry is the file registry to register the file descriptor. |
| 120 | // If nil, no registration occurs. |
| 121 | FilesRegistry *preg.Files |
| 122 | // TypesRegistry is the types registry to register each type descriptor. |
| 123 | // If nil, no registration occurs. |
| 124 | TypesRegistry *preg.Types |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // Init constructs a FileDescriptor given the parameters set in FileBuilder. |
| 128 | // It assumes that the inputs are well-formed and panics if any inconsistencies |
| 129 | // are encountered. |
| 130 | func (fb FileBuilder) Init() pref.FileDescriptor { |
| 131 | fd := newFileDesc(fb) |
| 132 | |
Joe Tsai | 08cd884 | 2019-03-18 13:46:39 -0700 | [diff] [blame] | 133 | // Keep v1 and v2 extension descriptors in sync. |
Joe Tsai | afb455e | 2019-03-14 16:08:22 -0700 | [diff] [blame] | 134 | if fb.LegacyExtensions != nil { |
| 135 | for i := range fd.allExtensions { |
| 136 | fd.allExtensions[i].legacyDesc = &fb.LegacyExtensions[i] |
| 137 | fb.LegacyExtensions[i].Type = &fd.allExtensions[i] |
| 138 | } |
| 139 | } |
| 140 | |
Joe Tsai | 08cd884 | 2019-03-18 13:46:39 -0700 | [diff] [blame] | 141 | // Copy type descriptors to the output. |
Joe Tsai | 35ec98f | 2019-03-25 14:41:32 -0700 | [diff] [blame] | 142 | // |
| 143 | // While iterating over the messages, we also determine whether the message |
| 144 | // is a map entry type. |
| 145 | messageGoTypes := fb.GoTypes[len(fd.allEnums):][:len(fd.allMessages)] |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 146 | for i := range fd.allEnums { |
| 147 | fb.EnumOutputTypes[i] = &fd.allEnums[i] |
| 148 | } |
| 149 | for i := range fd.allMessages { |
Joe Tsai | 35ec98f | 2019-03-25 14:41:32 -0700 | [diff] [blame] | 150 | if messageGoTypes[i] == nil { |
| 151 | fd.allMessages[i].isMapEntry = true |
| 152 | } else { |
| 153 | fb.MessageOutputTypes[i].GoType = reflect.TypeOf(messageGoTypes[i]) |
| 154 | fb.MessageOutputTypes[i].PBType = fd.allMessages[i].asDesc().(pref.MessageType) |
Joe Tsai | 4532dd7 | 2019-03-19 17:04:06 -0700 | [diff] [blame] | 155 | } |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 156 | } |
| 157 | for i := range fd.allExtensions { |
| 158 | fb.ExtensionOutputTypes[i] = &fd.allExtensions[i] |
| 159 | } |
Joe Tsai | 08cd884 | 2019-03-18 13:46:39 -0700 | [diff] [blame] | 160 | |
Joe Tsai | a2dd228 | 2019-04-10 16:45:04 -0700 | [diff] [blame] | 161 | // As a special-case for descriptor.proto, |
| 162 | // locally register concrete message type for the options. |
| 163 | if fd.Path() == "google/protobuf/descriptor.proto" && fd.Package() == "google.protobuf" { |
| 164 | for i := range fd.allMessages { |
| 165 | switch fd.allMessages[i].Name() { |
| 166 | case "FileOptions": |
| 167 | descopts.File = messageGoTypes[i].(pref.ProtoMessage) |
| 168 | case "EnumOptions": |
| 169 | descopts.Enum = messageGoTypes[i].(pref.ProtoMessage) |
| 170 | case "EnumValueOptions": |
| 171 | descopts.EnumValue = messageGoTypes[i].(pref.ProtoMessage) |
| 172 | case "MessageOptions": |
| 173 | descopts.Message = messageGoTypes[i].(pref.ProtoMessage) |
| 174 | case "FieldOptions": |
| 175 | descopts.Field = messageGoTypes[i].(pref.ProtoMessage) |
| 176 | case "OneofOptions": |
| 177 | descopts.Oneof = messageGoTypes[i].(pref.ProtoMessage) |
| 178 | case "ExtensionRangeOptions": |
| 179 | descopts.ExtensionRange = messageGoTypes[i].(pref.ProtoMessage) |
| 180 | case "ServiceOptions": |
| 181 | descopts.Service = messageGoTypes[i].(pref.ProtoMessage) |
| 182 | case "MethodOptions": |
| 183 | descopts.Method = messageGoTypes[i].(pref.ProtoMessage) |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
Joe Tsai | 08cd884 | 2019-03-18 13:46:39 -0700 | [diff] [blame] | 188 | // Register file and type descriptors. |
| 189 | if fb.FilesRegistry != nil { |
| 190 | if err := fb.FilesRegistry.Register(fd); err != nil { |
| 191 | panic(err) |
| 192 | } |
| 193 | } |
| 194 | if fb.TypesRegistry != nil { |
| 195 | for i := range fd.allEnums { |
| 196 | if err := fb.TypesRegistry.Register(&fd.allEnums[i]); err != nil { |
| 197 | panic(err) |
| 198 | } |
| 199 | } |
| 200 | for i := range fd.allMessages { |
Joe Tsai | 4532dd7 | 2019-03-19 17:04:06 -0700 | [diff] [blame] | 201 | if mt, _ := fd.allMessages[i].asDesc().(pref.MessageType); mt != nil { |
| 202 | if err := fb.TypesRegistry.Register(mt); err != nil { |
| 203 | panic(err) |
| 204 | } |
Joe Tsai | 08cd884 | 2019-03-18 13:46:39 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | for i := range fd.allExtensions { |
| 208 | if err := fb.TypesRegistry.Register(&fd.allExtensions[i]); err != nil { |
| 209 | panic(err) |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 214 | return fd |
| 215 | } |
| 216 | |
| 217 | type ( |
| 218 | // fileInit contains a copy of certain fields in FileBuilder for use during |
| 219 | // lazy initialization upon first use. |
| 220 | fileInit struct { |
| 221 | RawDescriptor []byte |
| 222 | GoTypes []interface{} |
| 223 | DependencyIndexes []int32 |
| 224 | } |
| 225 | fileDesc struct { |
| 226 | fileInit |
| 227 | |
| 228 | path string |
| 229 | protoPackage pref.FullName |
| 230 | |
| 231 | fileDecls |
| 232 | |
| 233 | enums enumDescs |
| 234 | messages messageDescs |
| 235 | extensions extensionDescs |
| 236 | services serviceDescs |
| 237 | |
| 238 | once sync.Once |
| 239 | lazy *fileLazy // protected by once |
| 240 | } |
| 241 | fileDecls struct { |
| 242 | allEnums []enumDesc |
| 243 | allMessages []messageDesc |
| 244 | allExtensions []extensionDesc |
| 245 | } |
| 246 | fileLazy struct { |
| 247 | syntax pref.Syntax |
| 248 | imports fileImports |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 249 | options []byte |
| 250 | } |
| 251 | ) |
| 252 | |
| 253 | func (fd *fileDesc) Parent() (pref.Descriptor, bool) { return nil, false } |
| 254 | func (fd *fileDesc) Index() int { return 0 } |
| 255 | func (fd *fileDesc) Syntax() pref.Syntax { return fd.lazyInit().syntax } |
| 256 | func (fd *fileDesc) Name() pref.Name { return fd.Package().Name() } |
| 257 | func (fd *fileDesc) FullName() pref.FullName { return fd.Package() } |
| 258 | func (fd *fileDesc) IsPlaceholder() bool { return false } |
Joe Tsai | a2dd228 | 2019-04-10 16:45:04 -0700 | [diff] [blame] | 259 | func (fd *fileDesc) Options() pref.ProtoMessage { |
| 260 | return unmarshalOptions(descopts.File, fd.lazyInit().options) |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 261 | } |
Damien Neil | 2300c18 | 2019-04-15 13:05:13 -0700 | [diff] [blame] | 262 | func (fd *fileDesc) Path() string { return fd.path } |
| 263 | func (fd *fileDesc) Package() pref.FullName { return fd.protoPackage } |
| 264 | func (fd *fileDesc) Imports() pref.FileImports { return &fd.lazyInit().imports } |
| 265 | func (fd *fileDesc) Enums() pref.EnumDescriptors { return &fd.enums } |
| 266 | func (fd *fileDesc) Messages() pref.MessageDescriptors { return &fd.messages } |
| 267 | func (fd *fileDesc) Extensions() pref.ExtensionDescriptors { return &fd.extensions } |
| 268 | func (fd *fileDesc) Services() pref.ServiceDescriptors { return &fd.services } |
| 269 | func (fd *fileDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, fd) } |
| 270 | func (fd *fileDesc) ProtoType(pref.FileDescriptor) {} |
| 271 | func (fd *fileDesc) ProtoInternal(pragma.DoNotImplement) {} |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 272 | |
| 273 | type ( |
| 274 | enumDesc struct { |
| 275 | baseDesc |
| 276 | |
| 277 | lazy *enumLazy // protected by fileDesc.once |
| 278 | } |
| 279 | enumLazy struct { |
| 280 | typ reflect.Type |
| 281 | new func(pref.EnumNumber) pref.Enum |
| 282 | |
| 283 | values enumValueDescs |
| 284 | resvNames names |
| 285 | resvRanges enumRanges |
| 286 | options []byte |
| 287 | } |
| 288 | enumValueDesc struct { |
| 289 | baseDesc |
| 290 | |
| 291 | number pref.EnumNumber |
| 292 | options []byte |
| 293 | } |
| 294 | ) |
| 295 | |
| 296 | func (ed *enumDesc) GoType() reflect.Type { return ed.lazyInit().typ } |
| 297 | func (ed *enumDesc) New(n pref.EnumNumber) pref.Enum { return ed.lazyInit().new(n) } |
Joe Tsai | a2dd228 | 2019-04-10 16:45:04 -0700 | [diff] [blame] | 298 | func (ed *enumDesc) Options() pref.ProtoMessage { |
| 299 | return unmarshalOptions(descopts.Enum, ed.lazyInit().options) |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 300 | } |
| 301 | func (ed *enumDesc) Values() pref.EnumValueDescriptors { return &ed.lazyInit().values } |
| 302 | func (ed *enumDesc) ReservedNames() pref.Names { return &ed.lazyInit().resvNames } |
| 303 | func (ed *enumDesc) ReservedRanges() pref.EnumRanges { return &ed.lazyInit().resvRanges } |
| 304 | func (ed *enumDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, ed) } |
| 305 | func (ed *enumDesc) ProtoType(pref.EnumDescriptor) {} |
| 306 | func (ed *enumDesc) lazyInit() *enumLazy { |
| 307 | ed.parentFile.lazyInit() // implicitly initializes enumLazy |
| 308 | return ed.lazy |
| 309 | } |
| 310 | |
Joe Tsai | a2dd228 | 2019-04-10 16:45:04 -0700 | [diff] [blame] | 311 | func (ed *enumValueDesc) Options() pref.ProtoMessage { |
| 312 | return unmarshalOptions(descopts.EnumValue, ed.options) |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 313 | } |
| 314 | func (ed *enumValueDesc) Number() pref.EnumNumber { return ed.number } |
| 315 | func (ed *enumValueDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, ed) } |
| 316 | func (ed *enumValueDesc) ProtoType(pref.EnumValueDescriptor) {} |
| 317 | |
| 318 | type ( |
Joe Tsai | 4532dd7 | 2019-03-19 17:04:06 -0700 | [diff] [blame] | 319 | messageType struct{ *messageDesc } |
| 320 | messageDescriptor struct{ *messageDesc } |
| 321 | |
| 322 | // messageDesc does not implement protoreflect.Descriptor to avoid |
| 323 | // accidental usages of it as such. Use the asDesc method to retrieve one. |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 324 | messageDesc struct { |
| 325 | baseDesc |
| 326 | |
| 327 | enums enumDescs |
| 328 | messages messageDescs |
| 329 | extensions extensionDescs |
| 330 | |
Joe Tsai | 4532dd7 | 2019-03-19 17:04:06 -0700 | [diff] [blame] | 331 | isMapEntry bool |
| 332 | lazy *messageLazy // protected by fileDesc.once |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 333 | } |
| 334 | messageLazy struct { |
| 335 | typ reflect.Type |
| 336 | new func() pref.Message |
| 337 | |
Joe Tsai | 1321a0e | 2019-03-20 09:46:22 -0700 | [diff] [blame] | 338 | isMessageSet bool |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 339 | fields fieldDescs |
| 340 | oneofs oneofDescs |
| 341 | resvNames names |
| 342 | resvRanges fieldRanges |
| 343 | reqNumbers fieldNumbers |
| 344 | extRanges fieldRanges |
| 345 | extRangeOptions [][]byte |
| 346 | options []byte |
| 347 | } |
| 348 | fieldDesc struct { |
| 349 | baseDesc |
| 350 | |
| 351 | number pref.FieldNumber |
| 352 | cardinality pref.Cardinality |
| 353 | kind pref.Kind |
| 354 | hasJSONName bool |
| 355 | jsonName string |
| 356 | hasPacked bool |
| 357 | isPacked bool |
| 358 | isWeak bool |
| 359 | isMap bool |
| 360 | defVal defaultValue |
| 361 | oneofType pref.OneofDescriptor |
| 362 | enumType pref.EnumDescriptor |
| 363 | messageType pref.MessageDescriptor |
| 364 | options []byte |
| 365 | } |
| 366 | oneofDesc struct { |
| 367 | baseDesc |
| 368 | |
| 369 | fields oneofFields |
| 370 | options []byte |
| 371 | } |
| 372 | ) |
| 373 | |
Joe Tsai | a2dd228 | 2019-04-10 16:45:04 -0700 | [diff] [blame] | 374 | func (md *messageDesc) options() pref.ProtoMessage { |
| 375 | return unmarshalOptions(descopts.Message, md.lazyInit().options) |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 376 | } |
Joe Tsai | 4532dd7 | 2019-03-19 17:04:06 -0700 | [diff] [blame] | 377 | func (md *messageDesc) IsMapEntry() bool { return md.isMapEntry } |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 378 | func (md *messageDesc) Fields() pref.FieldDescriptors { return &md.lazyInit().fields } |
| 379 | func (md *messageDesc) Oneofs() pref.OneofDescriptors { return &md.lazyInit().oneofs } |
| 380 | func (md *messageDesc) ReservedNames() pref.Names { return &md.lazyInit().resvNames } |
| 381 | func (md *messageDesc) ReservedRanges() pref.FieldRanges { return &md.lazyInit().resvRanges } |
| 382 | func (md *messageDesc) RequiredNumbers() pref.FieldNumbers { return &md.lazyInit().reqNumbers } |
| 383 | func (md *messageDesc) ExtensionRanges() pref.FieldRanges { return &md.lazyInit().extRanges } |
Joe Tsai | a2dd228 | 2019-04-10 16:45:04 -0700 | [diff] [blame] | 384 | func (md *messageDesc) ExtensionRangeOptions(i int) pref.ProtoMessage { |
| 385 | return unmarshalOptions(descopts.ExtensionRange, md.lazyInit().extRangeOptions[i]) |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 386 | } |
| 387 | func (md *messageDesc) Enums() pref.EnumDescriptors { return &md.enums } |
| 388 | func (md *messageDesc) Messages() pref.MessageDescriptors { return &md.messages } |
| 389 | func (md *messageDesc) Extensions() pref.ExtensionDescriptors { return &md.extensions } |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 390 | func (md *messageDesc) ProtoType(pref.MessageDescriptor) {} |
Joe Tsai | 4532dd7 | 2019-03-19 17:04:06 -0700 | [diff] [blame] | 391 | func (md *messageDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, md.asDesc()) } |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 392 | func (md *messageDesc) lazyInit() *messageLazy { |
| 393 | md.parentFile.lazyInit() // implicitly initializes messageLazy |
| 394 | return md.lazy |
| 395 | } |
| 396 | |
Joe Tsai | 1321a0e | 2019-03-20 09:46:22 -0700 | [diff] [blame] | 397 | // IsMessageSet is a pseudo-internal API for checking whether a message |
| 398 | // should serialize in the proto1 message format. |
Joe Tsai | a5f43e8 | 2019-04-10 17:11:20 -0700 | [diff] [blame] | 399 | // |
| 400 | // WARNING: This method is exempt from the compatibility promise and may be |
| 401 | // removed in the future without warning. |
Joe Tsai | 1321a0e | 2019-03-20 09:46:22 -0700 | [diff] [blame] | 402 | func (md *messageDesc) IsMessageSet() bool { |
| 403 | return md.lazyInit().isMessageSet |
| 404 | } |
| 405 | |
Joe Tsai | 4532dd7 | 2019-03-19 17:04:06 -0700 | [diff] [blame] | 406 | // asDesc returns a protoreflect.MessageDescriptor or protoreflect.MessageType |
| 407 | // depending on whether the message is a map entry or not. |
| 408 | func (mb *messageDesc) asDesc() pref.MessageDescriptor { |
| 409 | if !mb.isMapEntry { |
| 410 | return messageType{mb} |
| 411 | } |
| 412 | return messageDescriptor{mb} |
| 413 | } |
Joe Tsai | a2dd228 | 2019-04-10 16:45:04 -0700 | [diff] [blame] | 414 | func (mt messageType) GoType() reflect.Type { return mt.lazyInit().typ } |
| 415 | func (mt messageType) New() pref.Message { return mt.lazyInit().new() } |
| 416 | func (mt messageType) Options() pref.ProtoMessage { return mt.options() } |
| 417 | func (md messageDescriptor) Options() pref.ProtoMessage { return md.options() } |
Joe Tsai | 4532dd7 | 2019-03-19 17:04:06 -0700 | [diff] [blame] | 418 | |
Joe Tsai | a2dd228 | 2019-04-10 16:45:04 -0700 | [diff] [blame] | 419 | func (fd *fieldDesc) Options() pref.ProtoMessage { |
| 420 | return unmarshalOptions(descopts.Field, fd.options) |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 421 | } |
| 422 | func (fd *fieldDesc) Number() pref.FieldNumber { return fd.number } |
| 423 | func (fd *fieldDesc) Cardinality() pref.Cardinality { return fd.cardinality } |
| 424 | func (fd *fieldDesc) Kind() pref.Kind { return fd.kind } |
| 425 | func (fd *fieldDesc) HasJSONName() bool { return fd.hasJSONName } |
| 426 | func (fd *fieldDesc) JSONName() string { return fd.jsonName } |
| 427 | func (fd *fieldDesc) IsPacked() bool { return fd.isPacked } |
| 428 | func (fd *fieldDesc) IsWeak() bool { return fd.isWeak } |
| 429 | func (fd *fieldDesc) IsMap() bool { return fd.isMap } |
| 430 | func (fd *fieldDesc) HasDefault() bool { return fd.defVal.has } |
| 431 | func (fd *fieldDesc) Default() pref.Value { return fd.defVal.get() } |
| 432 | func (fd *fieldDesc) DefaultEnumValue() pref.EnumValueDescriptor { return fd.defVal.enum } |
| 433 | func (fd *fieldDesc) OneofType() pref.OneofDescriptor { return fd.oneofType } |
| 434 | func (fd *fieldDesc) ExtendedType() pref.MessageDescriptor { return nil } |
| 435 | func (fd *fieldDesc) EnumType() pref.EnumDescriptor { return fd.enumType } |
| 436 | func (fd *fieldDesc) MessageType() pref.MessageDescriptor { return fd.messageType } |
| 437 | func (fd *fieldDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, fd) } |
| 438 | func (fd *fieldDesc) ProtoType(pref.FieldDescriptor) {} |
| 439 | |
Joe Tsai | a2dd228 | 2019-04-10 16:45:04 -0700 | [diff] [blame] | 440 | func (od *oneofDesc) Options() pref.ProtoMessage { |
| 441 | return unmarshalOptions(descopts.Oneof, od.options) |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 442 | } |
| 443 | func (od *oneofDesc) Fields() pref.FieldDescriptors { return &od.fields } |
| 444 | func (od *oneofDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, od) } |
| 445 | func (od *oneofDesc) ProtoType(pref.OneofDescriptor) {} |
| 446 | |
| 447 | type ( |
| 448 | extensionDesc struct { |
| 449 | baseDesc |
| 450 | |
| 451 | number pref.FieldNumber |
| 452 | extendedType pref.MessageDescriptor |
| 453 | |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame] | 454 | legacyDesc *piface.ExtensionDescV1 |
Joe Tsai | afb455e | 2019-03-14 16:08:22 -0700 | [diff] [blame] | 455 | |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 456 | lazy *extensionLazy // protected by fileDesc.once |
| 457 | } |
| 458 | extensionLazy struct { |
| 459 | typ reflect.Type |
| 460 | new func() pref.Value |
| 461 | valueOf func(interface{}) pref.Value |
| 462 | interfaceOf func(pref.Value) interface{} |
| 463 | |
| 464 | cardinality pref.Cardinality |
| 465 | kind pref.Kind |
| 466 | // Extensions should not have JSON names, but older versions of protoc |
| 467 | // used to set one on the descriptor. Preserve it for now to maintain |
| 468 | // the property that protoc 3.6.1 descriptors can round-trip through |
| 469 | // this package losslessly. |
| 470 | // |
| 471 | // TODO: Consider whether to drop JSONName parsing from extensions. |
| 472 | hasJSONName bool |
| 473 | jsonName string |
| 474 | isPacked bool |
| 475 | defVal defaultValue |
| 476 | enumType pref.EnumType |
| 477 | messageType pref.MessageType |
| 478 | options []byte |
| 479 | } |
| 480 | ) |
| 481 | |
| 482 | func (xd *extensionDesc) GoType() reflect.Type { return xd.lazyInit().typ } |
| 483 | func (xd *extensionDesc) New() pref.Value { return xd.lazyInit().new() } |
| 484 | func (xd *extensionDesc) ValueOf(v interface{}) pref.Value { return xd.lazyInit().valueOf(v) } |
| 485 | func (xd *extensionDesc) InterfaceOf(v pref.Value) interface{} { return xd.lazyInit().interfaceOf(v) } |
Joe Tsai | a2dd228 | 2019-04-10 16:45:04 -0700 | [diff] [blame] | 486 | func (xd *extensionDesc) Options() pref.ProtoMessage { |
| 487 | return unmarshalOptions(descopts.Field, xd.lazyInit().options) |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 488 | } |
| 489 | func (xd *extensionDesc) Number() pref.FieldNumber { return xd.number } |
| 490 | func (xd *extensionDesc) Cardinality() pref.Cardinality { return xd.lazyInit().cardinality } |
| 491 | func (xd *extensionDesc) Kind() pref.Kind { return xd.lazyInit().kind } |
| 492 | func (xd *extensionDesc) HasJSONName() bool { return xd.lazyInit().hasJSONName } |
| 493 | func (xd *extensionDesc) JSONName() string { return xd.lazyInit().jsonName } |
| 494 | func (xd *extensionDesc) IsPacked() bool { return xd.lazyInit().isPacked } |
| 495 | func (xd *extensionDesc) IsWeak() bool { return false } |
| 496 | func (xd *extensionDesc) IsMap() bool { return false } |
| 497 | func (xd *extensionDesc) HasDefault() bool { return xd.lazyInit().defVal.has } |
| 498 | func (xd *extensionDesc) Default() pref.Value { return xd.lazyInit().defVal.get() } |
| 499 | func (xd *extensionDesc) DefaultEnumValue() pref.EnumValueDescriptor { return xd.lazyInit().defVal.enum } |
| 500 | func (xd *extensionDesc) OneofType() pref.OneofDescriptor { return nil } |
| 501 | func (xd *extensionDesc) ExtendedType() pref.MessageDescriptor { return xd.extendedType } |
| 502 | func (xd *extensionDesc) EnumType() pref.EnumDescriptor { return xd.lazyInit().enumType } |
| 503 | func (xd *extensionDesc) MessageType() pref.MessageDescriptor { return xd.lazyInit().messageType } |
| 504 | func (xd *extensionDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, xd) } |
| 505 | func (xd *extensionDesc) ProtoType(pref.FieldDescriptor) {} |
| 506 | func (xd *extensionDesc) ProtoInternal(pragma.DoNotImplement) {} |
| 507 | func (xd *extensionDesc) lazyInit() *extensionLazy { |
| 508 | xd.parentFile.lazyInit() // implicitly initializes extensionLazy |
| 509 | return xd.lazy |
| 510 | } |
| 511 | |
Joe Tsai | afb455e | 2019-03-14 16:08:22 -0700 | [diff] [blame] | 512 | // ProtoLegacyExtensionDesc is a pseudo-internal API for allowing the v1 code |
| 513 | // to be able to retrieve a v1 ExtensionDesc. |
Joe Tsai | a5f43e8 | 2019-04-10 17:11:20 -0700 | [diff] [blame] | 514 | // |
| 515 | // WARNING: This method is exempt from the compatibility promise and may be |
| 516 | // removed in the future without warning. |
| 517 | func (xd *extensionDesc) ProtoLegacyExtensionDesc() *piface.ExtensionDescV1 { |
| 518 | return xd.legacyDesc |
| 519 | } |
Joe Tsai | afb455e | 2019-03-14 16:08:22 -0700 | [diff] [blame] | 520 | |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 521 | type ( |
| 522 | serviceDesc struct { |
| 523 | baseDesc |
| 524 | |
| 525 | lazy *serviceLazy // protected by fileDesc.once |
| 526 | } |
| 527 | serviceLazy struct { |
| 528 | methods methodDescs |
| 529 | options []byte |
| 530 | } |
| 531 | methodDesc struct { |
| 532 | baseDesc |
| 533 | |
| 534 | inputType pref.MessageDescriptor |
| 535 | outputType pref.MessageDescriptor |
| 536 | isStreamingClient bool |
| 537 | isStreamingServer bool |
| 538 | options []byte |
| 539 | } |
| 540 | ) |
| 541 | |
Joe Tsai | a2dd228 | 2019-04-10 16:45:04 -0700 | [diff] [blame] | 542 | func (sd *serviceDesc) Options() pref.ProtoMessage { |
| 543 | return unmarshalOptions(descopts.Service, sd.lazyInit().options) |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 544 | } |
| 545 | func (sd *serviceDesc) Methods() pref.MethodDescriptors { return &sd.lazyInit().methods } |
| 546 | func (sd *serviceDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, sd) } |
| 547 | func (sd *serviceDesc) ProtoType(pref.ServiceDescriptor) {} |
| 548 | func (sd *serviceDesc) ProtoInternal(pragma.DoNotImplement) {} |
| 549 | func (sd *serviceDesc) lazyInit() *serviceLazy { |
| 550 | sd.parentFile.lazyInit() // implicitly initializes serviceLazy |
| 551 | return sd.lazy |
| 552 | } |
| 553 | |
Joe Tsai | a2dd228 | 2019-04-10 16:45:04 -0700 | [diff] [blame] | 554 | func (md *methodDesc) Options() pref.ProtoMessage { |
| 555 | return unmarshalOptions(descopts.Method, md.options) |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 556 | } |
| 557 | func (md *methodDesc) InputType() pref.MessageDescriptor { return md.inputType } |
| 558 | func (md *methodDesc) OutputType() pref.MessageDescriptor { return md.outputType } |
| 559 | func (md *methodDesc) IsStreamingClient() bool { return md.isStreamingClient } |
| 560 | func (md *methodDesc) IsStreamingServer() bool { return md.isStreamingServer } |
| 561 | func (md *methodDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, md) } |
| 562 | func (md *methodDesc) ProtoType(pref.MethodDescriptor) {} |
| 563 | func (md *methodDesc) ProtoInternal(pragma.DoNotImplement) {} |
| 564 | |
| 565 | type baseDesc struct { |
| 566 | parentFile *fileDesc |
| 567 | parent pref.Descriptor |
| 568 | index int |
| 569 | fullName |
| 570 | } |
| 571 | |
| 572 | func (d *baseDesc) Parent() (pref.Descriptor, bool) { return d.parent, true } |
| 573 | func (d *baseDesc) Index() int { return d.index } |
| 574 | func (d *baseDesc) Syntax() pref.Syntax { return d.parentFile.Syntax() } |
| 575 | func (d *baseDesc) IsPlaceholder() bool { return false } |
| 576 | func (d *baseDesc) ProtoInternal(pragma.DoNotImplement) {} |
| 577 | |
| 578 | type fullName struct { |
| 579 | shortLen int |
| 580 | fullName pref.FullName |
| 581 | } |
| 582 | |
| 583 | func (s *fullName) Name() pref.Name { return pref.Name(s.fullName[len(s.fullName)-s.shortLen:]) } |
| 584 | func (s *fullName) FullName() pref.FullName { return s.fullName } |
| 585 | |
Joe Tsai | a2dd228 | 2019-04-10 16:45:04 -0700 | [diff] [blame] | 586 | func unmarshalOptions(p pref.ProtoMessage, b []byte) pref.ProtoMessage { |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 587 | if b != nil { |
| 588 | // TODO: Consider caching the unmarshaled options message. |
Joe Tsai | a2dd228 | 2019-04-10 16:45:04 -0700 | [diff] [blame] | 589 | p = reflect.New(reflect.TypeOf(p).Elem()).Interface().(pref.ProtoMessage) |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 590 | if err := proto.Unmarshal(b, p.(proto.Message)); err != nil { |
| 591 | panic(err) |
| 592 | } |
| 593 | } |
| 594 | return p.(proto.Message) |
| 595 | } |