Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -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 impl |
| 6 | |
| 7 | import ( |
| 8 | pref "github.com/golang/protobuf/v2/reflect/protoreflect" |
| 9 | ptype "github.com/golang/protobuf/v2/reflect/prototype" |
| 10 | ) |
| 11 | |
| 12 | // Export is a zero-length named type that exists only to export a set of |
| 13 | // functions that we do not want to appear in godoc. |
| 14 | type Export struct{} |
| 15 | |
| 16 | // EnumOf returns the protoreflect.Enum interface over e. |
| 17 | // If e already implements proto.Enum, then it directly calls the |
| 18 | // ProtoReflect method, otherwise it wraps the v1 enum to implement |
| 19 | // the v2 reflective interface. |
| 20 | func (Export) EnumOf(e interface{}) pref.Enum { |
Damien Neil | a8593ba | 2019-01-08 16:18:07 -0800 | [diff] [blame] | 21 | if ev, ok := e.(pref.Enum); ok { |
| 22 | return ev |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 23 | } |
| 24 | return legacyWrapper.EnumOf(e) |
| 25 | } |
| 26 | |
| 27 | // EnumTypeOf returns the protoreflect.EnumType for e. |
| 28 | // If e already implements proto.Enum, then it obtains the type by directly |
| 29 | // calling the ProtoReflect.Type method, otherwise it derives an enum type |
| 30 | // from the v1 named int32 type. |
| 31 | func (Export) EnumTypeOf(e interface{}) pref.EnumType { |
Damien Neil | a8593ba | 2019-01-08 16:18:07 -0800 | [diff] [blame] | 32 | if ev, ok := e.(pref.Enum); ok { |
| 33 | return ev.Type() |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 34 | } |
| 35 | return legacyWrapper.EnumTypeOf(e) |
| 36 | } |
| 37 | |
| 38 | // MessageOf returns the protoreflect.Message interface over m. |
| 39 | // If m already implements proto.Message, then it directly calls the |
| 40 | // ProtoReflect method, otherwise it wraps the v1 message to implement |
| 41 | // the v2 reflective interface. |
| 42 | func (Export) MessageOf(m interface{}) pref.Message { |
| 43 | if mv, ok := m.(pref.ProtoMessage); ok { |
| 44 | return mv.ProtoReflect() |
| 45 | } |
| 46 | return legacyWrapper.MessageOf(m) |
| 47 | } |
| 48 | |
| 49 | // MessageTypeOf returns the protoreflect.MessageType for m. |
| 50 | // If m already implements proto.Message, then it obtains the type by directly |
| 51 | // calling the ProtoReflect.Type method, otherwise it derives a message type |
| 52 | // from the v1 message struct. |
| 53 | func (Export) MessageTypeOf(m interface{}) pref.MessageType { |
| 54 | if mv, ok := m.(pref.ProtoMessage); ok { |
| 55 | return mv.ProtoReflect().Type() |
| 56 | } |
| 57 | return legacyWrapper.MessageTypeOf(m) |
| 58 | } |
| 59 | |
| 60 | // ExtensionTypeOf returns a protoreflect.ExtensionType where the type of the |
| 61 | // field is t. The type t must be provided if the field is an enum or message. |
| 62 | // If t already implements proto.Enum or proto.Message, then this returns |
| 63 | // an extension type by directly calling prototype.GoExtension. |
| 64 | // Otherwise, it derives an extension type by wrapping the enum or message |
| 65 | // using EnumOf or MessageOf. |
| 66 | func (Export) ExtensionTypeOf(d pref.ExtensionDescriptor, t interface{}) pref.ExtensionType { |
| 67 | switch t := t.(type) { |
| 68 | case nil: |
| 69 | return ptype.GoExtension(d, nil, nil) |
Damien Neil | a8593ba | 2019-01-08 16:18:07 -0800 | [diff] [blame] | 70 | case pref.Enum: |
| 71 | return ptype.GoExtension(d, t.Type(), nil) |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 72 | case pref.ProtoMessage: |
| 73 | return ptype.GoExtension(d, nil, t.ProtoReflect().Type()) |
| 74 | } |
| 75 | return legacyWrapper.ExtensionTypeOf(d, t) |
| 76 | } |