-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_test.go
137 lines (119 loc) · 3.49 KB
/
model_test.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"strings"
"testing"
"time"
"github.com/karsto/glew/pkg"
)
func assertEqual(t *testing.T, a interface{}, b interface{}) {
if a != b {
t.Fatalf("%s != %s", a, b)
}
}
type TestModel struct {
StringField string `db:"string_field" json:"stringField"`
NumField int `db:"num_field" json:"numField"`
ID int `db:"id" json:"id"`
Code string `db:"code" json:"code"`
TimeField time.Time `db:"time_field" json:"timeField"`
}
type CreateTestModel struct {
StringField string `db:"string_field" json:"stringField"`
NumField int `db:"num_field" json:"numField"`
ID int `db:"id" json:"id"`
Code string `db:"code" json:"code"`
TimeField time.Time `db:"time_field" json:"timeField"`
}
type UpdateTestModel struct {
StringField string `db:"string_field" json:"stringField"`
NumField int `db:"num_field" json:"numField"`
ID int `db:"id" json:"id"`
Code string `db:"code" json:"code"`
TimeField time.Time `db:"time_field" json:"timeField"`
}
func TestPlayground(t *testing.T) {
/*
CONFIG
destDir - the output directory from this run
appName - the name of the application and docker runtime
importPath - application go import directory, aka the directory of the app to reference itself
*/
app := pkg.NewApp(pkg.Frontend{}, pkg.Backend{}, pkg.DB{})
destDir := "out"
appName := "testApp"
importPath := "github.com/karsto/glew/out"
verticals := []pkg.VerticalMeta{}
vertical, err := app.GenerateVerticalMeta(TestModel{}, "TestVertical", CreateTestModel{}, UpdateTestModel{})
if err != nil {
panic(err)
}
verticals = append(verticals, vertical)
ctx := pkg.BaseAPPCTX{
ImportPath: importPath, //TODO: hack to produce current import dir + out
}
features := pkg.NewConfig()
files, err := app.GenerateApp(features, destDir, appName, verticals, ctx)
if err != nil {
panic(err)
}
err = pkg.WriteFiles(files, destDir)
if err != nil {
panic(err)
}
}
var (
todoTokens = []string{"TODO", "FIXME"}
)
func isTodo(s string) (int, bool) {
for _, indent := range todoTokens {
if strings.HasPrefix(strings.ToUpper(s), indent) {
return len(indent), true
}
}
return 0, false
}
func TestParsePlayground(t *testing.T) {
fset := token.NewFileSet()
target, err := parser.ParseFile(fset, "./test-target.go", nil, parser.ParseComments)
if err != nil {
panic(err)
}
// ******* Comment Finder
//TODO: convert to util function to identify all todo places that need dev intervention in final program
for _, cg := range target.Comments {
for _, c := range cg.List {
for i, l := range strings.Split(c.Text, "\n") {
var t = strings.TrimSpace(l)
if strings.HasPrefix(t, "//") || strings.HasPrefix(t, "/*") || strings.HasPrefix(t, "*/") {
t = strings.TrimSpace(t[2:])
}
// To do found
if _, found := isTodo(t); found {
fmt.Printf("FOUND TODO on line: %v", fset.Position(c.Slash).Line+i)
}
}
}
}
// ******** End Comment Finder
// ******** Struct finder
ast.Inspect(target, func(n ast.Node) bool {
// TODO: extract meta info and convert to ModelMeta
// TODO: consider using ast field types ?
t, ok2 := n.(*ast.TypeSpec)
if ok2 {
fmt.Printf("Name:%v \n", t.Name.Name)
}
structBlock, ok := n.(*ast.StructType)
if ok {
for _, field := range structBlock.Fields.List {
fmt.Printf("Field: %s\n", field.Names[0].Name)
fmt.Printf("Tag: %s\n", field.Tag.Value)
}
}
return true
})
}