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" |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame^] | 10 | "sort" |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 11 | "strconv" |
| 12 | "strings" |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 13 | "sync" |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame^] | 14 | "sync/atomic" |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 15 | |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 16 | pvalue "google.golang.org/protobuf/internal/value" |
| 17 | pref "google.golang.org/protobuf/reflect/protoreflect" |
| 18 | piface "google.golang.org/protobuf/runtime/protoiface" |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 19 | ) |
| 20 | |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 21 | // MessageType provides protobuf related functionality for a given Go type |
| 22 | // that represents a message. A given instance of MessageType is tied to |
| 23 | // exactly one Go type, which must be a pointer to a struct type. |
| 24 | type MessageType struct { |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 25 | // GoType is the underlying message Go type and must be populated. |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 26 | // Once set, this field must never be mutated. |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 27 | GoType reflect.Type // pointer to struct |
| 28 | |
| 29 | // PBType is the underlying message descriptor type and must be populated. |
| 30 | // Once set, this field must never be mutated. |
| 31 | PBType pref.MessageType |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 32 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame^] | 33 | initMu sync.Mutex // protects all unexported fields |
| 34 | initDone uint32 |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 35 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame^] | 36 | // Keep a separate slice of fields for efficient field encoding in tag order |
| 37 | // and because iterating over a slice is substantially faster than a map. |
| 38 | fields map[pref.FieldNumber]*fieldInfo |
| 39 | fieldsOrdered []*fieldInfo |
| 40 | |
Joe Tsai | 4ec39c7 | 2019-04-03 13:40:53 -0700 | [diff] [blame] | 41 | oneofs map[pref.Name]*oneofInfo |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 42 | |
| 43 | unknownFields func(*messageDataType) pref.UnknownFields |
| 44 | extensionFields func(*messageDataType) pref.KnownFields |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame^] | 45 | methods piface.Methods |
| 46 | |
| 47 | extensionOffset offset |
| 48 | sizecacheOffset offset |
| 49 | unknownOffset offset |
| 50 | extensionFieldInfosMu sync.RWMutex |
| 51 | extensionFieldInfos map[*piface.ExtensionDescV1]*extensionFieldInfo |
| 52 | } |
| 53 | |
| 54 | var prefMessageType = reflect.TypeOf((*pref.Message)(nil)).Elem() |
| 55 | |
| 56 | // getMessageType returns the MessageType (if any) for a type. |
| 57 | // |
| 58 | // We find the MessageType by calling the ProtoReflect method on the type's |
| 59 | // zero value and looking at the returned type to see if it is a |
| 60 | // messageReflectWrapper. Note that the MessageType may still be uninitialized |
| 61 | // at this point. |
| 62 | func getMessageType(mt reflect.Type) (mi *MessageType, ok bool) { |
| 63 | method, ok := mt.MethodByName("ProtoReflect") |
| 64 | if !ok { |
| 65 | return nil, false |
| 66 | } |
| 67 | if method.Type.NumIn() != 1 || method.Type.NumOut() != 1 || method.Type.Out(0) != prefMessageType { |
| 68 | return nil, false |
| 69 | } |
| 70 | ret := reflect.Zero(mt).Method(method.Index).Call(nil) |
| 71 | m, ok := ret[0].Elem().Interface().(*messageReflectWrapper) |
| 72 | if !ok { |
| 73 | return nil, ok |
| 74 | } |
| 75 | return m.mi, true |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 78 | func (mi *MessageType) init() { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame^] | 79 | // This function is called in the hot path. Inline the sync.Once |
| 80 | // logic, since allocating a closure for Once.Do is expensive. |
| 81 | // Keep init small to ensure that it can be inlined. |
| 82 | if atomic.LoadUint32(&mi.initDone) == 1 { |
| 83 | return |
| 84 | } |
| 85 | mi.initOnce() |
| 86 | } |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 87 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame^] | 88 | func (mi *MessageType) initOnce() { |
| 89 | mi.initMu.Lock() |
| 90 | defer mi.initMu.Unlock() |
| 91 | if mi.initDone == 1 { |
| 92 | return |
| 93 | } |
| 94 | |
| 95 | t := mi.GoType |
| 96 | if t.Kind() != reflect.Ptr && t.Elem().Kind() != reflect.Struct { |
| 97 | panic(fmt.Sprintf("got %v, want *struct kind", t)) |
| 98 | } |
| 99 | |
| 100 | si := mi.makeStructInfo(t.Elem()) |
| 101 | mi.makeKnownFieldsFunc(si) |
| 102 | mi.makeUnknownFieldsFunc(t.Elem()) |
| 103 | mi.makeExtensionFieldsFunc(t.Elem()) |
| 104 | mi.makeMethods(t.Elem()) |
| 105 | |
| 106 | atomic.StoreUint32(&mi.initDone, 1) |
| 107 | } |
| 108 | |
| 109 | var sizecacheType = reflect.TypeOf(int32(0)) |
| 110 | |
| 111 | func (mi *MessageType) makeMethods(t reflect.Type) { |
| 112 | mi.extensionOffset = invalidOffset |
| 113 | if fx, _ := t.FieldByName("XXX_InternalExtensions"); fx.Type == extType { |
| 114 | mi.extensionOffset = offsetOf(fx) |
| 115 | } else if fx, _ = t.FieldByName("XXX_extensions"); fx.Type == extType { |
| 116 | mi.extensionOffset = offsetOf(fx) |
| 117 | } |
| 118 | mi.sizecacheOffset = invalidOffset |
| 119 | if fx, _ := t.FieldByName("XXX_sizecache"); fx.Type == sizecacheType { |
| 120 | mi.sizecacheOffset = offsetOf(fx) |
| 121 | } |
| 122 | mi.unknownOffset = invalidOffset |
| 123 | if fx, _ := t.FieldByName("XXX_unrecognized"); fx.Type == bytesType { |
| 124 | mi.unknownOffset = offsetOf(fx) |
| 125 | } |
| 126 | mi.methods.Flags = piface.MethodFlagDeterministicMarshal |
| 127 | mi.methods.MarshalAppend = mi.marshalAppend |
| 128 | mi.methods.Size = mi.size |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Damien Neil | 3eaddf0 | 2019-05-09 11:33:55 -0700 | [diff] [blame] | 131 | type structInfo struct { |
| 132 | fieldsByNumber map[pref.FieldNumber]reflect.StructField |
| 133 | oneofsByName map[pref.Name]reflect.StructField |
| 134 | oneofWrappersByType map[reflect.Type]pref.FieldNumber |
| 135 | oneofWrappersByNumber map[pref.FieldNumber]reflect.Type |
| 136 | } |
| 137 | |
| 138 | func (mi *MessageType) makeStructInfo(t reflect.Type) structInfo { |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 139 | // Generate a mapping of field numbers and names to Go struct field or type. |
Damien Neil | 3eaddf0 | 2019-05-09 11:33:55 -0700 | [diff] [blame] | 140 | si := structInfo{ |
| 141 | fieldsByNumber: map[pref.FieldNumber]reflect.StructField{}, |
| 142 | oneofsByName: map[pref.Name]reflect.StructField{}, |
| 143 | oneofWrappersByType: map[reflect.Type]pref.FieldNumber{}, |
| 144 | oneofWrappersByNumber: map[pref.FieldNumber]reflect.Type{}, |
| 145 | } |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 146 | fieldLoop: |
| 147 | for i := 0; i < t.NumField(); i++ { |
| 148 | f := t.Field(i) |
| 149 | for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") { |
| 150 | if len(s) > 0 && strings.Trim(s, "0123456789") == "" { |
| 151 | n, _ := strconv.ParseUint(s, 10, 64) |
Damien Neil | 3eaddf0 | 2019-05-09 11:33:55 -0700 | [diff] [blame] | 152 | si.fieldsByNumber[pref.FieldNumber(n)] = f |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 153 | continue fieldLoop |
| 154 | } |
| 155 | } |
| 156 | if s := f.Tag.Get("protobuf_oneof"); len(s) > 0 { |
Damien Neil | 3eaddf0 | 2019-05-09 11:33:55 -0700 | [diff] [blame] | 157 | si.oneofsByName[pref.Name(s)] = f |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 158 | continue fieldLoop |
| 159 | } |
| 160 | } |
Joe Tsai | d7e97bc | 2018-11-26 12:57:27 -0800 | [diff] [blame] | 161 | var oneofWrappers []interface{} |
Joe Tsai | 2c870bb | 2018-10-17 11:46:52 -0700 | [diff] [blame] | 162 | if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofFuncs"); ok { |
Joe Tsai | d7e97bc | 2018-11-26 12:57:27 -0800 | [diff] [blame] | 163 | oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3].Interface().([]interface{}) |
| 164 | } |
| 165 | if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofWrappers"); ok { |
| 166 | oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0].Interface().([]interface{}) |
| 167 | } |
| 168 | for _, v := range oneofWrappers { |
| 169 | tf := reflect.TypeOf(v).Elem() |
| 170 | f := tf.Field(0) |
| 171 | for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") { |
| 172 | if len(s) > 0 && strings.Trim(s, "0123456789") == "" { |
| 173 | n, _ := strconv.ParseUint(s, 10, 64) |
Damien Neil | 3eaddf0 | 2019-05-09 11:33:55 -0700 | [diff] [blame] | 174 | si.oneofWrappersByType[tf] = pref.FieldNumber(n) |
| 175 | si.oneofWrappersByNumber[pref.FieldNumber(n)] = tf |
Joe Tsai | d7e97bc | 2018-11-26 12:57:27 -0800 | [diff] [blame] | 176 | break |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | } |
Damien Neil | 3eaddf0 | 2019-05-09 11:33:55 -0700 | [diff] [blame] | 180 | return si |
| 181 | } |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 182 | |
Damien Neil | 3eaddf0 | 2019-05-09 11:33:55 -0700 | [diff] [blame] | 183 | // makeKnownFieldsFunc generates functions for operations that can be performed |
| 184 | // on each protobuf message field. It takes in a reflect.Type representing the |
| 185 | // Go struct and matches message fields with struct fields. |
| 186 | // |
| 187 | // This code assumes that the struct is well-formed and panics if there are |
| 188 | // any discrepancies. |
| 189 | func (mi *MessageType) makeKnownFieldsFunc(si structInfo) { |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 190 | mi.fields = map[pref.FieldNumber]*fieldInfo{} |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame^] | 191 | mi.fieldsOrdered = make([]*fieldInfo, 0, mi.PBType.Fields().Len()) |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 192 | for i := 0; i < mi.PBType.Descriptor().Fields().Len(); i++ { |
| 193 | fd := mi.PBType.Descriptor().Fields().Get(i) |
Damien Neil | 3eaddf0 | 2019-05-09 11:33:55 -0700 | [diff] [blame] | 194 | fs := si.fieldsByNumber[fd.Number()] |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 195 | var fi fieldInfo |
| 196 | switch { |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 197 | case fd.ContainingOneof() != nil: |
| 198 | fi = fieldInfoForOneof(fd, si.oneofsByName[fd.ContainingOneof().Name()], si.oneofWrappersByNumber[fd.Number()]) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame^] | 199 | // There is one fieldInfo for each proto message field, but only one struct |
| 200 | // field for all message fields in a oneof. We install the encoder functions |
| 201 | // on the fieldInfo for the first field in the oneof. |
| 202 | // |
| 203 | // A slightly simpler approach would be to have each fieldInfo's encoder |
| 204 | // handle the case where that field is set, but this would require more |
| 205 | // checks against the current oneof type than a single map lookup. |
| 206 | if fd.ContainingOneof().Fields().Get(0).Name() == fd.Name() { |
| 207 | fi.funcs = makeOneofFieldCoder(si.oneofsByName[fd.ContainingOneof().Name()], fd.ContainingOneof(), si.fieldsByNumber, si.oneofWrappersByNumber) |
| 208 | } |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 209 | case fd.IsMap(): |
| 210 | fi = fieldInfoForMap(fd, fs) |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 211 | case fd.IsList(): |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 212 | fi = fieldInfoForList(fd, fs) |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 213 | case fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind: |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 214 | fi = fieldInfoForMessage(fd, fs) |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 215 | default: |
| 216 | fi = fieldInfoForScalar(fd, fs) |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 217 | } |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame^] | 218 | fi.num = fd.Number() |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 219 | mi.fields[fd.Number()] = &fi |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame^] | 220 | mi.fieldsOrdered = append(mi.fieldsOrdered, &fi) |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 221 | } |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame^] | 222 | sort.Slice(mi.fieldsOrdered, func(i, j int) bool { |
| 223 | return mi.fieldsOrdered[i].num < mi.fieldsOrdered[j].num |
| 224 | }) |
Joe Tsai | 4ec39c7 | 2019-04-03 13:40:53 -0700 | [diff] [blame] | 225 | |
| 226 | mi.oneofs = map[pref.Name]*oneofInfo{} |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 227 | for i := 0; i < mi.PBType.Descriptor().Oneofs().Len(); i++ { |
| 228 | od := mi.PBType.Descriptor().Oneofs().Get(i) |
Damien Neil | 3eaddf0 | 2019-05-09 11:33:55 -0700 | [diff] [blame] | 229 | mi.oneofs[od.Name()] = makeOneofInfo(od, si.oneofsByName[od.Name()], si.oneofWrappersByType) |
Joe Tsai | 4ec39c7 | 2019-04-03 13:40:53 -0700 | [diff] [blame] | 230 | } |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 231 | } |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 232 | |
Joe Tsai | 95b0290 | 2018-10-31 18:23:42 -0700 | [diff] [blame] | 233 | func (mi *MessageType) makeUnknownFieldsFunc(t reflect.Type) { |
| 234 | if f := makeLegacyUnknownFieldsFunc(t); f != nil { |
Joe Tsai | e2afdc2 | 2018-10-25 14:06:56 -0700 | [diff] [blame] | 235 | mi.unknownFields = f |
| 236 | return |
| 237 | } |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 238 | mi.unknownFields = func(*messageDataType) pref.UnknownFields { |
| 239 | return emptyUnknownFields{} |
| 240 | } |
| 241 | } |
| 242 | |
Joe Tsai | 95b0290 | 2018-10-31 18:23:42 -0700 | [diff] [blame] | 243 | func (mi *MessageType) makeExtensionFieldsFunc(t reflect.Type) { |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 244 | if f := makeLegacyExtensionFieldsFunc(t); f != nil { |
| 245 | mi.extensionFields = f |
| 246 | return |
| 247 | } |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 248 | mi.extensionFields = func(*messageDataType) pref.KnownFields { |
| 249 | return emptyExtensionFields{} |
| 250 | } |
| 251 | } |
| 252 | |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 253 | func (mi *MessageType) MessageOf(p interface{}) pref.Message { |
Joe Tsai | 22b1ebd | 2019-03-11 13:45:14 -0700 | [diff] [blame] | 254 | return (*messageReflectWrapper)(mi.dataTypeOf(p)) |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 257 | func (mi *MessageType) Methods() *piface.Methods { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame^] | 258 | mi.init() |
| 259 | return &mi.methods |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 262 | func (mi *MessageType) dataTypeOf(p interface{}) *messageDataType { |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 263 | // TODO: Remove this check? This API is primarily used by generated code, |
| 264 | // and should not violate this assumption. Leave this check in for now to |
| 265 | // provide some sanity checks during development. This can be removed if |
| 266 | // it proves to be detrimental to performance. |
| 267 | if reflect.TypeOf(p) != mi.GoType { |
| 268 | panic(fmt.Sprintf("type mismatch: got %T, want %v", p, mi.GoType)) |
| 269 | } |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 270 | return &messageDataType{pointerOfIface(p), mi} |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | // messageDataType is a tuple of a pointer to the message data and |
| 274 | // a pointer to the message type. |
| 275 | // |
| 276 | // TODO: Unfortunately, we need to close over a pointer and MessageType, |
| 277 | // which incurs an an allocation. This pair is similar to a Go interface, |
| 278 | // which is essentially a tuple of the same thing. We can make this efficient |
| 279 | // with reflect.NamedOf (see https://golang.org/issues/16522). |
| 280 | // |
| 281 | // With that hypothetical API, we could dynamically create a new named type |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 282 | // that has the same underlying type as MessageType.GoType, and |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 283 | // dynamically create methods that close over MessageType. |
| 284 | // Since the new type would have the same underlying type, we could directly |
| 285 | // convert between pointers of those types, giving us an efficient way to swap |
| 286 | // out the method set. |
| 287 | // |
| 288 | // Barring the ability to dynamically create named types, the workaround is |
| 289 | // 1. either to accept the cost of an allocation for this wrapper struct or |
| 290 | // 2. generate more types and methods, at the expense of binary size increase. |
| 291 | type messageDataType struct { |
| 292 | p pointer |
| 293 | mi *MessageType |
| 294 | } |
| 295 | |
Joe Tsai | 22b1ebd | 2019-03-11 13:45:14 -0700 | [diff] [blame] | 296 | type messageReflectWrapper messageDataType |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 297 | |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 298 | // TODO: Remove this. |
Joe Tsai | 22b1ebd | 2019-03-11 13:45:14 -0700 | [diff] [blame] | 299 | func (m *messageReflectWrapper) Type() pref.MessageType { |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 300 | return m.mi.PBType |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 301 | } |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 302 | func (m *messageReflectWrapper) Descriptor() pref.MessageDescriptor { |
| 303 | return m.mi.PBType.Descriptor() |
| 304 | } |
Joe Tsai | 22b1ebd | 2019-03-11 13:45:14 -0700 | [diff] [blame] | 305 | func (m *messageReflectWrapper) KnownFields() pref.KnownFields { |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 306 | m.mi.init() |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 307 | return (*knownFields)(m) |
| 308 | } |
Joe Tsai | 22b1ebd | 2019-03-11 13:45:14 -0700 | [diff] [blame] | 309 | func (m *messageReflectWrapper) UnknownFields() pref.UnknownFields { |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 310 | m.mi.init() |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 311 | return m.mi.unknownFields((*messageDataType)(m)) |
| 312 | } |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 313 | func (m *messageReflectWrapper) New() pref.Message { |
| 314 | return m.mi.PBType.New() |
| 315 | } |
Joe Tsai | 22b1ebd | 2019-03-11 13:45:14 -0700 | [diff] [blame] | 316 | func (m *messageReflectWrapper) Interface() pref.ProtoMessage { |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 317 | if m, ok := m.ProtoUnwrap().(pref.ProtoMessage); ok { |
| 318 | return m |
| 319 | } |
Joe Tsai | 22b1ebd | 2019-03-11 13:45:14 -0700 | [diff] [blame] | 320 | return (*messageIfaceWrapper)(m) |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 321 | } |
Joe Tsai | 22b1ebd | 2019-03-11 13:45:14 -0700 | [diff] [blame] | 322 | func (m *messageReflectWrapper) ProtoUnwrap() interface{} { |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 323 | return m.p.AsIfaceOf(m.mi.GoType.Elem()) |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 324 | } |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 325 | |
Joe Tsai | 22b1ebd | 2019-03-11 13:45:14 -0700 | [diff] [blame] | 326 | var _ pvalue.Unwrapper = (*messageReflectWrapper)(nil) |
| 327 | |
| 328 | type messageIfaceWrapper messageDataType |
| 329 | |
| 330 | func (m *messageIfaceWrapper) ProtoReflect() pref.Message { |
| 331 | return (*messageReflectWrapper)(m) |
| 332 | } |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 333 | func (m *messageIfaceWrapper) XXX_Methods() *piface.Methods { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame^] | 334 | // TODO: Consider not recreating this on every call. |
| 335 | m.mi.init() |
| 336 | return &piface.Methods{ |
| 337 | Flags: piface.MethodFlagDeterministicMarshal, |
| 338 | MarshalAppend: m.marshalAppend, |
| 339 | Size: m.size, |
| 340 | } |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 341 | } |
Joe Tsai | 22b1ebd | 2019-03-11 13:45:14 -0700 | [diff] [blame] | 342 | func (m *messageIfaceWrapper) ProtoUnwrap() interface{} { |
| 343 | return m.p.AsIfaceOf(m.mi.GoType.Elem()) |
| 344 | } |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame^] | 345 | func (m *messageIfaceWrapper) marshalAppend(b []byte, _ pref.ProtoMessage, opts piface.MarshalOptions) ([]byte, error) { |
| 346 | return m.mi.marshalAppendPointer(b, m.p, newMarshalOptions(opts)) |
| 347 | } |
| 348 | func (m *messageIfaceWrapper) size(msg pref.ProtoMessage) (size int) { |
| 349 | return m.mi.sizePointer(m.p, 0) |
| 350 | } |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 351 | |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 352 | type knownFields messageDataType |
| 353 | |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 354 | func (fs *knownFields) Len() (cnt int) { |
| 355 | for _, fi := range fs.mi.fields { |
| 356 | if fi.has(fs.p) { |
| 357 | cnt++ |
| 358 | } |
| 359 | } |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 360 | return cnt + fs.extensionFields().Len() |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 361 | } |
| 362 | func (fs *knownFields) Has(n pref.FieldNumber) bool { |
| 363 | if fi := fs.mi.fields[n]; fi != nil { |
| 364 | return fi.has(fs.p) |
| 365 | } |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 366 | return fs.extensionFields().Has(n) |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 367 | } |
| 368 | func (fs *knownFields) Get(n pref.FieldNumber) pref.Value { |
| 369 | if fi := fs.mi.fields[n]; fi != nil { |
| 370 | return fi.get(fs.p) |
| 371 | } |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 372 | return fs.extensionFields().Get(n) |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 373 | } |
| 374 | func (fs *knownFields) Set(n pref.FieldNumber, v pref.Value) { |
| 375 | if fi := fs.mi.fields[n]; fi != nil { |
| 376 | fi.set(fs.p, v) |
| 377 | return |
| 378 | } |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 379 | if fs.mi.PBType.Descriptor().ExtensionRanges().Has(n) { |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 380 | fs.extensionFields().Set(n, v) |
| 381 | return |
| 382 | } |
| 383 | panic(fmt.Sprintf("invalid field: %d", n)) |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 384 | } |
| 385 | func (fs *knownFields) Clear(n pref.FieldNumber) { |
| 386 | if fi := fs.mi.fields[n]; fi != nil { |
| 387 | fi.clear(fs.p) |
| 388 | return |
| 389 | } |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 390 | if fs.mi.PBType.Descriptor().ExtensionRanges().Has(n) { |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 391 | fs.extensionFields().Clear(n) |
| 392 | return |
| 393 | } |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 394 | } |
Joe Tsai | 4ec39c7 | 2019-04-03 13:40:53 -0700 | [diff] [blame] | 395 | func (fs *knownFields) WhichOneof(s pref.Name) pref.FieldNumber { |
| 396 | if oi := fs.mi.oneofs[s]; oi != nil { |
| 397 | return oi.which(fs.p) |
| 398 | } |
| 399 | return 0 |
| 400 | } |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 401 | func (fs *knownFields) Range(f func(pref.FieldNumber, pref.Value) bool) { |
| 402 | for n, fi := range fs.mi.fields { |
| 403 | if fi.has(fs.p) { |
| 404 | if !f(n, fi.get(fs.p)) { |
| 405 | return |
| 406 | } |
| 407 | } |
| 408 | } |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 409 | fs.extensionFields().Range(f) |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 410 | } |
Joe Tsai | 3bc7d6f | 2019-01-09 02:57:13 -0800 | [diff] [blame] | 411 | func (fs *knownFields) NewMessage(n pref.FieldNumber) pref.Message { |
Damien Neil | 97e7f57 | 2018-12-07 14:28:33 -0800 | [diff] [blame] | 412 | if fi := fs.mi.fields[n]; fi != nil { |
Joe Tsai | 3bc7d6f | 2019-01-09 02:57:13 -0800 | [diff] [blame] | 413 | return fi.newMessage() |
Damien Neil | 97e7f57 | 2018-12-07 14:28:33 -0800 | [diff] [blame] | 414 | } |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 415 | if fs.mi.PBType.Descriptor().ExtensionRanges().Has(n) { |
Damien Neil | 97e7f57 | 2018-12-07 14:28:33 -0800 | [diff] [blame] | 416 | return fs.extensionFields().NewMessage(n) |
| 417 | } |
| 418 | panic(fmt.Sprintf("invalid field: %d", n)) |
| 419 | } |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 420 | func (fs *knownFields) ExtensionTypes() pref.ExtensionFieldTypes { |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 421 | return fs.extensionFields().ExtensionTypes() |
| 422 | } |
| 423 | func (fs *knownFields) extensionFields() pref.KnownFields { |
| 424 | return fs.mi.extensionFields((*messageDataType)(fs)) |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 427 | type emptyUnknownFields struct{} |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 428 | |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 429 | func (emptyUnknownFields) Len() int { return 0 } |
| 430 | func (emptyUnknownFields) Get(pref.FieldNumber) pref.RawFields { return nil } |
Joe Tsai | 34eb7ef | 2018-11-07 16:39:49 -0800 | [diff] [blame] | 431 | func (emptyUnknownFields) Set(pref.FieldNumber, pref.RawFields) { return } // noop |
| 432 | func (emptyUnknownFields) Range(func(pref.FieldNumber, pref.RawFields) bool) { return } |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 433 | func (emptyUnknownFields) IsSupported() bool { return false } |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 434 | |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 435 | type emptyExtensionFields struct{} |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 436 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 437 | func (emptyExtensionFields) Len() int { return 0 } |
| 438 | func (emptyExtensionFields) Has(pref.FieldNumber) bool { return false } |
| 439 | func (emptyExtensionFields) Get(pref.FieldNumber) pref.Value { return pref.Value{} } |
| 440 | func (emptyExtensionFields) Set(pref.FieldNumber, pref.Value) { panic("extensions not supported") } |
| 441 | func (emptyExtensionFields) Clear(pref.FieldNumber) { return } // noop |
Joe Tsai | 4ec39c7 | 2019-04-03 13:40:53 -0700 | [diff] [blame] | 442 | func (emptyExtensionFields) WhichOneof(pref.Name) pref.FieldNumber { return 0 } |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 443 | func (emptyExtensionFields) Range(func(pref.FieldNumber, pref.Value) bool) { return } |
Joe Tsai | 3bc7d6f | 2019-01-09 02:57:13 -0800 | [diff] [blame] | 444 | func (emptyExtensionFields) NewMessage(pref.FieldNumber) pref.Message { |
Damien Neil | 97e7f57 | 2018-12-07 14:28:33 -0800 | [diff] [blame] | 445 | panic("extensions not supported") |
| 446 | } |
| 447 | func (emptyExtensionFields) ExtensionTypes() pref.ExtensionFieldTypes { return emptyExtensionTypes{} } |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 448 | |
| 449 | type emptyExtensionTypes struct{} |
| 450 | |
| 451 | func (emptyExtensionTypes) Len() int { return 0 } |
| 452 | func (emptyExtensionTypes) Register(pref.ExtensionType) { panic("extensions not supported") } |
Joe Tsai | 34eb7ef | 2018-11-07 16:39:49 -0800 | [diff] [blame] | 453 | func (emptyExtensionTypes) Remove(pref.ExtensionType) { return } // noop |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 454 | func (emptyExtensionTypes) ByNumber(pref.FieldNumber) pref.ExtensionType { return nil } |
| 455 | func (emptyExtensionTypes) ByName(pref.FullName) pref.ExtensionType { return nil } |
Joe Tsai | 34eb7ef | 2018-11-07 16:39:49 -0800 | [diff] [blame] | 456 | func (emptyExtensionTypes) Range(func(pref.ExtensionType) bool) { return } |