kratos: github.com/bilibili/kratos/pkg/net/http/blademaster/binding Index | Examples | Files | Directories

package binding

import "github.com/bilibili/kratos/pkg/net/http/blademaster/binding"

Index

Examples

Package Files

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

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 Uses

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

Binding http binding request interface.

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
}

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
}

StructValidator http validator interface.

var Validator StructValidator = &defaultValidator{}

Validator default validator.

Directories

PathSynopsis
examplePackage example is a generated protocol buffer package.

Package binding imports 10 packages (graph) and is imported by 1 packages. Updated 2019-05-04. Refresh now. Tools for package owners.