blob: e4692a7fb64c8dc8c052342aff1c0515a07c9638 [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"
14)
15
16// Unwrapper unwraps the value to the underlying value.
17// This is implemented by Vector and Map.
18type Unwrapper interface {
19 Unwrap() interface{}
20}
21
22// messageV1 is the protoV1.Message interface.
23type messageV1 = interface {
24 Reset()
25 String() string
26 ProtoMessage()
27}
28
29var (
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.
55func NewConverter(t reflect.Type, k pref.Kind) Converter {
Joe Tsai6f9095c2018-11-10 14:12:21 -080056 return NewLegacyConverter(t, k, nil, nil)
Joe Tsai88bc5a72018-11-05 11:42:22 -080057}
58
59// NewLegacyConverter is identical to NewConverter,
60// but supports wrapping legacy v1 messages to implement the v2 message API
Joe Tsai6f9095c2018-11-10 14:12:21 -080061// using the provided wrapEnum and wrapMessage functions.
Joe Tsai88bc5a72018-11-05 11:42:22 -080062// The wrapped message must implement Unwrapper.
Joe Tsai6f9095c2018-11-10 14:12:21 -080063func NewLegacyConverter(t reflect.Type, k pref.Kind, wrapEnum func(reflect.Value) pref.ProtoEnum, wrapMessage func(reflect.Value) pref.ProtoMessage) Converter {
64 if (wrapEnum == nil) != (wrapMessage == nil) {
65 panic("legacy enum and message wrappers must both be populated or nil")
66 }
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 Tsai6f9095c2018-11-10 14:12:21 -0800128 if wrapEnum != nil && t.PkgPath() != "" && t.Kind() == reflect.Int32 {
129 et := wrapEnum(reflect.Zero(t)).ProtoReflect().Type()
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 Tsai6f9095c2018-11-10 14:12:21 -0800167 if wrapMessage != nil && t.Kind() == reflect.Ptr && t.Implements(messageIfaceV1) {
168 mt := wrapMessage(reflect.New(t.Elem())).ProtoReflect().Type()
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 Tsai6f9095c2018-11-10 14:12:21 -0800174 return pref.ValueOf(wrapMessage(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 Tsai88bc5a72018-11-05 11:42:22 -0800177 rv := reflect.ValueOf(v.Message().(Unwrapper).Unwrap())
178 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}