binder

package
v3.1.0+incompatible Latest Latest
Warning

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

Go to latest
Published: May 2, 2019 License: MIT Imports: 7 Imported by: 1

README

Binder

To bind a request body or a source input into a type, use a binder. It's currently support binding of JSON, XML and YAML. The binding execute Validate() if the type implements the binder.Validate interface after successfully bind the type.

The goal of implement Validate is to endorse the values linked to the type. This library intends for you to handle your own validations error.

  • FromReq(*http.Request, interface{}) error binds the data present in the request such as JSON request body.
  • FromSrc([]byte, interface{}) error reads passes body from supplied source instead of a request.

Usage

package main

import (
	"net/http"

	"github.com/pkg/errors"

	"github.com/ifreddyrondon/bastion"
	"github.com/ifreddyrondon/bastion/binder"
	"github.com/ifreddyrondon/bastion/render"
)

type address struct {
	Address *string `json:"address" xml:"address" yaml:"address"`
	Lat     float64 `json:"lat" xml:"lat" yaml:"lat"`
	Lng     float64 `json:"lng" xml:"lng" yaml:"lng"`
}

func (a *address) Validate() error {
	if a.Address == nil || *a.Address == "" {
		return errors.New("missing address field")
	}
	return nil
}

func main() {
	app := bastion.New()
	app.Post("/decode-json", func(w http.ResponseWriter, r *http.Request) {
		var a address
		if err := binder.JSON.FromReq(r, &a); err != nil {
			render.JSON.BadRequest(w, err)
			return
		}
		render.JSON.Send(w, a)
	})
	app.Post("/decode-xml", func(w http.ResponseWriter, r *http.Request) {
		var a address
		if err := binder.XML.FromReq(r, &a); err != nil {
			render.JSON.BadRequest(w, err)
			return
		}
		render.JSON.Send(w, a)
	})
	app.Post("/decode-yaml", func(w http.ResponseWriter, r *http.Request) {
		var a address
		if err := binder.YAML.FromReq(r, &a); err != nil {
			render.JSON.BadRequest(w, err)
			return
		}
		render.JSON.Send(w, a)
	})
	app.Serve()
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var JSON = NewJSON()

JSON default JSON binder.

View Source
var XML = NewXML()

XML default XML binder.

View Source
var YAML = NewYAML()

YAML default YAML binder.

Functions

func DisallowUnknownFields

func DisallowUnknownFields() func(*JSONBinder)

DisallowUnknownFields causes the Decoder to return an error when the destination is a struct and the input contains object keys which do not match any non-ignored, exported fields in the destination.

func EnableUseNumber

func EnableUseNumber() func(*JSONBinder)

EnableUseNumber is used to call the UseNumber method on the JSON Decoder instance. UseNumber causes the Decoder to unmarshal a number into an interface{} as a Number instead of as a float64.

func JSONDecodingErrMsg

func JSONDecodingErrMsg(msg string) func(*JSONBinder)

JSONDecodingErrMsg sets the error message output when json.Decode fails to decode an object.

func XMLDecodingErrMsg

func XMLDecodingErrMsg(msg string) func(*XMLBinder)

XMLDecodingErrMsg sets the error message output when xml.Decode fails to decode an object.

func YAMLDecodingErrMsg

func YAMLDecodingErrMsg(msg string) func(*YAMLBinder)

YAMLLDecodingErrMsg sets the error message output when yaml.Decode fails to decode an object.

Types

type Binding

type Binding interface {
	FromReq(*http.Request, interface{}) error
}

Binding describes the interface which needs to be implemented for binding the data present in the request such as JSON request body, query parameters or the form POST.

type BindingSrc

type BindingSrc interface {
	Binding
	FromSrc([]byte, interface{}) error
}

BindingSrc reads passes body from supplied source instead of a request.

type JSONBinder

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

JSONBinder bind the json encoded data present in the request. It implements the Binding and BindingBody interface.

func NewJSON

func NewJSON(opts ...func(*JSONBinder)) *JSONBinder

NewJSON returns a new JSONRender responder instance.

func (*JSONBinder) FromReq

func (b *JSONBinder) FromReq(req *http.Request, obj interface{}) error

Bind the JSON request body to an object, if the object implements Validate the valid method will be called.

func (*JSONBinder) FromSrc

func (b *JSONBinder) FromSrc(body []byte, obj interface{}) error

Bind the JSON body to an object, if the object implements Validate the valid method will be called.

type Validate

type Validate interface {
	Validate() error
}

Validate represents types capable of validating themselves.

type XMLBinder

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

XMLBinder bind the xml encoded data present in the request. It implements the Binding and BindingBody interface.

func NewXML

func NewXML(opts ...func(*XMLBinder)) *XMLBinder

NewXML returns a new XMLBinder responder instance.

func (*XMLBinder) FromReq

func (b *XMLBinder) FromReq(req *http.Request, obj interface{}) error

Bind the XML request body to an object, if the object implements Validate the valid method will be called.

func (*XMLBinder) FromSrc

func (b *XMLBinder) FromSrc(body []byte, obj interface{}) error

Bind the XML body to an object, if the object implements Validate the valid method will be called.

type YAMLBinder

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

YAMLBinder bind the yaml encoded data present in the request. It implements the Binding and BindingBody interface.

func NewYAML

func NewYAML(opts ...func(*YAMLBinder)) *YAMLBinder

NewYAML returns a new YAMLBinder responder instance.

func (*YAMLBinder) FromReq

func (b *YAMLBinder) FromReq(req *http.Request, obj interface{}) error

Bind the YAML request body to an object, if the object implements Validate the valid method will be called.

func (*YAMLBinder) FromSrc

func (b *YAMLBinder) FromSrc(body []byte, obj interface{}) error

Bind the YAML body to an object, if the object implements Validate the valid method will be called.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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