Skip to content

Commit

Permalink
mirc adapt go-mir v4
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Jul 11, 2023
1 parent 55f860e commit 9c56a16
Show file tree
Hide file tree
Showing 185 changed files with 3,451 additions and 4,496 deletions.
10 changes: 10 additions & 0 deletions assert/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ type Binding[T any] interface {
Bind(T) mir.Error
}

// Binding2[R, P] binding interface for custom T context
type Binding2[R, P any] interface {
Bind(R, P) mir.Error
}

// Render[T] render interface for custom T context
type Render[T any] interface {
Render(T)
Expand All @@ -46,6 +51,11 @@ func RegisterType2[B, R any]() {
_typeAssertor = anyTypeAssertor2[B, R]{}
}

// RegisterType2[B, P, R] register custom TypeAssertor to assert Binding[B]/Render[R] interface
func RegisterType3[B, P, R any]() {
_typeAssertor = anyTypeAssertor3[B, P, 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 @@ -10,6 +10,9 @@ type anyTypeAssertor[T any] struct{}
// anyTypeAssertor2 a common type assert for type B(Binding) and R(Render)
type anyTypeAssertor2[B, R any] struct{}

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

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

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

func (anyTypeAssertor3[B, P, R]) AssertRender(obj any) bool {
_, ok := obj.(Render[R])
return ok
}
7 changes: 7 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ func AssertType2[B, R any]() Option {
})
}

// AssertType3[B, P, R] register assert.TypeAssertor for custom B(Binding)/P(Params) and R(Render) type
func AssertType3[B, P, R any]() Option {
return optFunc(func(_opts *InitOpts) {
assert.RegisterType3[B, P, 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
2 changes: 1 addition & 1 deletion docs/content.en/docs/quick-start/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Mir is a toolkit to develop RESTful API backend service like develop service of

## Generate a simple template project
```bash
% go install github.com/alimy/mir/mirc/v3@latest
% go install github.com/alimy/mir/mirc/v4@latest
% mirc new -h
create template project

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: "Generate Template Project"

## Generate Template Project
```bash
% go install github.com/alimy/mir/mirc/v3@latest
% go install github.com/alimy/mir/mirc/v4@latest
% mirc new -h
create template project

Expand Down
2 changes: 1 addition & 1 deletion docs/content.zh/docs/quck-start/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Mir 是一套提供类似gRPC服务开发体验的快速开发RESTful API后端

## 生成样板项目
```bash
% go install github.com/alimy/mir/mirc/v3@latest
% go install github.com/alimy/mir/mirc/v4@latest
% mirc new -h
create template project

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: "生成样板项目"

## 生成样板项目
```bash
% go get github.com/alimy/mir/mirc/v3@latest
% go get github.com/alimy/mir/mirc/v4@latest
% mirc new -h
create template project

Expand Down
3 changes: 2 additions & 1 deletion engine/httprouter/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (

"github.com/alimy/mir/v4"
"github.com/alimy/mir/v4/assert"
"github.com/julienschmidt/httprouter"
)

func init() {
assert.Register(typeAssertor{})
}

type Binding interface {
Bind(*http.Request) mir.Error
Bind(*http.Request, httprouter.Params) mir.Error
}

type Render interface {
Expand Down
5 changes: 4 additions & 1 deletion engine/httprouter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/alimy/mir/engine/httprouter/v4

go 1.19

require github.com/alimy/mir/v4 v4.0.0-alpha.8
require (
github.com/alimy/mir/v4 v4.0.0-alpha.8
github.com/julienschmidt/httprouter v1.3.0
)
2 changes: 2 additions & 0 deletions engine/httprouter/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/alimy/mir/v4 v4.0.0-alpha.8 h1:nYhkbDosfDa9lyzfrAv/axbb8leUISHhCMi1F76c2PE=
github.com/alimy/mir/v4 v4.0.0-alpha.8/go.mod h1:d58dBvw2KImcVbAUANrciEV/of0arMNsI9c/5UNCMMc=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
2 changes: 1 addition & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/alimy/mir-example/v4

go 1.18
go 1.19

require (
github.com/alimy/mir/v4 v4.0.0-alpha.4
Expand Down
2 changes: 1 addition & 1 deletion examples/mirc/auto/api/site.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions examples/mirc/auto/api/v1/admin.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 0 additions & 13 deletions examples/mirc/auto/api/v1/site.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/mirc/auto/api/v2/site.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/mirc/auto/api/v3/site.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions internal/generator/templates/httprouter_iface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import (
)
{{- if .DeclareCoreInterface }}
type _binding_ interface {
Bind(*http.Request) mir.Error
Bind(*http.Request, httprouter.Params) mir.Error
}

type _render_ interface {
Render(http.ResponseWriter)
}

type _default_ interface {
Bind(*http.Request, any) mir.Error
Bind(*http.Request, httprouter.Params, any) mir.Error
Render(http.ResponseWriter, any, mir.Error)
}
{{- end }}
Expand Down Expand Up @@ -52,9 +52,9 @@ func Register{{.TypeName}}Servant(router *httprouter.Router, s {{.TypeName}}) {
req := new({{.InName}})
{{if .IsBindIn -}}
var bv _binding_ = req
if err := bv.Bind(hr); err != nil {
if err := bv.Bind(hr, p); err != nil {
{{- else -}}
if err := s.Bind(hr, req); err != nil {
if err := s.Bind(hr, p, req); err != nil {
{{- end }}
s.Render(rw, nil, err)
return
Expand Down Expand Up @@ -89,9 +89,9 @@ func Register{{.TypeName}}Servant(router *httprouter.Router, s {{.TypeName}}) {
req := new({{.InName}})
{{if .IsBindIn -}}
var bv _binding_ = req
if err := bv.Bind(hr); err != nil {
if err := bv.Bind(hr, p); err != nil {
{{- else -}}
if err := s.Bind(hr, req); err != nil {
if err := s.Bind(hr, p, req); err != nil {
{{- end }}
s.Render(rw, nil, err)
return
Expand All @@ -116,7 +116,8 @@ func Register{{.TypeName}}Servant(router *httprouter.Router, s {{.TypeName}}) {
{{range .AnyHttpMethods}} router.Handle("{{.}}", "{{joinPath $.Group $field.Path}}", h)
{{end}} }
{{end}}
{{end}}}
{{- end -}}
}

{{ $unimplementedServant := print "Unimplemented" .TypeName "Servant" }}
// {{$unimplementedServant}} can be embedded to have forward compatible implementations.
Expand Down
3 changes: 2 additions & 1 deletion internal/generator/templates/mux_iface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ func Register{{.TypeName}}Servant(e *mux.Router, s {{.TypeName}}) {
}).Methods({{.HttpMethodArgs}}){{if notEmptyStr .Host}}.
Host("{{.Host}}"){{end}}{{if valideQuery .Queries}}.
Queries({{inflateQuery .Queries}}){{end}}{{end}}
{{end}}}
{{- end -}}
}

{{ $unimplementedServant := print "Unimplemented" .TypeName "Servant" }}
// {{$unimplementedServant}} can be embedded to have forward compatible implementations.
Expand Down
2 changes: 1 addition & 1 deletion mirc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fmt:
.PHONY: check-all
check-all: check-debug-all

STYLES = chi hertz echo fiber fiber-v2 gin httprouter iris macaron mux
STYLES = chi hertz echo fiber gin httprouter iris macaron mux
.PHONY: check-debug-all
check-debug-all:
@for target in $(STYLES); do \
Expand Down
5 changes: 3 additions & 2 deletions mirc/templates/chi/go.mod.tmpl
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module {{ .PkgName }}

go 1.18
go 1.19

require (
github.com/alimy/mir/v4 v3.2.1
github.com/alimy/mir/v4 v4.0.0-beta.1
github.com/go-chi/chi/v5 v5.0.8
)

{{if notEmptyStr .MirPkgName }}
replace (
github.com/alimy/mir/v4 => {{ .MirPkgName }}
Expand Down
4 changes: 2 additions & 2 deletions mirc/templates/chi/go.sum.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
github.com/alimy/mir/v4 v3.2.1 h1:vnAHdqzMccP8C0DDyY3hAV50cVNBfDopgcyvv8TL9L4=
github.com/alimy/mir/v4 v3.2.1/go.mod h1:ybhT2ijOiDn0lLwWzIY6vXdv+uzZrctS7VFfczcXBWU=
github.com/alimy/mir/v4 v4.0.0-beta.1 h1:DEL1zr/SdN71Jz/xbF0RTXNN2GdUkQtb1jAAFwJG3bU=
github.com/alimy/mir/v4 v4.0.0-beta.1/go.mod h1:d58dBvw2KImcVbAUANrciEV/of0arMNsI9c/5UNCMMc=
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
Loading

0 comments on commit 9c56a16

Please sign in to comment.