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 ( |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 8 | "reflect" |
Joe Tsai | 8e506a8 | 2019-03-16 00:05:34 -0700 | [diff] [blame] | 9 | "strconv" |
| 10 | |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 11 | "google.golang.org/protobuf/encoding/prototext" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 12 | pref "google.golang.org/protobuf/reflect/protoreflect" |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 13 | piface "google.golang.org/protobuf/runtime/protoiface" |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | // Export is a zero-length named type that exists only to export a set of |
| 17 | // functions that we do not want to appear in godoc. |
| 18 | type Export struct{} |
| 19 | |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 20 | // enum is any enum type generated by protoc-gen-go |
| 21 | // and must be a named int32 type. |
| 22 | type enum = interface{} |
| 23 | |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 24 | // EnumOf returns the protoreflect.Enum interface over e. |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 25 | func (Export) EnumOf(e enum) pref.Enum { |
Damien Neil | a8593ba | 2019-01-08 16:18:07 -0800 | [diff] [blame] | 26 | if ev, ok := e.(pref.Enum); ok { |
| 27 | return ev |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 28 | } |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 29 | return legacyWrapEnum(reflect.ValueOf(e)) |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | // EnumTypeOf returns the protoreflect.EnumType for e. |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 33 | func (Export) EnumTypeOf(e enum) pref.EnumType { |
Damien Neil | a8593ba | 2019-01-08 16:18:07 -0800 | [diff] [blame] | 34 | if ev, ok := e.(pref.Enum); ok { |
Damien Neil | 16163b4 | 2019-08-06 15:43:25 -0700 | [diff] [blame^] | 35 | return ev.Type() |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 36 | } |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 37 | return legacyLoadEnumType(reflect.TypeOf(e)) |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 40 | // EnumDescriptorOf returns the protoreflect.EnumDescriptor for e. |
| 41 | func (Export) EnumDescriptorOf(e enum) pref.EnumDescriptor { |
| 42 | if ev, ok := e.(pref.Enum); ok { |
| 43 | return ev.Descriptor() |
| 44 | } |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 45 | return LegacyLoadEnumDesc(reflect.TypeOf(e)) |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Joe Tsai | 8e506a8 | 2019-03-16 00:05:34 -0700 | [diff] [blame] | 48 | // EnumStringOf returns the enum value as a string, either as the name if |
| 49 | // the number is resolvable, or the number formatted as a string. |
| 50 | func (Export) EnumStringOf(ed pref.EnumDescriptor, n pref.EnumNumber) string { |
| 51 | ev := ed.Values().ByNumber(n) |
| 52 | if ev != nil { |
| 53 | return string(ev.Name()) |
| 54 | } |
| 55 | return strconv.Itoa(int(n)) |
| 56 | } |
| 57 | |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 58 | // message is any message type generated by protoc-gen-go |
| 59 | // and must be a pointer to a named struct type. |
| 60 | type message = interface{} |
| 61 | |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 62 | // MessageOf returns the protoreflect.Message interface over m. |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 63 | func (Export) MessageOf(m message) pref.Message { |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 64 | if mv, ok := m.(pref.ProtoMessage); ok { |
| 65 | return mv.ProtoReflect() |
| 66 | } |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 67 | return legacyWrapMessage(reflect.ValueOf(m)).ProtoReflect() |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | // MessageTypeOf returns the protoreflect.MessageType for m. |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 71 | func (Export) MessageTypeOf(m message) pref.MessageType { |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 72 | if mv, ok := m.(pref.ProtoMessage); ok { |
Damien Neil | 16163b4 | 2019-08-06 15:43:25 -0700 | [diff] [blame^] | 73 | return mv.ProtoReflect().Type() |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 74 | } |
Damien Neil | 16163b4 | 2019-08-06 15:43:25 -0700 | [diff] [blame^] | 75 | return legacyLoadMessageInfo(reflect.TypeOf(m)) |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 78 | // MessageDescriptorOf returns the protoreflect.MessageDescriptor for m. |
| 79 | func (Export) MessageDescriptorOf(m message) pref.MessageDescriptor { |
| 80 | if mv, ok := m.(pref.ProtoMessage); ok { |
| 81 | return mv.ProtoReflect().Descriptor() |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 82 | } |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 83 | return LegacyLoadMessageDesc(reflect.TypeOf(m)) |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 84 | } |
Joe Tsai | 1321a0e | 2019-03-20 09:46:22 -0700 | [diff] [blame] | 85 | |
| 86 | // MessageStringOf returns the message value as a string, |
| 87 | // which is the message serialized in the protobuf text format. |
| 88 | func (Export) MessageStringOf(m pref.ProtoMessage) string { |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 89 | b, _ := prototext.MarshalOptions{AllowPartial: true}.Marshal(m) |
Joe Tsai | 1321a0e | 2019-03-20 09:46:22 -0700 | [diff] [blame] | 90 | return string(b) |
| 91 | } |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 92 | |
| 93 | // ExtensionDescFromType returns the legacy protoiface.ExtensionDescV1 for t. |
| 94 | func (Export) ExtensionDescFromType(t pref.ExtensionType) *piface.ExtensionDescV1 { |
| 95 | return legacyExtensionDescFromType(t) |
| 96 | } |
| 97 | |
| 98 | // ExtensionTypeFromDesc returns the v2 protoreflect.ExtensionType for d. |
| 99 | func (Export) ExtensionTypeFromDesc(d *piface.ExtensionDescV1) pref.ExtensionType { |
| 100 | return legacyExtensionTypeFromDesc(d) |
| 101 | } |