blob: 6e54939b658ab48f2f5f33ed778e3b193d5a4810 [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 (
Joe Tsai8e506a82019-03-16 00:05:34 -07008 "strconv"
9
Joe Tsai990b9f52019-03-13 12:56:39 -070010 ptype "github.com/golang/protobuf/v2/internal/prototype"
Joe Tsai08e00302018-11-26 22:32:06 -080011 pref "github.com/golang/protobuf/v2/reflect/protoreflect"
Joe Tsai08e00302018-11-26 22:32:06 -080012)
13
14// Export is a zero-length named type that exists only to export a set of
15// functions that we do not want to appear in godoc.
16type Export struct{}
17
18// EnumOf returns the protoreflect.Enum interface over e.
19// If e already implements proto.Enum, then it directly calls the
20// ProtoReflect method, otherwise it wraps the v1 enum to implement
21// the v2 reflective interface.
22func (Export) EnumOf(e interface{}) pref.Enum {
Damien Neila8593ba2019-01-08 16:18:07 -080023 if ev, ok := e.(pref.Enum); ok {
24 return ev
Joe Tsai08e00302018-11-26 22:32:06 -080025 }
26 return legacyWrapper.EnumOf(e)
27}
28
29// EnumTypeOf returns the protoreflect.EnumType for e.
30// If e already implements proto.Enum, then it obtains the type by directly
31// calling the ProtoReflect.Type method, otherwise it derives an enum type
32// from the v1 named int32 type.
33func (Export) EnumTypeOf(e interface{}) pref.EnumType {
Damien Neila8593ba2019-01-08 16:18:07 -080034 if ev, ok := e.(pref.Enum); ok {
35 return ev.Type()
Joe Tsai08e00302018-11-26 22:32:06 -080036 }
37 return legacyWrapper.EnumTypeOf(e)
38}
39
Joe Tsai8e506a82019-03-16 00:05:34 -070040// EnumStringOf returns the enum value as a string, either as the name if
41// the number is resolvable, or the number formatted as a string.
42func (Export) EnumStringOf(ed pref.EnumDescriptor, n pref.EnumNumber) string {
43 ev := ed.Values().ByNumber(n)
44 if ev != nil {
45 return string(ev.Name())
46 }
47 return strconv.Itoa(int(n))
48}
49
Joe Tsai08e00302018-11-26 22:32:06 -080050// MessageOf returns the protoreflect.Message interface over m.
51// If m already implements proto.Message, then it directly calls the
52// ProtoReflect method, otherwise it wraps the v1 message to implement
53// the v2 reflective interface.
54func (Export) MessageOf(m interface{}) pref.Message {
55 if mv, ok := m.(pref.ProtoMessage); ok {
56 return mv.ProtoReflect()
57 }
58 return legacyWrapper.MessageOf(m)
59}
60
61// MessageTypeOf returns the protoreflect.MessageType for m.
62// If m already implements proto.Message, then it obtains the type by directly
63// calling the ProtoReflect.Type method, otherwise it derives a message type
64// from the v1 message struct.
65func (Export) MessageTypeOf(m interface{}) pref.MessageType {
66 if mv, ok := m.(pref.ProtoMessage); ok {
67 return mv.ProtoReflect().Type()
68 }
69 return legacyWrapper.MessageTypeOf(m)
70}
71
72// ExtensionTypeOf returns a protoreflect.ExtensionType where the type of the
73// field is t. The type t must be provided if the field is an enum or message.
74// If t already implements proto.Enum or proto.Message, then this returns
75// an extension type by directly calling prototype.GoExtension.
76// Otherwise, it derives an extension type by wrapping the enum or message
77// using EnumOf or MessageOf.
78func (Export) ExtensionTypeOf(d pref.ExtensionDescriptor, t interface{}) pref.ExtensionType {
79 switch t := t.(type) {
80 case nil:
81 return ptype.GoExtension(d, nil, nil)
Damien Neila8593ba2019-01-08 16:18:07 -080082 case pref.Enum:
83 return ptype.GoExtension(d, t.Type(), nil)
Joe Tsai08e00302018-11-26 22:32:06 -080084 case pref.ProtoMessage:
85 return ptype.GoExtension(d, nil, t.ProtoReflect().Type())
86 }
87 return legacyWrapper.ExtensionTypeOf(d, t)
88}