path

package
v0.0.0-...-f5c3557 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2018 License: MIT Imports: 18 Imported by: 0

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

Examples

Constants

View Source
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

func GenerateFrom(fileOrDir string) error

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 Marshal

func Marshal(v interface{}, cache []byte) (b []byte, err error)

Marshal turns the given struct into a structured path

func MustMarshal

func MustMarshal(v interface{}, cache []byte) []byte

MustMarshal is like Marshal, except it panics on failure

func MustUnmarshal

func MustUnmarshal(b []byte, v interface{}) (unused []byte)

MustUnmarshal is like Unmarshal, except it panics on failure

func Unmarshal

func Unmarshal(b []byte, v interface{}) (unused []byte, err error)

Unmarshal is the reverse of Marshal, reading a serialized path into a struct.

Types

type Decoder

type Decoder struct {
	State
}

A Decoder is used for Unmarshaling paths. Never create this directly; use NewDecoder instead.

func (Decoder) ExpectBool

func (d Decoder) ExpectBool(v *bool) Decoder

ExpectBool decodes an encoded boolean value from the buffer

func (Decoder) ExpectByteArray

func (d Decoder) ExpectByteArray(v []byte) Decoder

ExpectByteArray decodes an encoded byte slice value from the buffer

func (Decoder) ExpectBytes

func (d Decoder) ExpectBytes(v *[]byte) Decoder

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

func (d Decoder) ExpectComplex64(v *complex64) Decoder

ExpectComplex64 decodes an encoded complex64 value from the buffer

func (Decoder) ExpectFloat32

func (d Decoder) ExpectFloat32(v *float32) Decoder

ExpectFloat32 decodes an encoded uint64 value from the buffer

func (Decoder) ExpectFloat64

func (d Decoder) ExpectFloat64(v *float64) Decoder

ExpectFloat64 decodes an encoded uint64 value from the buffer

func (Decoder) ExpectInt16

func (d Decoder) ExpectInt16(v *int16) Decoder

ExpectInt16 decodes an encoded int16 value from the buffer

func (Decoder) ExpectInt32

func (d Decoder) ExpectInt32(v *int32) Decoder

ExpectInt32 decodes an encoded int32 value from the buffer

func (Decoder) ExpectInt64

func (d Decoder) ExpectInt64(v *int64) Decoder

ExpectInt64 decodes an encoded int64 value from the buffer

func (Decoder) ExpectInt8

func (d Decoder) ExpectInt8(v *int8) Decoder

ExpectInt8 decodes an encoded int8 value from the buffer

func (Decoder) ExpectSep

func (d Decoder) ExpectSep() Decoder

ExpectSep looks for the delimiter as the next byte in the buffer

func (Decoder) ExpectString

func (d Decoder) ExpectString(v *string) Decoder

ExpectString decodes an encoded string value from the buffer

func (Decoder) ExpectTag

func (d Decoder) ExpectTag(tag string) Decoder

ExpectTag reads expects the next string it reads from the buffer to be the given tag

func (Decoder) ExpectTagBytes

func (d Decoder) ExpectTagBytes(tag []byte) Decoder

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

func (d Decoder) ExpectUint16(v *uint16) Decoder

ExpectUint16 decodes an encoded uint16 value from the buffer

func (Decoder) ExpectUint32

func (d Decoder) ExpectUint32(v *uint32) Decoder

ExpectUint32 decodes an encoded uint32 value from the buffer

func (Decoder) ExpectUint64

func (d Decoder) ExpectUint64(v *uint64) Decoder

ExpectUint64 decodes an encoded uint64 value from the buffer

func (Decoder) ExpectUint8

func (d Decoder) ExpectUint8(v *uint8) Decoder

ExpectUint8 decodes an encoded uint8 value from the buffer

func (Decoder) Must

func (d Decoder) Must() []byte

Must ensures that there were no Unmarshaling errors, returning the unused portion of the path.

func (Decoder) Unmarshal

func (d Decoder) Unmarshal(v interface{}) Decoder

