Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [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 ( |
| 8 | "fmt" |
| 9 | "reflect" |
| 10 | "strconv" |
| 11 | |
| 12 | "google.golang.org/protobuf/encoding/prototext" |
| 13 | "google.golang.org/protobuf/internal/errors" |
| 14 | "google.golang.org/protobuf/proto" |
| 15 | pref "google.golang.org/protobuf/reflect/protoreflect" |
| 16 | piface "google.golang.org/protobuf/runtime/protoiface" |
| 17 | ) |
| 18 | |
| 19 | // Export is a zero-length named type that exists only to export a set of |
| 20 | // functions that we do not want to appear in godoc. |
| 21 | type Export struct{} |
| 22 | |
| 23 | // NewError formats a string according to the format specifier and arguments and |
| 24 | // returns an error that has a "proto" prefix. |
| 25 | func (Export) NewError(f string, x ...interface{}) error { |
| 26 | return errors.New(f, x...) |
| 27 | } |
| 28 | |
| 29 | // enum is any enum type generated by protoc-gen-go |
| 30 | // and must be a named int32 type. |
| 31 | type enum = interface{} |
| 32 | |
| 33 | // EnumOf returns the protoreflect.Enum interface over e. |
| 34 | // It returns nil if e is nil. |
| 35 | func (Export) EnumOf(e enum) pref.Enum { |
| 36 | switch e := e.(type) { |
| 37 | case nil: |
| 38 | return nil |
| 39 | case pref.Enum: |
| 40 | return e |
| 41 | default: |
| 42 | return legacyWrapEnum(reflect.ValueOf(e)) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // EnumDescriptorOf returns the protoreflect.EnumDescriptor for e. |
| 47 | // It returns nil if e is nil. |
| 48 | func (Export) EnumDescriptorOf(e enum) pref.EnumDescriptor { |
| 49 | switch e := e.(type) { |
| 50 | case nil: |
| 51 | return nil |
| 52 | case pref.Enum: |
| 53 | return e.Descriptor() |
| 54 | default: |
| 55 | return LegacyLoadEnumDesc(reflect.TypeOf(e)) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // EnumTypeOf returns the protoreflect.EnumType for e. |
| 60 | // It returns nil if e is nil. |
| 61 | func (Export) EnumTypeOf(e enum) pref.EnumType { |
| 62 | switch e := e.(type) { |
| 63 | case nil: |
| 64 | return nil |
| 65 | case pref.Enum: |
| 66 | return e.Type() |
| 67 | default: |
| 68 | return legacyLoadEnumType(reflect.TypeOf(e)) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // EnumStringOf returns the enum value as a string, either as the name if |
| 73 | // the number is resolvable, or the number formatted as a string. |
| 74 | func (Export) EnumStringOf(ed pref.EnumDescriptor, n pref.EnumNumber) string { |
| 75 | ev := ed.Values().ByNumber(n) |
| 76 | if ev != nil { |
| 77 | return string(ev.Name()) |
| 78 | } |
| 79 | return strconv.Itoa(int(n)) |
| 80 | } |
| 81 | |
| 82 | // message is any message type generated by protoc-gen-go |
| 83 | // and must be a pointer to a named struct type. |
| 84 | type message = interface{} |
| 85 | |
| 86 | // legacyMessageWrapper wraps a v2 message as a v1 message. |
| 87 | type legacyMessageWrapper struct{ m pref.ProtoMessage } |
| 88 | |
| 89 | func (m legacyMessageWrapper) Reset() { proto.Reset(m.m) } |
| 90 | func (m legacyMessageWrapper) String() string { return Export{}.MessageStringOf(m.m) } |
| 91 | func (m legacyMessageWrapper) ProtoMessage() {} |
| 92 | |
| 93 | // ProtoMessageV1Of converts either a v1 or v2 message to a v1 message. |
| 94 | // It returns nil if m is nil. |
| 95 | func (Export) ProtoMessageV1Of(m message) piface.MessageV1 { |
| 96 | switch mv := m.(type) { |
| 97 | case nil: |
| 98 | return nil |
| 99 | case piface.MessageV1: |
| 100 | return mv |
| 101 | case unwrapper: |
| 102 | return Export{}.ProtoMessageV1Of(mv.protoUnwrap()) |
| 103 | case pref.ProtoMessage: |
| 104 | return legacyMessageWrapper{mv} |
| 105 | default: |
| 106 | panic(fmt.Sprintf("message %T is neither a v1 or v2 Message", m)) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func (Export) protoMessageV2Of(m message) pref.ProtoMessage { |
| 111 | switch mv := m.(type) { |
| 112 | case nil: |
| 113 | return nil |
| 114 | case pref.ProtoMessage: |
| 115 | return mv |
| 116 | case legacyMessageWrapper: |
| 117 | return mv.m |
| 118 | case piface.MessageV1: |
| 119 | return nil |
| 120 | default: |
| 121 | panic(fmt.Sprintf("message %T is neither a v1 or v2 Message", m)) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // ProtoMessageV2Of converts either a v1 or v2 message to a v2 message. |
| 126 | // It returns nil if m is nil. |
| 127 | func (Export) ProtoMessageV2Of(m message) pref.ProtoMessage { |
| 128 | if m == nil { |
| 129 | return nil |
| 130 | } |
| 131 | if mv := (Export{}).protoMessageV2Of(m); mv != nil { |
| 132 | return mv |
| 133 | } |
| 134 | return legacyWrapMessage(reflect.ValueOf(m)).Interface() |
| 135 | } |
| 136 | |
| 137 | // MessageOf returns the protoreflect.Message interface over m. |
| 138 | // It returns nil if m is nil. |
| 139 | func (Export) MessageOf(m message) pref.Message { |
| 140 | if m == nil { |
| 141 | return nil |
| 142 | } |
| 143 | if mv := (Export{}).protoMessageV2Of(m); mv != nil { |
| 144 | return mv.ProtoReflect() |
| 145 | } |
| 146 | return legacyWrapMessage(reflect.ValueOf(m)) |
| 147 | } |
| 148 | |
| 149 | // MessageDescriptorOf returns the protoreflect.MessageDescriptor for m. |
| 150 | // It returns nil if m is nil. |
| 151 | func (Export) MessageDescriptorOf(m message) pref.MessageDescriptor { |
| 152 | if m == nil { |
| 153 | return nil |
| 154 | } |
| 155 | if mv := (Export{}).protoMessageV2Of(m); mv != nil { |
| 156 | return mv.ProtoReflect().Descriptor() |
| 157 | } |
| 158 | return LegacyLoadMessageDesc(reflect.TypeOf(m)) |
| 159 | } |
| 160 | |
| 161 | // MessageTypeOf returns the protoreflect.MessageType for m. |
| 162 | // It returns nil if m is nil. |
| 163 | func (Export) MessageTypeOf(m message) pref.MessageType { |
| 164 | if m == nil { |
| 165 | return nil |
| 166 | } |
| 167 | if mv := (Export{}).protoMessageV2Of(m); mv != nil { |
| 168 | return mv.ProtoReflect().Type() |
| 169 | } |
| 170 | return legacyLoadMessageType(reflect.TypeOf(m), "") |
| 171 | } |
| 172 | |
| 173 | // MessageStringOf returns the message value as a string, |
| 174 | // which is the message serialized in the protobuf text format. |
| 175 | func (Export) MessageStringOf(m pref.ProtoMessage) string { |
| 176 | return prototext.MarshalOptions{Multiline: false}.Format(m) |
| 177 | } |