blob: 0a153c4e82ac135a071a0e5eed63e21b7fe3e5f3 [file] [log] [blame]
Joe Tsai08e00302018-11-26 22:32:06 -08001// 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 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.
14type 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.
20func (Export) EnumOf(e interface{}) pref.Enum {
Damien Neila8593ba2019-01-08 16:18:07 -080021 if ev, ok := e.(pref.Enum); ok {
22 return ev
Joe Tsai08e00302018-11-26 22:32:06 -080023 }
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.
31func (Export) EnumTypeOf(e interface{}) pref.EnumType {
Damien Neila8593ba2019-01-08 16:18:07 -080032 if ev, ok := e.(pref.Enum); ok {
33 return ev.Type()
Joe Tsai08e00302018-11-26 22:32:06 -080034 }
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.
42func (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.
53func (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.
66func (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 Neila8593ba2019-01-08 16:18:07 -080070 case pref.Enum:
71 return ptype.GoExtension(d, t.Type(), nil)
Joe Tsai08e00302018-11-26 22:32:06 -080072 case pref.ProtoMessage:
73 return ptype.GoExtension(d, nil, t.ProtoReflect().Type())
74 }
75 return legacyWrapper.ExtensionTypeOf(d, t)
76}