Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -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 value provides functionality for wrapping Go values to implement |
| 6 | // protoreflect values. |
| 7 | package value |
| 8 | |
| 9 | import ( |
| 10 | "fmt" |
| 11 | "reflect" |
| 12 | |
Joe Tsai | f18ab53 | 2018-11-27 17:25:04 -0800 | [diff] [blame] | 13 | papi "github.com/golang/protobuf/protoapi" |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 14 | pref "github.com/golang/protobuf/v2/reflect/protoreflect" |
| 15 | ) |
| 16 | |
| 17 | // Unwrapper unwraps the value to the underlying value. |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 18 | // This is implemented by List and Map. |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 19 | type Unwrapper interface { |
Joe Tsai | ba0ef9a | 2018-11-29 14:54:05 -0800 | [diff] [blame] | 20 | ProtoUnwrap() interface{} |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 21 | } |
| 22 | |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 23 | var ( |
| 24 | boolType = reflect.TypeOf(bool(false)) |
| 25 | int32Type = reflect.TypeOf(int32(0)) |
| 26 | int64Type = reflect.TypeOf(int64(0)) |
| 27 | uint32Type = reflect.TypeOf(uint32(0)) |
| 28 | uint64Type = reflect.TypeOf(uint64(0)) |
| 29 | float32Type = reflect.TypeOf(float32(0)) |
| 30 | float64Type = reflect.TypeOf(float64(0)) |
| 31 | stringType = reflect.TypeOf(string("")) |
| 32 | bytesType = reflect.TypeOf([]byte(nil)) |
| 33 | |
| 34 | enumIfaceV2 = reflect.TypeOf((*pref.ProtoEnum)(nil)).Elem() |
Joe Tsai | f18ab53 | 2018-11-27 17:25:04 -0800 | [diff] [blame] | 35 | messageIfaceV1 = reflect.TypeOf((*papi.Message)(nil)).Elem() |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 36 | messageIfaceV2 = reflect.TypeOf((*pref.ProtoMessage)(nil)).Elem() |
| 37 | |
| 38 | byteType = reflect.TypeOf(byte(0)) |
| 39 | ) |
| 40 | |
| 41 | // NewConverter matches a Go type with a protobuf kind and returns a Converter |
| 42 | // that converts between the two. NewConverter panics if it unable to provide a |
| 43 | // conversion between the two. The Converter methods also panic when they are |
| 44 | // called on incorrect Go types. |
| 45 | // |
| 46 | // This matcher deliberately supports a wider range of Go types than what |
| 47 | // protoc-gen-go historically generated to be able to automatically wrap some |
| 48 | // v1 messages generated by other forks of protoc-gen-go. |
| 49 | func NewConverter(t reflect.Type, k pref.Kind) Converter { |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame^] | 50 | return NewLegacyConverter(t, k, nil) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame^] | 53 | // LegacyWrapper is a set of wrapper methods that wraps legacy v1 Go types |
| 54 | // to implement the v2 reflection APIs. |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 55 | type ( |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame^] | 56 | LegacyWrapper interface { |
| 57 | EnumOf(interface{}) LegacyEnum |
| 58 | EnumTypeOf(interface{}) pref.EnumType |
| 59 | |
| 60 | MessageOf(interface{}) LegacyMessage |
| 61 | MessageTypeOf(interface{}) pref.MessageType |
| 62 | |
| 63 | ExtensionTypeOf(pref.ExtensionDescriptor, interface{}) pref.ExtensionType |
| 64 | |
| 65 | // TODO: Remove these eventually. See the TODOs in protoapi. |
| 66 | ExtensionDescFromType(pref.ExtensionType) *papi.ExtensionDesc |
| 67 | ExtensionTypeFromDesc(*papi.ExtensionDesc) pref.ExtensionType |
| 68 | } |
| 69 | |
| 70 | LegacyEnum = interface { |
| 71 | pref.Enum |
| 72 | ProtoUnwrap() interface{} |
| 73 | } |
| 74 | |
| 75 | LegacyMessage = interface { |
| 76 | pref.Message |
| 77 | ProtoUnwrap() interface{} |
| 78 | } |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 79 | ) |
| 80 | |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 81 | // NewLegacyConverter is identical to NewConverter, |
| 82 | // but supports wrapping legacy v1 messages to implement the v2 message API |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame^] | 83 | // using the provided LegacyWrapper. |
| 84 | func NewLegacyConverter(t reflect.Type, k pref.Kind, w LegacyWrapper) Converter { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 85 | switch k { |
| 86 | case pref.BoolKind: |
| 87 | if t.Kind() == reflect.Bool { |
| 88 | return makeScalarConverter(t, boolType) |
| 89 | } |
| 90 | case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind: |
| 91 | if t.Kind() == reflect.Int32 { |
| 92 | return makeScalarConverter(t, int32Type) |
| 93 | } |
| 94 | case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind: |
| 95 | if t.Kind() == reflect.Int64 { |
| 96 | return makeScalarConverter(t, int64Type) |
| 97 | } |
| 98 | case pref.Uint32Kind, pref.Fixed32Kind: |
| 99 | if t.Kind() == reflect.Uint32 { |
| 100 | return makeScalarConverter(t, uint32Type) |
| 101 | } |
| 102 | case pref.Uint64Kind, pref.Fixed64Kind: |
| 103 | if t.Kind() == reflect.Uint64 { |
| 104 | return makeScalarConverter(t, uint64Type) |
| 105 | } |
| 106 | case pref.FloatKind: |
| 107 | if t.Kind() == reflect.Float32 { |
| 108 | return makeScalarConverter(t, float32Type) |
| 109 | } |
| 110 | case pref.DoubleKind: |
| 111 | if t.Kind() == reflect.Float64 { |
| 112 | return makeScalarConverter(t, float64Type) |
| 113 | } |
| 114 | case pref.StringKind: |
| 115 | if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) { |
| 116 | return makeScalarConverter(t, stringType) |
| 117 | } |
| 118 | case pref.BytesKind: |
| 119 | if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) { |
| 120 | return makeScalarConverter(t, bytesType) |
| 121 | } |
| 122 | case pref.EnumKind: |
| 123 | // Handle v2 enums, which must satisfy the proto.Enum interface. |
| 124 | if t.Kind() != reflect.Ptr && t.Implements(enumIfaceV2) { |
| 125 | et := reflect.Zero(t).Interface().(pref.ProtoEnum).ProtoReflect().Type() |
| 126 | return Converter{ |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 127 | PBValueOf: func(v reflect.Value) pref.Value { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 128 | if v.Type() != t { |
| 129 | panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t)) |
| 130 | } |
| 131 | e := v.Interface().(pref.ProtoEnum) |
| 132 | return pref.ValueOf(e.ProtoReflect().Number()) |
| 133 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 134 | GoValueOf: func(v pref.Value) reflect.Value { |
Joe Tsai | 1c40f49 | 2018-11-09 17:02:57 -0800 | [diff] [blame] | 135 | rv := reflect.ValueOf(et.New(v.Enum())) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 136 | if rv.Type() != t { |
| 137 | panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), t)) |
| 138 | } |
| 139 | return rv |
| 140 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 141 | EnumType: et, |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | // Handle v1 enums, which we identify as simply a named int32 type. |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame^] | 146 | if w != nil && t.PkgPath() != "" && t.Kind() == reflect.Int32 { |
| 147 | et := w.EnumTypeOf(reflect.Zero(t).Interface()) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 148 | return Converter{ |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 149 | PBValueOf: func(v reflect.Value) pref.Value { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 150 | if v.Type() != t { |
| 151 | panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t)) |
| 152 | } |
| 153 | return pref.ValueOf(pref.EnumNumber(v.Int())) |
| 154 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 155 | GoValueOf: func(v pref.Value) reflect.Value { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 156 | return reflect.ValueOf(v.Enum()).Convert(t) |
| 157 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 158 | EnumType: et, |
| 159 | IsLegacy: true, |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | case pref.MessageKind, pref.GroupKind: |
| 163 | // Handle v2 messages, which must satisfy the proto.Message interface. |
| 164 | if t.Kind() == reflect.Ptr && t.Implements(messageIfaceV2) { |
| 165 | mt := reflect.Zero(t).Interface().(pref.ProtoMessage).ProtoReflect().Type() |
| 166 | return Converter{ |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 167 | PBValueOf: func(v reflect.Value) pref.Value { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 168 | if v.Type() != t { |
| 169 | panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t)) |
| 170 | } |
| 171 | return pref.ValueOf(v.Interface()) |
| 172 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 173 | GoValueOf: func(v pref.Value) reflect.Value { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 174 | rv := reflect.ValueOf(v.Message()) |
| 175 | if rv.Type() != t { |
| 176 | panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), t)) |
| 177 | } |
| 178 | return rv |
| 179 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 180 | MessageType: mt, |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | // Handle v1 messages, which we need to wrap as a v2 message. |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame^] | 185 | if w != nil && t.Kind() == reflect.Ptr && t.Implements(messageIfaceV1) { |
| 186 | mt := w.MessageTypeOf(reflect.Zero(t).Interface()) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 187 | return Converter{ |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 188 | PBValueOf: func(v reflect.Value) pref.Value { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 189 | if v.Type() != t { |
| 190 | panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t)) |
| 191 | } |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame^] | 192 | return pref.ValueOf(w.MessageOf(v.Interface())) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 193 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 194 | GoValueOf: func(v pref.Value) reflect.Value { |
Joe Tsai | ba0ef9a | 2018-11-29 14:54:05 -0800 | [diff] [blame] | 195 | rv := reflect.ValueOf(v.Message().(Unwrapper).ProtoUnwrap()) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 196 | if rv.Type() != t { |
| 197 | panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), t)) |
| 198 | } |
| 199 | return rv |
| 200 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 201 | MessageType: mt, |
| 202 | IsLegacy: true, |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | } |
| 206 | panic(fmt.Sprintf("invalid Go type %v for protobuf kind %v", t, k)) |
| 207 | } |
| 208 | |
| 209 | func makeScalarConverter(goType, pbType reflect.Type) Converter { |
| 210 | return Converter{ |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 211 | PBValueOf: func(v reflect.Value) pref.Value { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 212 | if v.Type() != goType { |
| 213 | panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), goType)) |
| 214 | } |
| 215 | if goType.Kind() == reflect.String && pbType.Kind() == reflect.Slice && v.Len() == 0 { |
| 216 | return pref.ValueOf([]byte(nil)) // ensure empty string is []byte(nil) |
| 217 | } |
| 218 | return pref.ValueOf(v.Convert(pbType).Interface()) |
| 219 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 220 | GoValueOf: func(v pref.Value) reflect.Value { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 221 | rv := reflect.ValueOf(v.Interface()) |
| 222 | if rv.Type() != pbType { |
| 223 | panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), pbType)) |
| 224 | } |
| 225 | if pbType.Kind() == reflect.String && goType.Kind() == reflect.Slice && rv.Len() == 0 { |
| 226 | return reflect.Zero(goType) // ensure empty string is []byte(nil) |
| 227 | } |
| 228 | return rv.Convert(goType) |
| 229 | }, |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | // Converter provides functions for converting to/from Go reflect.Value types |
| 234 | // and protobuf protoreflect.Value types. |
| 235 | type Converter struct { |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 236 | PBValueOf func(reflect.Value) pref.Value |
| 237 | GoValueOf func(pref.Value) reflect.Value |
| 238 | EnumType pref.EnumType |
| 239 | MessageType pref.MessageType |
| 240 | IsLegacy bool |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 241 | } |