blob: 1722d014f281474276d270b907701c903e7f3e48 [file] [log] [blame]
Joe Tsai88bc5a72018-11-05 11:42:22 -08001// 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.
7package value
8
9import (
10 "fmt"
11 "reflect"
12
Joe Tsaif18ab532018-11-27 17:25:04 -080013 papi "github.com/golang/protobuf/protoapi"
Joe Tsai88bc5a72018-11-05 11:42:22 -080014 pref "github.com/golang/protobuf/v2/reflect/protoreflect"
15)
16
17// Unwrapper unwraps the value to the underlying value.
Joe Tsai4b7aff62018-11-14 14:05:19 -080018// This is implemented by List and Map.
Joe Tsai88bc5a72018-11-05 11:42:22 -080019type Unwrapper interface {
Joe Tsaiba0ef9a2018-11-29 14:54:05 -080020 ProtoUnwrap() interface{}
Joe Tsai88bc5a72018-11-05 11:42:22 -080021}
22
Joe Tsai88bc5a72018-11-05 11:42:22 -080023var (
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 Tsaif18ab532018-11-27 17:25:04 -080035 messageIfaceV1 = reflect.TypeOf((*papi.Message)(nil)).Elem()
Joe Tsai88bc5a72018-11-05 11:42:22 -080036 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.
49func NewConverter(t reflect.Type, k pref.Kind) Converter {
Joe Tsai08e00302018-11-26 22:32:06 -080050 return NewLegacyConverter(t, k, nil)
Joe Tsai88bc5a72018-11-05 11:42:22 -080051}
52
Joe Tsai08e00302018-11-26 22:32:06 -080053// LegacyWrapper is a set of wrapper methods that wraps legacy v1 Go types
54// to implement the v2 reflection APIs.
Joe Tsaif0c01e42018-11-06 13:05:20 -080055type (
Joe Tsai08e00302018-11-26 22:32:06 -080056 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 Tsaif0c01e42018-11-06 13:05:20 -080079)
80
Joe Tsai88bc5a72018-11-05 11:42:22 -080081// NewLegacyConverter is identical to NewConverter,
82// but supports wrapping legacy v1 messages to implement the v2 message API
Joe Tsai08e00302018-11-26 22:32:06 -080083// using the provided LegacyWrapper.
84func NewLegacyConverter(t reflect.Type, k pref.Kind, w LegacyWrapper) Converter {
Joe Tsai88bc5a72018-11-05 11:42:22 -080085 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 Tsai6f9095c2018-11-10 14:12:21 -0800127 PBValueOf: func(v reflect.Value) pref.Value {
Joe Tsai88bc5a72018-11-05 11:42:22 -0800128 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 Tsai6f9095c2018-11-10 14:12:21 -0800134 GoValueOf: func(v pref.Value) reflect.Value {
Joe Tsai1c40f492018-11-09 17:02:57 -0800135 rv := reflect.ValueOf(et.New(v.Enum()))
Joe Tsai88bc5a72018-11-05 11:42:22 -0800136 if rv.Type() != t {
137 panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), t))
138 }
139 return rv
140 },
Joe Tsai6f9095c2018-11-10 14:12:21 -0800141 EnumType: et,
Joe Tsai88bc5a72018-11-05 11:42:22 -0800142 }
143 }
144
145 // Handle v1 enums, which we identify as simply a named int32 type.
Joe Tsai08e00302018-11-26 22:32:06 -0800146 if w != nil && t.PkgPath() != "" && t.Kind() == reflect.Int32 {
147 et := w.EnumTypeOf(reflect.Zero(t).Interface())
Joe Tsai88bc5a72018-11-05 11:42:22 -0800148 return Converter{
Joe Tsai6f9095c2018-11-10 14:12:21 -0800149 PBValueOf: func(v reflect.Value) pref.Value {
Joe Tsai88bc5a72018-11-05 11:42:22 -0800150 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 Tsai6f9095c2018-11-10 14:12:21 -0800155 GoValueOf: func(v pref.Value) reflect.Value {
Joe Tsai88bc5a72018-11-05 11:42:22 -0800156 return reflect.ValueOf(v.Enum()).Convert(t)
157 },
Joe Tsai6f9095c2018-11-10 14:12:21 -0800158 EnumType: et,
159 IsLegacy: true,
Joe Tsai88bc5a72018-11-05 11:42:22 -0800160 }
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 Tsai6f9095c2018-11-10 14:12:21 -0800167 PBValueOf: func(v reflect.Value) pref.Value {
Joe Tsai88bc5a72018-11-05 11:42:22 -0800168 if v.Type() != t {
169 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t))
170 }
Damien Neil9ecf75b2018-12-12 16:09:08 -0800171 return pref.ValueOf(v.Interface().(pref.ProtoMessage).ProtoReflect())
Joe Tsai88bc5a72018-11-05 11:42:22 -0800172 },
Joe Tsai6f9095c2018-11-10 14:12:21 -0800173 GoValueOf: func(v pref.Value) reflect.Value {
Damien Neil887028d2018-12-11 14:16:44 -0800174 rv := reflect.ValueOf(v.Message().Interface())
Joe Tsai88bc5a72018-11-05 11:42:22 -0800175 if rv.Type() != t {
176 panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), t))
177 }
178 return rv
179 },
Joe Tsai6f9095c2018-11-10 14:12:21 -0800180 MessageType: mt,
Joe Tsai88bc5a72018-11-05 11:42:22 -0800181 }
182 }
183
184 // Handle v1 messages, which we need to wrap as a v2 message.
Joe Tsai08e00302018-11-26 22:32:06 -0800185 if w != nil && t.Kind() == reflect.Ptr && t.Implements(messageIfaceV1) {
186 mt := w.MessageTypeOf(reflect.Zero(t).Interface())
Joe Tsai88bc5a72018-11-05 11:42:22 -0800187 return Converter{
Joe Tsai6f9095c2018-11-10 14:12:21 -0800188 PBValueOf: func(v reflect.Value) pref.Value {
Joe Tsai88bc5a72018-11-05 11:42:22 -0800189 if v.Type() != t {
190 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t))
191 }
Joe Tsai08e00302018-11-26 22:32:06 -0800192 return pref.ValueOf(w.MessageOf(v.Interface()))
Joe Tsai88bc5a72018-11-05 11:42:22 -0800193 },
Joe Tsai6f9095c2018-11-10 14:12:21 -0800194 GoValueOf: func(v pref.Value) reflect.Value {
Joe Tsaiba0ef9a2018-11-29 14:54:05 -0800195 rv := reflect.ValueOf(v.Message().(Unwrapper).ProtoUnwrap())
Joe Tsai88bc5a72018-11-05 11:42:22 -0800196 if rv.Type() != t {
197 panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), t))
198 }
199 return rv
200 },
Joe Tsai6f9095c2018-11-10 14:12:21 -0800201 MessageType: mt,
202 IsLegacy: true,
Joe Tsai88bc5a72018-11-05 11:42:22 -0800203 }
204 }
205 }
206 panic(fmt.Sprintf("invalid Go type %v for protobuf kind %v", t, k))
207}
208
209func makeScalarConverter(goType, pbType reflect.Type) Converter {
210 return Converter{
Joe Tsai6f9095c2018-11-10 14:12:21 -0800211 PBValueOf: func(v reflect.Value) pref.Value {
Joe Tsai88bc5a72018-11-05 11:42:22 -0800212 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 Tsai6f9095c2018-11-10 14:12:21 -0800220 GoValueOf: func(v pref.Value) reflect.Value {
Joe Tsai88bc5a72018-11-05 11:42:22 -0800221 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.
235type Converter struct {
Joe Tsai6f9095c2018-11-10 14:12:21 -0800236 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 Tsai88bc5a72018-11-05 11:42:22 -0800241}