Documentation ¶
Overview ¶
Package path provides Marshaling and Unmarshaling for [ ]byte-encoded paths.
For example, if you have a path like "/pies/apple/0/", of the standard form "/pies/<Type:string>/<Index:uint32>/", this helps you Unmarshal that into a Pies struct with the fields "Type" and "Index", and vis-versa.
This encodes into a binary, non-human-readable format when using anything but strings. It also only supports Marshaling and Unmarshaling into structs using primitive types (with fixed byte sizes), which means that your paths must be strictly defined before using this.
Warning: do not use this for anything that will change over time and that needs to remain backwards compatible. For that, you should use something like capnproto.
Example (Usage) ¶
v := struct { Type string Index uint32 Desirability uint64 }{ Type: "apple", Index: 123, Desirability: 99828, } // This path isn't human-readable, so printing it anywhere is pointless path := MustMarshal(v, nil) // Reset everything, just in case v.Type = "" v.Index = 0 v.Desirability = 0 MustUnmarshal(path, &v) fmt.Println("Type:", v.Type) fmt.Println("Index:", v.Index) fmt.Println("Desirability:", v.Desirability)
Output: Type: apple Index: 123 Desirability: 99828
Index ¶
- Constants
- func GenerateFrom(fileOrDir string) error
- func Marshal(v interface{}, cache []byte) (b []byte, err error)
- func MustMarshal(v interface{}, cache []byte) []byte
- func MustUnmarshal(b []byte, v interface{}) (unused []byte)
- func Unmarshal(b []byte, v interface{}) (unused []byte, err error)
- type Decoder
- func (d Decoder) ExpectBool(v *bool) Decoder
- func (d Decoder) ExpectByteArray(v []byte) Decoder
- func (d Decoder) ExpectBytes(v *[]byte) Decoder
- func (d Decoder) ExpectComplex128(v *complex128) Decoder
- func (d Decoder) ExpectComplex64(v *complex64) Decoder
- func (d Decoder) ExpectFloat32(v *float32) Decoder
- func (d Decoder) ExpectFloat64(v *float64) Decoder
- func (d Decoder) ExpectInt16(v *int16) Decoder
- func (d Decoder) ExpectInt32(v *int32) Decoder
- func (d Decoder) ExpectInt64(v *int64) Decoder
- func (d Decoder) ExpectInt8(v *int8) Decoder
- func (d Decoder) ExpectSep() Decoder
- func (d Decoder) ExpectString(v *string) Decoder
- func (d Decoder) ExpectTag(tag string) Decoder
- func (d Decoder) ExpectTagBytes(tag []byte) Decoder
- func (d Decoder) ExpectUint16(v *uint16) Decoder
- func (d Decoder) ExpectUint32(v *uint32) Decoder
- func (d Decoder) ExpectUint64(v *uint64) Decoder
- func (d Decoder) ExpectUint8(v *uint8) Decoder
- func (d Decoder) Must() []byte
- func (d Decoder) Unmarshal(v interface{}) Decoder
- type Encoder
- func (e Encoder) EmitBool(v bool) Encoder
- func (e Encoder) EmitBytes(v []byte) Encoder
- func (e Encoder) EmitComplex128(v complex128) Encoder
- func (e Encoder) EmitComplex64(v complex64) Encoder
- func (e Encoder) EmitFloat32(v float32) Encoder
- func (e Encoder) EmitFloat64(v float64) Encoder
- func (e Encoder) EmitInt16(v int16) Encoder
- func (e Encoder) EmitInt32(v int32) Encoder
- func (e Encoder) EmitInt64(v int64) Encoder
- func (e Encoder) EmitInt8(v int8) Encoder
- func (e Encoder) EmitSep() Encoder
- func (e Encoder) EmitString(v string) Encoder
- func (e Encoder) EmitUint16(v uint16) Encoder
- func (e Encoder) EmitUint32(v uint32) Encoder
- func (e Encoder) EmitUint64(v uint64) Encoder
- func (e Encoder) EmitUint8(v uint8) Encoder
- func (e Encoder) Marshal(v interface{}) Encoder
- func (e Encoder) Must() []byte
- type Marshaler
- type Separator
- func (s Separator) Marshal(v interface{}, cache []byte) ([]byte, error)
- func (s Separator) MustMarshal(v interface{}, cache []byte) []byte
- func (s Separator) MustUnmarshal(b []byte, p interface{}) (unused []byte)
- func (s Separator) NewDecoder(b []byte) (dec Decoder)
- func (s Separator) NewEncoder(b []byte) (enc Encoder)
- func (s Separator) Unmarshal(b []byte, v interface{}) (unused []byte, err error)
- type State
- type Static
- type Unmarshaler
Examples ¶
Constants ¶
const DefSep = Separator('/')
DefSep is the path separator that you're typically going to want to use.
Variables ¶
This section is empty.
Functions ¶
func GenerateFrom ¶
GenerateFrom generates MarshalPath and UnmarshalPath methods for all types found in the given file or directory. Marshal-implementation files are named as "$BASE_FILE_cog_path.go".
func MustMarshal ¶
MustMarshal is like Marshal, except it panics on failure
func MustUnmarshal ¶
MustUnmarshal is like Unmarshal, except it panics on failure
Types ¶
type Decoder ¶
type Decoder struct {
State
}
A Decoder is used for Unmarshaling paths. Never create this directly; use NewDecoder instead.
func (Decoder) ExpectBool ¶
ExpectBool decodes an encoded boolean value from the buffer
func (Decoder) ExpectByteArray ¶
ExpectByteArray decodes an encoded byte slice value from the buffer
func (Decoder) ExpectBytes ¶
ExpectBytes decodes an encoded byte slice value from the buffer
func (Decoder) ExpectComplex128 ¶
func (d Decoder) ExpectComplex128(v *complex128) Decoder
ExpectComplex128 decodes an encoded complex128 value from the buffer
func (Decoder) ExpectComplex64 ¶
ExpectComplex64 decodes an encoded complex64 value from the buffer
func (Decoder) ExpectFloat32 ¶
ExpectFloat32 decodes an encoded uint64 value from the buffer
func (Decoder) ExpectFloat64 ¶
ExpectFloat64 decodes an encoded uint64 value from the buffer
func (Decoder) ExpectInt16 ¶
ExpectInt16 decodes an encoded int16 value from the buffer
func (Decoder) ExpectInt32 ¶
ExpectInt32 decodes an encoded int32 value from the buffer
func (Decoder) ExpectInt64 ¶
ExpectInt64 decodes an encoded int64 value from the buffer
func (Decoder) ExpectInt8 ¶
ExpectInt8 decodes an encoded int8 value from the buffer
func (Decoder) ExpectString ¶
ExpectString decodes an encoded string value from the buffer
func (Decoder) ExpectTag ¶
ExpectTag reads expects the next string it reads from the buffer to be the given tag
func (Decoder) ExpectTagBytes ¶
ExpectTagBytes reads expects the next byte slice it reads from the buffer to be the given tag. This is a faster version of ExpectTag since it requires no memory allocations (assuming you pass in a pre-allocated, read-only byte slice).
func (Decoder) ExpectUint16 ¶
ExpectUint16 decodes an encoded uint16 value from the buffer
func (Decoder) ExpectUint32 ¶
ExpectUint32 decodes an encoded uint32 value from the buffer
func (Decoder) ExpectUint64 ¶
ExpectUint64 decodes an encoded uint64 value from the buffer
func (Decoder) ExpectUint8 ¶
ExpectUint8 decodes an encoded uint8 value from the buffer
type Encoder ¶
type Encoder struct {
State
}
An Encoder is used for Marshaling paths. Never create this directly; use NewEncoder instead.
func (Encoder) EmitComplex128 ¶
func (e Encoder) EmitComplex128(v complex128) Encoder
EmitComplex128 writes a complex128 to the output
func (Encoder) EmitComplex64 ¶
EmitComplex64 writes a complex64 to the output
func (Encoder) EmitFloat32 ¶
EmitFloat32 writes a float32 to the output
func (Encoder) EmitFloat64 ¶
EmitFloat64 writes a float64 to the output
func (Encoder) EmitString ¶
EmitString writes an encoded string value into the buffer
func (Encoder) EmitUint16 ¶
EmitUint16 writes a uint16 to the output
func (Encoder) EmitUint32 ¶
EmitUint32 writes a uint32 to the output
func (Encoder) EmitUint64 ¶
EmitUint64 writes a uint64 to the output
type Marshaler ¶
Marshaler is the interface implemented by objects that can marshal themselves into a valid path
type Separator ¶
type Separator byte
Separator allows you to change the path separator used.
func (Separator) MustMarshal ¶
MustMarshal is like Marshal, except it panics on failure
func (Separator) MustUnmarshal ¶
MustUnmarshal is like Unmarshal, except it panics on failure
func (Separator) NewDecoder ¶
NewDecoder creates a new Decoder and checks that the first byte of the path is the appropriate separator.
func (Separator) NewEncoder ¶
NewEncoder creates a new Encoder, adding the separator as the first byte.
type State ¶
type State struct { // Where bytes are appended to and removed from B []byte // Any error encountered along the way Err error // contains filtered or unexported fields }
State is the state of the Marshaler or Unmarshaler.
type Static ¶
type Static struct{}
Static is used to place a static, unchanging element into the path.
Example ¶
v := struct { _ Static `path:"pie"` // Prefix this path with "/pie/" Type string Taste string }{ Type: "apple", Taste: "fantastic", } path := MustMarshal(v, nil) fmt.Println(string(path))
Output: /pie/apple/fantastic/
type Unmarshaler ¶
Unmarshaler is the interface implemented by objects that can unmarshal themselves from a []byte