processor

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2020 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

package processor defines what a ResponseProcessor is, and provides four standard implementations: JSON, XML, CSV and plain text.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RenderJSON added in v0.9.0

func RenderJSON(indent string) func(http.ResponseWriter, string, interface{}) error

RenderJSON returns a rendering function that converts some data into JSON.

func WriteWithNewline added in v0.7.0

func WriteWithNewline(w io.Writer, x []byte) error

WriteWithNewline is a helper function that writes some bytes to a Writer. If the byte slice is empty or if the last byte is *not* newline, an extra newline is also written, as required for HTTP responses.

Types

type ContentTypeSettable

type ContentTypeSettable interface {
	WithContentType(contentType string) ResponseProcessor
}

ContentTypeSettable interface provides for those response processors that allow the response Content-Type to be set explicitly.

type ResponseProcessor

type ResponseProcessor interface {
	// CanProcess is the predicate that determines whether this processor
	// will handle a given request.
	CanProcess(mediaRange string, lang string) bool
	// ContentType returns the content type for this response.
	ContentType() string
	// Process renders the data model to the response writer, without setting any headers.
	// If the processor encounters an error, it should panic.
	Process(w http.ResponseWriter, template string, dataModel interface{}) error
}

ResponseProcessor interface creates the contract for custom content negotiation.

func CSV

func CSV(comma ...rune) ResponseProcessor

CSV creates an output processor that serialises a dataModel in CSV form. With no arguments, the default format is comma-separated; you can supply any rune to be used as an alternative separator.

Model values should be one of the following:

* string or []string, or [][]string

* fmt.Stringer or []fmt.Stringer, or [][]fmt.Stringer

* []int or similar (bool, int8, int16, int32, int64, uint8, uint16, uint32, uint63, float32, float64, complex)

* [][]int or similar (bool, int8, int16, int32, int64, uint8, uint16, uint32, uint63, float32, float64, complex)

* struct for some struct in which all the fields are exported and of simple types (as above).

* []struct for some struct in which all the fields are exported and of simple types (as above).

func IndentedXML

func IndentedXML(index string) ResponseProcessor

IndentedXML creates a new processor for XML with a specified indentation.

func JSON

func JSON(indent ...string) ResponseProcessor

JSON creates a new processor for JSON with a specified indentation. It handles all requests except Ajax requests.

func TXT

func TXT() ResponseProcessor

TXT creates an output processor that serialises strings in text/plain form. Model values should be one of the following:

* string

* fmt.Stringer

* encoding.TextMarshaler

func XML

func XML() ResponseProcessor

XML creates a new processor for XML without indentation.

Jump to

Keyboard shortcuts

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