github.com/bytedance/go-tagexpr@v2.7.5-0.20210114074101-de5b8743ad85+incompatible/binding/example_test.go (about)

     1  package binding_test
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"net/http"
     8  	"strings"
     9  	"time"
    10  
    11  	"github.com/henrylee2cn/goutil/httpbody"
    12  
    13  	"github.com/bytedance/go-tagexpr/binding"
    14  )
    15  
    16  func Example() {
    17  	type InfoRequest struct {
    18  		Name          string   `path:"name"`
    19  		Year          []int    `query:"year"`
    20  		Email         *string  `json:"email" vd:"email($)"`
    21  		Friendly      bool     `json:"friendly"`
    22  		Pie           float32  `json:"pie,required"`
    23  		Hobby         []string `json:",required"`
    24  		BodyNotFound  *int     `json:"BodyNotFound"`
    25  		Authorization string   `header:"Authorization,required" vd:"$=='Basic 123456'"`
    26  		userIdHeader  string   `header:"x-user_ID,required"`
    27  		SessionID     string   `cookie:"sessionid,required"`
    28  		AutoBody      string
    29  		AutoNotFound  *string
    30  		TimeRFC3339   time.Time `query:"t"`
    31  	}
    32  
    33  	args := new(InfoRequest)
    34  	binder := binding.New(nil)
    35  	err := binder.BindAndValidate(args, requestExample(), new(testPathParams))
    36  
    37  	fmt.Println("bind and validate result:")
    38  
    39  	fmt.Printf("error: %v\n", err)
    40  
    41  	b, _ := json.MarshalIndent(args, "", "	")
    42  	fmt.Printf("args JSON string:\n%s\n", b)
    43  
    44  	// Output:
    45  	// request:
    46  	// POST /info/henrylee2cn?year=2018&year=2019&t=2019-09-04T18%3A04%3A08%2B08%3A00 HTTP/1.1
    47  	// Host: localhost
    48  	// User-Agent: Go-http-client/1.1
    49  	// Transfer-Encoding: chunked
    50  	// Authorization: Basic 123456
    51  	// Content-Type: application/json;charset=utf-8
    52  	// Cookie: sessionid=987654
    53  	// X-User_id: 123456
    54  	//
    55  	// 83
    56  	// {"AutoBody":"autobody_test","Hobby":["Coding","Mountain climbing"],"email":"henrylee2cn@gmail.com","friendly":true,"pie":3.1415926}
    57  	// 0
    58  	//
    59  	// bind and validate result:
    60  	// error: <nil>
    61  	// args JSON string:
    62  	// {
    63  	// 	"Name": "henrylee2cn",
    64  	// 	"Year": [
    65  	// 		2018,
    66  	// 		2019
    67  	// 	],
    68  	// 	"email": "henrylee2cn@gmail.com",
    69  	// 	"friendly": true,
    70  	// 	"pie": 3.1415925,
    71  	// 	"Hobby": [
    72  	// 		"Coding",
    73  	// 		"Mountain climbing"
    74  	// 	],
    75  	// 	"BodyNotFound": null,
    76  	// 	"Authorization": "Basic 123456",
    77  	// 	"SessionID": "987654",
    78  	// 	"AutoBody": "autobody_test",
    79  	// 	"AutoNotFound": null,
    80  	// 	"TimeRFC3339": "2019-09-04T18:04:08+08:00"
    81  	// }
    82  }
    83  
    84  func requestExample() *http.Request {
    85  	contentType, bodyReader, _ := httpbody.NewJSONBody(map[string]interface{}{
    86  		"email":    "henrylee2cn@gmail.com",
    87  		"friendly": true,
    88  		"pie":      3.1415926,
    89  		"Hobby":    []string{"Coding", "Mountain climbing"},
    90  		"AutoBody": "autobody_test",
    91  	})
    92  	header := make(http.Header)
    93  	header.Add("Content-Type", contentType)
    94  	header.Add("Authorization", "Basic 123456")
    95  	header.Add("x-user_ID", "123456")
    96  	cookies := []*http.Cookie{
    97  		{Name: "sessionid", Value: "987654"},
    98  	}
    99  	req := newRequest("http://localhost/info/henrylee2cn?year=2018&year=2019&t=2019-09-04T18%3A04%3A08%2B08%3A00", header, cookies, bodyReader)
   100  	req.Method = "POST"
   101  	var w bytes.Buffer
   102  	req.Write(&w)
   103  	fmt.Printf("request:\n%s", strings.Replace(w.String(), "\r\n", "\n", -1))
   104  
   105  	bodyReader.(*bytes.Reader).Seek(0, 0)
   106  	return req
   107  }