comms

package
v0.3.1-0...-ca91996 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2021 License: MIT Imports: 6 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReceiveData

func ReceiveData(input []byte, target interface{}) error

ReceiveData loads JSON input into a target object.

Types

type Receiver

type Receiver interface {
	// Receive invalidates the Receiver's state and overwrites it with the data
	// provided by the given JSON input.
	//
	// An error is returned if there are any problems with the data.
	// If an error is returned, the Receiver's previous state must be preserved.
	Receive(input json.RawMessage, ctx server.Context) error
}

Receiver is the counterpart of Sender, it is able to receive JSON data.

type Sender

type Sender interface {
	// Send returns a view of the data structure that should be serialized to
	// JSON to communicate between server and client.
	//
	// The returned view will be serialized as JSON, possibly as part of a
	// larger structure. If you need to manually serialize the structure, return
	// a json.RawMessage. Generally, if the returned value implements
	// json.Marshaler, that will be used for serializing.
	Send(ctx server.Context) interface{}
}

Sender describes an object that can be serialized to JSON and sent via HTTP between server and client.

type ValidatedInt

type ValidatedInt struct {
	// data is loaded into this
	Value int
	// inclusive required range
	Min, Max int
}

ValidatedInt can be used to load an integer value that must be in a specified range.

func (*ValidatedInt) UnmarshalJSON

func (vi *ValidatedInt) UnmarshalJSON(data []byte) error

UnmarshalJSON loads the given JSON input as int value and on success checks whether the loaded value is inside the required range.

type ValidatedSlice

type ValidatedSlice struct {
	Data     interface{}
	MinItems int
	MaxItems int
}

ValidatedSlice takes a pointer to a slice as data and deserializes a JSON value into it, checking the required bounds.

func (*ValidatedSlice) UnmarshalJSON

func (vs *ValidatedSlice) UnmarshalJSON(data []byte) error

UnmarshalJSON loads JSON input into a slice, validating its length

type ValidatedString

type ValidatedString struct {
	// data is loaded into this
	Value string
	// ignored if -1
	MinLen, MaxLen int
}

ValidatedString can be used to load a string value whose length must be in a specified range.

func (*ValidatedString) UnmarshalJSON

func (vs *ValidatedString) UnmarshalJSON(data []byte) error

UnmarshalJSON loads the given JSON input as string value and on success checks whether the loaded value's length is in the required range.

type ValidatedStruct

type ValidatedStruct struct {
	Value interface{}
}

ValidatedStruct can be used to load a struct value for which each field must exist in the input.

func (*ValidatedStruct) UnmarshalJSON

func (vs *ValidatedStruct) UnmarshalJSON(data []byte) error

UnmarshalJSON loads the given JSON input as object and assigns each value to the target's field with the same name (honoring a field's json tag). It requires each field to be given a value.

Jump to

Keyboard shortcuts

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