Skip to content

Commit

Permalink
add engine support for option for generate interface method code tha…
Browse files Browse the repository at this point in the history
…t contain HTTP Request Context.
  • Loading branch information
alimy committed Feb 9, 2024
1 parent 9d1f2e5 commit b777fba
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 161 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
All notable changes to paopao-ce are documented in this file.

## 4.2.0 ([`dev`](https://github.com/alimy/mir))
TODO;
- add `gin` engine support for `UseRequstContext` option for generate interface method code that contain HTTP Request Context.

## 4.1.0
### Added
Expand Down
26 changes: 19 additions & 7 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,18 @@ type InitOpts struct {
EnginePkgName string
EngineImportAlias string
WatchCtxDone bool
UseRequestCtx bool
NoneQuery bool
Cleanup bool
}

// ParserOpts used for initial parser
type ParserOpts struct {
EngineInfo *EngineInfo
DefaultTag string
WatchCtxDone bool
NoneQuery bool
EngineInfo *EngineInfo
DefaultTag string
WatchCtxDone bool
UseRequestCtx bool
NoneQuery bool
}

// GeneratorOpts used for initial generator
Expand Down Expand Up @@ -95,9 +97,10 @@ func (opts Options) InitOpts() *InitOpts {
// ParserOpts return a ParserOpts instance
func (opts *InitOpts) ParserOpts() *ParserOpts {
return &ParserOpts{
DefaultTag: opts.DefaultTag,
WatchCtxDone: opts.WatchCtxDone,
NoneQuery: opts.NoneQuery,
DefaultTag: opts.DefaultTag,
WatchCtxDone: opts.WatchCtxDone,
UseRequestCtx: opts.UseRequestCtx,
NoneQuery: opts.NoneQuery,
EngineInfo: &EngineInfo{
PkgName: opts.EnginePkgName,
ImportAlias: opts.EngineImportAlias,
Expand Down Expand Up @@ -292,6 +295,14 @@ func WatchCtxDone(enable bool) Option {
})
}

// UseRequestContext use HTTP Request Context in generated interface method code.
// Default is disable use HTTP Request Context.
func UseRequestContext() Option {
return optFunc(func(opts *InitOpts) {
opts.UseRequestCtx = true
})
}

// Cleanup set generator cleanup out first when re-generate code
func Cleanup(enable bool) Option {
return optFunc(func(opts *InitOpts) {
Expand Down Expand Up @@ -399,6 +410,7 @@ func defaultInitOpts() *InitOpts {
SinkPath: ".auto",
DefaultTag: "mir",
WatchCtxDone: true,
UseRequestCtx: false,
Cleanup: true,
}
}
48 changes: 31 additions & 17 deletions core/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

var (
VerInfo = &VersionInfo{
MirVer: "v4.1.0",
MirVer: "v4.2.0",
}
)

Expand All @@ -33,22 +33,23 @@ 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
IsFieldChain bool
IsUseContext bool
IsBindIn bool
IsRenderOut 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
IsUseContext bool
IsUseRequestContext bool
IsBindIn bool
IsRenderOut 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 All @@ -65,6 +66,7 @@ type IfaceDescriptor struct {
EngineInfo *EngineInfo
VerInfo *VersionInfo
WatchCtxDone bool
UseRequestCtx bool
DeclareCoreInterface bool // whether need to declare core interface, default is false
}

Expand Down Expand Up @@ -238,6 +240,18 @@ func (d *IfaceDescriptor) IsUseBinding() bool {
return false
}

func (d *IfaceDescriptor) IsUseRequestContext() bool {
if !d.UseRequestCtx {
return false
}
for _, f := range d.Fields {
if f.IsUseRequestContext {
return true
}
}
return false
}

// BindingFields return Binding's fields
func (d *IfaceDescriptor) BindingFields() (fields []*FieldDescriptor) {
for _, f := range d.Fields {
Expand Down
4 changes: 3 additions & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/alimy/mir-example/v4
go 1.19

require (
github.com/alimy/mir/v4 v4.1.0
github.com/alimy/mir/v4 v4.2.0
github.com/gin-gonic/gin v1.9.1
)

Expand Down Expand Up @@ -35,3 +35,5 @@ require (
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/alimy/mir/v4 => ../
45 changes: 23 additions & 22 deletions examples/mirc/auto/api/site.go

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

27 changes: 14 additions & 13 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.

Loading

0 comments on commit b777fba

Please sign in to comment.