Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [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 impl |
| 6 | |
| 7 | import ( |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 8 | "fmt" |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 9 | "reflect" |
| 10 | "strconv" |
| 11 | "strings" |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 12 | "sync" |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 13 | |
Joe Tsai | 01ab296 | 2018-09-21 17:44:00 -0700 | [diff] [blame] | 14 | pref "github.com/golang/protobuf/v2/reflect/protoreflect" |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 15 | ptype "github.com/golang/protobuf/v2/reflect/prototype" |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 16 | ) |
| 17 | |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 18 | // MessageType provides protobuf related functionality for a given Go type |
| 19 | // that represents a message. A given instance of MessageType is tied to |
| 20 | // exactly one Go type, which must be a pointer to a struct type. |
| 21 | type MessageType struct { |
| 22 | // Desc is an optionally provided message descriptor. If nil, the descriptor |
| 23 | // is lazily derived from the Go type information of generated messages |
| 24 | // for the v1 API. |
| 25 | // |
| 26 | // Once set, this field must never be mutated. |
| 27 | Desc pref.MessageDescriptor |
| 28 | |
| 29 | once sync.Once // protects all unexported fields |
| 30 | |
| 31 | goType reflect.Type // pointer to struct |
| 32 | pbType pref.MessageType // only valid if goType does not implement proto.Message |
| 33 | |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 34 | // TODO: Split fields into dense and sparse maps similar to the current |
| 35 | // table-driven implementation in v1? |
| 36 | fields map[pref.FieldNumber]*fieldInfo |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 37 | |
| 38 | unknownFields func(*messageDataType) pref.UnknownFields |
| 39 | extensionFields func(*messageDataType) pref.KnownFields |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 42 | // init lazily initializes the MessageType upon first use and |
| 43 | // also checks that the provided pointer p is of the correct Go type. |
| 44 | // |
| 45 | // It must be called at the start of every exported method. |
| 46 | func (mi *MessageType) init(p interface{}) { |
| 47 | mi.once.Do(func() { |
| 48 | v := reflect.ValueOf(p) |
| 49 | t := v.Type() |
| 50 | if t.Kind() != reflect.Ptr && t.Elem().Kind() != reflect.Struct { |
| 51 | panic(fmt.Sprintf("got %v, want *struct kind", t)) |
| 52 | } |
| 53 | mi.goType = t |
| 54 | |
| 55 | // Derive the message descriptor if unspecified. |
Joe Tsai | 95b0290 | 2018-10-31 18:23:42 -0700 | [diff] [blame] | 56 | if mi.Desc == nil { |
| 57 | mi.Desc = loadMessageDesc(t) |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // Initialize the Go message type wrapper if the Go type does not |
| 61 | // implement the proto.Message interface. |
| 62 | // |
| 63 | // Otherwise, we assume that the Go type manually implements the |
| 64 | // interface and is internally consistent such that: |
| 65 | // goType == reflect.New(goType.Elem()).Interface().(proto.Message).ProtoReflect().Type().GoType() |
| 66 | // |
| 67 | // Generated code ensures that this property holds. |
| 68 | if _, ok := p.(pref.ProtoMessage); !ok { |
Joe Tsai | 1c40f49 | 2018-11-09 17:02:57 -0800 | [diff] [blame^] | 69 | mi.pbType = ptype.GoMessage(mi.Desc, func(pref.MessageType) pref.ProtoMessage { |
| 70 | p := reflect.New(t.Elem()).Interface() |
| 71 | return (*message)(mi.dataTypeOf(p)) |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 72 | }) |
| 73 | } |
| 74 | |
Joe Tsai | 95b0290 | 2018-10-31 18:23:42 -0700 | [diff] [blame] | 75 | mi.makeKnownFieldsFunc(t.Elem()) |
| 76 | mi.makeUnknownFieldsFunc(t.Elem()) |
| 77 | mi.makeExtensionFieldsFunc(t.Elem()) |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 78 | }) |
| 79 | |
| 80 | // TODO: Remove this check? This API is primarily used by generated code, |
| 81 | // and should not violate this assumption. Leave this check in for now to |
| 82 | // provide some sanity checks during development. This can be removed if |
| 83 | // it proves to be detrimental to performance. |
| 84 | if reflect.TypeOf(p) != mi.goType { |
| 85 | panic(fmt.Sprintf("type mismatch: got %T, want %v", p, mi.goType)) |
| 86 | } |
| 87 | } |
| 88 | |
Joe Tsai | 95b0290 | 2018-10-31 18:23:42 -0700 | [diff] [blame] | 89 | // makeKnownFieldsFunc generates per-field functions for all operations |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 90 | // to be performed on each field. It takes in a reflect.Type representing the |
| 91 | // Go struct, and a protoreflect.MessageDescriptor to match with the fields |
| 92 | // in the struct. |
| 93 | // |
| 94 | // This code assumes that the struct is well-formed and panics if there are |
| 95 | // any discrepancies. |
Joe Tsai | 95b0290 | 2018-10-31 18:23:42 -0700 | [diff] [blame] | 96 | func (mi *MessageType) makeKnownFieldsFunc(t reflect.Type) { |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 97 | // Generate a mapping of field numbers and names to Go struct field or type. |
| 98 | fields := map[pref.FieldNumber]reflect.StructField{} |
| 99 | oneofs := map[pref.Name]reflect.StructField{} |
| 100 | oneofFields := map[pref.FieldNumber]reflect.Type{} |
| 101 | special := map[string]reflect.StructField{} |
| 102 | fieldLoop: |
| 103 | for i := 0; i < t.NumField(); i++ { |
| 104 | f := t.Field(i) |
| 105 | for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") { |
| 106 | if len(s) > 0 && strings.Trim(s, "0123456789") == "" { |
| 107 | n, _ := strconv.ParseUint(s, 10, 64) |
| 108 | fields[pref.FieldNumber(n)] = f |
| 109 | continue fieldLoop |
| 110 | } |
| 111 | } |
| 112 | if s := f.Tag.Get("protobuf_oneof"); len(s) > 0 { |
| 113 | oneofs[pref.Name(s)] = f |
| 114 | continue fieldLoop |
| 115 | } |
| 116 | switch f.Name { |
| 117 | case "XXX_weak", "XXX_unrecognized", "XXX_sizecache", "XXX_extensions", "XXX_InternalExtensions": |
| 118 | special[f.Name] = f |
| 119 | continue fieldLoop |
| 120 | } |
| 121 | } |
Joe Tsai | 2c870bb | 2018-10-17 11:46:52 -0700 | [diff] [blame] | 122 | if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofFuncs"); ok { |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 123 | vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3] |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 124 | oneofLoop: |
| 125 | for _, v := range vs.Interface().([]interface{}) { |
| 126 | tf := reflect.TypeOf(v).Elem() |
| 127 | f := tf.Field(0) |
| 128 | for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") { |
| 129 | if len(s) > 0 && strings.Trim(s, "0123456789") == "" { |
| 130 | n, _ := strconv.ParseUint(s, 10, 64) |
| 131 | oneofFields[pref.FieldNumber(n)] = tf |
| 132 | continue oneofLoop |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | mi.fields = map[pref.FieldNumber]*fieldInfo{} |
Joe Tsai | 95b0290 | 2018-10-31 18:23:42 -0700 | [diff] [blame] | 139 | for i := 0; i < mi.Desc.Fields().Len(); i++ { |
| 140 | fd := mi.Desc.Fields().Get(i) |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 141 | fs := fields[fd.Number()] |
| 142 | var fi fieldInfo |
| 143 | switch { |
| 144 | case fd.IsWeak(): |
| 145 | fi = fieldInfoForWeak(fd, special["XXX_weak"]) |
| 146 | case fd.OneofType() != nil: |
| 147 | fi = fieldInfoForOneof(fd, oneofs[fd.OneofType().Name()], oneofFields[fd.Number()]) |
| 148 | case fd.IsMap(): |
| 149 | fi = fieldInfoForMap(fd, fs) |
| 150 | case fd.Cardinality() == pref.Repeated: |
| 151 | fi = fieldInfoForVector(fd, fs) |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 152 | case fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind: |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 153 | fi = fieldInfoForMessage(fd, fs) |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 154 | default: |
| 155 | fi = fieldInfoForScalar(fd, fs) |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 156 | } |
| 157 | mi.fields[fd.Number()] = &fi |
| 158 | } |
| 159 | } |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 160 | |
Joe Tsai | 95b0290 | 2018-10-31 18:23:42 -0700 | [diff] [blame] | 161 | func (mi *MessageType) makeUnknownFieldsFunc(t reflect.Type) { |
| 162 | if f := makeLegacyUnknownFieldsFunc(t); f != nil { |
Joe Tsai | e2afdc2 | 2018-10-25 14:06:56 -0700 | [diff] [blame] | 163 | mi.unknownFields = f |
| 164 | return |
| 165 | } |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 166 | mi.unknownFields = func(*messageDataType) pref.UnknownFields { |
| 167 | return emptyUnknownFields{} |
| 168 | } |
| 169 | } |
| 170 | |
Joe Tsai | 95b0290 | 2018-10-31 18:23:42 -0700 | [diff] [blame] | 171 | func (mi *MessageType) makeExtensionFieldsFunc(t reflect.Type) { |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 172 | // TODO |
| 173 | mi.extensionFields = func(*messageDataType) pref.KnownFields { |
| 174 | return emptyExtensionFields{} |
| 175 | } |
| 176 | } |
| 177 | |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 178 | func (mi *MessageType) MessageOf(p interface{}) pref.Message { |
| 179 | mi.init(p) |
| 180 | if m, ok := p.(pref.ProtoMessage); ok { |
| 181 | // We assume p properly implements protoreflect.Message. |
| 182 | // See the comment in MessageType.init regarding pbType. |
| 183 | return m.ProtoReflect() |
| 184 | } |
| 185 | return (*message)(mi.dataTypeOf(p)) |
| 186 | } |
| 187 | |
| 188 | func (mi *MessageType) KnownFieldsOf(p interface{}) pref.KnownFields { |
| 189 | mi.init(p) |
| 190 | return (*knownFields)(mi.dataTypeOf(p)) |
| 191 | } |
| 192 | |
| 193 | func (mi *MessageType) UnknownFieldsOf(p interface{}) pref.UnknownFields { |
| 194 | mi.init(p) |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 195 | return mi.unknownFields(mi.dataTypeOf(p)) |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | func (mi *MessageType) dataTypeOf(p interface{}) *messageDataType { |
| 199 | return &messageDataType{pointerOfIface(&p), mi} |
| 200 | } |
| 201 | |
| 202 | // messageDataType is a tuple of a pointer to the message data and |
| 203 | // a pointer to the message type. |
| 204 | // |
| 205 | // TODO: Unfortunately, we need to close over a pointer and MessageType, |
| 206 | // which incurs an an allocation. This pair is similar to a Go interface, |
| 207 | // which is essentially a tuple of the same thing. We can make this efficient |
| 208 | // with reflect.NamedOf (see https://golang.org/issues/16522). |
| 209 | // |
| 210 | // With that hypothetical API, we could dynamically create a new named type |
| 211 | // that has the same underlying type as MessageType.goType, and |
| 212 | // dynamically create methods that close over MessageType. |
| 213 | // Since the new type would have the same underlying type, we could directly |
| 214 | // convert between pointers of those types, giving us an efficient way to swap |
| 215 | // out the method set. |
| 216 | // |
| 217 | // Barring the ability to dynamically create named types, the workaround is |
| 218 | // 1. either to accept the cost of an allocation for this wrapper struct or |
| 219 | // 2. generate more types and methods, at the expense of binary size increase. |
| 220 | type messageDataType struct { |
| 221 | p pointer |
| 222 | mi *MessageType |
| 223 | } |
| 224 | |
| 225 | type message messageDataType |
| 226 | |
| 227 | func (m *message) Type() pref.MessageType { |
| 228 | return m.mi.pbType |
| 229 | } |
| 230 | func (m *message) KnownFields() pref.KnownFields { |
| 231 | return (*knownFields)(m) |
| 232 | } |
| 233 | func (m *message) UnknownFields() pref.UnknownFields { |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 234 | return m.mi.unknownFields((*messageDataType)(m)) |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 235 | } |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 236 | func (m *message) Unwrap() interface{} { // TODO: unexport? |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 237 | return m.p.asType(m.mi.goType.Elem()).Interface() |
| 238 | } |
| 239 | func (m *message) Interface() pref.ProtoMessage { |
| 240 | return m |
| 241 | } |
| 242 | func (m *message) ProtoReflect() pref.Message { |
| 243 | return m |
| 244 | } |
| 245 | func (m *message) ProtoMutable() {} |
| 246 | |
| 247 | type knownFields messageDataType |
| 248 | |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 249 | func (fs *knownFields) Len() (cnt int) { |
| 250 | for _, fi := range fs.mi.fields { |
| 251 | if fi.has(fs.p) { |
| 252 | cnt++ |
| 253 | } |
| 254 | } |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 255 | return cnt + fs.extensionFields().Len() |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 256 | } |
| 257 | func (fs *knownFields) Has(n pref.FieldNumber) bool { |
| 258 | if fi := fs.mi.fields[n]; fi != nil { |
| 259 | return fi.has(fs.p) |
| 260 | } |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 261 | return fs.extensionFields().Has(n) |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 262 | } |
| 263 | func (fs *knownFields) Get(n pref.FieldNumber) pref.Value { |
| 264 | if fi := fs.mi.fields[n]; fi != nil { |
| 265 | return fi.get(fs.p) |
| 266 | } |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 267 | return fs.extensionFields().Get(n) |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 268 | } |
| 269 | func (fs *knownFields) Set(n pref.FieldNumber, v pref.Value) { |
| 270 | if fi := fs.mi.fields[n]; fi != nil { |
| 271 | fi.set(fs.p, v) |
| 272 | return |
| 273 | } |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 274 | fs.extensionFields().Set(n, v) |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 275 | } |
| 276 | func (fs *knownFields) Clear(n pref.FieldNumber) { |
| 277 | if fi := fs.mi.fields[n]; fi != nil { |
| 278 | fi.clear(fs.p) |
| 279 | return |
| 280 | } |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 281 | fs.extensionFields().Clear(n) |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 282 | } |
| 283 | func (fs *knownFields) Mutable(n pref.FieldNumber) pref.Mutable { |
| 284 | if fi := fs.mi.fields[n]; fi != nil { |
| 285 | return fi.mutable(fs.p) |
| 286 | } |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 287 | return fs.extensionFields().Mutable(n) |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 288 | } |
| 289 | func (fs *knownFields) Range(f func(pref.FieldNumber, pref.Value) bool) { |
| 290 | for n, fi := range fs.mi.fields { |
| 291 | if fi.has(fs.p) { |
| 292 | if !f(n, fi.get(fs.p)) { |
| 293 | return |
| 294 | } |
| 295 | } |
| 296 | } |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 297 | fs.extensionFields().Range(f) |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 298 | } |
| 299 | func (fs *knownFields) ExtensionTypes() pref.ExtensionFieldTypes { |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 300 | return fs.extensionFields().ExtensionTypes() |
| 301 | } |
| 302 | func (fs *knownFields) extensionFields() pref.KnownFields { |
| 303 | return fs.mi.extensionFields((*messageDataType)(fs)) |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 306 | type emptyUnknownFields struct{} |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 307 | |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 308 | func (emptyUnknownFields) Len() int { return 0 } |
| 309 | func (emptyUnknownFields) Get(pref.FieldNumber) pref.RawFields { return nil } |
Joe Tsai | 34eb7ef | 2018-11-07 16:39:49 -0800 | [diff] [blame] | 310 | func (emptyUnknownFields) Set(pref.FieldNumber, pref.RawFields) { return } // noop |
| 311 | func (emptyUnknownFields) Range(func(pref.FieldNumber, pref.RawFields) bool) { return } |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 312 | func (emptyUnknownFields) IsSupported() bool { return false } |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 313 | |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 314 | type emptyExtensionFields struct{} |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 315 | |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 316 | func (emptyExtensionFields) Len() int { return 0 } |
| 317 | func (emptyExtensionFields) Has(pref.FieldNumber) bool { return false } |
| 318 | func (emptyExtensionFields) Get(pref.FieldNumber) pref.Value { return pref.Value{} } |
Joe Tsai | 34eb7ef | 2018-11-07 16:39:49 -0800 | [diff] [blame] | 319 | func (emptyExtensionFields) Set(pref.FieldNumber, pref.Value) { panic("extensions not supported") } |
| 320 | func (emptyExtensionFields) Clear(pref.FieldNumber) { return } // noop |
| 321 | func (emptyExtensionFields) Mutable(pref.FieldNumber) pref.Mutable { panic("extensions not supported") } |
| 322 | func (emptyExtensionFields) Range(f func(pref.FieldNumber, pref.Value) bool) { return } |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 323 | func (emptyExtensionFields) ExtensionTypes() pref.ExtensionFieldTypes { return emptyExtensionTypes{} } |
| 324 | |
| 325 | type emptyExtensionTypes struct{} |
| 326 | |
| 327 | func (emptyExtensionTypes) Len() int { return 0 } |
| 328 | func (emptyExtensionTypes) Register(pref.ExtensionType) { panic("extensions not supported") } |
Joe Tsai | 34eb7ef | 2018-11-07 16:39:49 -0800 | [diff] [blame] | 329 | func (emptyExtensionTypes) Remove(pref.ExtensionType) { return } // noop |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 330 | func (emptyExtensionTypes) ByNumber(pref.FieldNumber) pref.ExtensionType { return nil } |
| 331 | func (emptyExtensionTypes) ByName(pref.FullName) pref.ExtensionType { return nil } |
Joe Tsai | 34eb7ef | 2018-11-07 16:39:49 -0800 | [diff] [blame] | 332 | func (emptyExtensionTypes) Range(func(pref.ExtensionType) bool) { return } |