blob: 5dae0100c856b5290f4beaa71f3e3adafdcc57bd [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 Tsai1321a0e2019-03-20 09:46:22 -070010 "github.com/golang/protobuf/v2/encoding/textpb"
Joe Tsai990b9f52019-03-13 12:56:39 -070011 ptype "github.com/golang/protobuf/v2/internal/prototype"
Joe Tsai08e00302018-11-26 22:32:06 -080012 pref "github.com/golang/protobuf/v2/reflect/protoreflect"
Joe Tsai08e00302018-11-26 22:32:06 -080013)
14
15// Export is a zero-length named type that exists only to export a set of
16// functions that we do not want to appear in godoc.
17type Export struct{}
18
19// EnumOf returns the protoreflect.Enum interface over e.
20// If e already implements proto.Enum, then it directly calls the
21// ProtoReflect method, otherwise it wraps the v1 enum to implement
22// the v2 reflective interface.
23func (Export) EnumOf(e interface{}) pref.Enum {
Damien Neila8593ba2019-01-08 16:18:07 -080024 if ev, ok := e.(pref.Enum); ok {
25 return ev
Joe Tsai08e00302018-11-26 22:32:06 -080026 }
27 return legacyWrapper.EnumOf(e)
28}
29
30// EnumTypeOf returns the protoreflect.EnumType for e.
31// If e already implements proto.Enum, then it obtains the type by directly
32// calling the ProtoReflect.Type method, otherwise it derives an enum type
33// from the v1 named int32 type.
34func (Export) EnumTypeOf(e interface{}) pref.EnumType {
Damien Neila8593ba2019-01-08 16:18:07 -080035 if ev, ok := e.(pref.Enum); ok {
36 return ev.Type()
Joe Tsai08e00302018-11-26 22:32:06 -080037 }
38 return legacyWrapper.EnumTypeOf(e)
39}
40
Joe Tsai8e506a82019-03-16 00:05:34 -070041// EnumStringOf returns the enum value as a string, either as the name if
42// the number is resolvable, or the number formatted as a string.
43func (Export) EnumStringOf(ed pref.EnumDescriptor, n pref.EnumNumber) string {
44 ev := ed.Values().ByNumber(n)
45 if ev != nil {
46 return string(ev.Name())
47 }
48 return strconv.Itoa(int(n))
49}
50
Joe Tsai08e00302018-11-26 22:32:06 -080051// MessageOf returns the protoreflect.Message interface over m.
52// If m already implements proto.Message, then it directly calls the
53// ProtoReflect method, otherwise it wraps the v1 message to implement
54// the v2 reflective interface.
55func (Export) MessageOf(m interface{}) pref.Message {
56 if mv, ok := m.(pref.ProtoMessage); ok {
57 return mv.ProtoReflect()
58 }
59 return legacyWrapper.MessageOf(m)
60}
61
62// MessageTypeOf returns the protoreflect.MessageType for m.
63// If m already implements proto.Message, then it obtains the type by directly
64// calling the ProtoReflect.Type method, otherwise it derives a message type
65// from the v1 message struct.
66func (Export) MessageTypeOf(m interface{}) pref.MessageType {
67 if mv, ok := m.(pref.ProtoMessage); ok {
68 return mv.ProtoReflect().Type()
69 }
70 return legacyWrapper.MessageTypeOf(m)
71}
72
73// ExtensionTypeOf returns a protoreflect.ExtensionType where the type of the
74// field is t. The type t must be provided if the field is an enum or message.
75// If t already implements proto.Enum or proto.Message, then this returns
76// an extension type by directly calling prototype.GoExtension.
77// Otherwise, it derives an extension type by wrapping the enum or message
78// using EnumOf or MessageOf.
79func (Export) ExtensionTypeOf(d pref.ExtensionDescriptor, t interface{}) pref.ExtensionType {
80 switch t := t.(type) {
81 case nil:
82 return ptype.GoExtension(d, nil, nil)
Damien Neila8593ba2019-01-08 16:18:07 -080083 case pref.Enum:
84 return ptype.GoExtension(d, t.Type(), nil)
Joe Tsai08e00302018-11-26 22:32:06 -080085 case pref.ProtoMessage:
86 return ptype.GoExtension(d, nil, t.ProtoReflect().Type())
87 }
88 return legacyWrapper.ExtensionTypeOf(d, t)
89}
Joe Tsai1321a0e2019-03-20 09:46:22 -070090
91// MessageStringOf returns the message value as a string,
92// which is the message serialized in the protobuf text format.
93func (Export) MessageStringOf(m pref.ProtoMessage) string {
Herbie Ong42577ea2019-03-26 16:26:22 -070094 b, _ := textpb.MarshalOptions{AllowPartial: true}.Marshal(m)
Joe Tsai1321a0e2019-03-20 09:46:22 -070095 return string(b)
96}