blob: 4f83c990591b4406be492718591b1d22dbb1127b [file] [log] [blame]
Joe Tsai90fe9962018-10-18 11:06:29 -07001// 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
5package impl
6
7import (
8 "fmt"
9 "math"
10 "reflect"
11 "sync"
12
13 descriptorV1 "github.com/golang/protobuf/protoc-gen-go/descriptor"
14 pref "github.com/golang/protobuf/v2/reflect/protoreflect"
15 ptype "github.com/golang/protobuf/v2/reflect/prototype"
16)
17
18var enumDescCache sync.Map // map[reflect.Type]protoreflect.EnumDescriptor
19
20// loadEnumDesc returns an EnumDescriptor derived from the Go type,
21// which must be an int32 kind and not implement the v2 API already.
22func loadEnumDesc(t reflect.Type) pref.EnumDescriptor {
23 // Fast-path: check if an EnumDescriptor is cached for this concrete type.
24 if v, ok := enumDescCache.Load(t); ok {
25 return v.(pref.EnumDescriptor)
26 }
27
28 // Slow-path: initialize EnumDescriptor from the proto descriptor.
29 if t.Kind() != reflect.Int32 {
30 panic(fmt.Sprintf("got %v, want int32 kind", t))
31 }
32
33 // Derive the enum descriptor from the raw descriptor proto.
34 e := new(ptype.StandaloneEnum)
35 ev := reflect.Zero(t).Interface()
36 if _, ok := ev.(pref.ProtoEnum); ok {
37 panic(fmt.Sprintf("%v already implements proto.Enum", t))
38 }
39 if ed, ok := ev.(legacyEnum); ok {
40 b, idxs := ed.EnumDescriptor()
41 fd := loadFileDesc(b)
42
43 // Derive syntax.
44 switch fd.GetSyntax() {
45 case "proto2", "":
46 e.Syntax = pref.Proto2
47 case "proto3":
48 e.Syntax = pref.Proto3
49 }
50
51 // Derive the full name and correct enum descriptor.
52 var ed *descriptorV1.EnumDescriptorProto
53 e.FullName = pref.FullName(fd.GetPackage())
54 if len(idxs) == 1 {
55 ed = fd.EnumType[idxs[0]]
56 e.FullName = e.FullName.Append(pref.Name(ed.GetName()))
57 } else {
58 md := fd.MessageType[idxs[0]]
59 e.FullName = e.FullName.Append(pref.Name(md.GetName()))
60 for _, i := range idxs[1 : len(idxs)-1] {
61 md = md.NestedType[i]
62 e.FullName = e.FullName.Append(pref.Name(md.GetName()))
63 }
64 ed = md.EnumType[idxs[len(idxs)-1]]
65 e.FullName = e.FullName.Append(pref.Name(ed.GetName()))
66 }
67
68 // Derive the enum values.
69 for _, vd := range ed.GetValue() {
70 e.Values = append(e.Values, ptype.EnumValue{
71 Name: pref.Name(vd.GetName()),
72 Number: pref.EnumNumber(vd.GetNumber()),
73 })
74 }
75 } else {
76 // If the type does not implement legacyEnum, then there is no reliable
77 // way to derive the original protobuf type information.
78 // We are unable to use the global enum registry since it is
79 // unfortunately keyed by the full name, which we do not know.
80 // Furthermore, some generated enums register with a fork of
81 // golang/protobuf so the enum may not even be found in the registry.
82 //
83 // Instead, create a bogus enum descriptor to ensure that
84 // most operations continue to work. For example, textpb and jsonpb
85 // will be unable to parse a message with an enum value by name.
86 e.Syntax = pref.Proto2
87 e.FullName = deriveFullName(t)
88 e.Values = []ptype.EnumValue{{Name: "INVALID", Number: math.MinInt32}}
89 }
90
91 ed, err := ptype.NewEnum(e)
92 if err != nil {
93 panic(err)
94 }
95 enumDescCache.Store(t, ed)
96 return ed
97}