Back to godoc.org

Package binding

v0.3.3
Latest Go to latest

The highest tagged major version is .

Published: Jan 20, 2020 | License: MIT | Module: github.com/bilibili/kratos

Index

Examples

Constants

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

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

Binding

type Binding

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

Binding http binding request interface.

Example

Code:

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
}
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.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier