# README

binding GoDoc

A powerful HTTP request parameters binder that supports struct tag expression.

Example

package binding_test

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"strings"

	"github.com/bytedance/go-tagexpr/binding"
	"github.com/henrylee2cn/goutil/httpbody"
)

func Example() {
	type InfoRequest struct {
		Name          string   `api:"{path:'name'}"`
		Year          []int    `api:"{query:'year'}"`
		Email         *string  `api:"{body:'email'}{@:email($)}"`
		Friendly      bool     `api:"{body:'friendly'}"`
		Pie           float32  `api:"{body:'pie'}{required:true}"`
		Hobby         []string `api:"{body:'hobby'}"`
		BodyNotFound  *int     `api:"{body:'xxx'}"`
		Authorization string   `api:"{header:'Authorization'}{required:true}{@:$=='Basic 123456'}"`
		SessionID     string   `api:"{cookie:'sessionid'}{required:true}"`
		AutoBody      string
		AutoQuery     string
		AutoNotFound  *string
	}

	args := new(InfoRequest)
	binder := binding.New("api")
	err := binder.BindAndValidate(args, requestExample(), new(testPathParams))

	fmt.Println("bind and validate result:")

	fmt.Printf("error: %v\n", err)

	b, _ := json.MarshalIndent(args, "", "	")
	fmt.Printf("args JSON string:\n%s\n", b)

	// Output:
	// request:
	// POST /info/henrylee2cn?year=2018&year=2019&AutoQuery=autoquery_test HTTP/1.1
	// Host: localhost
	// User-Agent: Go-http-client/1.1
	// Transfer-Encoding: chunked
	// Authorization: Basic 123456
	// Content-Type: application/json;charset=utf-8
	// Cookie: sessionid=987654
	//
	// 83
	// {"AutoBody":"autobody_test","email":"[email protected]","friendly":true,"hobby":["Coding","Mountain climbing"],"pie":3.1415926}
	// 0
	//
	// bind and validate result:
	// error: <nil>
	// args JSON string:
	// {
	// 	"Name": "henrylee2cn",
	// 	"Year": [
	// 		2018,
	// 		2019
	// 	],
	// 	"Email": "[email protected]",
	// 	"Friendly": true,
	// 	"Pie": 3.1415925,
	// 	"Hobby": [
	// 		"Coding",
	// 		"Mountain climbing"
	// 	],
	// 	"BodyNotFound": null,
	// 	"Authorization": "Basic 123456",
	// 	"SessionID": "987654",
	// 	"AutoBody": "autobody_test",
	// 	"AutoQuery": "autoquery_test",
	// 	"AutoNotFound": null
	// }
}
...

Position

The parameter position in HTTP request:

expressiondescription
{path:'$name'}URL path parameter
{query:'$name'}URL query parameter
{body:'$name'}The field in body, support:
application/json,
application/x-www-form-urlencoded,
multipart/form-data
{header:'$name'}Header parameter
{cookie:'$name'}Cookie parameter

NOTE:

  • '$name' is variable placeholder
  • If '$name' is empty, use the name of field
  • If no position is tagged, use body first, followed by query
  • Expression {required:true} indicates that the parameter is required

Level

The level of handling tags:

leveldefaultdescription
OnlyFirstNoHandle only the first level fields
FirstAndTaggedYesHandle the first level fields and all the tagged fields
AnyNoHandle any level fields

# Packages

No description provided by the author

# Functions

New creates a binding tool.

# Constants

Any handle any level fields.
FirstAndTagged handle the first level fields and all the tagged fields.
OnlyFirst handle only the first level fields.

# Structs

Binding binding and verification tool for http request.
Error validate error.

# Interfaces

PathParams parameter acquisition interface on the URL path.

# Type aliases

Level the level of handling tags.