form

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2020 License: BSD-3-Clause Imports: 9 Imported by: 5

README

Form Decoder

Build Status Coverage Status Go.Dev reference Go Report Card Release

A form decoder that decode request body of any types(xml, json, form, multipart form...) into a sctruct by same codebase.

By default, form decoder can handles the following content types:

  • Form(application/x-www-form-urlencoded)
  • Multipart Form(multipart/form-data)
  • JSON(application/json)
  • XML(application/xml)

Form and multipart form are built on top of gorilla schema, tag name is schema.

Register allow to register particular decoder or replace default decoder for the specified content type.

Installation

$ go get clevergo.tech/form

Usage

import (
	"net/http"

	"clevergo.tech/form"
)

var decoders = form.New()

type user struct {
	Username string `schema:"username" json:"username" xml:"username"`
	Password string `schema:"password" json:"password" xml:"password"`
}

func init() {
	// replaces multipart form decoder.
	decoders.Register(form.ContentTypeMultipartForm, form.NewMultipartForm(10*1024*1024))
	// registers other decoder
	// decoders.Register(contentType, decoder)
}

func(w http.ResponseWriter, r *http.Request) {
	u := user{}
	if err := decoders.Decode(r, &u); err != nil {
		http.Error(w, http.StatusInternalServerError, err.Error())
		return
	}
	// ...
}
Example

Checkout example for details.

Documentation

Overview

Package form is a form decoder that decode request body into a struct.

Index

Constants

View Source
const (
	ContentType              = "Content-Type"
	ContentTypeForm          = "application/x-www-form-urlencoded"
	ContentTypeMultipartForm = "multipart/form-data"
	ContentTypeJSON          = "application/json"
	ContentTypeXML           = "application/xml"
)

Content type constants.

Variables

This section is empty.

Functions

func Decode

func Decode(r *http.Request, v interface{}) error

Decode data from a request into v, v should be a pointer.

func JSON

func JSON(r *http.Request, v interface{}) error

JSON is a JSON decoder.

func Register

func Register(contentType string, decoder Decoder)

Register a decoder for the given content type.

func XML

func XML(r *http.Request, v interface{}) error

XML is an XML decoder.

Types

type Decoder

type Decoder func(req *http.Request, v interface{}) error

Decoder is a function that decode data from request into v.

func NewForm

func NewForm(decoder *schema.Decoder) Decoder

NewForm returns a post form decoder with the given schema decoder.

func NewMultipartForm

func NewMultipartForm(maxMemory int64) Decoder

NewMultipartForm returns a multipart form decoder with the given schema decoder.

type Decoders

type Decoders map[string]Decoder

Decoders is a map that mapping from content type to decoder.

func New

func New() *Decoders

New returns a decoders.

func (*Decoders) Decode

func (d *Decoders) Decode(r *http.Request, v interface{}) error

Decode data from a request into v, v should be a pointer.

func (*Decoders) Register

func (d *Decoders) Register(contentType string, decoder Decoder)

Register a decoder for the given content type.

type Validatable

type Validatable interface {
	Validate() error
}

Validatable indicates whether a value can be validated.

Jump to

Keyboard shortcuts

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