-
-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: feature generate from object (#407)
* chore(examples): only generate model * feat: generate from object * feat(generate): optimize generated tag * feat: remove verbose method * feat: optimize filename * feat: add FileName on Object
- Loading branch information
Showing
16 changed files
with
337 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package main | ||
|
||
import ( | ||
"gorm.io/gen" | ||
"gorm.io/gen/helper" | ||
) | ||
|
||
var detail, data helper.Object | ||
|
||
func init() { | ||
detail = &Demo{ | ||
structName: "Detail", | ||
fileName: "diy_data_detail", | ||
fields: []helper.Field{ | ||
&DemoField{ | ||
name: "Username", | ||
typ: "string", | ||
jsonTag: "username", | ||
comment: "用户名", | ||
}, | ||
&DemoField{ | ||
name: "Age", | ||
typ: "uint", | ||
jsonTag: "age", | ||
comment: "用户年龄", | ||
}, | ||
&DemoField{ | ||
name: "Phone", | ||
typ: "string", | ||
jsonTag: "phone", | ||
comment: "手机号", | ||
}, | ||
}, | ||
} | ||
|
||
data = &Demo{ | ||
structName: "Data", | ||
tableName: "data", | ||
fileName: "diy_data", | ||
fields: []helper.Field{ | ||
&DemoField{ | ||
name: "ID", | ||
typ: "uint", | ||
gormTag: "column:id;type:bigint unsigned;primaryKey;autoIncrement:true", | ||
jsonTag: "id", | ||
tag: `kms:"enc:aes"`, | ||
comment: "主键", | ||
}, | ||
&DemoField{ | ||
name: "UserInfo", | ||
typ: "[]Detail", | ||
jsonTag: "user_info", | ||
comment: "用户信息", | ||
}, | ||
&DemoField{ | ||
name: "Remark", | ||
typ: "json.RawMessage", | ||
gormTag: "column:detail", | ||
jsonTag: "remark", | ||
tag: `kms:"enc:aes"`, | ||
comment: "备注\n详细信息", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func main() { | ||
g := gen.NewGenerator(gen.Config{ | ||
OutPath: "/tmp/gentest/query", | ||
ModelPkgPath: "/tmp/gentest/demo", | ||
}) | ||
|
||
g.GenerateModelFrom(detail) | ||
|
||
g.ApplyBasic(g.GenerateModelFrom(data)) | ||
|
||
g.Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package main | ||
|
||
import "gorm.io/gen/helper" | ||
|
||
var _ helper.Object = new(Demo) | ||
|
||
type Demo struct { | ||
structName string | ||
tableName string | ||
fileName string | ||
fields []helper.Field | ||
} | ||
|
||
func (d *Demo) TableName() string { return d.tableName } | ||
func (d *Demo) StructName() string { return d.structName } | ||
func (d *Demo) FileName() string { return d.fileName } | ||
func (d *Demo) ImportPkgPaths() []string { return nil } | ||
func (d *Demo) Fields() []helper.Field { return d.fields } | ||
|
||
type DemoField struct { | ||
name string | ||
typ string | ||
gormTag string | ||
jsonTag string | ||
tag string | ||
comment string | ||
} | ||
|
||
func (f *DemoField) Name() string { return f.name } | ||
func (f *DemoField) Type() string { return f.typ } | ||
func (f *DemoField) GORMTag() string { return f.gormTag } | ||
func (f *DemoField) JSONTag() string { return f.jsonTag } | ||
func (f *DemoField) Tag() string { return f.tag } | ||
func (f *DemoField) Comment() string { return f.comment } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package main | ||
|
||
import ( | ||
"gorm.io/gen" | ||
"gorm.io/gen/examples/conf" | ||
"gorm.io/gen/examples/dal" | ||
) | ||
|
||
func init() { | ||
dal.DB = dal.ConnectDB(conf.MySQLDSN).Debug() | ||
|
||
prepare(dal.DB) // prepare table for generate | ||
} | ||
|
||
func main() { | ||
g := gen.NewGenerator(gen.Config{ | ||
OutPath: "/tmp/gentest/query", | ||
}) | ||
|
||
g.UseDB(dal.DB) | ||
|
||
g.GenerateAllTable() | ||
|
||
g.Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import ( | ||
"gorm.io/gorm" | ||
) | ||
|
||
// prepare table for test | ||
|
||
const mytableSQL = "CREATE TABLE IF NOT EXISTS `mytables` (" + | ||
" `ID` int(11) NOT NULL," + | ||
" `username` varchar(16) DEFAULT NULL," + | ||
" `age` int(8) NOT NULL," + | ||
" `phone` varchar(11) NOT NULL," + | ||
" INDEX `idx_username` (`username`)" + | ||
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;" | ||
|
||
func prepare(db *gorm.DB) { | ||
db.Exec(mytableSQL) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package conf | ||
|
||
const MySQLDSN = "root:local_maridb_test@tcp(localhost:3306)/test?charset=utf8mb4&parseTime=True" | ||
const MySQLDSN = "root:local_mysql_test@tcp(localhost:3306)/test?charset=utf8mb4&parseTime=True" | ||
|
||
const SQLiteDBName = "gen_sqlite.db" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package helper | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
type Object interface { | ||
TableName() string | ||
StructName() string | ||
FileName() string | ||
ImportPkgPaths() []string | ||
|
||
Fields() []Field | ||
} | ||
|
||
type Field interface { | ||
Name() string | ||
Type() string | ||
|
||
GORMTag() string | ||
JSONTag() string | ||
Tag() string | ||
|
||
Comment() string | ||
} | ||
|
||
func CheckObject(obj Object) error { | ||
if obj.StructName() == "" { | ||
return errors.New("Object's StructName() cannot be empty") | ||
} | ||
|
||
for _, field := range obj.Fields() { | ||
switch "" { | ||
case field.Name(): | ||
return fmt.Errorf("Object %s's Field.Name() cannot be empty", obj.StructName()) | ||
case field.Type(): | ||
return fmt.Errorf("Object %s's Field.Type() cannot be empty", obj.StructName()) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.