makeKnownFieldsFunc generates functions for operations that can be performed on each protobuf message field. It takes in a reflect.Type representing the Go struct and matches message fields with struct fields. This code assumes that the struct is well-formed and panics if there are any discrepancies.
MessageState is a data structure that is nested as the first field in a concrete message. It provides a way to implement the ProtoReflect method in an allocation-free way without needing to have a shadow Go type generated for every message type. This technique only works using unsafe. Example generated code: type M struct { state protoimpl.MessageState Field1 int32 Field2 string Field3 *BarMessage ... } func (m *M) ProtoReflect() protoreflect.Message { mi := &file_fizz_buzz_proto_msgInfos[5] if protoimpl.UnsafeEnabled && m != nil { ms := protoimpl.X.MessageStateOf(Pointer(m)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(m) } The MessageState type holds a *MessageInfo, which must be atomically set to the message info associated with a given message instance. By unsafely converting a *M into a *MessageState, the MessageState object has access to all the information needed to implement protobuf reflection. It has access to the message info as its first field, and a pointer to the MessageState is identical to a pointer to the concrete message value. Requirements: • The type M must implement protoreflect.ProtoMessage. • The address of m must not be nil. • The address of m and the address of m.state must be equal, even though they are different Go types.
messageDataType is a tuple of a pointer to the message data and a pointer to the message type. It is a generalized way of providing a reflective view over a message instance. The disadvantage of this approach is the need to allocate this tuple of 16B.
MessageOf returns a reflective view over a message. The input must be a pointer to a named Go struct. If the provided type has a ProtoReflect method, it must be implemented by calling this method.
The pages are generated with Goldsv0.3.2-preview. (GOOS=darwin GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.