Skip to content

Commit

Permalink
feat: refactor interceptor (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
sysulq committed Apr 2, 2024
1 parent f8d8415 commit 93c0acc
Show file tree
Hide file tree
Showing 54 changed files with 586 additions and 358 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ func (f *foo) Stop(context.Context) error {
Kod has built-in common interceptors, and components can implement the following methods to inject these interceptors into component methods:

```go
func (f *foo) Interceptors() []kod.Interceptor {
return []kod.Interceptor{
func (f *foo) Interceptors() []interceptor.Interceptor {
return []interceptor.Interceptor{
kmetric.New(),
ktrace.New(),
}
Expand Down
4 changes: 2 additions & 2 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ func (f *foo) Stop(context.Context) error {
Kod 内置了常用的拦截器,组件可以实现以下拦截器方法,将这些拦截器注入到组件方法中:

```go
func (f *foo) Interceptors() []kod.Interceptor {
return []kod.Interceptor{
func (f *foo) Interceptors() []interceptor.Interceptor {
return []interceptor.Interceptor{
kmetric.New(),
ktrace.New(),
}
Expand Down
10 changes: 9 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,12 @@ tasks:
- cd tests && go mod tidy
- cd examples && go mod tidy
- cd ext && go mod tidy
- cd interceptor && go mod tidy
- cd interceptor && go mod tidy

release:
vars:
VERSION: v0.6.0
cmds:
- git tag {{.VERSION}}
- git tag interceptor/{{.VERSION}}
- git tag ext/{{.VERSION}}
12 changes: 6 additions & 6 deletions cmd/kod/internal/generate_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,9 +676,9 @@ func (g *generator) generateRegisteredComponents(p printFn) {

g.interceptor()
localStubFn := fmt.Sprintf(`func(ctx context.Context, info *kod.LocalStubFnInfo) any {
var interceptors []kod.Interceptor
if h, ok := info.Impl.(interface{ Interceptors() []kod.Interceptor }); ok {
interceptors = h.Interceptors()
interceptors := info.Interceptors
if h, ok := info.Impl.(interface{ Interceptors() []interceptor.Interceptor }); ok {
interceptors = append(interceptors, h.Interceptors()...)
}
%s
Expand Down Expand Up @@ -751,7 +751,7 @@ func (g *generator) generateLocalStubs(p printFn) {
p(`type %s struct{`, stub)
p(` impl %s`, g.componentRef(comp))
p(` name string`)
p(` interceptor kod.Interceptor`)
p(` interceptor interceptor.Interceptor`)
p(`}`)

p(``)
Expand Down Expand Up @@ -788,14 +788,14 @@ func (g *generator) generateLocalStubs(p printFn) {
}
`, g.returnsList(mt), m.Name(), g.argList(comp, mt))

p(`call := func(ctx context.Context, info kod.CallInfo, req, res []any) (err error) {`)
p(`call := func(ctx context.Context, info interceptor.CallInfo, req, res []any) (err error) {`)

p(` %s s.impl.%s(%s)
%sreturn
}
`, g.returnsList(mt), m.Name(), g.argList(comp, mt), g.setReturnsList(mt))

p(`info := kod.CallInfo {
p(`info := interceptor.CallInfo {
Impl: s.impl,
Component: s.name,
FullMethod: "%s.%s",
Expand Down
20 changes: 12 additions & 8 deletions examples/app/helloworld/kod_gen.go

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

10 changes: 6 additions & 4 deletions examples/cmd/app/helloworld/kod_gen.go

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

10 changes: 6 additions & 4 deletions examples/config/kod_gen.go

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

18 changes: 10 additions & 8 deletions examples/domain/ranklist/kod_gen.go

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

5 changes: 3 additions & 2 deletions examples/domain/ranklist/ranklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/go-kod/kod"
"github.com/go-kod/kod/ext/client/kredis"
"github.com/go-kod/kod/interceptor"
"github.com/go-kod/kod/interceptor/kvalidate"
"github.com/redis/go-redis/v9"
)
Expand All @@ -27,8 +28,8 @@ func (i *impl) Init(ctx context.Context) error {
return nil
}

func (i *impl) Interceptors() []kod.Interceptor {
return []kod.Interceptor{
func (i *impl) Interceptors() []interceptor.Interceptor {
return []interceptor.Interceptor{
kvalidate.Interceptor(),
}
}
Expand Down
18 changes: 10 additions & 8 deletions examples/domain/shorturl/kod_gen.go

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

6 changes: 3 additions & 3 deletions examples/domain/shorturl/short_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/go-kod/kod"
"github.com/go-kod/kod/ext/client/kredis"
"github.com/go-kod/kod/interceptor"
"github.com/go-kod/kod/interceptor/kvalidate"
"github.com/google/uuid"
)
Expand Down Expand Up @@ -45,7 +46,6 @@ type GenerateResponse struct {

// Generate generates a short url.
func (i *impl) Generate(ctx context.Context, req *GenerateRequest) (*GenerateResponse, error) {

short := i.uuid.String()
key := i.Config().Prefix + short
_, err := i.redis.SetEx(ctx, key, req.URL, req.Duration).Result()
Expand Down Expand Up @@ -80,8 +80,8 @@ func (i *impl) Get(ctx context.Context, req *GetRequest) (*GetResponse, error) {
}, nil
}

func (i *impl) Interceptors() []kod.Interceptor {
return []kod.Interceptor{
func (i *impl) Interceptors() []interceptor.Interceptor {
return []interceptor.Interceptor{
kvalidate.Interceptor(),
}
}
14 changes: 8 additions & 6 deletions examples/domain/snowflake/kod_gen.go

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

Loading

0 comments on commit 93c0acc

Please sign in to comment.