Skip to content

Commit

Permalink
docs: introduce generate mode
Browse files Browse the repository at this point in the history
  • Loading branch information
tr1v3r committed Jun 29, 2022
1 parent a18d9be commit 2221495
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
15 changes: 15 additions & 0 deletions README.ZH_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- [项目路径](#project-directory)
- [API 示例](#api-examples)
- [生成](#generate)
- [生成模式](#generate-mode)
- [模型生成](#generate-model)
- [类型映射](#data-mapping)
- [字段表达式](#field-expression)
Expand Down Expand Up @@ -237,6 +238,20 @@ demo

### <span id="generate">生成</span>

#### <span id="generate-mode">生成模式</span>

```go
g := gen.NewGenerator(gen.Config{
...
Mode: gen.WithoutContext|gen.WithDefaultQuery|gen.WithQueryInterface,
...
})
```

- `WithDefaultQuery` 生成默认查询结构体(作为全局变量使用)
- `WithoutContext` 生成没有context调用限制的代码供查询
- `WithQueryInterface` 生成interface形式的查询代码(可导出)

#### <span id="generate-model">模型生成</span>

```go
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Gen: Friendly & Safer [GORM](https://github.com/go-gorm/gorm) powered by Code Ge
- [Project Directory](#project-directory)
- [API Examples](#api-examples)
- [Generate](#generate)
- [Generate Mode](#generate-mode)
- [Generate Model](#generate-model)
- [Data Mapping](#data-mapping)
- [Field Expression](#field-expression)
Expand Down Expand Up @@ -233,6 +234,20 @@ demo

### Generate

#### Generate Mode

```go
g := gen.NewGenerator(gen.Config{
...
Mode: gen.WithoutContext|gen.WithDefaultQuery|gen.WithQueryInterface,
...
})
```

- `WithDefaultQuery` generate default query struct
- `WithoutContext` generate code without context constrain
- `WithQueryInterface` generate interface instead of struct for querying

#### Generate Model

```go
Expand Down
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const (
// WithoutContext generate code without context constrain
WithoutContext

// WithQueryIface generate code with exported interface object
WithQueryIface
// WithQueryInterface generate code with exported interface object
WithQueryInterface
)

// Config generator's basic configuration
Expand Down
4 changes: 2 additions & 2 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func (g *Generator) generateSingleQueryFile(data *genInfo) (err error) {
return err
}

data.BaseStruct = data.BaseStruct.IfaceMode(g.judgeMode(WithQueryIface))
data.BaseStruct = data.BaseStruct.IfaceMode(g.judgeMode(WithQueryInterface))

structTmpl := tmpl.TableQueryStructWithContext
if g.judgeMode(WithoutContext) {
Expand All @@ -386,7 +386,7 @@ func (g *Generator) generateSingleQueryFile(data *genInfo) (err error) {
return err
}

if g.judgeMode(WithQueryIface) {
if g.judgeMode(WithQueryInterface) {
err = render(tmpl.TableQueryIface, &buf, data)
if err != nil {
return err
Expand Down

0 comments on commit 2221495

Please sign in to comment.