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 | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 50 | return NewLegacyConverter(t, k, nil, nil, nil) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 53 | // Legacy enums and messages do not self-report their own protoreflect types. |
| 54 | // Thus, the caller needs to provide functions for retrieving those when |
| 55 | // a v1 enum or message is encountered. |
| 56 | type ( |
| 57 | enumTypeOf = func(reflect.Type) pref.EnumType |
| 58 | messageTypeOf = func(reflect.Type) pref.MessageType |
| 59 | messageValueOf = func(reflect.Value) pref.ProtoMessage |
| 60 | ) |
| 61 | |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 62 | // NewLegacyConverter is identical to NewConverter, |
| 63 | // but supports wrapping legacy v1 messages to implement the v2 message API |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 64 | // using the provided enumTypeOf, messageTypeOf and messageValueOf functions. |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 65 | // The wrapped message must implement Unwrapper. |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 66 | func NewLegacyConverter(t reflect.Type, k pref.Kind, etOf enumTypeOf, mtOf messageTypeOf, mvOf messageValueOf) Converter { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 67 | switch k { |
| 68 | case pref.BoolKind: |
| 69 | if t.Kind() == reflect.Bool { |
| 70 | return makeScalarConverter(t, boolType) |
| 71 | } |
| 72 | case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind: |
| 73 | if t.Kind() == reflect.Int32 { |
| 74 | return makeScalarConverter(t, int32Type) |
| 75 | } |
| 76 | case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind: |
| 77 | if t.Kind() == reflect.Int64 { |
| 78 | return makeScalarConverter(t, int64Type) |
| 79 | } |
| 80 | case pref.Uint32Kind, pref.Fixed32Kind: |
| 81 | if t.Kind() == reflect.Uint32 { |
| 82 | return makeScalarConverter(t, uint32Type) |
| 83 | } |
| 84 | case pref.Uint64Kind, pref.Fixed64Kind: |
| 85 | if t.Kind() == reflect.Uint64 { |
| 86 | return makeScalarConverter(t, uint64Type) |
| 87 | } |
| 88 | case pref.FloatKind: |
| 89 | if t.Kind() == reflect.Float32 { |
| 90 | return makeScalarConverter(t, float32Type) |
| 91 | } |
| 92 | case pref.DoubleKind: |
| 93 | if t.Kind() == reflect.Float64 { |
| 94 | return makeScalarConverter(t, float64Type) |
| 95 | } |
| 96 | case pref.StringKind: |
| 97 | if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) { |
| 98 | return makeScalarConverter(t, stringType) |
| 99 | } |
| 100 | case pref.BytesKind: |
| 101 | if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) { |
| 102 | return makeScalarConverter(t, bytesType) |
| 103 | } |
| 104 | case pref.EnumKind: |
| 105 | // Handle v2 enums, which must satisfy the proto.Enum interface. |
| 106 | if t.Kind() != reflect.Ptr && t.Implements(enumIfaceV2) { |
| 107 | et := reflect.Zero(t).Interface().(pref.ProtoEnum).ProtoReflect().Type() |
| 108 | return Converter{ |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 109 | PBValueOf: func(v reflect.Value) pref.Value { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 110 | if v.Type() != t { |
| 111 | panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t)) |
| 112 | } |
| 113 | e := v.Interface().(pref.ProtoEnum) |
| 114 | return pref.ValueOf(e.ProtoReflect().Number()) |
| 115 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 116 | GoValueOf: func(v pref.Value) reflect.Value { |
Joe Tsai | 1c40f49 | 2018-11-09 17:02:57 -0800 | [diff] [blame] | 117 | rv := reflect.ValueOf(et.New(v.Enum())) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 118 | if rv.Type() != t { |
| 119 | panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), t)) |
| 120 | } |
| 121 | return rv |
| 122 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 123 | EnumType: et, |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | // Handle v1 enums, which we identify as simply a named int32 type. |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 128 | if etOf != nil && t.PkgPath() != "" && t.Kind() == reflect.Int32 { |
| 129 | et := etOf(t) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 130 | return Converter{ |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 131 | PBValueOf: func(v reflect.Value) pref.Value { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 132 | if v.Type() != t { |
| 133 | panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t)) |
| 134 | } |
| 135 | return pref.ValueOf(pref.EnumNumber(v.Int())) |
| 136 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 137 | GoValueOf: func(v pref.Value) reflect.Value { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 138 | return reflect.ValueOf(v.Enum()).Convert(t) |
| 139 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 140 | EnumType: et, |
| 141 | IsLegacy: true, |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | case pref.MessageKind, pref.GroupKind: |
| 145 | // Handle v2 messages, which must satisfy the proto.Message interface. |
| 146 | if t.Kind() == reflect.Ptr && t.Implements(messageIfaceV2) { |
| 147 | mt := reflect.Zero(t).Interface().(pref.ProtoMessage).ProtoReflect().Type() |
| 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(v.Interface()) |
| 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 | rv := reflect.ValueOf(v.Message()) |
| 157 | if rv.Type() != t { |
| 158 | panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), t)) |
| 159 | } |
| 160 | return rv |
| 161 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 162 | MessageType: mt, |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | // Handle v1 messages, which we need to wrap as a v2 message. |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 167 | if mtOf != nil && t.Kind() == reflect.Ptr && t.Implements(messageIfaceV1) { |
| 168 | mt := mtOf(t) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 169 | return Converter{ |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 170 | PBValueOf: func(v reflect.Value) pref.Value { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 171 | if v.Type() != t { |
| 172 | panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t)) |
| 173 | } |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 174 | return pref.ValueOf(mvOf(v).ProtoReflect()) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 175 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 176 | GoValueOf: func(v pref.Value) reflect.Value { |
Joe Tsai | ba0ef9a | 2018-11-29 14:54:05 -0800 | [diff] [blame^] | 177 | rv := reflect.ValueOf(v.Message().(Unwrapper).ProtoUnwrap()) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 178 | if rv.Type() != t { |
| 179 | panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), t)) |
| 180 | } |
| 181 | return rv |
| 182 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 183 | MessageType: mt, |
| 184 | IsLegacy: true, |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | } |
| 188 | panic(fmt.Sprintf("invalid Go type %v for protobuf kind %v", t, k)) |
| 189 | } |
| 190 | |
| 191 | func makeScalarConverter(goType, pbType reflect.Type) Converter { |
| 192 | return Converter{ |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 193 | PBValueOf: func(v reflect.Value) pref.Value { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 194 | if v.Type() != goType { |
| 195 | panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), goType)) |
| 196 | } |
| 197 | if goType.Kind() == reflect.String && pbType.Kind() == reflect.Slice && v.Len() == 0 { |
| 198 | return pref.ValueOf([]byte(nil)) // ensure empty string is []byte(nil) |
| 199 | } |
| 200 | return pref.ValueOf(v.Convert(pbType).Interface()) |
| 201 | }, |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 202 | GoValueOf: func(v pref.Value) reflect.Value { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 203 | rv := reflect.ValueOf(v.Interface()) |
| 204 | if rv.Type() != pbType { |
| 205 | panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), pbType)) |
| 206 | } |
| 207 | if pbType.Kind() == reflect.String && goType.Kind() == reflect.Slice && rv.Len() == 0 { |
| 208 | return reflect.Zero(goType) // ensure empty string is []byte(nil) |
| 209 | } |
| 210 | return rv.Convert(goType) |
| 211 | }, |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // Converter provides functions for converting to/from Go reflect.Value types |
| 216 | // and protobuf protoreflect.Value types. |
| 217 | type Converter struct { |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 218 | PBValueOf func(reflect.Value) pref.Value |
| 219 | GoValueOf func(pref.Value) reflect.Value |
| 220 | EnumType pref.EnumType |
| 221 | MessageType pref.MessageType |
| 222 | IsLegacy bool |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 223 | } |