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 | |
| 13 | pref "github.com/golang/protobuf/v2/reflect/protoreflect" |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame] | 14 | piface "github.com/golang/protobuf/v2/runtime/protoiface" |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 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 | |
Damien Neil | a8593ba | 2019-01-08 16:18:07 -0800 | [diff] [blame] | 34 | enumIfaceV2 = reflect.TypeOf((*pref.Enum)(nil)).Elem() |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame] | 35 | messageIfaceV1 = reflect.TypeOf((*piface.MessageV1)(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 | |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame] | 65 | // TODO: Remove these eventually. |
| 66 | // See the TODOs in internal/impl/legacy_extension.go. |
| 67 | ExtensionDescFromType(pref.ExtensionType) *piface.ExtensionDescV1 |
| 68 | ExtensionTypeFromDesc(*piface.ExtensionDescV1) pref.ExtensionType |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | LegacyEnum = interface { |
| 72 | pref.Enum |
| 73 | ProtoUnwrap() interface{} |
| 74 | } |
| 75 | |
| 76 | LegacyMessage = interface { |
| 77 | pref.Message |
| 78 | ProtoUnwrap() interface{} |
| 79 | } |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 80 | ) |
| 81 | |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 82 | // NewLegacyConverter is identical to NewConverter, |
| 83 | // but supports wrapping legacy v1 messages to implement the v2 message API |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 84 | // using the provided LegacyWrapper. |
| 85 | func NewLegacyConverter(t reflect.Type, k pref.Kind, w LegacyWrapper) Converter { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 86 | switch k { |
| 87 | case pref.BoolKind: |
| 88 | if t.Kind() == reflect.Bool { |
| 89 | return makeScalarConverter(t, boolType) |
| 90 | } |
| 91 | case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind: |
| 92 | if t.Kind() == reflect.Int32 { |
| 93 | return makeScalarConverter(t, int32Type) |
| 94 | } |
| 95 | case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind: |
| 96 | if t.Kind() == reflect.Int64 { |
| 97 | return makeScalarConverter(t, int64Type) |
| 98 | } |
| 99 | case pref.Uint32Kind, pref.Fixed32Kind: |
| 100 | if t.Kind() == reflect.Uint32 { |
| 101 | return makeScalarConverter(t, uint32Type) |
| 102 | } |
| 103 | case pref.Uint64Kind, pref.Fixed64Kind: |
| 104 | if t.Kind() == reflect.Uint64 { |
| 105 | return makeScalarConverter(t, uint64Type) |
| 106 | } |
| 107 | case pref.FloatKind: |
| 108 | if t.Kind() == reflect.Float32 { |
| 109 | return makeScalarConverter(t, float32Type) |
| 110 | } |
| 111 | case pref.DoubleKind: |
| 112 | if t.Kind() == reflect.Float64 { |
| 113 | return makeScalarConverter(t, float64Type) |
| 114 | } |
| 115 | case pref.StringKind: |
| 116 | if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) { |
| 117 | return makeScalarConverter(t, stringType) |
| 118 | } |
| 119 | case pref.BytesKind: |
| 120 | if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) { |
| 121 | return makeScalarConverter(t, bytesType) |
| 122 | } |
| 123 | case pref.EnumKind: |
| 124 | // Handle v2 enums, which must satisfy the proto.Enum interface. |
| 125 | if t.Kind() != reflect.Ptr && t.Implements(enumIfaceV2) { |
Damien Neil | a8593ba | 2019-01-08 16:18:07 -0800 | [diff] [blame] | 126 | et := reflect.Zero(t).Interface().(pref.Enum).Type() |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 127 | return Converter{ |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 128 | PBValueOf: func(v reflect.Value) pref.Value { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 129 | if v.Type() != t { |
| 130 | panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t)) |
| 131 | } |
Damien Neil | a8593ba | 2019-01-08 16:18:07 -0800 | [diff] [blame] | 132 | e := v.Interface().(pref.Enum) |
| 133 | return pref.ValueOf(e.Number()) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 134 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 135 | GoValueOf: func(v pref.Value) reflect.Value { |
Joe Tsai | 1c40f49 | 2018-11-09 17:02:57 -0800 | [diff] [blame] | 136 | rv := reflect.ValueOf(et.New(v.Enum())) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 137 | if rv.Type() != t { |
| 138 | panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), t)) |
| 139 | } |
| 140 | return rv |
| 141 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 142 | EnumType: et, |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | // Handle v1 enums, which we identify as simply a named int32 type. |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 147 | if w != nil && t.PkgPath() != "" && t.Kind() == reflect.Int32 { |
| 148 | et := w.EnumTypeOf(reflect.Zero(t).Interface()) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 149 | return Converter{ |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 150 | PBValueOf: func(v reflect.Value) pref.Value { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 151 | if v.Type() != t { |
| 152 | panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t)) |
| 153 | } |
| 154 | return pref.ValueOf(pref.EnumNumber(v.Int())) |
| 155 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 156 | GoValueOf: func(v pref.Value) reflect.Value { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 157 | return reflect.ValueOf(v.Enum()).Convert(t) |
| 158 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 159 | EnumType: et, |
| 160 | IsLegacy: true, |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | case pref.MessageKind, pref.GroupKind: |
| 164 | // Handle v2 messages, which must satisfy the proto.Message interface. |
| 165 | if t.Kind() == reflect.Ptr && t.Implements(messageIfaceV2) { |
| 166 | mt := reflect.Zero(t).Interface().(pref.ProtoMessage).ProtoReflect().Type() |
| 167 | return Converter{ |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 168 | PBValueOf: func(v reflect.Value) pref.Value { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 169 | if v.Type() != t { |
| 170 | panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t)) |
| 171 | } |
Damien Neil | 9ecf75b | 2018-12-12 16:09:08 -0800 | [diff] [blame] | 172 | return pref.ValueOf(v.Interface().(pref.ProtoMessage).ProtoReflect()) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 173 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 174 | GoValueOf: func(v pref.Value) reflect.Value { |
Damien Neil | 887028d | 2018-12-11 14:16:44 -0800 | [diff] [blame] | 175 | rv := reflect.ValueOf(v.Message().Interface()) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 176 | if rv.Type() != t { |
| 177 | panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), t)) |
| 178 | } |
| 179 | return rv |
| 180 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 181 | MessageType: mt, |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | // Handle v1 messages, which we need to wrap as a v2 message. |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 186 | if w != nil && t.Kind() == reflect.Ptr && t.Implements(messageIfaceV1) { |
| 187 | mt := w.MessageTypeOf(reflect.Zero(t).Interface()) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 188 | return Converter{ |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 189 | PBValueOf: func(v reflect.Value) pref.Value { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 190 | if v.Type() != t { |
| 191 | panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t)) |
| 192 | } |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 193 | return pref.ValueOf(w.MessageOf(v.Interface())) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 194 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 195 | GoValueOf: func(v pref.Value) reflect.Value { |
Joe Tsai | ba0ef9a | 2018-11-29 14:54:05 -0800 | [diff] [blame] | 196 | rv := reflect.ValueOf(v.Message().(Unwrapper).ProtoUnwrap()) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 197 | if rv.Type() != t { |
| 198 | panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), t)) |
| 199 | } |
| 200 | return rv |
| 201 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 202 | MessageType: mt, |
| 203 | IsLegacy: true, |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | } |
| 207 | panic(fmt.Sprintf("invalid Go type %v for protobuf kind %v", t, k)) |
| 208 | } |
| 209 | |
| 210 | func makeScalarConverter(goType, pbType reflect.Type) Converter { |
| 211 | return Converter{ |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 212 | PBValueOf: func(v reflect.Value) pref.Value { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 213 | if v.Type() != goType { |
| 214 | panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), goType)) |
| 215 | } |
| 216 | if goType.Kind() == reflect.String && pbType.Kind() == reflect.Slice && v.Len() == 0 { |
| 217 | return pref.ValueOf([]byte(nil)) // ensure empty string is []byte(nil) |
| 218 | } |
| 219 | return pref.ValueOf(v.Convert(pbType).Interface()) |
| 220 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 221 | GoValueOf: func(v pref.Value) reflect.Value { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 222 | rv := reflect.ValueOf(v.Interface()) |
| 223 | if rv.Type() != pbType { |
| 224 | panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), pbType)) |
| 225 | } |
| 226 | if pbType.Kind() == reflect.String && goType.Kind() == reflect.Slice && rv.Len() == 0 { |
| 227 | return reflect.Zero(goType) // ensure empty string is []byte(nil) |
| 228 | } |
| 229 | return rv.Convert(goType) |
| 230 | }, |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | // Converter provides functions for converting to/from Go reflect.Value types |
| 235 | // and protobuf protoreflect.Value types. |
| 236 | type Converter struct { |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 237 | PBValueOf func(reflect.Value) pref.Value |
| 238 | GoValueOf func(pref.Value) reflect.Value |
| 239 | EnumType pref.EnumType |
| 240 | MessageType pref.MessageType |
| 241 | IsLegacy bool |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 242 | } |