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