-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathonce_cls_gen_test.go
104 lines (87 loc) · 3.16 KB
/
once_cls_gen_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
package gormcngen_test
import (
"testing"
"time"
"github.com/yyle88/gormcngen"
"github.com/yyle88/neatjson/neatjsons"
)
func TestMain(m *testing.M) {
m.Run()
}
func TestGen(t *testing.T) {
type Example struct {
ID int32 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"`
Name string `gorm:"not null,type:text"`
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
}
config := gormcngen.NewSchemaConfig(&Example{}, gormcngen.NewOptions())
output := config.Gen()
t.Log(output.GetMethodCode())
t.Log(output.GetStructCode())
t.Log(neatjsons.S(output.GetPkgImports()))
}
func TestGen_UseTagName(t *testing.T) {
type Example struct {
ID int32 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"`
Name string `gorm:"not null,type:text" form:"name" json:"name" validate:"required" cnm:"V名称"`
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
}
options := gormcngen.NewOptions().
WithUseTagName(true)
config := gormcngen.NewSchemaConfig(&Example{}, options)
output := config.Gen()
t.Log(output.GetMethodCode())
t.Log(output.GetStructCode())
t.Log(neatjsons.S(output.GetPkgImports()))
}
func TestGen_ExcludeUntaggedFields(t *testing.T) {
type Example struct {
ID int32 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"`
Name string `gorm:"not null,type:text" form:"name" json:"name" validate:"required" cnm:"V名称"`
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
}
options := gormcngen.NewOptions().
WithUseTagName(true).
WithExcludeUntaggedFields(true)
config := gormcngen.NewSchemaConfig(&Example{}, options)
output := config.Gen()
t.Log(output.GetMethodCode())
t.Log(output.GetStructCode())
t.Log(neatjsons.S(output.GetPkgImports()))
}
func TestGen_ColumnsMethodRecvName(t *testing.T) {
type Example struct {
ID int32 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"`
Name string `gorm:"not null,type:text" form:"name" json:"name" validate:"required" cnm:"V名称"`
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
}
options := gormcngen.NewOptions().
WithUseTagName(true).
WithColumnsMethodRecvName("example")
config := gormcngen.NewSchemaConfig(&Example{}, options)
output := config.Gen()
t.Log(output.GetMethodCode())
t.Log(output.GetStructCode())
t.Log(neatjsons.S(output.GetPkgImports()))
}
func TestGen_ColumnsCheckFieldType(t *testing.T) {
type Example struct {
ID int32 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"`
Name string `gorm:"not null,type:text" form:"name" json:"name" validate:"required" cnm:"V名称"`
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
}
options := gormcngen.NewOptions().
WithUseTagName(true).
WithColumnsMethodRecvName("example").
WithColumnsCheckFieldType(true)
config := gormcngen.NewSchemaConfig(&Example{}, options)
output := config.Gen()
t.Log(output.GetMethodCode())
t.Log(output.GetStructCode())
t.Log(neatjsons.S(output.GetPkgImports()))
}