Skip to content

Commit

Permalink
add Chi engine support and optimize type assert logic
Browse files Browse the repository at this point in the history
* add Chi engine support
* add assert.RegisterType2[B, R]() function
  • Loading branch information
alimy committed Jun 30, 2023
1 parent 88ae894 commit 47e8c12
Show file tree
Hide file tree
Showing 8 changed files with 286 additions and 89 deletions.
7 changes: 6 additions & 1 deletion assert/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,16 @@ func Register(ta TypeAssertor) {
_typeAssertor = ta
}

// RegisterType register custom TypeAssertor to assert Binding[T]/Render[T] interface
// RegisterType[T] register custom TypeAssertor to assert Binding[T]/Render[T] interface
func RegisterType[T any]() {
_typeAssertor = anyTypeAssertor[T]{}
}

// RegisterType2[B, R] register custom TypeAssertor to assert Binding[B]/Render[R] interface
func RegisterType2[B, R any]() {
_typeAssertor = anyTypeAssertor2[B, R]{}
}

// AssertBinding assert Binding interface for obj
func AssertBinding(obj any) bool {
return _typeAssertor.AssertBinding(obj)
Expand Down
13 changes: 13 additions & 0 deletions assert/assert_any.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ package assert
// anyTypeAssertor a common type assert for type T
type anyTypeAssertor[T any] struct{}

// anyTypeAssertor2 a common type assert for type B(Binding) and R(Render)
type anyTypeAssertor2[B, R any] struct{}

func (anyTypeAssertor[T]) AssertBinding(obj any) bool {
_, ok := obj.(Binding[T])
return ok
Expand All @@ -16,3 +19,13 @@ func (anyTypeAssertor[T]) AssertRender(obj any) bool {
_, ok := obj.(Render[T])
return ok
}

func (anyTypeAssertor2[B, R]) AssertBinding(obj any) bool {
_, ok := obj.(Binding[B])
return ok
}

func (anyTypeAssertor2[B, R]) AssertRender(obj any) bool {
_, ok := obj.(Render[R])
return ok
}
91 changes: 91 additions & 0 deletions assert/assert_test.go
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")
}
}
9 changes: 8 additions & 1 deletion core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,20 @@ func SinkPath(path string) Option {
})
}

// AssertType register assert.TypeAssertor for custom T type
// AssertType[T] register assert.TypeAssertor for custom T type
func AssertType[T any]() Option {
return optFunc(func(_opts *InitOpts) {
assert.RegisterType[T]()
})
}

// AssertType2[B, R] register assert.TypeAssertor for custom B(Binding) and R(Render) type
func AssertType2[B, R any]() Option {
return optFunc(func(_opts *InitOpts) {
assert.RegisterType2[B, R]()
})
}

// WatchCtxDone set generator whether watch context done when Register Servants in
// generated code. default watch context done.
func WatchCtxDone(enable bool) Option {
Expand Down
36 changes: 36 additions & 0 deletions engine/chi/assert.go
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
}
5 changes: 5 additions & 0 deletions engine/chi/go.mod
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
2 changes: 2 additions & 0 deletions engine/chi/go.sum
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=
Loading

0 comments on commit 47e8c12

Please sign in to comment.