mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-02-19 12:40:24 +00:00
Generate everything related to extensions: extension descriptors, XXX_InternalExtensions fields, etc. Tweak the order in which we generate code for descriptors to ensure consistent output with the previous protoc-gen-go. Change Field.ContainingType to Field.ParentMessage, since we need to get at the parent of both message fields and extensions (and the "containing type" of an extension field is the extended message, under existing terminology). Change-Id: I5d045ca80536436e7c987bca3d8fb8c1e1521e55 Reviewed-on: https://go-review.googlesource.com/136155 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
42 lines
1.2 KiB
Go
42 lines
1.2 KiB
Go
// Copyright 2018 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// This file contains functions for fetching the options for a protoreflect descriptor.
|
|
//
|
|
// TODO: Replace this with the appropriate protoreflect API, once it exists.
|
|
|
|
package main
|
|
|
|
import (
|
|
descpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
|
|
"google.golang.org/proto/protogen"
|
|
"google.golang.org/proto/reflect/protoreflect"
|
|
)
|
|
|
|
// messageOptions returns the MessageOptions for a message.
|
|
func messageOptions(gen *protogen.Plugin, message *protogen.Message) *descpb.MessageOptions {
|
|
file, ok := descriptorFile(gen, message.Desc)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
desc := file.Proto.MessageType[message.Path[1]]
|
|
for i := 3; i < len(message.Path); i += 2 {
|
|
desc = desc.NestedType[message.Path[1]]
|
|
}
|
|
return desc.GetOptions()
|
|
}
|
|
|
|
func descriptorFile(gen *protogen.Plugin, desc protoreflect.Descriptor) (*protogen.File, bool) {
|
|
for {
|
|
if fdesc, ok := desc.(protoreflect.FileDescriptor); ok {
|
|
return gen.FileByName(fdesc.Path())
|
|
}
|
|
var ok bool
|
|
desc, ok = desc.Parent()
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
}
|
|
}
|