Skip to content

descriptor: add Enum interface and ForEnum function #302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions descriptor/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"compress/gzip"
"fmt"
"io/ioutil"
"reflect"

"github.com/golang/protobuf/proto"
protobuf "github.com/golang/protobuf/protoc-gen-go/descriptor"
Expand Down Expand Up @@ -91,3 +92,37 @@ func ForMessage(msg Message) (fd *protobuf.FileDescriptorProto, md *protobuf.Des
}
return fd, md
}

// Enum declares a method to return an protobuf enum's descriptor.
//
// Enum types generated by the protocol compiler always satisfy
// the Enum interface.
type Enum interface {
EnumDescriptor() ([]byte, []int)
String() string
}

// ForEnum returns a FileDescriptorProto and an EnumDescriptorProto from within
// it describing the given enum.
func ForEnum(e Enum) (*protobuf.FileDescriptorProto, *protobuf.EnumDescriptorProto) {
// EnumDescriptor isn't defined for pointer receivers.
if re := reflect.ValueOf(e); re.Kind() == reflect.Ptr && re.IsNil() {
e = reflect.Zero(re.Type().Elem()).Interface().(Enum)
}

gz, path := e.EnumDescriptor()
fd, err := extractFile(gz)
if err != nil {
panic(fmt.Sprintf("invalid FileDescriptorProto for %T: %v", e, err))
}

if len(path) == 1 {
return fd, fd.EnumType[path[0]]
}

md := fd.MessageType[path[0]]
for _, i := range path[1 : len(path)-1] {
md = md.NestedType[i]
}
return fd, md.EnumType[path[len(path)-1]]
}
25 changes: 25 additions & 0 deletions descriptor/descriptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ func TestMessage(t *testing.T) {
}
}

func TestEnum(t *testing.T) {
var enum *protobuf.FieldDescriptorProto_Type
fd, ed := descriptor.ForEnum(enum)
if pkg, want := fd.GetPackage(), "google.protobuf"; pkg != want {
t.Errorf("descriptor.ForEnum(%T).GetPackage() = %q; want %q", enum, pkg, want)
}
if name, want := ed.GetName(), "Type"; name != want {
t.Errorf("descriptor.ForEnum(%T).GetName() = %q; want %q", enum, name, want)
}
if value, want := ed.GetValue()[0].GetName(), "TYPE_DOUBLE"; value != want {
t.Errorf("descriptor.ForEnum(%T).GetValue()[0].GetName() = %v; want %v", enum, value, want)
}
}

func Example_Options() {
var msg *tpb.MyMessageSet
_, md := descriptor.ForMessage(msg)
Expand All @@ -30,3 +44,14 @@ func Example_Options() {
// Output:
// MyMessageSet uses option message_set_wire_format.
}

func Example_EnumOptions() {
var enum *tpb.EnumAllowingAlias_NUMBER
_, ed := descriptor.ForEnum(enum)
if ed.GetOptions().GetAllowAlias() {
fmt.Printf("%v uses option allow_alias.\n", ed.GetName())
}

// Output:
// NUMBER uses option allow_alias.
}
Loading