Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add query interface #870

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,17 @@ func (g *Generator) generateQueryFile() (err error) {
return err
}

if g.judgeMode(WithQueryInterface) {
queryInterfaceTmpl := tmpl.QueryInterfaceWithContext
if g.judgeMode(WithoutContext) {
queryInterfaceTmpl = tmpl.QueryInterfaceWithoutContext
}
err = render(queryInterfaceTmpl, &buf, g)
if err != nil {
return err
}
}

err = g.output(g.OutFile, buf.Bytes())
if err != nil {
return err
Expand Down
36 changes: 36 additions & 0 deletions internal/template/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,42 @@ func (q *QueryTx) RollbackTo(name string) error {

`

// QueryInterfaceWithContext interface method template with context
const QueryInterfaceWithContext = `
var _ IQuery = (*Query)(nil)

type IQuery interface{
{{range $name,$d :=.Data -}}
{{$d.ModelStructName}}Do(ctx context.Context) I{{$d.ModelStructName}}Do
{{end -}}
}

{{range $name,$d :=.Data -}}
func (q *Query) {{$d.ModelStructName}}Do(ctx context.Context) I{{$d.ModelStructName}}Do {
return q.{{$d.ModelStructName}}.WithContext(ctx)
}

{{end -}}
`

// QueryInterfaceWithoutContext interface method template without context
const QueryInterfaceWithoutContext = `
var _ IQuery = (*Query)(nil)

type IQuery interface{
{{range $name,$d :=.Data -}}
{{$d.ModelStructName}}Do() I{{$d.ModelStructName}}Do
{{end -}}
}

{{range $name,$d :=.Data -}}
func (q *Query) {{$d.ModelStructName}}Do() I{{$d.ModelStructName}}Do {
return &q.{{$d.ModelStructName}}.{{$d.QueryStructName}}Do
}

{{end -}}
`

// QueryMethodTest query method test template
const QueryMethodTest = `

Expand Down
30 changes: 30 additions & 0 deletions tests/.expect/dal_3/query/gen.go

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

30 changes: 30 additions & 0 deletions tests/.expect/dal_4/query/gen.go

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

13 changes: 9 additions & 4 deletions tests/.expect/dal_5/model/users.gen.go

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

10 changes: 10 additions & 0 deletions tests/.expect/dal_5/query/gen.go

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

2 changes: 2 additions & 0 deletions tests/.expect/dal_5/query/users.gen.go

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

13 changes: 9 additions & 4 deletions tests/.expect/dal_6/model/users.gen.go

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

10 changes: 10 additions & 0 deletions tests/.expect/dal_6/query/gen.go

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

2 changes: 2 additions & 0 deletions tests/.expect/dal_6/query/users.gen.go

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

5 changes: 2 additions & 3 deletions tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ go 1.16

require (
github.com/mattn/go-sqlite3 v1.14.16 // indirect
golang.org/x/tools v0.5.0 // indirect
gorm.io/driver/mysql v1.4.5
gorm.io/driver/mysql v1.5.1-0.20230509030346-3715c134c25b
gorm.io/driver/sqlite v1.4.4
gorm.io/gen v0.3.19
gorm.io/gorm v1.24.3
gorm.io/gorm v1.25.1-0.20230505075827-e61b98d69677
gorm.io/hints v1.1.1 // indirect
gorm.io/plugin/dbresolver v1.4.0
)
Expand Down