dnode

package
v0.0.0-...-baa1a54 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2018 License: MIT Imports: 8 Imported by: 68

Documentation

Overview

Package dnode implements a dnode scrubber. See the following URL for details: https://github.com/substack/dnode-protocol/blob/master/doc/protocol.markdown

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseCallbacks

func ParseCallbacks(msg *Message, sender func(id uint64, args []interface{}) error) error

parseCallbacks parses the message's "callbacks" field and prepares callback functions in "arguments" field.

Types

type ArgumentError

type ArgumentError struct {
	// contains filtered or unexported fields
}

ArgumentError is returned when received message contains invalid arguments.

func (ArgumentError) Error

func (e ArgumentError) Error() string

type CallbackNotFoundError

type CallbackNotFoundError struct {
	ID   uint64
	Args *Partial
}

CallbackNotFoundError is returned when there is no registered callback for received message.

func (CallbackNotFoundError) Error

func (e CallbackNotFoundError) Error() string

type CallbackSpec

type CallbackSpec struct {
	// Path represents the callback's path in the arguments structure.
	Path     Path
	Function Function
}

CallbackSpec is a structure encapsulating a Function and it's Path. It is the type of the values in callbacks map.

type Function

type Function struct {
	Caller caller
}

Function is the type for sending and receiving functions in dnode messages.

func Callback

func Callback(f func(*Partial)) Function

Callback is the wrapper for function when sending.

func (Function) Call

func (f Function) Call(args ...interface{}) error

Call the received function.

func (Function) IsValid

func (f Function) IsValid() bool

IsValid returns true if f represents a Function. It returns false if f is the zero Value.

func (Function) MarshalJSON

func (f Function) MarshalJSON() ([]byte, error)

func (*Function) UnmarshalJSON

func (*Function) UnmarshalJSON(data []byte) error

type Message

type Message struct {
	// Method can be an integer or string.
	Method interface{} `json:"method"`

	// Array of arguments
	Arguments *Partial `json:"arguments"`

	// Integer map of callback paths in arguments
	Callbacks map[string]Path `json:"callbacks"`
}

Message is the JSON object to call a method at the other side.

type MethodNotFoundError

type MethodNotFoundError struct {
	Method string
	Args   *Partial
}

MethodNotFoundError is returned when there is no registered handler for received method.

func (MethodNotFoundError) Error

func (e MethodNotFoundError) Error() string

type Partial

type Partial struct {
	Raw           []byte
	CallbackSpecs []CallbackSpec
}

Partial is the type of "arguments" field in dnode.Message.

func (*Partial) Bool

func (p *Partial) Bool() (b bool, err error)

Bool is a helper to unmarshal a JSON Boolean.

func (*Partial) Float64

func (p *Partial) Float64() (f float64, err error)

Float64 is a helper to unmarshal a JSON Number.

func (*Partial) Function

func (p *Partial) Function() (f Function, err error)

Function is a helper to unmarshal a callback function.

func (*Partial) Map

func (p *Partial) Map() (m map[string]*Partial, err error)

Map is a helper method to unmarshal to a JSON Object.

func (*Partial) MarshalJSON

func (p *Partial) MarshalJSON() ([]byte, error)

MarshalJSON returns the raw bytes of the Partial.

func (*Partial) MustBool

func (p *Partial) MustBool() bool

func (*Partial) MustFloat64

func (p *Partial) MustFloat64() float64

func (*Partial) MustFunction

func (p *Partial) MustFunction() Function

func (*Partial) MustMap

func (p *Partial) MustMap() map[string]*Partial

func (*Partial) MustSlice

func (p *Partial) MustSlice() []*Partial

func (*Partial) MustSliceOfLength

func (p *Partial) MustSliceOfLength(length int) []*Partial

func (*Partial) MustString

func (p *Partial) MustString() string

func (*Partial) MustUnmarshal

func (p *Partial) MustUnmarshal(v interface{})

func (*Partial) One

func (p *Partial) One() *Partial

func (*Partial) Slice

func (p *Partial) Slice() (a []*Partial, err error)

Slice is a helper method to unmarshal a JSON Array.

func (*Partial) SliceOfLength

func (p *Partial) SliceOfLength(length int) (a []*Partial, err error)

SliceOfLength is a helper method to unmarshal a JSON Array with specified length.

func (*Partial) String

func (p *Partial) String() (s string, err error)

String is a helper to unmarshal a JSON String.

func (*Partial) Unmarshal

func (p *Partial) Unmarshal(v interface{}) error

Unmarshal unmarshals the raw data (p.Raw) into v and prepares callbacks. v must be a struct that is the type of expected arguments.

func (*Partial) UnmarshalJSON

func (p *Partial) UnmarshalJSON(data []byte) error

UnmarshalJSON puts the data into Partial.Raw.

type Path

type Path []interface{}

Path represents a callback function's path in the arguments structure. Contains mixture of string and integer values.

type Scrubber

type Scrubber struct {

	// Reference to sent callbacks are saved in this map.
	sync.Mutex // protects
	// contains filtered or unexported fields
}

func NewScrubber

func NewScrubber() *Scrubber

New returns a pointer to a new Scrubber.

func (*Scrubber) GetCallback

func (s *Scrubber) GetCallback(id uint64) func(*Partial)

func (*Scrubber) RemoveCallback

func (s *Scrubber) RemoveCallback(id uint64)

RemoveCallback removes the callback with id from callbacks. Can be used to remove unused callbacks to free memory.

func (*Scrubber) Scrub

func (s *Scrubber) Scrub(obj interface{}) (callbacks map[string]Path)

Scrub creates an object that represents "callbacks" field in dnode message. The obj argument which will be scrubbed must be of array, slice, struct, or map type. If structure is passed, the returned callbacks map will contain its exported methods of func(*Partial) signature. Other functions must be wrapped by Callback function.

func (*Scrubber) Unscrub

func (s *Scrubber) Unscrub(arguments interface{}, callbacks map[string]Path, f func(uint64) functionReceived) error

Jump to

Keyboard shortcuts

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