forked from xorm/reverse
A flexsible and powerful command line tool to convert database to codes
8c0b7497de
目的和变更概述 1. 项目目录结构调整,贴近但不完全照搬 [golang-standards/project-layout](https://github.com/golang-standards/project-layout),提供更清晰的目录结构 2. language 结构改为 interface 以提供更好的扩展性和可读性;language 接口支持绑定 target,便于 language 接口提供的 FuncMap 等工具能根据 target 的配置来改变自己的行为,扩展性更好 3. 分离配置结构和反序列化方法到单独的包 `pkg/conf` ,使目录结构和代码更清晰可读 4. 提供多 document 的 yaml 支持,方便要处理多个数据源的场景 5. 添加 underscore ,没什么特别的理由,主要是这套函数式的工具真的很好用,能提高代码可读性;但 underscore 这个库也有问题,因为没泛型全靠 reflect 一把梭,underscore 内部报 reflect 相关错误会比较难排查处理。 6. 添加 `column_name` 选项,生成字段 `ID int64 `xorm:"id pk autoincr"` 这样的 tag,显式建立结构字段名和列名的映射关系。参见 [#27](xorm/reverse#27) Co-authored-by: liuweiqing <liuweiqing@donview.cn> Co-authored-by: weakptr <weakptr@outlook.com> Co-authored-by: weakptr <weak_ptr@outlook.com> Reviewed-on: xorm/reverse#28 Co-authored-by: weakptr <weakptr@noreply.gitea.io> Co-committed-by: weakptr <weakptr@noreply.gitea.io> |
||
---|---|---|
cmd | ||
example | ||
models | ||
pkg | ||
testdata | ||
vendor | ||
.drone.yml | ||
.gitignore | ||
go.mod | ||
go.sum | ||
LICENSE | ||
main.go | ||
README_CN.md | ||
README.md |
Reverse
A flexsible and powerful command line tool to convert database to codes.
Installation
go get xorm.io/reverse
Usage
reverse -f example/custom.yml
Configuration File
How does the simplest configuration file look like?
kind: reverse
name: mydb
source:
database: sqlite3
conn_str: '../testdata/test.db'
targets:
- type: codes
language: golang
output_dir: ../models
A language
defines some default configuration items, also you can define all yourselves.
kind: reverse
name: mydb
source:
database: sqlite
conn_str: ../testdata/test.db
targets:
- type: codes
include_tables: # tables included, you can use **
- a
- b
exclude_tables: # tables excluded, you can use **
- c
table_mapper: snake # how table name map to class or struct name
column_mapper: snake # how column name map to class or struct field name
table_prefix: "" # table prefix
multiple_files: true # generate multiple files or one
language: golang
template: | # template for code file, it has higher perior than template_path
package models
{{$ilen := len .Imports}}
{{if gt $ilen 0}}
import (
{{range .Imports}}"{{.}}"{{end}}
)
{{end}}
{{range .Tables}}
type {{TableMapper .Name}} struct {
{{$table := .}}
{{range .ColumnsSeq}}{{$col := $table.GetColumn .}} {{ColumnMapper $col.Name}} {{Type $col}} `{{Tag $table $col}}`
{{end}}
}
func (m *{{TableMapper .Name}}) TableName() string {
return "{{$table.Name}}"
}
{{end}}
template_path: ./template/goxorm.tmpl # template path for code file, it has higher perior than template field on language
output_dir: ./models # code output directory
Template Funcs
- UnTitle: Convert first charator of the word to lower.
- Upper: Convert word to all upper.
- TableMapper: Mapper method to convert table name to class/struct name.
- ColumnMapper: Mapper method to convert column name to class/struct field name.
Golang Template Funcs
- Type: return column's golang type
- Tag: return golang struct tag for column
Template Vars
- Tables: All tables.
- Imports: All imports needed.