Skip to content

Commit

Permalink
add custom field chain support, just support gin engine now.
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed May 23, 2023
1 parent fbad9bd commit f7371ac
Show file tree
Hide file tree
Showing 20 changed files with 711 additions and 144 deletions.
47 changes: 34 additions & 13 deletions core/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

var (
VerInfo = &VersionInfo{
MirVer: "v3.1.1",
MirVer: "v3.2.0",
}
)

Expand All @@ -32,18 +32,19 @@ type VersionInfo struct {

// FieldDescriptor field Descriptor info
type FieldDescriptor struct {
Imports map[string]string
PkgPath string
Host string
Path string
Queries []string
HttpMethods []string
IsAnyMethod bool
In reflect.Type
Out reflect.Type
InOuts []reflect.Type
MethodName string
Comment string // not support now so always empty
Imports map[string]string
PkgPath string
Host string
Path string
Queries []string
HttpMethods []string
IsAnyMethod bool
IsFieldChain bool
In reflect.Type
Out reflect.Type
InOuts []reflect.Type
MethodName string
Comment string // not support now so always empty
}

// IfaceDescriptor interface Descriptor info
Expand Down Expand Up @@ -183,6 +184,26 @@ func (d *IfaceDescriptor) AllInOuts() []reflect.Type {
return inouts
}

// ChainFields return field chains
func (d *IfaceDescriptor) ChainFields() (fields []*FieldDescriptor) {
for _, f := range d.Fields {
if f.IsFieldChain {
fields = append(fields, f)
}
}
return
}

// IsUseFieldChain whether use field chain
func (d *IfaceDescriptor) IsUseFieldChain() bool {
for _, f := range d.Fields {
if f.IsFieldChain {
return true
}
}
return false
}

// IsUseBinding return whether use binding interface
func (d *IfaceDescriptor) IsUseBinding() bool {
for _, f := range d.Fields {
Expand Down
5 changes: 3 additions & 2 deletions docs/tutorials.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,11 @@ type User struct {
}
```
* `func(...)...` 表示接口的方法定义;函数中的参数与返回值有着特殊意义:
* 函数至少有一个参数,可以有多个参数;
* 函数参数的类型限定为 `mir.(Get/Put/Post/Delete/Head/Patch/Trace/Connect/Options/Any)`类型、Go `struct`类型;
* 函数可以有多个参数,也可无参数,无参数时表示将这个接口注册为所有HTTP Method handler;
* 函数参数的类型限定为 `mir.(Get/Put/Post/Delete/Head/Patch/Trace/Connect/Options/Any/Chain)`类型、Go `struct`类型;
* Go `struct`类型作为函数参数只能放置在最后一个参数位置,表示接口需要这个`struct`类型表示的参数类型的从http request中Binding后的结果对象作为请求参数;
* 函数参数中的非最后一个参数,可以有多个,类型限定为`mir.(Get/Put/Post/Delete/Head/Patch/Trace/Connect/Options/Any)`类型;表示的意思是这个接口将注册为相应的HTTP Method handler,比如`mir.Post`表示将这个接口注册为 HTTP Method 为`POST` 的handler `router.Handle("POST", "/login/", func(...){...})`; `mir.Any` 表示将这个接口注册为所有HTTP Method handler;
* 函数参数中如果有`Chain`表示这个接口有自定义的HTTP引擎中间件的方法,比如gin引擎的`gin.HandlersChain`,会与接口一起注册,目前仅支持`gin`引擎;
* 函数返回值至多只有一个,可以没有,也可以有一个,限定类型为Go `struct`类型;
* 函数中的最后一个参数和返回值,如果有的话,限定类型为Go `struct`类型,并且可以说与接口定义的同一个包中的结构体,也可以是其他包中的结构体;
Expand Down
4 changes: 4 additions & 0 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ require (
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace (
github.com/alimy/mir/v3 => ../
)
52 changes: 44 additions & 8 deletions examples/mirc/auto/api/site.go

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

84 changes: 75 additions & 9 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.

Loading

0 comments on commit f7371ac

Please sign in to comment.