binding

package
v0.3.2-0...-6d6c435 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2019 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	MIMEJSON              = "application/json"
	MIMEHTML              = "text/html"
	MIMEXML               = "application/xml"
	MIMEXML2              = "text/xml"
	MIMEPlain             = "text/plain"
	MIMEPOSTForm          = "application/x-www-form-urlencoded"
	MIMEMultipartPOSTForm = "multipart/form-data"
)

MIME

Variables

View Source
var (
	JSON          = jsonBinding{}
	XML           = xmlBinding{}
	Form          = formBinding{}
	Query         = queryBinding{}
	FormPost      = formPostBinding{}
	FormMultipart = formMultipartBinding{}
)

Binding

Functions

This section is empty.

Types

type Binding

type Binding interface {
	Name() string
	Bind(*http.Request, interface{}) error
}

Binding http binding request interface.

Example
package main

import (
	"fmt"
	"log"
	"net/http"
)

type Arg struct {
	Max   int64 `form:"max" validate:"max=10"`
	Min   int64 `form:"min" validate:"min=2"`
	Range int64 `form:"range" validate:"min=1,max=10"`
	// use split option to split arg 1,2,3 into slice [1 2 3]
	// otherwise slice type with parse  url.Values (eg:a=b&a=c) default.
	Slice []int64 `form:"slice,split" validate:"min=1"`
}

func main() {
	req := initHTTP("max=9&min=3&range=3&slice=1,2,3")
	arg := new(Arg)
	if err := Form.Bind(req, arg); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("arg.Max %d\narg.Min %d\narg.Range %d\narg.Slice %v", arg.Max, arg.Min, arg.Range, arg.Slice)
}

func initHTTP(params string) (req *http.Request) {
	req, _ = http.NewRequest("GET", "http://api.bilibili.com/test?"+params, nil)
	req.ParseForm()
	return
}
Output:

arg.Max 9
arg.Min 3
arg.Range 3
arg.Slice [1 2 3]

func Default

func Default(method, contentType string) Binding

Default get by binding type by method and contexttype.

type StructValidator

type StructValidator interface {
	// ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right.
	// If the received type is not a struct, any validation should be skipped and nil must be returned.
	// If the received type is a struct or pointer to a struct, the validation should be performed.
	// If the struct is not valid or the validation itself fails, a descriptive error should be returned.
	// Otherwise nil must be returned.
	ValidateStruct(interface{}) error

	// RegisterValidation adds a validation Func to a Validate's map of validators denoted by the key
	// NOTE: if the key already exists, the previous validation function will be replaced.
	// NOTE: this method is not thread-safe it is intended that these all be registered prior to any validation
	RegisterValidation(string, validator.Func) error
}

StructValidator http validator interface.

var Validator StructValidator = &defaultValidator{}

Validator default validator.

Directories

Path Synopsis
Package example is a generated protocol buffer package.
Package example is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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