Add support for orm model generator
This commit is contained in:
parent
4263fcd035
commit
9a62740eed
2 changed files with 174 additions and 23 deletions
|
@ -14,38 +14,31 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
fnPatterns = map[string]string{
|
||||
fnTemplatePatterns = map[string]string{
|
||||
"all": "%s",
|
||||
"show": "%s_show",
|
||||
"add_update": "%s_add_update",
|
||||
}
|
||||
fnOrmPatterns = map[string]string{
|
||||
"orm": "%s",
|
||||
}
|
||||
|
||||
TemplateInputDir string
|
||||
TemplateOutputDir string
|
||||
ModelInputDir string
|
||||
ModelOutputDir string
|
||||
)
|
||||
|
||||
func toUpper(str string) string {
|
||||
return strings.Title(str)
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
outDir := flag.String("outdir", "./templates", "Output directory")
|
||||
tplDir := flag.String("tpldir", "./template_generator/templates", "Generator template directory")
|
||||
flag.Parse()
|
||||
func toLower(str string) string {
|
||||
return strings.ToLower(str)
|
||||
}
|
||||
|
||||
if len(flag.Args()) == 0 {
|
||||
flag.Usage()
|
||||
return
|
||||
}
|
||||
|
||||
funcMap := template.FuncMap{
|
||||
"toUpper": toUpper,
|
||||
}
|
||||
|
||||
model := flag.Args()[0]
|
||||
|
||||
for tplName, pattern := range fnPatterns {
|
||||
func generateTemplates(patterns map[string]string, model string, funcMap ...template.FuncMap) error {
|
||||
for tplName, pattern := range patterns {
|
||||
var data struct {
|
||||
Model string
|
||||
Models string
|
||||
|
@ -56,11 +49,11 @@ func main() {
|
|||
|
||||
name := fmt.Sprintf(pattern, strings.ToLower(data.Models))
|
||||
|
||||
tpl, err := tpl_util.LoadTextTemplate(filepath.Join(*tplDir, tplName+".tpl"), funcMap)
|
||||
tpl, err := tpl_util.LoadTextTemplate(filepath.Join(TemplateInputDir, tplName+".tpl"), funcMap...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
filename := filepath.Join(*outDir, name+".html.tpl")
|
||||
filename := filepath.Join(TemplateOutputDir, name+".html.tpl")
|
||||
if _, err := os.Stat(filename); err != nil {
|
||||
oFn, err := os.Create(filename)
|
||||
if err != nil {
|
||||
|
@ -78,4 +71,37 @@ func main() {
|
|||
log.Printf("Template %s already exists. Skipping.", filename)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
|
||||
TemplateOutputDir = *flag.String("tpl-output-dir", "./templates", "Template output directory")
|
||||
TemplateInputDir = *flag.String("tpl-input-dir", "./template_generator/templates", "Generator template directory")
|
||||
ModelOutputDir = *flag.String("model-input-dir", "./orm", "ORM output directory")
|
||||
ModelInputDir = *flag.String("model-input-dir", "./template_generator/orm", "Generator ORM template directory")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
if len(flag.Args()) == 0 {
|
||||
flag.Usage()
|
||||
return
|
||||
}
|
||||
|
||||
funcMap := template.FuncMap{
|
||||
"toUpper": toUpper,
|
||||
"toLower": toLower,
|
||||
}
|
||||
|
||||
model := flag.Args()[0]
|
||||
|
||||
if err := generateTemplates(fnTemplatePatterns, model, funcMap); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
|
125
template_generator/orm/model.tpl
Normal file
125
template_generator/orm/model.tpl
Normal file
|
@ -0,0 +1,125 @@
|
|||
package orm
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.andreafazzi.eu/andrea/oef/renderer"
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
type {{.Model}} struct {
|
||||
gorm.Model
|
||||
|
||||
// SelectedElement map[uint]string `gorm:"-"`
|
||||
// AllElements []*Element `gorm:"-"`
|
||||
}
|
||||
|
||||
func (model *{{.Model}}) GetID() uint { return q.ID }
|
||||
|
||||
func (model *{{.Model}}) String() string {
|
||||
return "" // Please implement this.
|
||||
}
|
||||
|
||||
func (model *{{.Model}}) Create(args map[string]string, r *http.Request) (interface{}, error) {
|
||||
if r.Method == "GET" {
|
||||
{{.Model|toLower}} := new({{.Model}})
|
||||
// if err := DB().Find(&{{.Model|toLower}}.AllContests).Error; err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
return {{.Model|toLower}}, nil
|
||||
} else {
|
||||
{{.Model|toLower}} := new({{.Model}})
|
||||
err := renderer.Decode({{.Model|toLower}}, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
{{.Model|toLower}}, err = Create{{.Model}}({{.Model|toLower}})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return {{.Model|toLower}}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (model *{{.Model}}) Read(args map[string]string, r *http.Request) (interface{}, error) {
|
||||
var {{.Model|toLower}} {{.Model}}
|
||||
|
||||
id := args["id"]
|
||||
|
||||
if err := DB()/*.Preload("Something").*/First(&{{.Model|toLower}}, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &{{.Model|toLower}}, nil
|
||||
}
|
||||
|
||||
func (model *{{.Model}}) ReadAll(args map[string]string, r *http.Request) (interface{}, error) {
|
||||
var {{.Models|toLower}} []*{{.Model}}
|
||||
if err := DB()/*.Preload("Something")*/.Order("created_at").Find(&{{.Models|toLower}}).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return {{.Models|toLower}}, nil
|
||||
}
|
||||
|
||||
func (model *{{.Model}}) Update(args map[string]string, r *http.Request) (interface{}, error) {
|
||||
if r.Method == "GET" {
|
||||
result, err := q.Read(args, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
{{.Model|toLower}} := result.(*{{.Model}})
|
||||
|
||||
// if err := DB().Find(&{{.Model|toLower}}.AllElements).Error; err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
// {{.Model|toLower}}.SelectedElement = make(map[uint]string)
|
||||
// {{.Model|toLower}}.SelectedElement[{{.Model|toLower}}.ElementID] = "selected"
|
||||
|
||||
return {{.Model|toLower}}, nil
|
||||
} else {
|
||||
{{.Model|toLower}}, err := q.Read(args, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = renderer.Decode({{.Model|toLower}}, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = Save{{.Model}}({{.Model|toLower}})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
{{.Model|toLower}}, err = q.Read(args, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return {{.Model|toLower}}.(*{{.Model}}), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (model *{{.Model}}) Delete(args map[string]string, r *http.Request) (interface{}, error) {
|
||||
{{.Model|toLower}}, err := t.Read(args, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := DB().Unscoped().Delete({{.Model|toLower}}.(*{{.Model|ToLower}})).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return {{.Model|toLower}}.(*{{.Model|ToLower}}), nil
|
||||
}
|
||||
|
||||
func Create{{.Model}}({{.Model|toLower}} *{{.Model}}) (*{{.Model}}, error) {
|
||||
if err := DB().Create({{.Model|toLower}}).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return {{.Model|toLower}}, nil
|
||||
}
|
||||
|
||||
func Save{{.Model}}({{.Model|toLower}} interface{}) (interface{}, error) {
|
||||
if err := DB()/*.Omit("Something")*/.Save({{.Model|toLower}}).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return {{.Model|toLower}}, nil
|
||||
}
|
Loading…
Reference in a new issue