blob: cc9d94d3f2ddc7e762ec8e59b767fd3a98286fe7 [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 Tsaif0c01e42018-11-06 13:05:20 -080050 return NewLegacyConverter(t, k, nil, nil, nil)
Joe Tsai88bc5a72018-11-05 11:42:22 -080051}
52
Joe Tsaif0c01e42018-11-06 13:05:20 -080053// 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.
56type (
57 enumTypeOf = func(reflect.Type) pref.EnumType
58 messageTypeOf = func(reflect.Type) pref.MessageType
59 messageValueOf = func(reflect.Value) pref.ProtoMessage
60)
61
Joe Tsai88bc5a72018-11-05 11:42:22 -080062// NewLegacyConverter is identical to NewConverter,
63// but supports wrapping legacy v1 messages to implement the v2 message API
Joe Tsaif0c01e42018-11-06 13:05:20 -080064// using the provided enumTypeOf, messageTypeOf and messageValueOf functions.
Joe Tsai88bc5a72018-11-05 11:42:22 -080065// The wrapped message must implement Unwrapper.
Joe Tsaif0c01e42018-11-06 13:05:20 -080066func NewLegacyConverter(t reflect.Type, k pref.Kind, etOf enumTypeOf, mtOf messageTypeOf, mvOf messageValueOf) Converter {
Joe Tsai88bc5a72018-11-05 11:42:22 -080067 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 Tsai6f9095c2018-11-10 14:12:21 -0800109 PBValueOf: func(v reflect.Value) pref.Value {
Joe Tsai88bc5a72018-11-05 11:42:22 -0800110 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 Tsai6f9095c2018-11-10 14:12:21 -0800116 GoValueOf: func(v pref.Value) reflect.Value {
Joe Tsai1c40f492018-11-09 17:02:57 -0800117 rv := reflect.ValueOf(et.New(v.Enum()))
Joe Tsai88bc5a72018-11-05 11:42:22 -0800118 if rv.Type() != t {
119 panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), t))
120 }
121 return rv
122 },
Joe Tsai6f9095c2018-11-10 14:12:21 -0800123 EnumType: et,
Joe Tsai88bc5a72018-11-05 11:42:22 -0800124 }
125 }
126
127 // Handle v1 enums, which we identify as simply a named int32 type.
Joe Tsaif0c01e42018-11-06 13:05:20 -0800128 if etOf != nil && t.PkgPath() != "" && t.Kind() == reflect.Int32 {
129 et := etOf(t)
Joe Tsai88bc5a72018-11-05 11:42:22 -0800130 return Converter{
Joe Tsai6f9095c2018-11-10 14:12:21 -0800131 PBValueOf: func(v reflect.Value) pref.Value {
Joe Tsai88bc5a72018-11-05 11:42:22 -0800132 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 Tsai6f9095c2018-11-10 14:12:21 -0800137 GoValueOf: func(v pref.Value) reflect.Value {
Joe Tsai88bc5a72018-11-05 11:42:22 -0800138 return reflect.ValueOf(v.Enum()).Convert(t)
139 },
Joe Tsai6f9095c2018-11-10 14:12:21 -0800140 EnumType: et,
141 IsLegacy: true,
Joe Tsai88bc5a72018-11-05 11:42:22 -0800142 }
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 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(v.Interface())
154 },
Joe Tsai6f9095c2018-11-10 14:12:21 -0800155 GoValueOf: func(v pref.Value) reflect.Value {
Joe Tsai88bc5a72018-11-05 11:42:22 -0800156 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 Tsai6f9095c2018-11-10 14:12:21 -0800162 MessageType: mt,
Joe Tsai88bc5a72018-11-05 11:42:22 -0800163 }
164 }
165
166 // Handle v1 messages, which we need to wrap as a v2 message.
Joe Tsaif0c01e42018-11-06 13:05:20 -0800167 if mtOf != nil && t.Kind() == reflect.Ptr && t.Implements(messageIfaceV1) {
168 mt := mtOf(t)
Joe Tsai88bc5a72018-11-05 11:42:22 -0800169 return Converter{
Joe Tsai6f9095c2018-11-10 14:12:21 -0800170 PBValueOf: func(v reflect.Value) pref.Value {
Joe Tsai88bc5a72018-11-05 11:42:22 -0800171 if v.Type() != t {
172 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t))
173 }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800174 return pref.ValueOf(mvOf(v).ProtoReflect())
Joe Tsai88bc5a72018-11-05 11:42:22 -0800175 },
Joe Tsai6f9095c2018-11-10 14:12:21 -0800176 GoValueOf: func(v pref.Value) reflect.Value {
Joe Tsaiba0ef9a2018-11-29 14:54:05 -0800177 rv := reflect.ValueOf(v.Message().(Unwrapper).ProtoUnwrap())
Joe Tsai88bc5a72018-11-05 11:42:22 -0800178 if rv.Type() != t {
179 panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), t))
180 }
181 return rv
182 },
Joe Tsai6f9095c2018-11-10 14:12:21 -0800183 MessageType: mt,
184 IsLegacy: true,
Joe Tsai88bc5a72018-11-05 11:42:22 -0800185 }
186 }
187 }
188 panic(fmt.Sprintf("invalid Go type %v for protobuf kind %v", t, k))
189}
190
191func makeScalarConverter(goType, pbType reflect.Type) Converter {
192 return Converter{
Joe Tsai6f9095c2018-11-10 14:12:21 -0800193 PBValueOf: func(v reflect.Value) pref.Value {
Joe Tsai88bc5a72018-11-05 11:42:22 -0800194 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 Tsai6f9095c2018-11-10 14:12:21 -0800202 GoValueOf: func(v pref.Value) reflect.Value {
Joe Tsai88bc5a72018-11-05 11:42:22 -0800203 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.
217type Converter struct {
Joe Tsai6f9095c2018-11-10 14:12:21 -0800218 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 Tsai88bc5a72018-11-05 11:42:22 -0800223}