Unmarshal unmarshals from the current state into the given value

type Encoder

type Encoder struct {
	State
}

An Encoder is used for Marshaling paths. Never create this directly; use NewEncoder instead.

func (Encoder) EmitBool

func (e Encoder) EmitBool(v bool) Encoder

EmitBool writes an encoded boolean value into the buffer

func (Encoder) EmitBytes

func (e Encoder) EmitBytes(v []byte) Encoder

EmitBytes writes an encoded byte slice value into the buffer

func (Encoder) EmitComplex128

func (e Encoder) EmitComplex128(v complex128) Encoder

EmitComplex128 writes a complex128 to the output

func (Encoder) EmitComplex64

func (e Encoder) EmitComplex64(v complex64) Encoder

EmitComplex64 writes a complex64 to the output

func (Encoder) EmitFloat32

func (e Encoder) EmitFloat32(v float32) Encoder

EmitFloat32 writes a float32 to the output

func (Encoder) EmitFloat64

func (e Encoder) EmitFloat64(v float64) Encoder

EmitFloat64 writes a float64 to the output

func (Encoder) EmitInt16

func (e Encoder) EmitInt16(v int16) Encoder

EmitInt16 writes an int16 to the output

func (Encoder) EmitInt32

func (e Encoder) EmitInt32(v int32) Encoder

EmitInt32 writes an int32 to the output

func (Encoder) EmitInt64

func (e Encoder) EmitInt64(v int64) Encoder

EmitInt64 writes an int64 to the output

func (Encoder) EmitInt8

func (e Encoder) EmitInt8(v int8) Encoder

EmitInt8 writes an int8 to the output

func (Encoder) EmitSep

func (e Encoder) EmitSep() Encoder

EmitSep writes the path separator to the output if !s.DisableSep

func (Encoder) EmitString

func (e Encoder) EmitString(v string) Encoder

EmitString writes an encoded string value into the buffer

func (Encoder) EmitUint16

func (e Encoder) EmitUint16(v uint16) Encoder

EmitUint16 writes a uint16 to the output

func (Encoder) EmitUint32

func (e Encoder) EmitUint32(v uint32) Encoder

EmitUint32 writes a uint32 to the output

func (Encoder) EmitUint64

func (e Encoder) EmitUint64(v uint64) Encoder

EmitUint64 writes a uint64 to the output

func (Encoder) EmitUint8

func (e Encoder) EmitUint8(v uint8) Encoder

EmitUint8 writes a uint8 to the output

func (Encoder) Marshal

func (e Encoder) Marshal(v interface{}) Encoder

Marshal marshals a new value in the current state

func (Encoder) Must

func (e Encoder) Must() []byte

Must ensures that there were no Marshaling errors

type Marshaler

type Marshaler interface {
	MarshalPath(e Encoder) Encoder
}

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) Marshal

func (s Separator) Marshal(v interface{}, cache []byte) ([]byte, error)

Marshal turns the given struct into a structured path

func (Separator) MustMarshal

func (s Separator) MustMarshal(v interface{}, cache []byte) []byte

MustMarshal is like Marshal, except it panics on failure

func (Separator) MustUnmarshal

func (s Separator) MustUnmarshal(b []byte, p interface{}) (unused []byte)

MustUnmarshal is like Unmarshal, except it panics on failure

func (Separator) NewDecoder

func (s Separator) NewDecoder(b []byte) (dec Decoder)

NewDecoder creates a new Decoder and checks that the first byte of the path is the appropriate separator.

func (Separator) NewEncoder

func (s Separator) NewEncoder(b []byte) (enc Encoder)

NewEncoder creates a new Encoder, adding the separator as the first byte.

func (Separator) Unmarshal

func (s Separator) Unmarshal(b []byte, v interface{}) (unused []byte, err error)

Unmarshal is the reverse of Marshal, reading a serialized path into a struct.

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

type Unmarshaler interface {
	UnmarshalPath(d Decoder) Decoder
}

Unmarshaler is the interface implemented by objects that can unmarshal themselves from a []byte

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL