You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/tools/goctl/model/sqlmodel/gen.go

47 lines
895 B
Go

4 years ago
package sqlmodel
import (
"go/format"
"zero/core/logx"
)
type (
Column struct {
DataType string `json:"dataType"`
Name string `json:"name"`
Comment string `json:"comment"`
}
Table struct {
Name string `json:"table"`
PrimaryKey string `json:"primaryKey"`
Columns []*Column `json:"columns"`
}
)
func GenMysqlGoModel(req *Table, conditions []string) (string, error) {
resp, err := generateTypeModel(req.Name, req.Columns)
if err != nil {
return "", err
}
resp.PrimaryKey = req.PrimaryKey
s, err := NewStructExp(*resp)
if len(conditions) == 0 {
s.conditions = []string{resp.PrimaryKey}
} else {
s.conditions = conditions
}
if err != nil {
return "", err
}
result, err := s.genMysqlCRUD()
// code format
bts, err := format.Source([]byte(result))
if err != nil {
logx.Errorf("%+v", err)
return "", err
}
return string(bts), err
}