mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-03-10 16:14:39 +00:00
This change was created by running: git ls-files | xargs sed -i "s|google.golang.org/proto|github.com/golang/protobuf/v2|g" This change is *not* an endorsement of "github.com/golang/protobuf/v2" as the final import path when the v2 API is eventually released as stable. We continue to reserve the right to make breaking changes as we see fit. This change enables us to host the v2 API on a repository that is go-gettable (since go.googlesource.com is not a known host by the "go get" tool; and google.golang.org/proto was just a stub URL that is not currently served). Thus, we can start work on a forked version of the v1 API that explores what it would take to implement v1 in terms of v2 in a backwards compatible way. Change-Id: Ia3ebc41ac4238af62ee140200d3158b53ac9ec48 Reviewed-on: https://go-review.googlesource.com/136736 Reviewed-by: Damien Neil <dneil@google.com>
123 lines
3.3 KiB
Go
123 lines
3.3 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 (
|
|
"github.com/golang/protobuf/proto"
|
|
descpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
|
|
"github.com/golang/protobuf/v2/protogen"
|
|
"github.com/golang/protobuf/v2/reflect/protoreflect"
|
|
)
|
|
|
|
// messageOptions returns the MessageOptions for a message.
|
|
func messageOptions(gen *protogen.Plugin, message *protogen.Message) *descpb.MessageOptions {
|
|
d := getDescriptorProto(gen, message.Desc, message.Path)
|
|
if d == nil {
|
|
return nil
|
|
}
|
|
return d.(*descpb.DescriptorProto).GetOptions()
|
|
}
|
|
|
|
// fieldOptions returns the FieldOptions for a message.
|
|
func fieldOptions(gen *protogen.Plugin, field *protogen.Field) *descpb.FieldOptions {
|
|
d := getDescriptorProto(gen, field.Desc, field.Path)
|
|
if d == nil {
|
|
return nil
|
|
}
|
|
return d.(*descpb.FieldDescriptorProto).GetOptions()
|
|
}
|
|
|
|
// enumOptions returns the EnumOptions for an enum
|
|
func enumOptions(gen *protogen.Plugin, enum *protogen.Enum) *descpb.EnumOptions {
|
|
d := getDescriptorProto(gen, enum.Desc, enum.Path)
|
|
if d == nil {
|
|
return nil
|
|
}
|
|
return d.(*descpb.EnumDescriptorProto).GetOptions()
|
|
}
|
|
|
|
// enumValueOptions returns the EnumValueOptions for an enum value
|
|
func enumValueOptions(gen *protogen.Plugin, value *protogen.EnumValue) *descpb.EnumValueOptions {
|
|
d := getDescriptorProto(gen, value.Desc, value.Path)
|
|
if d == nil {
|
|
return nil
|
|
}
|
|
return d.(*descpb.EnumValueDescriptorProto).GetOptions()
|
|
}
|
|
|
|
func getDescriptorProto(gen *protogen.Plugin, desc protoreflect.Descriptor, path []int32) proto.Message {
|
|
var p proto.Message
|
|
// Look up the FileDescriptorProto.
|
|
for {
|
|
if fdesc, ok := desc.(protoreflect.FileDescriptor); ok {
|
|
file, ok := gen.FileByName(fdesc.Path())
|
|
if !ok {
|
|
return nil
|
|
}
|
|
p = file.Proto
|
|
break
|
|
}
|
|
var ok bool
|
|
desc, ok = desc.Parent()
|
|
if !ok {
|
|
return nil
|
|
}
|
|
}
|
|
const (
|
|
// field numbers in FileDescriptorProto
|
|
filePackageField = 2 // package
|
|
fileMessageField = 4 // message_type
|
|
fileEnumField = 5 // enum_type
|
|
fileExtensionField = 7 // extension
|
|
// field numbers in DescriptorProto
|
|
messageFieldField = 2 // field
|
|
messageMessageField = 3 // nested_type
|
|
messageEnumField = 4 // enum_type
|
|
messageExtensionField = 6 // extension
|
|
messageOneofField = 8 // oneof_decl
|
|
// field numbers in EnumDescriptorProto
|
|
enumValueField = 2 // value
|
|
)
|
|
for len(path) > 1 {
|
|
switch d := p.(type) {
|
|
case *descpb.FileDescriptorProto:
|
|
switch path[0] {
|
|
case fileMessageField:
|
|
p = d.MessageType[path[1]]
|
|
case fileEnumField:
|
|
p = d.EnumType[path[1]]
|
|
default:
|
|
return nil
|
|
}
|
|
case *descpb.DescriptorProto:
|
|
switch path[0] {
|
|
case messageFieldField:
|
|
p = d.Field[path[1]]
|
|
case messageMessageField:
|
|
p = d.NestedType[path[1]]
|
|
case messageEnumField:
|
|
p = d.EnumType[path[1]]
|
|
default:
|
|
return nil
|
|
}
|
|
case *descpb.EnumDescriptorProto:
|
|
switch path[0] {
|
|
case enumValueField:
|
|
p = d.Value[path[1]]
|
|
default:
|
|
return nil
|
|
}
|
|
default:
|
|
return nil
|
|
}
|
|
path = path[2:]
|
|
}
|
|
return p
|
|
}
|