Skip to content

Commit

Permalink
docs: add AddMethod doc (go-gorm#483)
Browse files Browse the repository at this point in the history
* feat: add AddMethod to doc

* fix:add Chinese doc and a little fix
  • Loading branch information
idersec authored Jun 22, 2022
1 parent 4f25f58 commit 54c9342
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
18 changes: 18 additions & 0 deletions README.ZH_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,24 @@ FieldRelate // specify relationship with other tables
FieldRelateModel // specify relationship with exist models
```

**生成结构体绑定自定义方法**
```Go
type User struct{
ID int32
}
func (u *User)IsEmpty()bool{
if m == nil {
return true
}
return m.ID == 0
}
user := User{}
// 可以直接添加一个绑定了结构体的方法
g.GenerateModel("people").AddMethod(user.IsEmpty)
// 也可以传入一个结构体,会将这个结构体上绑定的所有方法绑定到新生成的结构体上
g.GenerateModel("people").AddMethod(User{})
```

#### <span id="data-mapping">类型映射</span>

指定你期望的数据映射关系,如自定义数据库字段类型和 Go 类型的映射关系。
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,24 @@ FieldRelate // specify relationship with other tables
FieldRelateModel // specify relationship with exist models
```

Generate model bind custom method
```Go
type User struct{
ID int32
}
func (u *User)IsEmpty()bool{
if m == nil {
return true
}
return m.ID == 0
}
user := User{}
// add custom method to generated model struct
g.GenerateModel("people").AddMethod(user.IsEmpty)
// also you can input a struct,will bind all method
g.GenerateModel("people").AddMethod(User{})
```

#### Data Mapping

Specify data mapping relationship to be whatever you want.
Expand Down
2 changes: 1 addition & 1 deletion helper/clause.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func trimLeft(input string) string {
switch {
case strings.HasPrefix(lowercase, "and "):
return input[4:]
case strings.HasPrefix(lowercase, "or"):
case strings.HasPrefix(lowercase, "or "):
return input[3:]
case strings.HasPrefix(lowercase, "xor "):
return input[4:]
Expand Down
8 changes: 4 additions & 4 deletions internal/check/struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestBaseStruct_AddMethod(t *testing.T) {
Type: "bool",
},
},
Body: "{\n\tif u == nil {\n\t\treturn true\n\t}\n\n\treturn u.ID == 0\n}\n",
Body: "{\n\tif u == nil {\n\t\treturn true\n\t}\n\n\treturn u.ID == 0\n}",
},
},
},
Expand All @@ -98,7 +98,7 @@ func TestBaseStruct_AddMethod(t *testing.T) {
Type: "bool",
},
},
Body: "{\n\tif u == nil {\n\t\treturn true\n\t}\n\n\treturn u.ID == 0\n}\n",
Body: "{\n\tif u == nil {\n\t\treturn true\n\t}\n\n\treturn u.ID == 0\n}",
},
{
BaseStruct: parser.Param{
Expand All @@ -114,7 +114,7 @@ func TestBaseStruct_AddMethod(t *testing.T) {
Type: "string",
},
},
Body: "{\n\tu.Name = name\n}\n",
Body: "{\n\tu.Name = name\n}",
},
{
BaseStruct: parser.Param{
Expand All @@ -129,7 +129,7 @@ func TestBaseStruct_AddMethod(t *testing.T) {
Type: "string",
},
},
Body: "{\n\treturn strings.ToLower(u.Name)\n}\n",
Body: "{\n\treturn strings.ToLower(u.Name)\n}",
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func getBody(fileName string, start, end int) string {
return "{}"
}

return string(f1[start-1 : end])
return string(f1[start-1 : end-1])
}

// LoadMethods ast read file get diy method
Expand Down

0 comments on commit 54c9342

Please sign in to comment.