Skip to content

Commit cb41579

Browse files
committed
refactor!: 重新梳理与 types.Context 相关的接口
1 parent b455c61 commit cb41579

20 files changed

+216
-163
lines changed

examples/ctx/ctx_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ func TestContextRouter_Params(t *testing.T) {
1616
tt := routertest.NewTester[Handler](call, HandlerFunc(notFound), methodNotAllowedBuilder, optionsHandlerBuilder)
1717

1818
a.Run("params", func(a *assert.Assertion) {
19-
tt.Params(a, func(ps *types.Params) Handler {
19+
tt.Params(a, func(ctx *types.Context) Handler {
2020
return HandlerFunc(func(c *CTX) {
2121
if c.P != nil {
2222
c.P.Params().Range(func(k, v string) {
23-
(*ps).Set(k, v)
23+
ctx.Set(k, v)
2424
})
2525
}
2626
})

examples/std/std_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ func TestRouter(t *testing.T) {
2424
tt := routertest.NewTester(call, http.NotFoundHandler(), methodNotAllowedBuilder, optionsHandlerBuilder)
2525

2626
a.Run("params", func(a *assert.Assertion) {
27-
tt.Params(a, func(ps *types.Params) http.Handler {
27+
tt.Params(a, func(ctx *types.Context) http.Handler {
2828
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2929
p := GetParams(r)
3030
if p != nil {
3131
p.Params().Range(func(k, v string) {
32-
(*ps).Set(k, v)
32+
ctx.Set(k, v)
3333
})
3434
}
3535
})
@@ -52,11 +52,11 @@ func TestWithValue(t *testing.T) {
5252
a.NotEqual(WithValue(r, &types.Context{}), r)
5353

5454
r = rest.Get(a, "/to/path").Request()
55-
pp := types.NewContext("")
55+
pp := types.NewContext()
5656
pp.Set("k1", "v1")
5757
r = WithValue(r, pp)
5858

59-
pp = types.NewContext("")
59+
pp = types.NewContext()
6060
pp.Set("k2", "v2")
6161
r = WithValue(r, pp)
6262
ps := GetParams(r)
@@ -72,7 +72,7 @@ func TestGetParams(t *testing.T) {
7272
ps := GetParams(r)
7373
a.Nil(ps)
7474

75-
c := types.NewContext("")
75+
c := types.NewContext()
7676
c.Set("key1", "1")
7777
r = rest.Get(a, "/to/path").Request()
7878
ctx := context.WithValue(r.Context(), contextKeyParams, c)

internal/syntax/bench_test.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ func BenchmarkSegment_Match_Named(b *testing.B) {
1919
a.NotError(err).NotNil(seg)
2020

2121
for i := 0; i < b.N; i++ {
22-
p := types.NewContext("100000/author")
22+
p := types.NewContext()
23+
p.Path = "100000/author"
2324
a.True(seg.Match(p)).Equal(p.MustString("id", "not-exits"), "100000")
2425
}
2526
}
@@ -34,7 +35,8 @@ func BenchmarkSegment_Match_Named_withMatcher(b *testing.B) {
3435
a.NotError(err).NotNil(seg)
3536

3637
for i := 0; i < b.N; i++ {
37-
p := types.NewContext("100000/author")
38+
p := types.NewContext()
39+
p.Path = "100000/author"
3840
a.True(seg.Match(p)).Equal(p.MustString("id", "not-exits"), "100000")
3941
}
4042
}
@@ -48,7 +50,8 @@ func BenchmarkSegment_Match_String(b *testing.B) {
4850
a.NotError(err).NotNil(seg)
4951

5052
for i := 0; i < b.N; i++ {
51-
p := types.NewContext("/posts/author")
53+
p := types.NewContext()
54+
p.Path = "/posts/author"
5255
a.True(seg.Match(p)).Zero(p.Count())
5356
}
5457
}
@@ -62,7 +65,8 @@ func BenchmarkSegment_Match_Regexp(b *testing.B) {
6265
a.NotError(err).NotNil(seg)
6366

6467
for i := 0; i < b.N; i++ {
65-
p := types.NewContext("1/author")
68+
p := types.NewContext()
69+
p.Path = "1/author"
6670
a.True(seg.Match(p)).Equal(p.MustString("id", "not-exists"), "1")
6771
}
6872
}

internal/syntax/segment_test.go

+44-22
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,17 @@ func TestSegment_Match(t *testing.T) {
272272
// Named:any
273273
seg, err := i.NewSegment("{id:any}/author")
274274
a.NotError(err).NotNil(seg)
275-
p := types.NewContext("1/author")
275+
p := types.NewContext()
276+
p.Path = "1/author"
276277
a.True(seg.Match(p)).
277278
Empty(p.Path).
278279
Equal(1, p.Count()).Equal(p.MustString("id", "not-exists"), "1")
279280

280281
// Named 完全匹配
281282
seg, err = i.NewSegment("{id}/author")
282283
a.NotError(err).NotNil(seg)
283-
p = types.NewContext("1/author")
284+
p = types.NewContext()
285+
p.Path = "1/author"
284286
a.True(seg.Match(p)).
285287
Empty(p.Path).
286288
Equal(1, p.Count()).
@@ -289,7 +291,8 @@ func TestSegment_Match(t *testing.T) {
289291
// Named 部分匹配
290292
seg, err = i.NewSegment("{id}/author")
291293
a.NotError(err).NotNil(seg)
292-
p = types.NewContext("1/author/email")
294+
p = types.NewContext()
295+
p.Path = "1/author/email"
293296
a.True(seg.Match(p))
294297
a.Equal(p.Path, "/email").
295298
Equal(1, p.Count()).
@@ -298,14 +301,16 @@ func TestSegment_Match(t *testing.T) {
298301
// Named 不匹配
299302
seg, err = i.NewSegment("{id}/author")
300303
a.NotError(err).NotNil(seg)
301-
p = types.NewContext("1/aut")
304+
p = types.NewContext()
305+
p.Path = "1/aut"
302306
a.False(seg.Match(p))
303307
a.Equal(p.Path, "1/aut").Zero(p.Count())
304308

305309
// Named 1/2 匹配 {id}
306310
seg, err = i.NewSegment("{id}/author")
307311
a.NotError(err).NotNil(seg)
308-
p = types.NewContext("1/2/author")
312+
p = types.NewContext()
313+
p.Path = "1/2/author"
309314
a.True(seg.Match(p)).
310315
Equal(p.Path, "").
311316
Equal(1, p.Count()).
@@ -314,29 +319,33 @@ func TestSegment_Match(t *testing.T) {
314319
// Interceptor 1/2 匹配 {id}
315320
seg, err = i.NewSegment("{id:any}/author")
316321
a.NotError(err).NotNil(seg)
317-
p = types.NewContext("1/2/author")
322+
p = types.NewContext()
323+
p.Path = "1/2/author"
318324
a.True(seg.Match(p)).
319325
Equal(p.Path, "").
320326
Equal(1, p.Count()).
321327
Equal(p.MustString("id", "not-exists"), "1/2")
322328

323329
seg, err = i.NewSegment("{any}/123")
324330
a.NotError(err).NotNil(seg)
325-
p = types.NewContext("123")
331+
p = types.NewContext()
332+
p.Path = "123"
326333
a.False(seg.Match(p)).
327334
Equal(p.Path, "123").Zero(p.Count())
328335

329336
seg, err = i.NewSegment("{any:any}/123")
330337
a.NotError(err).NotNil(seg)
331-
p = types.NewContext("123")
338+
p = types.NewContext()
339+
p.Path = "123"
332340
a.False(seg.Match(p)).
333341
Equal(p.Path, "123").Zero(p.Count())
334342

335343
// 命名参数,any 匹配到了空.
336344
seg, err = i.NewSegment("{any}123")
337345
a.NotError(err).NotNil(seg)
338346
a.Equal(seg.Type, Named)
339-
p = types.NewContext("123123")
347+
p = types.NewContext()
348+
p.Path = "123123"
340349
a.True(seg.Match(p)).
341350
Equal(p.Path, "123").
342351
Equal(1, p.Count()).
@@ -346,23 +355,26 @@ func TestSegment_Match(t *testing.T) {
346355
seg, err = i.NewSegment("{any:any}123")
347356
a.NotError(err).NotNil(seg)
348357
a.Equal(seg.Type, Interceptor)
349-
p = types.NewContext("123123")
358+
p = types.NewContext()
359+
p.Path = "123123"
350360
a.True(seg.Match(p))
351361
a.Empty(p.Path).
352362
Equal(1, p.Count()).
353363
Equal(p.MustString("any", "not-exists"), "123")
354364

355365
seg, err = i.NewSegment("{any:any}123")
356366
a.NotError(err).NotNil(seg)
357-
p = types.NewContext("12345123")
367+
p = types.NewContext()
368+
p.Path = "12345123"
358369
a.True(seg.Match(p)).
359370
Empty(p.Path).
360371
Equal(1, p.Count()).
361372
Equal(p.MustString("any", "not-exists"), "12345")
362373

363374
seg, err = i.NewSegment("{any:digit}123")
364375
a.NotError(err).NotNil(seg)
365-
p = types.NewContext("12345123")
376+
p = types.NewContext()
377+
p.Path = "12345123"
366378
a.True(seg.Match(p)).
367379
Empty(p.Path).
368380
Equal(1, p.Count()).
@@ -371,7 +383,8 @@ func TestSegment_Match(t *testing.T) {
371383
// Named Endpoint 匹配
372384
seg, err = i.NewSegment("{path}")
373385
a.NotError(err).NotNil(seg)
374-
p = types.NewContext("/posts/author")
386+
p = types.NewContext()
387+
p.Path = "/posts/author"
375388
a.True(seg.Match(p)).
376389
Empty(p.Path).
377390
Equal(1, p.Count()).
@@ -380,7 +393,8 @@ func TestSegment_Match(t *testing.T) {
380393
// Named:digit Endpoint 匹配
381394
seg, err = i.NewSegment("{id:digit}")
382395
a.NotError(err).NotNil(seg)
383-
p = types.NewContext("123")
396+
p = types.NewContext()
397+
p.Path = "123"
384398
a.True(seg.Match(p))
385399
a.Empty(p.Path).
386400
Equal(1, p.Count()).
@@ -389,7 +403,8 @@ func TestSegment_Match(t *testing.T) {
389403
// Named:digit Endpoint 不匹配,不会删除传入的参数
390404
seg, err = i.NewSegment("{id:digit}")
391405
a.NotError(err).NotNil(seg)
392-
p = types.NewContext("one")
406+
p = types.NewContext()
407+
p.Path = "one"
393408
p.Set("p1", "v1")
394409
a.False(seg.Match(p)).
395410
Equal(p.Path, "one").
@@ -401,12 +416,14 @@ func TestSegment_Match(t *testing.T) {
401416
a.NotError(err).NotNil(seg)
402417

403418
// Named:digit 不匹配
404-
p = types.NewContext("1/aut")
419+
p = types.NewContext()
420+
p.Path = "1/aut"
405421
a.False(seg.Match(p)).
406422
Equal(p.Path, "1/aut").Zero(p.Count())
407423

408424
// Named:digit 类型不匹配
409-
p = types.NewContext("xx/author")
425+
p = types.NewContext()
426+
p.Path = "xx/author"
410427
a.False(seg.Match(p)).
411428
Equal(p.Path, "xx/author").
412429
Zero(p.Count())
@@ -416,15 +433,17 @@ func TestSegment_Match(t *testing.T) {
416433
a.NotError(err).NotNil(seg)
417434

418435
// String 匹配
419-
p = types.NewContext("/posts/author")
436+
p = types.NewContext()
437+
p.Path = "/posts/author"
420438
p.Set("p1", "v1")
421439
a.True(seg.Match(p)).
422440
Empty(p.Path).
423441
Equal(1, p.Count()).
424442
Equal(p.MustString("p1", "not-exists"), "v1")
425443

426444
// String 不匹配
427-
p = types.NewContext("/posts/author/email")
445+
p = types.NewContext()
446+
p.Path = "/posts/author/email"
428447
a.True(seg.Match(p))
429448
a.Equal(p.Path, "/email").Zero(p.Count())
430449

@@ -433,20 +452,23 @@ func TestSegment_Match(t *testing.T) {
433452
a.NotError(err).NotNil(seg)
434453

435454
// Regexp 完全匹配
436-
p = types.NewContext("1/author")
455+
p = types.NewContext()
456+
p.Path = "1/author"
437457
a.True(seg.Match(p)).
438458
Empty(p.Path).
439459
Equal(1, p.Count()).
440460
Equal(p.MustString("id", "not-exists"), "1")
441461

442462
// Regexp 不匹配
443-
p = types.NewContext("xxx/author")
463+
p = types.NewContext()
464+
p.Path = "xxx/author"
444465
a.False(seg.Match(p)).
445466
Equal(p.Path, "xxx/author").
446467
Zero(p.Count())
447468

448469
// Regexp 部分匹配
449-
p = types.NewContext("1/author/email")
470+
p = types.NewContext()
471+
p.Path = "1/author/email"
450472
a.True(seg.Match(p)).
451473
Equal(p.Path, "/email").
452474
Equal(1, p.Count()).

internal/tree/bench_test.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ func BenchmarkTree_Match(b *testing.B) {
4141

4242
for i := 0; i < b.N; i++ {
4343
index := i % len(paths)
44-
h := tree.match(types.NewContext(paths[index]))
44+
p := types.NewContext()
45+
p.Path = paths[index]
46+
h := tree.match(p)
4547
a.True(len(h.handlers) > 0)
4648
}
4749
}
@@ -74,7 +76,9 @@ func BenchmarkTree_ServeHTTP(b *testing.B) {
7476

7577
for i := 0; i < b.N; i++ {
7678
index := i % len(paths)
77-
h := tree.match(types.NewContext(paths[index]))
79+
p := types.NewContext()
80+
p.Path = paths[index]
81+
h := tree.match(p)
7882
hh := h.handlers[http.MethodGet]
7983

8084
w := httptest.NewRecorder()

0 commit comments

Comments
 (0)