Documentation
¶
Overview ¶
Package protostructure provides a mechanism for encoding and decoding a struct _type_ using protocol buffers. To be clear: this encodes the _type_ and not the _value_.
Most importantly, this lets you do things such as transferring a struct that supports JSON decoding across a protobuf RPC, and then decoding a JSON value directly into it since you have access to things such as struct tags from the remote end.
For a pure JSON use case, it may make sense to instead send the JSON rather than send the struct type. There are other scenarios where sending the type is easier and this library facilitates those use cases.
The primary functions you want to look at are "Encode" and "New".
Index ¶
- Variables
- func New(s *Struct) (result interface{}, err error)
- type Container
- func (*Container) Descriptor() ([]byte, []int)deprecated
- func (x *Container) GetCount() int32
- func (x *Container) GetElem() *Type
- func (x *Container) GetKind() uint32
- func (*Container) ProtoMessage()
- func (x *Container) ProtoReflect() protoreflect.Message
- func (x *Container) Reset()
- func (x *Container) String() string
- type InternalStruct
- type Primitive
- type Struct
- type Struct_Field
- func (*Struct_Field) Descriptor() ([]byte, []int)deprecated
- func (x *Struct_Field) GetName() string
- func (x *Struct_Field) GetPkgPath() string
- func (x *Struct_Field) GetTag() string
- func (x *Struct_Field) GetType() *Type
- func (*Struct_Field) ProtoMessage()
- func (x *Struct_Field) ProtoReflect() protoreflect.Message
- func (x *Struct_Field) Reset()
- func (x *Struct_Field) String() string
- type Type
- func (*Type) Descriptor() ([]byte, []int)deprecated
- func (x *Type) GetContainer() *Container
- func (x *Type) GetInternalStruct() *InternalStruct
- func (x *Type) GetPrimitive() *Primitive
- func (m *Type) GetType() isType_Type
- func (*Type) ProtoMessage()
- func (x *Type) ProtoReflect() protoreflect.Message
- func (x *Type) Reset()
- func (x *Type) String() string
- type Type_Container
- type Type_InternalStruct
- type Type_Primitive
Constants ¶
This section is empty.
Variables ¶
var File_protostructure_proto protoreflect.FileDescriptor
Functions ¶
Types ¶
type Container ¶
type Container struct { // kind must be one of: array, ptr, slice Kind uint32 `protobuf:"varint,1,opt,name=kind,proto3" json:"kind,omitempty"` // elem is the type of the element of this container Elem *Type `protobuf:"bytes,2,opt,name=elem,proto3" json:"elem,omitempty"` // count is the number of elements, only if kind == array Count int32 `protobuf:"varint,4,opt,name=count,proto3" json:"count,omitempty"` // contains filtered or unexported fields }
Container represents any "container" type such as a sliec, array, map, etc.
func (*Container) Descriptor
deprecated
func (*Container) ProtoMessage ¶
func (*Container) ProtoMessage()
func (*Container) ProtoReflect ¶
func (x *Container) ProtoReflect() protoreflect.Message
type InternalStruct ¶
type InternalStruct struct { Name string `protobuf:"bytes,1,opt,name=Name,proto3" json:"Name,omitempty"` // contains filtered or unexported fields }
func (*InternalStruct) Descriptor
deprecated
func (*InternalStruct) Descriptor() ([]byte, []int)
Deprecated: Use InternalStruct.ProtoReflect.Descriptor instead.
func (*InternalStruct) GetName ¶
func (x *InternalStruct) GetName() string
func (*InternalStruct) ProtoMessage ¶
func (*InternalStruct) ProtoMessage()
func (*InternalStruct) ProtoReflect ¶
func (x *InternalStruct) ProtoReflect() protoreflect.Message
func (*InternalStruct) Reset ¶
func (x *InternalStruct) Reset()
func (*InternalStruct) String ¶
func (x *InternalStruct) String() string
type Primitive ¶
type Primitive struct { // kind is the reflect.Kind value for this primitive. This MUST be // a primitive value. For example, reflect.Ptr would be invalid here. Kind uint32 `protobuf:"varint,1,opt,name=kind,proto3" json:"kind,omitempty"` // contains filtered or unexported fields }
Primitive is a primitive type such as int, bool, etc.
func (*Primitive) Descriptor
deprecated
func (*Primitive) ProtoMessage ¶
func (*Primitive) ProtoMessage()
func (*Primitive) ProtoReflect ¶
func (x *Primitive) ProtoReflect() protoreflect.Message
type Struct ¶
type Struct struct { StructName string `protobuf:"bytes,1,opt,name=StructName,proto3" json:"StructName,omitempty"` // fields is the list of fields in the struct Fields []*Struct_Field `protobuf:"bytes,2,rep,name=fields,proto3" json:"fields,omitempty"` // contains filtered or unexported fields }
Struct represents a struct type.
This has the following limitations:
- Circular references are not allowed between any struct types
- Embedded structs are not supported
- Methods are not preserved
func Encode ¶
Encode converts a struct to a *Struct which implements proto.Message and can therefore be sent over the wire. Note that only the _structure_ of the struct is encoded and NOT any fields values.
Encoding has a number of limitations:
- Circular references are not allowed between any struct types
- Embedded structs are not supported
- Methods are not preserved
- Field types cannot be: interfaces, channels, functions
func (*Struct) Descriptor
deprecated
func (*Struct) GetFields ¶
func (x *Struct) GetFields() []*Struct_Field
func (*Struct) GetStructName ¶
func (*Struct) ProtoMessage ¶
func (*Struct) ProtoMessage()
func (*Struct) ProtoReflect ¶
func (x *Struct) ProtoReflect() protoreflect.Message
type Struct_Field ¶
type Struct_Field struct { Name string `protobuf:"bytes,1,opt,name=Name,proto3" json:"Name,omitempty"` PkgPath string `protobuf:"bytes,2,opt,name=PkgPath,proto3" json:"PkgPath,omitempty"` Tag string `protobuf:"bytes,3,opt,name=Tag,proto3" json:"Tag,omitempty"` Type *Type `protobuf:"bytes,4,opt,name=type,proto3" json:"type,omitempty"` // contains filtered or unexported fields }
Field is a field type. See reflect.StructField in the Go stdlib since the fields in this message match that almost exactly.
func (*Struct_Field) Descriptor
deprecated
func (*Struct_Field) Descriptor() ([]byte, []int)
Deprecated: Use Struct_Field.ProtoReflect.Descriptor instead.
func (*Struct_Field) GetName ¶
func (x *Struct_Field) GetName() string
func (*Struct_Field) GetPkgPath ¶
func (x *Struct_Field) GetPkgPath() string
func (*Struct_Field) GetTag ¶
func (x *Struct_Field) GetTag() string
func (*Struct_Field) GetType ¶
func (x *Struct_Field) GetType() *Type
func (*Struct_Field) ProtoMessage ¶
func (*Struct_Field) ProtoMessage()
func (*Struct_Field) ProtoReflect ¶
func (x *Struct_Field) ProtoReflect() protoreflect.Message
func (*Struct_Field) Reset ¶
func (x *Struct_Field) Reset()
func (*Struct_Field) String ¶
func (x *Struct_Field) String() string
type Type ¶
type Type struct { // Types that are assignable to Type: // *Type_Primitive // *Type_Container // *Type_InternalStruct Type isType_Type `protobuf_oneof:"type"` // contains filtered or unexported fields }
Type represents a Go type.
func (*Type) Descriptor
deprecated
func (*Type) GetContainer ¶
func (*Type) GetInternalStruct ¶
func (x *Type) GetInternalStruct() *InternalStruct
func (*Type) GetPrimitive ¶
func (*Type) ProtoMessage ¶
func (*Type) ProtoMessage()
func (*Type) ProtoReflect ¶
func (x *Type) ProtoReflect() protoreflect.Message
type Type_Container ¶
type Type_Container struct {
Container *Container `protobuf:"bytes,2,opt,name=container,proto3,oneof"`
}
type Type_InternalStruct ¶
type Type_InternalStruct struct {
InternalStruct *InternalStruct `protobuf:"bytes,3,opt,name=internal_struct,json=internalStruct,proto3,oneof"`
}
type Type_Primitive ¶
type Type_Primitive struct {
Primitive *Primitive `protobuf:"bytes,1,opt,name=primitive,proto3,oneof"`
}