encoding

package
v0.0.0-...-464f0ff Latest Latest
Warning

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

Go to latest
Published: May 8, 2016 License: MIT Imports: 13 Imported by: 0

README

Scaffold Encoding

This package adds encoding and decoding to scaffold for apis. It uses the Content-Type and Accept headers to work out what data type to return.

Example:

This example uses the handler builder to auto decode the request and encode the response:

package main

import (
    "net/http"

    "github.com/ThatsMrTalbot/scaffold"
    "github.com/ThatsMrTalbot/scaffold/encoding"
    "github.com/ThatsMrTalbot/scaffold/errors"
	"golang.org/x/net/context"
)

type ExampleRequest struct {
    SomeValue string `json:"someValue" xml:"SomeValue"`
    OtherValue int `json:"otherValue" xml:"OtherValue"`
}

type ExampleResponse struct {
    SomeValue string `json:"someValue" xml:"SomeValue"`
    OtherValue int `json:"otherValue" xml:"OtherValue"`
}

// This handler will receive the decoded request in `request`
// The response will be encoded and sent to the client
// Eg `{"someValue":"Some value","otherValue":5}`
func ExampleHandler1(request ExampleRequest) (ExampleResponse, error) {
    return ExampleResponse{
        SomeValue: request.SomeValue,
        OtherValue: request.OtherValue,
    }, nil
}

// This handler will receive the decoded request in `request`
// The error will be sent to the client, eg `{"error":"Some error"}`
//
// If the handler has context.Context, *http.Request or http.ResponseWriter
// parameters they will be passed in.
func ExampleHandler2(request ExampleRequest, ctx context.Context) (ExampleResponse, error) {
    return ExampleResponse{}, errors.NewErrorStatus(500, "Some error")
}

func main() {
    dispatcher := scaffold.DefaultDispatcher()
    router := scaffold.New(dispatcher)

    // Add handler builder to accept handlers that need decoding/encoding
    router.AddHandlerBuilder(encoding.DefaultHandlerBuilder)

    // Route the example handlers
    router.Handle("/example1", ExampleHandler1)
    router.Handle("/example2", ExampleHandler2)

    http.ListenAndServe(":8080", dispatcher)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultEncoder        = NewEncoder(JSONEncoding, XMLEncoding)
	DefaultHandlerBuilder = DefaultEncoder.HandlerBuilder
)

Defaults for ease of use

Functions

This section is empty.

Types

type Encoder

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

Encoder encodes and decodes requests based on the mime type

func NewEncoder

func NewEncoder(defaultEncoding Encoding, encodings ...Encoding) *Encoder

NewEncoder creates encoder

func (*Encoder) HandlerBuilder

func (e *Encoder) HandlerBuilder(handler interface{}) (scaffold.Handler, error)

HandlerBuilder can be used in scaffold to create handlers based on function

func (*Encoder) Parser

func (e *Encoder) Parser(r *http.Request) Parser

Parser returns a parser based on the mime, if none can be matched the default is returned

func (*Encoder) Responder

func (e *Encoder) Responder(r *http.Request) Responder

Responder returns a responder based on the mime, if none can be matched the default is returned

type Encoding

type Encoding interface {
	Mime() string
	Responder
	Parser
}

Encoding is a encoder

var (
	JSONEncoding Encoding = &jsonResponder{}
	XMLEncoding  Encoding = &xmlResponder{}
)

Responders & Responders

type Parser

type Parser interface {
	Parse(result interface{}, r *http.Request) error
}

Parser is a request parser

type Responder

type Responder interface {
	Respond(status int, w http.ResponseWriter, response interface{}) error
}

Responder is a response encoder

Jump to

Keyboard shortcuts

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