forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go.todo
69 lines (56 loc) · 1.35 KB
/
builder.go.todo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package uadmin
import (
"github.com/uadmin/uadmin/helper"
"io/ioutil"
"strings"
)
type ThemeList int
type Builder struct {
Model
Name string `required:"true"`
OverideSave bool
CreatedByField bool
UpdatedByField bool
FormModifier string
ListModifier string
IncludeFormJS string
IncludeListJS string
FormTheme string
ListTheme string
}
func (b *Builder) GetCode() string {
return ""
}
func (b *Builder) Save() {
Save(b)
fields := []BuilderField{}
Filter(&fields, "builder_id = ?", b.ID)
imports := []string{"\t\"github.com/uadmin/uadmin\""}
fCodes := []string{"\tuadmin.Model"}
for _, f := range fields {
fCode, fImport := f.GetCode()
fCodes = append(fCodes, "\t"+fCode)
// check if fImport is not empty
if fImport != "" {
// check if fImport already exists in imports
imported := false
for _, imp := range imports {
if imp == "\t"+"\""+fImport+"\"" {
imported = true
break
}
}
if !imported {
imports = append(imports, "\t"+"\""+fImport+"\"")
}
}
}
code := "package models\n\n"
code += "import (\n"
code += strings.Join(imports, "\n") + "\n"
code += ")\n\n"
code += "type " + helper.ToCamel(b.Name) + " struct {\n"
code += strings.Join(fCodes, "\n") + "\n"
code += "}\n"
ioutil.WriteFile(strings.ToLower("models/"+helper.ToCamel(b.Name)+".go"), []byte(code), 0644)
}