object

package
v0.0.0-...-646a310 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2014 License: MIT Imports: 17 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultArchetype = Archetype{
	ContentMarshaler: ContentMarshaler{
		"":                 Json,
		"application/json": Json,
		"application/xml":  Xml,
	},
}

Functions

func Marshaler

func Marshaler(cs []ContentMarshaler, cts ...ContentType) (mf MarshalFunc, ct ContentType)

func RequestMarshaler

func RequestMarshaler(r *http.Request, cs ...ContentMarshaler) (mf MarshalFunc, c ContentType)

RequestMarshaler returns the first MarshalFunc mf that matches the MIME types specified in the *http.Request from the []ContentMarshaler cs in order of preference, as well as the selected ContentType. The extension of the request path is assumed to be the most preferable MIME type.

Types

type Archetype

type Archetype struct {
	ContentMarshaler
}

func (*Archetype) Bind

func (a *Archetype) Bind()

Function BindFweight binds this Archetype to Fweight's OptionsHandler and MethodNotAllowed handlers.

func (*Archetype) Handler

func (a *Archetype) Handler() Handler

func (*Archetype) Router

func (a *Archetype) Router(g Getter) *HTTPHandler

func (*Archetype) RouterFunc

func (a *Archetype) RouterFunc(g GetterFunc) *HTTPHandler

type ContentMarshaler

type ContentMarshaler map[MediaType]MarshalFunc

func (ContentMarshaler) Marshaler

func (c ContentMarshaler) Marshaler(cts ...ContentType) (mf MarshalFunc, ct ContentType)

Marshaler returns the MarshalFunc present in c that matches one of the ContentTypes with pattern matching (text/*) in order of preference, as well as the chosen ContentType. If mf is nil ct may be some random data.

func (ContentMarshaler) RequestMarshaler

func (c ContentMarshaler) RequestMarshaler(r *http.Request) (mf MarshalFunc, ct ContentType)

RequestMarshalFunc returns the MarshalFunc mf that matches the *http.Request rq, as well as the selected ContentType. The extension of the request path is assumed to be the most preferable MIME type.

type ContentType

type ContentType struct {
	MediaType
	Params map[string]string
}

func ParseContentType

func ParseContentType(cts string) (c []ContentType, err error)

Function ParseContentType returns an array of ContentTypes corresponding to the Content-Type header string cts.

type Getter

type Getter interface {
	Get(ResponseWriter, *http.Request) interface{}
}

func NewGetter

func NewGetter(g interface {
	Get() interface{}
}) Getter

type GetterFunc

type GetterFunc func(ResponseWriter, *http.Request) interface{}

func (GetterFunc) Get

func (g GetterFunc) Get(rs ResponseWriter, r *http.Request) interface{}

type HTTPHandler

type HTTPHandler struct {
	Getter
	Handler
}

func Router

func Router(g Getter) *HTTPHandler

func RouterFunc

func RouterFunc(g GetterFunc) *HTTPHandler

func (*HTTPHandler) Bind

func (h *HTTPHandler) Bind(m MediaType, mf MarshalFunc)

func (HTTPHandler) RouteHTTP

func (h HTTPHandler) RouteHTTP(_ *http.Request) route.Router

func (HTTPHandler) ServeHTTP

func (h HTTPHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request)

type Handler

type Handler struct {
	*Archetype
	ContentMarshaler
}

func (Handler) ServeObject

func (h Handler) ServeObject(o interface{}, rw http.ResponseWriter, rq *http.Request)

Serveobject serves an interface 'o' using the handler. If o is empty, the response will be empty.

type MarshalFunc

type MarshalFunc func(r Responder, rq Request) (err error)
var Gob MarshalFunc = func(r Responder, rq Request) (err error) {
	r.ContentType("application/gob")
	err = gob.NewEncoder(r).Encode(r.I)

	return
}
var Json MarshalFunc = func(r Responder, rq Request) error {
	r.ContentType("application/json;charset=utf8")

	return json.NewEncoder(r).Encode(r.I)
}
var JsonArray MarshalFunc = func(r Responder, rq Request) (err error) {
	r.ContentType("application/json;charset=utf8")

	data, err := jsarray.Marshal(r.I)
	if err != nil {
		return
	}

	_, err = io.Copy(r, bytes.NewReader(data))
	return
}

See github.com/TShadwell/jsarray for details.

var Plain MarshalFunc = func(r Responder, rq Request) (err error) {
	r.ContentType("text/plain;charset=utf8")

	_, err = io.Copy(r, strings.NewReader(r.I.(string)))
	return
}

Plaintext. The underlying value of v must be string.

var Plane MarshalFunc = func(r Responder, rq Request) (err error) {

	rand.Seed(time.Now().Unix())

	r.ContentType("text/plane")

	rn := make([]rune, 100)
	for i, ed := 0, cap(rn); i < ed; i++ {
		rn[i] = planes[rand.Intn(nPlanes)]
	}

	_, err = io.Copy(r, strings.NewReader(string(rn)))

	return
}

Planetext.

🛧🛪🛨🛦🛫🛦 🛫🛨 🛨✈🛦🛧🛦 ✈🛬🛫🛫🛬🛫 🛩🛦✈🛩🛬🛬🛨 🛫 🛬🛨✈ 🛩 🛨

var Xml MarshalFunc = func(r Responder, rq Request) error {
	r.ContentType("application/xml;charset=utf8")

	return xml.NewEncoder(r).Encode(r.I)
}

func HTMLTemplate

func HTMLTemplate(t *htmltemplate.Template) MarshalFunc

HTMLTemplate returns a MarshalFunc that executes the data on template `t` using the html/template package

func TextTemplate

func TextTemplate(t *texttemplate.Template) MarshalFunc

TextTemplate returns a MarshalFunc that executes the data on template `t` using the text/template package.

func (MarshalFunc) ContentType

func (m MarshalFunc) ContentType(ctt string) MarshalFunc

Overwrites the ContentType of a MarshalFunc.

type MediaType

type MediaType string

type Request

type Request struct {
	*http.Request
	Params    map[string]string
	MediaType MediaType
}

type Responder

type Responder struct {
	I interface{}
	http.ResponseWriter
	// contains filtered or unexported fields
}

func (Responder) ContentType

func (r Responder) ContentType(ctt string)

The first call to this function sets the content type of the response.

type ResponseWriter

type ResponseWriter interface {
	Header() http.Header
	WriteHeader(int)
}

Directories

Path Synopsis
Package goval exposes functions to return strings containing a roundabout type declaration for passed interfaces.
Package goval exposes functions to return strings containing a roundabout type declaration for passed interfaces.

Jump to

Keyboard shortcuts

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