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