encoding

package
v0.0.0-...-fd52f74 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2022 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Overview

Package encoding is used to encode go objects using encoding types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Data

type Data struct {
	Data        []byte
	ContentType string
}

Data is a struct used for encoding raw bytes into the http response body. It implements the Encoding interface.

func (Data) EncodeInto

func (d Data) EncodeInto(w http.ResponseWriter) error

EncodeInto sets the content type header to "application/octet-stream" and writes the data to the response body.

type Encoding

type Encoding interface {
	// EncodeInto encodes data into the response body and if necessary returns an error.
	EncodeInto(w http.ResponseWriter) error
}

Encode is a very basic interface that all encoders (structs which are used to encode data into the response body) should implement. There are already a few encoders built into raam, but this interface lets anyone create their own custom encoder.

type JSON

type JSON struct {
	Data interface{}
}

JSON is a struct used for encoding objects in JSON format into the http response body. It implements the Encoding interface.

func (JSON) EncodeInto

func (j JSON) EncodeInto(w http.ResponseWriter) error

EncodeInto sets the content type header to "application/json" and encodes and writes the data into the response body.

type MsgPack

type MsgPack struct {
	Data interface{}
}

MsgPack is a struct used for encoding objects using msgpack into the http response body. It implements the Encoding interface.

func (MsgPack) EncodeInto

func (p MsgPack) EncodeInto(w http.ResponseWriter) error

EncodeInto sets the content type header to "application/x-protobuf" and encodes and writes the data into the response body.

type ProtoBuf

type ProtoBuf struct {
	Data interface{}
}

ProtoBuf is a struct used for encoding protocol buffers into the http response body. It implements the Encoding interface.

func (ProtoBuf) EncodeInto

func (p ProtoBuf) EncodeInto(w http.ResponseWriter) error

EncodeInto sets the content type header to "application/x-protobuf" and encodes and writes the data into the response body.

type Reader

type Reader struct {
	Reader        io.Reader
	ContentLength int64
	ContentType   string
}

Reader is a struct used for encoding bytes into the http response body (By reading from a reader). It implements the Encoding interface.

func (Reader) EncodeInto

func (r Reader) EncodeInto(w http.ResponseWriter) error

EncodeInto sets the content type header to the defined content-type header (or application/octet-stream if not set) and reads from reader into the response body.

type Redirect

type Redirect struct {
	Code     int
	Request  *http.Request
	Location string
}

Data is a struct used for encoding raw bytes into the http response body. It implements the Encoding interface.

func (Redirect) EncodeInto

func (r Redirect) EncodeInto(w http.ResponseWriter) error

EncodeInto replies to the request with a redirect to a location.

type SSEvent

type SSEvent struct {
	// Data represents the data sent to the client.
	Data []byte
	// Id represents the unique identifier of the event.
	Id string
	// Name represents the name of the event.
	Name string
	// RetryAfter represents the time after which the event should be retried (the used value will be in milliseconds).
	RetryAfter time.Duration
}

A struct used to encode Server-sent events according to the specification https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events.

func (SSEvent) EncodeInto

func (s SSEvent) EncodeInto(w http.ResponseWriter) (err error)

EncodeInto encodes the data in the SSE format into the response body. It also sets the Content-Type header to text/event-stream, Cache-Control to no-cache and Connection to keep-alive.

type String

type String struct {
	Format string
	Args   []interface{}
}

String is a struct used for encoding strings into the repsonse body. It implements the Encoding interface.

func (String) EncodeInto

func (s String) EncodeInto(w http.ResponseWriter) error

EncodeInto sets the content type header to "text/plain" and writes the data into the response body.

type XML

type XML struct {
	Data interface{}
}

XML is a struct used for encoding objects in XML format into the http response body. It implements the Encoding interface.

func (XML) EncodeInto

func (e XML) EncodeInto(w http.ResponseWriter) error

EncodeInto sets the content type header to "application/xml" and encodes and writes the data into the response body.

type YAML

type YAML struct {
	Data interface{}
}

YAML is a struct used for encoding objects in YAML format into the http response body. It implements the Encoding interface.

func (YAML) EncodeInto

func (y YAML) EncodeInto(w http.ResponseWriter) error

EncodeInto sets the content type header to "application/x-yaml" and encodes and writes the data into the response body.

Jump to

Keyboard shortcuts

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