emissione

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2020 License: MIT Imports: 9 Imported by: 1

README

Build Status GoDoc Go Report Card codecov

emissione-go

emissione-go is a small (no dependencies!) framework, which provide dynamic switching support for http response content types.

E.g. this allows you to transparently serve both XML and JSON output in your API.

All this is controlled via the Accept header.

emissione-go is inspired by senlinms/gores and the render types of gin-gonic/gin.

Download:

go get github.com/kernle32dll/emissione-go

Detailed documentation can be found on GoDoc.

Getting started

emissione-go provides two ways for getting started:

You can either use a opinionated default handler by using emissione.Default(), or define one yourself via emissione.New(...). The latter allows you to define a custom mapping, and a default handler. You can look at the source of emissione.Default() to get an idea.

Here is a quick example, using the defaults:

package main

import (
	"github.com/kernle32dll/emissione-go"

	"log"
	"net/http"
)

// User is a just sample struct for showcasing.
type User struct {
	Name string `json:"name",xml:"Name"`
}

func main() {
	router := http.NewServeMux()

	em := emissione.Default()

	router.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) {
		em.Write(w, r, http.StatusOK, User{Name: "Björn Gerdau"})
	})

	log.Fatal(http.ListenAndServe(":8080", router))
}

Use the following curl calls, to see the code in action:

curl localhost:8080/user

curl -H "Accept: application/xml" localhost:8080/user

Extending emissione

Extending emissione is straight forward. Simple use emissione.New(...) to define a custom mapping, and if necessary implement the emissione Writer interface. A simple implementation used by emissione itself is SimpleWriter, which simply implements the Writer interface by delegating to a marshalling method, and setting the appropriate Content-Type header. This allows for drop-in usage of Go's own marshall methods, such as json.Marshal and xml.Marshal, or jsoniter.

Compatibility

emissione-go is automatically tested against Go 1.13.X, 1.14.X and 1.15.X.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AcceptSlice

type AcceptSlice []string

AcceptSlice attaches the methods of Interface to []string, sorting in increasing order by its quality value.

func (AcceptSlice) Len

func (p AcceptSlice) Len() int

func (AcceptSlice) Less

func (p AcceptSlice) Less(i, j int) bool

func (AcceptSlice) Swap

func (p AcceptSlice) Swap(i, j int)

type Handler

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

Handler is the core of emissione, defining the mapping of accept header values to Writers.

func Default

func Default() *Handler

Default returns a opinionated configured emissione Handler.

func New

func New(defaultHandler Writer, handlerMapping WriterMapping) *Handler

New returns a user-configured emissione Handler.

func (Handler) Write

func (h Handler) Write(w http.ResponseWriter, r *http.Request, code int, i interface{})

Write writes the given status code and object to the ResponseWriter. The Request object is used to resolve the desired output type.

type SimpleWriter

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

SimpleWriter is a simple implementation of emissiones Writer interface, delegating writing duty to a specific marshaller, and setting the appropriate content type header.

func (SimpleWriter) Write

func (writer SimpleWriter) Write(w http.ResponseWriter, i interface{}) error

type Writer

type Writer interface {
	Write(w http.ResponseWriter, i interface{}) error
}

Writer is a handler used by emissione, to deliver a specific output to the client.

func NewJSONIndentWriter

func NewJSONIndentWriter(prefix string, indent string, options ...WriterOption) Writer

NewJSONIndentWriter creates a new SimpleWriter, marshalling via json.MarshalIndent.

func NewJSONWriter

func NewJSONWriter(options ...WriterOption) Writer

NewJSONWriter creates a new SimpleWriter, marshalling via json.Marshal.

func NewSimpleWriter

func NewSimpleWriter(setters ...WriterOption) Writer

NewSimpleWriter instantiates a new SimpleWriter.

See the documentation for WriterOption for configuration options.

func NewXmlIndentWriter

func NewXmlIndentWriter(prefix string, indent string, options ...WriterOption) Writer

NewXmlIndentWriter creates a new SimpleWriter, marshalling via xml.MarshalIndent.

func NewXmlWriter

func NewXmlWriter(options ...WriterOption) Writer

NewXmlWriter creates a new SimpleWriter, marshalling via xml.Marshal.

type WriterMapping

type WriterMapping map[string]Writer

WriterMapping is a convenience alias for map[string]Writer

type WriterOption

type WriterOption func(*WriterOptions)

WriterOption is functional option to WriterOptions.

func ContentType

func ContentType(contentType string) WriterOption

ContentType sets the content-type string, which will be set as the header of the same name.

func MarshallMethod

func MarshallMethod(method func(v interface{}) ([]byte, error)) WriterOption

MarshallMethod defines the marshalling method used by a SimpleWriter to marshall output objects.

type WriterOptions

type WriterOptions struct {
	MarshallMethod func(v interface{}) ([]byte, error)
	ContentType    string
}

WriterOptions is the option-wrapper for defining the workings of a Writer.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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