Documentation ¶
Index ¶
- Variables
- func UvarintSize(i uint64) int
- type ConfigFormat
- func (cf *ConfigFormat) DefaultValueFor(value *ConfigTypeDefinition) string
- func (cf *ConfigFormat) Execute(name string, data interface{}) ([]byte, error)
- func (cf *ConfigFormat) ExecuteString(name string, data interface{}) (string, error)
- func (cf *ConfigFormat) GenerateCode() ([]byte, error)
- func (cf *ConfigFormat) GenerateFuzz() ([]byte, error)
- func (cf *ConfigFormat) GenerateTest() ([]byte, error)
- func (cf *ConfigFormat) GetType(name string) (Type, error)
- type ConfigInterface
- type ConfigMatch
- type ConfigRequire
- type ConfigType
- func (typ *ConfigType) ConfigFormat() *ConfigFormat
- func (typ *ConfigType) ConstantSize(instance TypeInstance) (bool, error)
- func (ct *ConfigType) HasLen(instance TypeInstance) (bool, error)
- func (typ *ConfigType) InterfaceAdapter(instance TypeInstance) TypeInstance
- func (ct *ConfigType) IsInterface() bool
- func (typ *ConfigType) MinimumSize(instance TypeInstance) (uint64, error)
- func (ct *ConfigType) Name() string
- func (ct *ConfigType) PointerType(instance TypeInstance) bool
- func (ct *ConfigType) Read(instance TypeInstance) (string, error)
- func (typ *ConfigType) SetConfigFormat(cf *ConfigFormat)
- func (ct *ConfigType) Type(instance TypeInstance) (string, error)
- func (ct *ConfigType) Write(instance TypeInstance) (string, error)
- func (ct *ConfigType) WriteSize(instance TypeInstance) (string, error)
- type ConfigTypeDefinition
- func (field *ConfigTypeDefinition) ConfigFormat() *ConfigFormat
- func (field *ConfigTypeDefinition) FieldInstance() TypeInstance
- func (field *ConfigTypeDefinition) GetInterface() *ConfigInterface
- func (field *ConfigTypeDefinition) GetType() (Type, error)
- func (field *ConfigTypeDefinition) Initializer() (string, error)
- func (value *ConfigTypeDefinition) IsBytesType() bool
- func (field *ConfigTypeDefinition) IsInterface() bool
- func (value *ConfigTypeDefinition) IsNativeType() bool
- func (field *ConfigTypeDefinition) ItemInstance() TypeInstance
- func (field *ConfigTypeDefinition) MaybeItemInstance() TypeInstance
- func (field *ConfigTypeDefinition) NeedsInitializer() (bool, error)
- func (field *ConfigTypeDefinition) QualName() string
- func (field *ConfigTypeDefinition) SetConfigFormat(cf *ConfigFormat)
- func (field *ConfigTypeDefinition) SymbolName() string
- type FieldInstance
- func (instance *FieldInstance) ConfigFormat() *ConfigFormat
- func (instance *FieldInstance) GetLength() (uint64, error)
- func (instance *FieldInstance) GetMaxLength() (uint64, error)
- func (instance *FieldInstance) HasLen() (bool, error)
- func (instance *FieldInstance) IsPointer() bool
- func (instance *FieldInstance) QualName() string
- func (instance *FieldInstance) ReadSymbolName() string
- func (instance *FieldInstance) Static() bool
- func (instance *FieldInstance) WriteSymbolName() string
- type InputInstanceAdapter
- func (instance *InputInstanceAdapter) ConfigFormat() *ConfigFormat
- func (instance *InputInstanceAdapter) GetLength() (uint64, error)
- func (instance *InputInstanceAdapter) GetMaxLength() (uint64, error)
- func (instance *InputInstanceAdapter) HasLen() (bool, error)
- func (instance *InputInstanceAdapter) IsPointer() bool
- func (instance *InputInstanceAdapter) QualName() string
- func (instance *InputInstanceAdapter) ReadSymbolName() string
- func (instance *InputInstanceAdapter) Static() bool
- func (instance *InputInstanceAdapter) WriteSymbolName() string
- type Integral
- func (typ *Integral) ConstantSize(instance TypeInstance) (bool, error)
- func (typ *Integral) HasLen(instance TypeInstance) (bool, error)
- func (typ *Integral) MinimumSize(instance TypeInstance) (uint64, error)
- func (typ *Integral) Name() string
- func (typ *Integral) PointerType(instance TypeInstance) bool
- func (typ *Integral) Read(instance TypeInstance) (string, error)
- func (typ *Integral) Type(instance TypeInstance) (string, error)
- func (typ *Integral) Write(instance TypeInstance) (string, error)
- func (typ *Integral) WriteSize(instance TypeInstance) (string, error)
- type ItemInstance
- func (instance *ItemInstance) ConfigFormat() *ConfigFormat
- func (instance *ItemInstance) GetLength() (uint64, error)
- func (instance *ItemInstance) GetMaxLength() (uint64, error)
- func (instance *ItemInstance) HasLen() (bool, error)
- func (instance *ItemInstance) IsPointer() bool
- func (instance *ItemInstance) QualName() string
- func (instance *ItemInstance) ReadSymbolName() string
- func (instance *ItemInstance) Static() bool
- func (instance *ItemInstance) WriteSymbolName() string
- type Marshaler
- type Strings
- func (typ *Strings) ConstantSize(instance TypeInstance) (bool, error)
- func (typ *Strings) HasLen(instance TypeInstance) (bool, error)
- func (typ *Strings) MinimumSize(instance TypeInstance) (uint64, error)
- func (typ *Strings) Name() string
- func (typ *Strings) PointerType(instance TypeInstance) bool
- func (typ *Strings) Read(instance TypeInstance) (string, error)
- func (typ *Strings) Type(instance TypeInstance) (string, error)
- func (typ *Strings) Write(instance TypeInstance) (string, error)
- func (typ *Strings) WriteSize(instance TypeInstance) (string, error)
- type Type
- type TypeInstance
- type UInt8
- func (typ *UInt8) ConstantSize(instance TypeInstance) (bool, error)
- func (typ *UInt8) HasLen(instance TypeInstance) (bool, error)
- func (typ *UInt8) MinimumSize(instance TypeInstance) (uint64, error)
- func (typ *UInt8) Name() string
- func (typ *UInt8) PointerType(instance TypeInstance) bool
- func (typ *UInt8) Read(instance TypeInstance) (string, error)
- func (typ *UInt8) Type(instance TypeInstance) (string, error)
- func (typ *UInt8) Write(instance TypeInstance) (string, error)
- func (typ *UInt8) WriteSize(instance TypeInstance) (string, error)
Constants ¶
This section is empty.
Variables ¶
var ErrBadInterface = errors.New("bad interface type")
ErrBadInterface is for when interface types (enums) are unaccounted for.
var ErrLeftOverBytes = errors.New("leftover bytes during unmarshal")
ErrLeftOverBytes is from when unmarhsals leave dangling content
var ErrLengthMismatch = errors.New("lengths do not match")
ErrLengthMismatch is when two lengths should match but do not.
var ErrMarshalLength = errors.New("marshal length does not match generated content")
ErrMarshalLength is returned when the marshal length does not match the generated content.
var ErrShortRead = errors.New("short read during unmarshal")
ErrShortRead indicates to users when an unmarshaling error has occurred because we ran out of bytes.
var ErrShortWrite = errors.New("short write during marshal")
ErrShortWrite indicates to users when a marshal error has occurred because we ran out of bytes.
var ErrTooLarge = errors.New("value is too large for type")
ErrTooLarge is for when values exceeed their input boundaries
var ErrTooMany = errors.New("too many items during (un)marshal")
ErrTooMany is for when a length constraint was exceeded.
Functions ¶
func UvarintSize ¶
UvarintSize calculates the size for a binary.Uvarint
Types ¶
type ConfigFormat ¶
type ConfigFormat struct { // Package is the name of the package we're generating into. Package string `yaml:"package"` // Types is the listing of types. Each definition consists of a Type name as // map key and properties as values. Types map[string]*ConfigType `yaml:"types"` // Comment adds a comment to the package declaration. Comment string `yaml:"comment"` // contains filtered or unexported fields }
ConfigFormat defines the format of the configuration file used to generate code -- this is the top level.
func Parse ¶
func Parse(content []byte) (*ConfigFormat, error)
Parse parses the content and returns a ConfigFormat; if an error is returned ConfigFormat is invalid.
func ParseFile ¶
func ParseFile(filename string) (*ConfigFormat, error)
ParseFile parses the content in a file specified by filename and returns a ConfigFormat; if an error is returned ConfigFormat is invalid.
func (*ConfigFormat) DefaultValueFor ¶
func (cf *ConfigFormat) DefaultValueFor(value *ConfigTypeDefinition) string
DefaultValueFor creates a default of a type. This differs very slightly from a zero value in that a pointer type is initialised to a default value of that type rather than to nil.
func (*ConfigFormat) Execute ¶
func (cf *ConfigFormat) Execute(name string, data interface{}) ([]byte, error)
Execute a named template with some data and return the string it renders.
func (*ConfigFormat) ExecuteString ¶
func (cf *ConfigFormat) ExecuteString(name string, data interface{}) (string, error)
func (*ConfigFormat) GenerateCode ¶
func (cf *ConfigFormat) GenerateCode() ([]byte, error)
GenerateCode generates the source code.
func (*ConfigFormat) GenerateFuzz ¶
func (cf *ConfigFormat) GenerateFuzz() ([]byte, error)
GenerateFuzz generates the fuzzer code.
func (*ConfigFormat) GenerateTest ¶
func (cf *ConfigFormat) GenerateTest() ([]byte, error)
GenerateTest generates the test code.
type ConfigInterface ¶
type ConfigInterface struct { // Output method. This is a method which is called on the member to determine // the type information for embedding into the marshal. Output string `yaml:"output"` // Input type. This is a type we can unmarshal out of a byte stream to // determine what type in cases to use. Input string `yaml:"input"` // Cases is a list of cases; this is a list of one-key maps that correspond // comparisons in case statement, and result in a type marshal of the value. // Types specified in this manner must still conform to ranger.Marshaler. Cases []map[string]string `yaml:"cases"` }
ConfigInterface defines a polymorphic type to marshal. It requires an input type, an output method for returning the type, and a series of cases for what member corresponds to what type.
type ConfigMatch ¶
type ConfigMatch struct { // LengthOfField indicates the field must match the Member field specified in the value. LengthOfField string `yaml:"length_of_field"` }
ConfigMatch specifies matching rules for validations. Matching means in this context, that two fields must match each other in some way.
type ConfigRequire ¶
type ConfigRequire struct { // MaxLength means the field must have a length no longer than this. MaxLength uint64 `yaml:"max_length"` // Length means the field must be exactly this length. Length uint64 `yaml:"length"` }
ConfigRequire is hard requirements for validations.
type ConfigType ¶
type ConfigType struct { // Fields is a list of fields in serialisation order. This is ordered because byte stability in the // serialisation format is a requirement. Fields []*ConfigTypeDefinition `yaml:"fields"` // Interface is a polymorphic type definition; see ConfigInterface for more. A type defined with an interface // configuration must have no fields defined. Interface *ConfigInterface `yaml:"interface,omitempty"` // Comment is a field to add a comment to the type's declaration. Comment string `yaml:"comment"` TypeName string `yaml:"-"` // populated by parse // contains filtered or unexported fields }
ConfigType is the type definition wrapper; used for specifying member fields as well as any struct-level operations to apply such as validations or filtering.
func (*ConfigType) ConfigFormat ¶
func (typ *ConfigType) ConfigFormat() *ConfigFormat
func (*ConfigType) ConstantSize ¶
func (typ *ConfigType) ConstantSize(instance TypeInstance) (bool, error)
func (*ConfigType) HasLen ¶
func (ct *ConfigType) HasLen(instance TypeInstance) (bool, error)
Does the type support len()
func (*ConfigType) InterfaceAdapter ¶
func (typ *ConfigType) InterfaceAdapter(instance TypeInstance) TypeInstance
InterfaceAdapter creates an interface adapter for TypeInstance
func (*ConfigType) IsInterface ¶
func (ct *ConfigType) IsInterface() bool
func (*ConfigType) MinimumSize ¶
func (typ *ConfigType) MinimumSize(instance TypeInstance) (uint64, error)
MinimumSize returns the minimum serialized size of the type. This is the sum of the minimum size of the serialized fields of the type.
func (*ConfigType) PointerType ¶
func (ct *ConfigType) PointerType(instance TypeInstance) bool
PointerType returns whether the type instance is a value or a pointer to value
func (*ConfigType) Read ¶
func (ct *ConfigType) Read(instance TypeInstance) (string, error)
Read returns code to deserialise an instance of the type
func (*ConfigType) SetConfigFormat ¶
func (typ *ConfigType) SetConfigFormat(cf *ConfigFormat)
SetConfigFormat provides the type with a reference to the root of the structure for rendering templates etc
func (*ConfigType) Type ¶
func (ct *ConfigType) Type(instance TypeInstance) (string, error)
func (*ConfigType) Write ¶
func (ct *ConfigType) Write(instance TypeInstance) (string, error)
Write returns code to serialise an instance of the
func (*ConfigType) WriteSize ¶
func (ct *ConfigType) WriteSize(instance TypeInstance) (string, error)
WriteSize returns code to caculate the size of an instance of the type when serialized
type ConfigTypeDefinition ¶
type ConfigTypeDefinition struct { // FieldName is the name of the field in the containing type. While the yaml file can represent multiple fields with // the same name this is a semantic error and will produce bad code that won't compile. FieldName string `yaml:"name"` // StructureType is the type of data structure; this may be "struct" or // "array". This determines how the field is marshaled by wrapping length // headers over array sections for ease of reading. StructureType string `yaml:"structure_type"` // ValueType is the type that we'll use for the actual marshaling. The // ValueType must conform to ranger.Marshaler or be a built in type (uint, // string, etc) that we support marshaling to/from natively. ValueType string `yaml:"value_type"` // Static ensures that the value is of a fixed size for integer types. Static bool `yaml:"static"` // Match is a list of matching rules for validations. Match ConfigMatch `yaml:"match,omitempty"` // Require is a list of requirements for validations. Require ConfigRequire `yaml:"require,omitempty"` // ItemRequire defines length/maxlength etc for the elements of an array. // This is a compromise vs a full type system overhaul that will probably be // Ranger v2. ItemRequire *ConfigRequire `yaml:"item_require,omitempty"` // Marshal if false, will not marshal in or out. Marshal *bool `yaml:"marshal,omitempty"` // Embedded defines this field as embedded in the struct. Embedded bool `yaml:"embedded"` // Comment is a field to add a comment to the field's declaration. Comment string `yaml:"comment"` TypeName string `yaml:"-"` // populated by parse // contains filtered or unexported fields }
ConfigTypeDefinition is the definition of an individual type member.
func (*ConfigTypeDefinition) ConfigFormat ¶
func (field *ConfigTypeDefinition) ConfigFormat() *ConfigFormat
ConfigFormat returns the config format for the occasional cases where we need global context
func (*ConfigTypeDefinition) FieldInstance ¶
func (field *ConfigTypeDefinition) FieldInstance() TypeInstance
FieldInstance returns a TypeInstance implementation adapted to the field in structure form.
func (*ConfigTypeDefinition) GetInterface ¶
func (field *ConfigTypeDefinition) GetInterface() *ConfigInterface
func (*ConfigTypeDefinition) GetType ¶
func (field *ConfigTypeDefinition) GetType() (Type, error)
GetType returns the type of this field. For array fields this is the type of the items of the array.
func (*ConfigTypeDefinition) Initializer ¶
func (field *ConfigTypeDefinition) Initializer() (string, error)
Initializer returns the value to assign for unmarshaling into
func (*ConfigTypeDefinition) IsBytesType ¶
func (value *ConfigTypeDefinition) IsBytesType() bool
func (*ConfigTypeDefinition) IsInterface ¶
func (field *ConfigTypeDefinition) IsInterface() bool
func (*ConfigTypeDefinition) IsNativeType ¶
func (value *ConfigTypeDefinition) IsNativeType() bool
func (*ConfigTypeDefinition) ItemInstance ¶
func (field *ConfigTypeDefinition) ItemInstance() TypeInstance
ItemInstance returns a TypeInstance implementation adapted to the field in array item form.
func (*ConfigTypeDefinition) MaybeItemInstance ¶
func (field *ConfigTypeDefinition) MaybeItemInstance() TypeInstance
MaybeItemInstance returns ItemInstance if the field is an array, FieldInstance otherwise.
func (*ConfigTypeDefinition) NeedsInitializer ¶
func (field *ConfigTypeDefinition) NeedsInitializer() (bool, error)
NeedsInitializer returns true if the zero value isn't usable for unmarshaling into.
func (*ConfigTypeDefinition) QualName ¶
func (field *ConfigTypeDefinition) QualName() string
QualName returns the qualified name of the field. For instance Transaction.Version
func (*ConfigTypeDefinition) SetConfigFormat ¶
func (field *ConfigTypeDefinition) SetConfigFormat(cf *ConfigFormat)
SetConfigFormat provides a reference to the ConfigFormat instance for lookups of referenced type definitions
func (*ConfigTypeDefinition) SymbolName ¶
func (field *ConfigTypeDefinition) SymbolName() string
type FieldInstance ¶
type FieldInstance struct {
// contains filtered or unexported fields
}
func (*FieldInstance) ConfigFormat ¶
func (instance *FieldInstance) ConfigFormat() *ConfigFormat
func (*FieldInstance) GetLength ¶
func (instance *FieldInstance) GetLength() (uint64, error)
func (*FieldInstance) GetMaxLength ¶
func (instance *FieldInstance) GetMaxLength() (uint64, error)
func (*FieldInstance) HasLen ¶
func (instance *FieldInstance) HasLen() (bool, error)
func (*FieldInstance) IsPointer ¶
func (instance *FieldInstance) IsPointer() bool
func (*FieldInstance) QualName ¶
func (instance *FieldInstance) QualName() string
func (*FieldInstance) ReadSymbolName ¶
func (instance *FieldInstance) ReadSymbolName() string
func (*FieldInstance) Static ¶
func (instance *FieldInstance) Static() bool
func (*FieldInstance) WriteSymbolName ¶
func (instance *FieldInstance) WriteSymbolName() string
type InputInstanceAdapter ¶
type InputInstanceAdapter struct {
// contains filtered or unexported fields
}
func (*InputInstanceAdapter) ConfigFormat ¶
func (instance *InputInstanceAdapter) ConfigFormat() *ConfigFormat
func (*InputInstanceAdapter) GetLength ¶
func (instance *InputInstanceAdapter) GetLength() (uint64, error)
func (*InputInstanceAdapter) GetMaxLength ¶
func (instance *InputInstanceAdapter) GetMaxLength() (uint64, error)
func (*InputInstanceAdapter) HasLen ¶
func (instance *InputInstanceAdapter) HasLen() (bool, error)
func (*InputInstanceAdapter) IsPointer ¶
func (instance *InputInstanceAdapter) IsPointer() bool
func (*InputInstanceAdapter) QualName ¶
func (instance *InputInstanceAdapter) QualName() string
func (*InputInstanceAdapter) ReadSymbolName ¶
func (instance *InputInstanceAdapter) ReadSymbolName() string
func (*InputInstanceAdapter) Static ¶
func (instance *InputInstanceAdapter) Static() bool
func (*InputInstanceAdapter) WriteSymbolName ¶
func (instance *InputInstanceAdapter) WriteSymbolName() string
type Integral ¶
type Integral struct {
// contains filtered or unexported fields
}
Integral represents integers that can be static or variable length.
func (*Integral) ConstantSize ¶
func (typ *Integral) ConstantSize(instance TypeInstance) (bool, error)
func (*Integral) MinimumSize ¶
func (typ *Integral) MinimumSize(instance TypeInstance) (uint64, error)
func (*Integral) PointerType ¶
func (typ *Integral) PointerType(instance TypeInstance) bool
type ItemInstance ¶
type ItemInstance struct {
// contains filtered or unexported fields
}
ItemInstance adapts a field for use in arrays
func (*ItemInstance) ConfigFormat ¶
func (instance *ItemInstance) ConfigFormat() *ConfigFormat
func (*ItemInstance) GetLength ¶
func (instance *ItemInstance) GetLength() (uint64, error)
func (*ItemInstance) GetMaxLength ¶
func (instance *ItemInstance) GetMaxLength() (uint64, error)
func (*ItemInstance) HasLen ¶
func (instance *ItemInstance) HasLen() (bool, error)
The schema cannot specify arrays of arrays, so this is always false unless the contained type supports len.
func (*ItemInstance) IsPointer ¶
func (instance *ItemInstance) IsPointer() bool
We only support pointers to structs in arrays
func (*ItemInstance) QualName ¶
func (instance *ItemInstance) QualName() string
func (*ItemInstance) ReadSymbolName ¶
func (instance *ItemInstance) ReadSymbolName() string
func (*ItemInstance) Static ¶
func (instance *ItemInstance) Static() bool
func (*ItemInstance) WriteSymbolName ¶
func (instance *ItemInstance) WriteSymbolName() string
type Marshaler ¶
type Marshaler interface { // Allocate storage Marshal() ([]byte, error) // Marshal into preallocated storage and returned consumed bytes. MarshalTo(data []byte) (int, error) // Unmarshal the first document in data Unmarshal(data []byte) error // Unmarshal the first document in data returning consumed bytes UnmarshalFrom(data []byte) (int, error) // Return the size that the struct will need to Marshal successfully. If the // struct cannot be marshalled successfully for some reason, will return a // sufficient number of bytes (perhaps even 0) to allow marshalTo to reason // the point where that marshalling error will occur, and will itself not // panic. MarshalTo will then proceed to attempt to marshal into this space // and the marshaling will fail. The failure should still be due to the // underlying reason, not due to a shortness of space returned from Size. Size() int }
Marshaler is an interface to define how marshaling happens in Ranger. All specified types must be either a supported, native type such as uint or string, or implement this interface by either doing it by hand or through rangergen.
This is deliberately compatible with the way that GRPC/gogoproto generated code wants to call things, so that generated types can be embedded as foreign types in protobufs.
type Strings ¶
type Strings struct {
// contains filtered or unexported fields
}
func (*Strings) ConstantSize ¶
func (typ *Strings) ConstantSize(instance TypeInstance) (bool, error)
func (*Strings) MinimumSize ¶
func (typ *Strings) MinimumSize(instance TypeInstance) (uint64, error)
func (*Strings) PointerType ¶
func (typ *Strings) PointerType(instance TypeInstance) bool
type Type ¶
type Type interface { // ConstantSize returns true if the type is always the same size ConstantSize(TypeInstance) (bool, error) // Does the type support len() HasLen(TypeInstance) (bool, error) // MinimumSize returns the minimum serialized size of the type. MinimumSize(TypeInstance) (uint64, error) // The name of the type Name() string // PointerType returns whether the type instance is a value or a pointer to // value PointerType(TypeInstance) bool // Read returns code to deserialise an instance of the type Read(TypeInstance) (string, error) // Type returns the go code to describe the type - e.g. [32]byte. Type(TypeInstance) (string, error) // WriteSize returns code to caculate the size of an instance of the type when serialized WriteSize(TypeInstance) (string, error) // Write returns code to serialise an instance of the Write(TypeInstance) (string, error) }
A Type is a type for which we can emit (de)serialisation code. Built in types cover the basic native types. Users can compose these into custom types.
type TypeInstance ¶
type TypeInstance interface { // Get at the ConfigFormat for e.g. running templates ConfigFormat() *ConfigFormat // What if any Length is configured for 'instance'. // NB: this should move to being part of the type. GetLength() (uint64, error) // What max length is configured for 'instance'. GetMaxLength() (uint64, error) // Does the type support len() HasLen() (bool, error) // Is this a Reference IsPointer() bool // Whats the fully qualified name (for human errors) QualName() string // How is this instance address in code when reading in ReadSymbolName() string // How is this instance addressed in code when writing out WriteSymbolName() string // Is this instance statically sized? (Separate to static array lengths at // least for now) Static() bool }
TypeInstance is an instance of a type e.g. Type == struct definition.
TypeInstance == usage - plain, in an array, or as a pointer.
type UInt8 ¶
type UInt8 struct{}
A static sized UInt8
func (*UInt8) ConstantSize ¶
func (typ *UInt8) ConstantSize(instance TypeInstance) (bool, error)
func (*UInt8) MinimumSize ¶
func (typ *UInt8) MinimumSize(instance TypeInstance) (uint64, error)
func (*UInt8) PointerType ¶
func (typ *UInt8) PointerType(instance TypeInstance) bool