blob: 3bf5488382b73d04878cd0911edcec48ed74c6c7 [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
13 pref "github.com/golang/protobuf/v2/reflect/protoreflect"
Joe Tsai4fddeba2019-03-20 18:29:32 -070014 piface "github.com/golang/protobuf/v2/runtime/protoiface"
Joe Tsai88bc5a72018-11-05 11:42:22 -080015)
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
Damien Neila8593ba2019-01-08 16:18:07 -080034 enumIfaceV2 = reflect.TypeOf((*pref.Enum)(nil)).Elem()
Joe Tsai4fddeba2019-03-20 18:29:32 -070035 messageIfaceV1 = reflect.TypeOf((*piface.MessageV1)(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
Joe Tsai4fddeba2019-03-20 18:29:32 -070065 // 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 Tsai08e00302018-11-26 22:32:06 -080069 }
70
71 LegacyEnum = interface {
72 pref.Enum
73 ProtoUnwrap() interface{}
74 }
75
76 LegacyMessage = interface {
77 pref.Message
78 ProtoUnwrap() interface{}
79 }
Joe Tsaif0c01e42018-11-06 13:05:20 -080080)
81
Joe Tsai88bc5a72018-11-05 11:42:22 -080082// NewLegacyConverter is identical to NewConverter,
83// but supports wrapping legacy v1 messages to implement the v2 message API
Joe Tsai08e00302018-11-26 22:32:06 -080084// using the provided LegacyWrapper.
85func NewLegacyConverter(t reflect.Type, k pref.Kind, w LegacyWrapper) Converter {
Joe Tsai88bc5a72018-11-05 11:42:22 -080086 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 Neila8593ba2019-01-08 16:18:07 -0800126 et := reflect.Zero(t).Interface().(pref.Enum).Type()
Joe Tsai88bc5a72018-11-05 11:42:22 -0800127 return Converter{
Joe Tsai6f9095c2018-11-10 14:12:21 -0800128 PBValueOf: func(v reflect.Value) pref.Value {
Joe Tsai88bc5a72018-11-05 11:42:22 -0800129 if v.Type() != t {
130 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t))
131 }
Damien Neila8593ba2019-01-08 16:18:07 -0800132 e := v.Interface().(pref.Enum)
133 return pref.ValueOf(e.Number())
Joe Tsai88bc5a72018-11-05 11:42:22 -0800134 },
Joe Tsai6f9095c2018-11-10 14:12:21 -0800135 GoValueOf: func(v pref.Value) reflect.Value {
Joe Tsai1c40f492018-11-09 17:02:57 -0800136 rv := reflect.ValueOf(et.New(v.Enum()))
Joe Tsai88bc5a72018-11-05 11:42:22 -0800137 if rv.Type() != t {
138 panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), t))
139 }
140 return rv
141 },
Joe Tsai6f9095c2018-11-10 14:12:21 -0800142 EnumType: et,
Joe Tsai88bc5a72018-11-05 11:42:22 -0800143 }
144 }
145
146 // Handle v1 enums, which we identify as simply a named int32 type.
Joe Tsai08e00302018-11-26 22:32:06 -0800147 if w != nil && t.PkgPath() != "" && t.Kind() == reflect.Int32 {
148 et := w.EnumTypeOf(reflect.Zero(t).Interface())
Joe Tsai88bc5a72018-11-05 11:42:22 -0800149 return Converter{
Joe Tsai6f9095c2018-11-10 14:12:21 -0800150 PBValueOf: func(v reflect.Value) pref.Value {
Joe Tsai88bc5a72018-11-05 11:42:22 -0800151 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 Tsai6f9095c2018-11-10 14:12:21 -0800156 GoValueOf: func(v pref.Value) reflect.Value {
Joe Tsai88bc5a72018-11-05 11:42:22 -0800157 return reflect.ValueOf(v.Enum()).Convert(t)
158 },
Joe Tsai6f9095c2018-11-10 14:12:21 -0800159 EnumType: et,
160 IsLegacy: true,
Joe Tsai88bc5a72018-11-05 11:42:22 -0800161 }
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 Tsai6f9095c2018-11-10 14:12:21 -0800168 PBValueOf: func(v reflect.Value) pref.Value {
Joe Tsai88bc5a72018-11-05 11:42:22 -0800169 if v.Type() != t {
170 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t))
171 }
Damien Neil9ecf75b2018-12-12 16:09:08 -0800172 return pref.ValueOf(v.Interface().(pref.ProtoMessage).ProtoReflect())
Joe Tsai88bc5a72018-11-05 11:42:22 -0800173 },
Joe Tsai6f9095c2018-11-10 14:12:21 -0800174 GoValueOf: func(v pref.Value) reflect.Value {
Damien Neil887028d2018-12-11 14:16:44 -0800175 rv := reflect.ValueOf(v.Message().Interface())
Joe Tsai88bc5a72018-11-05 11:42:22 -0800176 if rv.Type() != t {
177 panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), t))
178 }
179 return rv
180 },
Joe Tsai6f9095c2018-11-10 14:12:21 -0800181 MessageType: mt,
Joe Tsai88bc5a72018-11-05 11:42:22 -0800182 }
183 }
184
185 // Handle v1 messages, which we need to wrap as a v2 message.
Joe Tsai08e00302018-11-26 22:32:06 -0800186 if w != nil && t.Kind() == reflect.Ptr && t.Implements(messageIfaceV1) {
187 mt := w.MessageTypeOf(reflect.Zero(t).Interface())
Joe Tsai88bc5a72018-11-05 11:42:22 -0800188 return Converter{
Joe Tsai6f9095c2018-11-10 14:12:21 -0800189 PBValueOf: func(v reflect.Value) pref.Value {
Joe Tsai88bc5a72018-11-05 11:42:22 -0800190 if v.Type() != t {
191 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t))
192 }
Joe Tsai08e00302018-11-26 22:32:06 -0800193 return pref.ValueOf(w.MessageOf(v.Interface()))
Joe Tsai88bc5a72018-11-05 11:42:22 -0800194 },
Joe Tsai6f9095c2018-11-10 14:12:21 -0800195 GoValueOf: func(v pref.Value) reflect.Value {
Joe Tsaiba0ef9a2018-11-29 14:54:05 -0800196 rv := reflect.ValueOf(v.Message().(Unwrapper).ProtoUnwrap())
Joe Tsai88bc5a72018-11-05 11:42:22 -0800197 if rv.Type() != t {
198 panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), t))
199 }
200 return rv
201 },
Joe Tsai6f9095c2018-11-10 14:12:21 -0800202 MessageType: mt,
203 IsLegacy: true,
Joe Tsai88bc5a72018-11-05 11:42:22 -0800204 }
205 }
206 }
207 panic(fmt.Sprintf("invalid Go type %v for protobuf kind %v", t, k))
208}
209
210func makeScalarConverter(goType, pbType reflect.Type) Converter {
211 return Converter{
Joe Tsai6f9095c2018-11-10 14:12:21 -0800212 PBValueOf: func(v reflect.Value) pref.Value {
Joe Tsai88bc5a72018-11-05 11:42:22 -0800213 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 Tsai6f9095c2018-11-10 14:12:21 -0800221 GoValueOf: func(v pref.Value) reflect.Value {
Joe Tsai88bc5a72018-11-05 11:42:22 -0800222 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.
236type Converter struct {
Joe Tsai6f9095c2018-11-10 14:12:21 -0800237 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 Tsai88bc5a72018-11-05 11:42:22 -0800242}