kratos: Index | Examples | Files | Directories

package binding

import ""



Package Files

binding.go default_validator.go form.go form_mapping.go json.go query.go tags.go xml.go


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"



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


type Binding Uses

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

Binding http binding request interface.


package main

import (

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 {
    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", ""+params, nil)

func Default Uses

func Default(method, contentType string) Binding

Default get by binding type by method and contexttype.

type StructValidator Uses

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

    //GetValidate return the default validate
    GetValidate() *validator.Validate

StructValidator http validator interface.

var Validator StructValidator = &defaultValidator{}

Validator default validator.


examplePackage example is a generated protocol buffer package.

Package binding imports 10 packages (graph) and is imported by 2 packages. Updated 2020-04-26. Refresh now. Tools for package owners.