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