Skip to content

Commit 5e098d6

Browse files
committed
chore: Change the separator between multiple expressions to ';'
Change-Id: Ie37c9fa2b7852f394c86153bc931fa5ee7905ca2
1 parent c6d971f commit 5e098d6

File tree

11 files changed

+295
-163
lines changed

11 files changed

+295
-163
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ func Example() {
3636
type T struct {
3737
A int `tagexpr:"$<0||$>=100"`
3838
B string `tagexpr:"len($)>1 && regexp('^\\w*$')"`
39-
C bool `tagexpr:"{expr1:(f.g)$>0 && $}{expr2:'C must be true when T.f.g>0'}"`
40-
d []string `tagexpr:"{@:len($)>0 && $[0]=='D'} {msg:sprintf('invalid d: %v',$)}"`
39+
C bool `tagexpr:"expr1:(f.g)$>0 && $; expr2:'C must be true when T.f.g>0'"`
40+
d []string `tagexpr:"@:len($)>0 && $[0]=='D'; msg:sprintf('invalid d: %v',$)"`
4141
e map[string]int `tagexpr:"len($)==$['len']"`
4242
e2 map[string]*int `tagexpr:"len($)==$['len']"`
4343
f struct {
@@ -100,7 +100,7 @@ type T struct {
100100
// Single model
101101
Field1 T1 `tagName:"expression"`
102102
// Multiple model
103-
Field2 T2 `tagName:"{exprName:expression} [{exprName2:expression2}]..."`
103+
Field2 T2 `tagName:"exprName:expression; [exprName2:expression2;]..."`
104104
...
105105
}
106106
```

binding/README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ import (
2020

2121
func Example() {
2222
type InfoRequest struct {
23-
Name string `api:"{path:'name'}"`
24-
Year []int `api:"{query:'year'}"`
25-
Email *string `api:"{body:'email'}{@:email($)}"`
26-
Friendly bool `api:"{body:'friendly'}"`
27-
Pie float32 `api:"{body:'pie'}{required:true}"`
28-
Hobby []string `api:"{body:'hobby'}"`
29-
BodyNotFound *int `api:"{body:'xxx'}"`
30-
Authorization string `api:"{header:'Authorization'}{required:true}{@:$=='Basic 123456'}"`
31-
SessionID string `api:"{cookie:'sessionid'}{required:true}"`
23+
Name string `api:"path:'name'"`
24+
Year []int `api:"query:'year'"`
25+
Email *string `api:"body:'email'; @:email($)"`
26+
Friendly bool `api:"body:'friendly'"`
27+
Pie float32 `api:"body:'pie'; required:true"`
28+
Hobby []string `api:"body:'hobby'"`
29+
BodyNotFound *int `api:"body:'xxx'"`
30+
Authorization string `api:"header:'Authorization'; required:true; @:$=='Basic 123456'"`
31+
SessionID string `api:"cookie:'sessionid'; required:true"`
3232
AutoBody string
3333
AutoQuery string
3434
AutoNotFound *string
@@ -92,18 +92,18 @@ The parameter position in HTTP request:
9292

9393
|expression|description|
9494
|---------------|-----------|
95-
|`{path:'$name'}`|URL path parameter
96-
|`{query:'$name'}`|URL query parameter
97-
|`{body:'$name'}`|The field in body, support:<br>`application/json`,<br>`application/x-www-form-urlencoded`,<br>`multipart/form-data`
98-
|`{header:'$name'}`|Header parameter
99-
|`{cookie:'$name'}`|Cookie parameter
95+
|`path:'$name'`|URL path parameter
96+
|`query:'$name'`|URL query parameter
97+
|`body:'$name'`|The field in body, support:<br>`application/json`,<br>`application/x-www-form-urlencoded`,<br>`multipart/form-data`
98+
|`header:'$name'`|Header parameter
99+
|`cookie:'$name'`|Cookie parameter
100100

101101
**NOTE:**
102102

103103
- `'$name'` is variable placeholder
104104
- If `'$name'` is empty, use the name of field
105105
- If no position is tagged, use `body` first, followed by `query`
106-
- Expression `{required:true}` indicates that the parameter is required
106+
- Expression `required:true` indicates that the parameter is required
107107

108108

109109
## Level

binding/bind_test.go

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ import (
1717
func TestRawBody(t *testing.T) {
1818
type Recv struct {
1919
rawBody **struct {
20-
A []byte `api:"{raw_body:nil}"`
21-
B *[]byte `api:"{raw_body:nil}"`
22-
C **[]byte `api:"{raw_body:nil}"`
23-
D string `api:"{raw_body:nil}"`
24-
E *string `api:"{raw_body:nil}"`
25-
F **string `api:"{raw_body:nil}{@:len($)<3}{msg:'too long'}"`
20+
A []byte `api:"raw_body:nil"`
21+
B *[]byte `api:"raw_body:nil"`
22+
C **[]byte `api:"raw_body:nil"`
23+
D string `api:"raw_body:nil"`
24+
E *string `api:"raw_body:nil"`
25+
F **string `api:"raw_body:nil; @:len($)<3; msg:'too long'"`
2626
}
27-
S string `api:"{raw_body:nil}"`
27+
S string `api:"raw_body:nil"`
2828
}
2929
bodyBytes := []byte("rawbody.............")
3030
req := newRequest("", nil, nil, bytes.NewReader(bodyBytes))
@@ -49,13 +49,13 @@ func TestRawBody(t *testing.T) {
4949
func TestQueryString(t *testing.T) {
5050
type Recv struct {
5151
X **struct {
52-
A []string `api:"{query:'a'}"`
53-
B string `api:"{query:'b'}"`
54-
C *[]string `api:"{query:'c'}{required:true}"`
55-
D *string `api:"{query:'d'}"`
52+
A []string `api:"query:'a'"`
53+
B string `api:"query:'b'"`
54+
C *[]string `api:"query:'c'; required:true"`
55+
D *string `api:"query:'d'"`
5656
}
57-
Y string `api:"{query:'y'}{required:true}"`
58-
Z *string `api:"{query:'z'}"`
57+
Y string `api:"query:'y'; required:true"`
58+
Z *string `api:"query:'z'"`
5959
}
6060
req := newRequest("http://localhost:8080/?a=a1&a=a2&b=b1&c=c1&c=c2&d=d1&d=d2&y=y1", nil, nil, nil)
6161
recv := new(Recv)
@@ -73,13 +73,13 @@ func TestQueryString(t *testing.T) {
7373
func TestQueryNum(t *testing.T) {
7474
type Recv struct {
7575
X **struct {
76-
A []int `api:"{query:'a'}"`
77-
B int32 `api:"{query:'b'}"`
78-
C *[]uint16 `api:"{query:'c'}{required:true}"`
79-
D *float32 `api:"{query:'d'}"`
76+
A []int `api:"query:'a'"`
77+
B int32 `api:"query:'b'"`
78+
C *[]uint16 `api:"query:'c'; required:true"`
79+
D *float32 `api:"query:'d'"`
8080
}
81-
Y bool `api:"{query:'y'}{required:true}"`
82-
Z *int64 `api:"{query:'z'}"`
81+
Y bool `api:"query:'y'; required:true"`
82+
Z *int64 `api:"query:'z'"`
8383
}
8484
req := newRequest("http://localhost:8080/?a=11&a=12&b=21&c=31&c=32&d=41&d=42&y=true", nil, nil, nil)
8585
recv := new(Recv)
@@ -97,13 +97,13 @@ func TestQueryNum(t *testing.T) {
9797
func TestHeaderString(t *testing.T) {
9898
type Recv struct {
9999
X **struct {
100-
A []string `api:"{header:'X-A'}"`
101-
B string `api:"{header:'X-B'}"`
102-
C *[]string `api:"{header:'X-C'}{required:true}"`
103-
D *string `api:"{header:'X-D'}"`
100+
A []string `api:"header:'X-A'"`
101+
B string `api:"header:'X-B'"`
102+
C *[]string `api:"header:'X-C'; required:true"`
103+
D *string `api:"header:'X-D'"`
104104
}
105-
Y string `api:"{header:'X-Y'}{required:true}"`
106-
Z *string `api:"{header:'X-Z'}"`
105+
Y string `api:"header:'X-Y'; required:true"`
106+
Z *string `api:"header:'X-Z'"`
107107
}
108108
header := make(http.Header)
109109
header.Add("X-A", "a1")
@@ -130,13 +130,13 @@ func TestHeaderString(t *testing.T) {
130130
func TestHeaderNum(t *testing.T) {
131131
type Recv struct {
132132
X **struct {
133-
A []int `api:"{header:'X-A'}"`
134-
B int32 `api:"{header:'X-B'}"`
135-
C *[]uint16 `api:"{header:'X-C'}{required:true}"`
136-
D *float32 `api:"{header:'X-D'}"`
133+
A []int `api:"header:'X-A'"`
134+
B int32 `api:"header:'X-B'"`
135+
C *[]uint16 `api:"header:'X-C'; required:true"`
136+
D *float32 `api:"header:'X-D'"`
137137
}
138-
Y bool `api:"{header:'X-Y'}{required:true}"`
139-
Z *int64 `api:"{header:'X-Z'}"`
138+
Y bool `api:"header:'X-Y'; required:true"`
139+
Z *int64 `api:"header:'X-Z'"`
140140
}
141141
header := make(http.Header)
142142
header.Add("X-A", "11")
@@ -163,13 +163,13 @@ func TestHeaderNum(t *testing.T) {
163163
func TestCookieString(t *testing.T) {
164164
type Recv struct {
165165
X **struct {
166-
A []string `api:"{cookie:'a'}"`
167-
B string `api:"{cookie:'b'}"`
168-
C *[]string `api:"{cookie:'c'}{required:true}"`
169-
D *string `api:"{cookie:'d'}"`
166+
A []string `api:"cookie:'a'"`
167+
B string `api:"cookie:'b'"`
168+
C *[]string `api:"cookie:'c'; required:true"`
169+
D *string `api:"cookie:'d'"`
170170
}
171-
Y string `api:"{cookie:'y'}{required:true}"`
172-
Z *string `api:"{cookie:'z'}"`
171+
Y string `api:"cookie:'y'; required:true"`
172+
Z *string `api:"cookie:'z'"`
173173
}
174174
cookies := []*http.Cookie{
175175
{Name: "a", Value: "a1"},
@@ -197,13 +197,13 @@ func TestCookieString(t *testing.T) {
197197
func TestCookieNum(t *testing.T) {
198198
type Recv struct {
199199
X **struct {
200-
A []int `api:"{cookie:'a'}"`
201-
B int32 `api:"{cookie:'b'}"`
202-
C *[]uint16 `api:"{cookie:'c'}{required:true}"`
203-
D *float32 `api:"{cookie:'d'}"`
200+
A []int `api:"cookie:'a'"`
201+
B int32 `api:"cookie:'b'"`
202+
C *[]uint16 `api:"cookie:'c'; required:true"`
203+
D *float32 `api:"cookie:'d'"`
204204
}
205-
Y bool `api:"{cookie:'y'}{required:true}"`
206-
Z *int64 `api:"{cookie:'z'}"`
205+
Y bool `api:"cookie:'y'; required:true"`
206+
Z *int64 `api:"cookie:'z'"`
207207
}
208208
cookies := []*http.Cookie{
209209
{Name: "a", Value: "11"},
@@ -231,13 +231,13 @@ func TestCookieNum(t *testing.T) {
231231
func TestFormString(t *testing.T) {
232232
type Recv struct {
233233
X **struct {
234-
A []string `api:"{body:'a'}"`
235-
B string `api:"{body:'b'}"`
236-
C *[]string `api:"{body:'c'}{required:true}"`
237-
D *string `api:"{body:'d'}"`
234+
A []string `api:"body:'a'"`
235+
B string `api:"body:'b'"`
236+
C *[]string `api:"body:'c'; required:true"`
237+
D *string `api:"body:'d'"`
238238
}
239-
Y string `api:"{body:'y'}{required:true}"`
240-
Z *string `api:"{body:'z'}"`
239+
Y string `api:"body:'y'; required:true"`
240+
Z *string `api:"body:'z'"`
241241
}
242242
values := make(url.Values)
243243
values.Add("a", "a1")
@@ -273,13 +273,13 @@ func TestFormString(t *testing.T) {
273273
func TestFormNum(t *testing.T) {
274274
type Recv struct {
275275
X **struct {
276-
A []int `api:"{body:'a'}"`
277-
B int32 `api:"{body:'b'}"`
278-
C *[]uint16 `api:"{body:'c'}{required:true}"`
279-
D *float32 `api:"{body:'d'}"`
276+
A []int `api:"body:'a'"`
277+
B int32 `api:"body:'b'"`
278+
C *[]uint16 `api:"body:'c'; required:true"`
279+
D *float32 `api:"body:'d'"`
280280
}
281-
Y bool `api:"{body:'y'}{required:true}"`
282-
Z *int64 `api:"{body:'z'}"`
281+
Y bool `api:"body:'y'; required:true"`
282+
Z *int64 `api:"body:'z'"`
283283
}
284284
values := make(url.Values)
285285
values.Add("a", "11")
@@ -315,12 +315,12 @@ func TestFormNum(t *testing.T) {
315315
func TestJSON(t *testing.T) {
316316
type Recv struct {
317317
X **struct {
318-
A []string `api:"{body:'a'}"`
318+
A []string `api:"body:'a'"`
319319
B int32 `api:""`
320-
C *[]uint16 `api:"{required:true}"`
321-
D *float32 `api:"{body:'d'}"`
320+
C *[]uint16 `api:"required:true"`
321+
D *float32 `api:"body:'d'"`
322322
}
323-
Y string `api:"{body:'y'}{required:true}"`
323+
Y string `api:"body:'y'; required:true"`
324324
Z *int64 `api:""`
325325
}
326326

@@ -373,12 +373,12 @@ func (testPathParams) Get(name string) (string, bool) {
373373
func TestPath(t *testing.T) {
374374
type Recv struct {
375375
X **struct {
376-
A []string `api:"{path:'a'}"`
377-
B int32 `api:"{path:'b'}"`
378-
C *[]uint16 `api:"{path:'c'}{required:true}"`
379-
D *float32 `api:"{path:'d'}"`
376+
A []string `api:"path:'a'"`
377+
B int32 `api:"path:'b'"`
378+
C *[]uint16 `api:"path:'c'; required:true"`
379+
D *float32 `api:"path:'d'"`
380380
}
381-
Y string `api:"{path:'y'}{required:true}"`
381+
Y string `api:"path:'y'; required:true"`
382382
Z *int64
383383
}
384384

@@ -400,10 +400,10 @@ func TestAuto(t *testing.T) {
400400
X **struct {
401401
A []string `api:""`
402402
B int32 `api:""`
403-
C *[]uint16 `api:"{required:true}"`
403+
C *[]uint16 `api:"required:true"`
404404
D *float32
405405
}
406-
Y string `api:"{required:true}"`
406+
Y string `api:"required:true"`
407407
Z *int64
408408
}
409409
query := make(url.Values)

binding/example_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ import (
1313

1414
func Example() {
1515
type InfoRequest struct {
16-
Name string `api:"{path:'name'}"`
17-
Year []int `api:"{query:'year'}"`
18-
Email *string `api:"{body:'email'}{@:email($)}"`
19-
Friendly bool `api:"{body:'friendly'}"`
20-
Pie float32 `api:"{body:'pie'}{required:true}"`
21-
Hobby []string `api:"{body:'hobby'}"`
22-
BodyNotFound *int `api:"{body:'xxx'}"`
23-
Authorization string `api:"{header:'Authorization'}{required:true}{@:$=='Basic 123456'}"`
24-
SessionID string `api:"{cookie:'sessionid'}{required:true}"`
16+
Name string `api:"path:'name'"`
17+
Year []int `api:"query:'year'"`
18+
Email *string `api:"body:'email'; @:email($)"`
19+
Friendly bool `api:"body:'friendly'"`
20+
Pie float32 `api:"body:'pie'; required:true"`
21+
Hobby []string `api:"body:'hobby'"`
22+
BodyNotFound *int `api:"body:'xxx'"`
23+
Authorization string `api:"header:'Authorization'; required:true; @:$=='Basic 123456'"`
24+
SessionID string `api:"cookie:'sessionid'; required:true"`
2525
AutoBody string
2626
AutoQuery string
2727
AutoNotFound *string

example_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ func Example() {
2424
type T struct {
2525
A int `tagexpr:"$<0||$>=100"`
2626
B string `tagexpr:"len($)>1 && regexp('^\\w*$')"`
27-
C bool `tagexpr:"{expr1:(f.g)$>0 && $}{expr2:'C must be true when T.f.g>0'}"`
28-
d []string `tagexpr:"{@:len($)>0 && $[0]=='D'} {msg:sprintf('invalid d: %v',$)}"`
27+
C bool `tagexpr:"expr1:(f.g)$>0 && $; expr2:'C must be true when T.f.g>0'"`
28+
d []string `tagexpr:"@:len($)>0 && $[0]=='D'; msg:sprintf('invalid d: %v',$)"`
2929
e map[string]int `tagexpr:"len($)==$['len']"`
3030
e2 map[string]*int `tagexpr:"len($)==$['len']"`
3131
f struct {

expr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func parseExpr(expr string) (*Expr, error) {
3232
s := expr
3333
_, err := p.parseExprNode(&s, e)
3434
if err != nil {
35-
return nil, fmt.Errorf("%q (syntax incorrect): %s", expr, err.Error())
35+
return nil, fmt.Errorf("%q (syntax error): %s", expr, err.Error())
3636
}
3737
sortPriority(e.RightOperand())
3838
err = p.checkSyntax()

0 commit comments

Comments
 (0)