-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Chi engine support and optimize type assert logic
* add Chi engine support * add assert.RegisterType2[B, R]() function
- Loading branch information
Showing
8 changed files
with
286 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2023 Michael Li <alimy@gility.net>. All rights reserved. | ||
// Use of this source code is governed by Apache License 2.0 that | ||
// can be found in the LICENSE file. | ||
|
||
package assert | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/alimy/mir/v4" | ||
) | ||
|
||
type ( | ||
fakeCtx struct{} | ||
fakeBindingCtx struct{} | ||
fakeRenderCtx struct{} | ||
fakeObjectType struct{} | ||
fakeObjectType2 struct{} | ||
) | ||
|
||
func (fakeObjectType) Bind(_c fakeCtx) mir.Error { | ||
// nothing | ||
return nil | ||
} | ||
|
||
func (fakeObjectType) Render(_c fakeCtx) { | ||
// nothing | ||
} | ||
|
||
func (fakeObjectType2) Bind(_c fakeBindingCtx) mir.Error { | ||
// nothing | ||
return nil | ||
} | ||
|
||
func (fakeObjectType2) Render(_c fakeRenderCtx) { | ||
// nothing | ||
} | ||
|
||
func TestAssertType(t *testing.T) { | ||
var ta TypeAssertor = anyTypeAssertor[fakeCtx]{} | ||
fakeObj := new(fakeObjectType) | ||
if ok := ta.AssertBinding(fakeObj); !ok { | ||
t.Error("want assert binding true but not") | ||
} | ||
if ok := ta.AssertRender(fakeObj); !ok { | ||
t.Error("want assert render true but not") | ||
} | ||
} | ||
|
||
func TestAssertType2(t *testing.T) { | ||
var ta TypeAssertor = anyTypeAssertor2[fakeBindingCtx, fakeRenderCtx]{} | ||
fakeObj := new(fakeObjectType2) | ||
if ok := ta.AssertBinding(fakeObj); !ok { | ||
t.Error("want assert binding true but not") | ||
} | ||
if ok := ta.AssertRender(fakeObj); !ok { | ||
t.Error("want assert render true but not") | ||
} | ||
} | ||
|
||
func TestAssertBinding(t *testing.T) { | ||
fakeObj := new(fakeObjectType) | ||
if ok := AssertBinding(fakeObj); ok { | ||
t.Error("want assert binding false but not") | ||
} | ||
if ok := AssertRender(fakeObj); ok { | ||
t.Error("want assert render false but not") | ||
} | ||
} | ||
|
||
func TestRegisterType(t *testing.T) { | ||
fakeObj := new(fakeObjectType) | ||
RegisterType[fakeCtx]() | ||
if ok := AssertBinding(fakeObj); !ok { | ||
t.Error("want assert binding true but not") | ||
} | ||
if ok := AssertRender(fakeObj); !ok { | ||
t.Error("want assert render true but not") | ||
} | ||
} | ||
|
||
func TestRegisterType2(t *testing.T) { | ||
fakeObj := new(fakeObjectType2) | ||
RegisterType2[fakeBindingCtx, fakeRenderCtx]() | ||
if ok := AssertBinding(fakeObj); !ok { | ||
t.Error("want assert binding true but not") | ||
} | ||
if ok := AssertRender(fakeObj); !ok { | ||
t.Error("want assert render true but not") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2023 Michael Li <alimy@gility.net>. All rights reserved. | ||
// Use of this source code is governed by Apache License 2.0 that | ||
// can be found in the LICENSE file. | ||
|
||
package engine_chi | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/alimy/mir/v4" | ||
"github.com/alimy/mir/v4/assert" | ||
) | ||
|
||
func init() { | ||
assert.Register(typeAssertor{}) | ||
} | ||
|
||
type Binding interface { | ||
Bind(*http.Request) mir.Error | ||
} | ||
|
||
type Render interface { | ||
Render(http.ResponseWriter) | ||
} | ||
|
||
type typeAssertor struct{} | ||
|
||
func (typeAssertor) AssertBinding(obj any) bool { | ||
_, ok := obj.(Binding) | ||
return ok | ||
} | ||
|
||
func (typeAssertor) AssertRender(obj any) bool { | ||
_, ok := obj.(Render) | ||
return ok | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/alimy/mir/engine/chi/v4 | ||
|
||
go 1.19 | ||
|
||
require github.com/alimy/mir/v4 v4.0.0-alpha.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/alimy/mir/v4 v4.0.0-alpha.7 h1:ysAg2U3MDLj9QMB6lDqqGaz8uWwlq/ndDs9gmFceZWw= | ||
github.com/alimy/mir/v4 v4.0.0-alpha.7/go.mod h1:d58dBvw2KImcVbAUANrciEV/of0arMNsI9c/5UNCMMc= |
Oops, something went wrong.