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 ¶
- func New(s *Struct) (result interface{}, err error)
- type Container
- func (*Container) Descriptor() ([]byte, []int)
- func (m *Container) GetCount() int32
- func (m *Container) GetElem() *Type
- func (m *Container) GetKey() *Type
- func (m *Container) GetKind() uint32
- func (*Container) ProtoMessage()
- func (m *Container) Reset()
- func (m *Container) String() string
- func (m *Container) XXX_DiscardUnknown()
- func (m *Container) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
- func (dst *Container) XXX_Merge(src proto.Message)
- func (m *Container) XXX_Size() int
- func (m *Container) XXX_Unmarshal(b []byte) error
- type Primitive
- func (*Primitive) Descriptor() ([]byte, []int)
- func (m *Primitive) GetKind() uint32
- func (*Primitive) ProtoMessage()
- func (m *Primitive) Reset()
- func (m *Primitive) String() string
- func (m *Primitive) XXX_DiscardUnknown()
- func (m *Primitive) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
- func (dst *Primitive) XXX_Merge(src proto.Message)
- func (m *Primitive) XXX_Size() int
- func (m *Primitive) XXX_Unmarshal(b []byte) error
- type Struct
- func (*Struct) Descriptor() ([]byte, []int)
- func (m *Struct) GetFields() []*Struct_Field
- func (*Struct) ProtoMessage()
- func (m *Struct) Reset()
- func (m *Struct) String() string
- func (m *Struct) XXX_DiscardUnknown()
- func (m *Struct) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
- func (dst *Struct) XXX_Merge(src proto.Message)
- func (m *Struct) XXX_Size() int
- func (m *Struct) XXX_Unmarshal(b []byte) error
- type Struct_Field
- func (*Struct_Field) Descriptor() ([]byte, []int)
- func (m *Struct_Field) GetName() string
- func (m *Struct_Field) GetPkgPath() string
- func (m *Struct_Field) GetTag() string
- func (m *Struct_Field) GetType() *Type
- func (*Struct_Field) ProtoMessage()
- func (m *Struct_Field) Reset()
- func (m *Struct_Field) String() string
- func (m *Struct_Field) XXX_DiscardUnknown()
- func (m *Struct_Field) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
- func (dst *Struct_Field) XXX_Merge(src proto.Message)
- func (m *Struct_Field) XXX_Size() int
- func (m *Struct_Field) XXX_Unmarshal(b []byte) error
- type Type
- func (*Type) Descriptor() ([]byte, []int)
- func (m *Type) GetContainer() *Container
- func (m *Type) GetPrimitive() *Primitive
- func (m *Type) GetStruct() *Struct
- func (m *Type) GetType() isType_Type
- func (*Type) ProtoMessage()
- func (m *Type) Reset()
- func (m *Type) String() string
- func (m *Type) XXX_DiscardUnknown()
- func (m *Type) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
- func (dst *Type) XXX_Merge(src proto.Message)
- func (*Type) XXX_OneofFuncs() (func(msg proto.Message, b *proto.Buffer) error, ...)
- func (m *Type) XXX_Size() int
- func (m *Type) XXX_Unmarshal(b []byte) error
- type Type_Container
- type Type_Primitive
- type Type_Struct
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Container ¶
type Container struct { // kind must be one of: array, map, 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"` // key is the type of the key, only if kind == map Key *Type `protobuf:"bytes,3,opt,name=key,proto3" json:"key,omitempty"` // count is the number of elements, only if kind == array Count int32 `protobuf:"varint,4,opt,name=count,proto3" json:"count,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` }
Container represents any "container" type such as a sliec, array, map, etc.
func (*Container) Descriptor ¶
func (*Container) ProtoMessage ¶
func (*Container) ProtoMessage()
func (*Container) XXX_DiscardUnknown ¶
func (m *Container) XXX_DiscardUnknown()
func (*Container) XXX_Marshal ¶
func (*Container) XXX_Unmarshal ¶
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"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` }
Primitive is a primitive type such as int, bool, etc.
func (*Primitive) Descriptor ¶
func (*Primitive) ProtoMessage ¶
func (*Primitive) ProtoMessage()
func (*Primitive) XXX_DiscardUnknown ¶
func (m *Primitive) XXX_DiscardUnknown()
func (*Primitive) XXX_Marshal ¶
func (*Primitive) XXX_Unmarshal ¶
type Struct ¶
type Struct struct { // fields is the list of fields in the struct Fields []*Struct_Field `protobuf:"bytes,1,rep,name=fields,proto3" json:"fields,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` }
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 ¶
func (*Struct) GetFields ¶
func (m *Struct) GetFields() []*Struct_Field
func (*Struct) ProtoMessage ¶
func (*Struct) ProtoMessage()
func (*Struct) XXX_DiscardUnknown ¶
func (m *Struct) XXX_DiscardUnknown()
func (*Struct) XXX_Marshal ¶
func (*Struct) XXX_Unmarshal ¶
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"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` }
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 ¶
func (*Struct_Field) Descriptor() ([]byte, []int)
func (*Struct_Field) GetName ¶
func (m *Struct_Field) GetName() string
func (*Struct_Field) GetPkgPath ¶
func (m *Struct_Field) GetPkgPath() string
func (*Struct_Field) GetTag ¶
func (m *Struct_Field) GetTag() string
func (*Struct_Field) GetType ¶
func (m *Struct_Field) GetType() *Type
func (*Struct_Field) ProtoMessage ¶
func (*Struct_Field) ProtoMessage()
func (*Struct_Field) Reset ¶
func (m *Struct_Field) Reset()
func (*Struct_Field) String ¶
func (m *Struct_Field) String() string
func (*Struct_Field) XXX_DiscardUnknown ¶
func (m *Struct_Field) XXX_DiscardUnknown()
func (*Struct_Field) XXX_Marshal ¶
func (m *Struct_Field) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
func (*Struct_Field) XXX_Merge ¶
func (dst *Struct_Field) XXX_Merge(src proto.Message)
func (*Struct_Field) XXX_Size ¶
func (m *Struct_Field) XXX_Size() int
func (*Struct_Field) XXX_Unmarshal ¶
func (m *Struct_Field) XXX_Unmarshal(b []byte) error
type Type ¶
type Type struct { // Types that are valid to be assigned to Type: // *Type_Primitive // *Type_Container // *Type_Struct Type isType_Type `protobuf_oneof:"type"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` }
Type represents a Go type.
func (*Type) Descriptor ¶
func (*Type) GetContainer ¶
func (*Type) GetPrimitive ¶
func (*Type) ProtoMessage ¶
func (*Type) ProtoMessage()
func (*Type) XXX_DiscardUnknown ¶
func (m *Type) XXX_DiscardUnknown()
func (*Type) XXX_OneofFuncs ¶
func (*Type) XXX_OneofFuncs() (func(msg proto.Message, b *proto.Buffer) error, func(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error), func(msg proto.Message) (n int), []interface{})
XXX_OneofFuncs is for the internal use of the proto package.
func (*Type) XXX_Unmarshal ¶
type Type_Container ¶
type Type_Container struct {
Container *Container `protobuf:"bytes,2,opt,name=container,proto3,oneof"`
}
type Type_Primitive ¶
type Type_Primitive struct {
Primitive *Primitive `protobuf:"bytes,1,opt,name=primitive,proto3,oneof"`
}
type Type_Struct ¶
type Type_Struct struct {
Struct *Struct `protobuf:"bytes,3,opt,name=struct,proto3,oneof"`
}