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