-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
145 lines (131 loc) · 2.61 KB
/
main_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
138
139
140
141
142
143
144
145
package procstruct_test
import (
"fmt"
"go/ast"
"reflect"
"testing"
"github.com/anqur/procstruct"
"github.com/anqur/procstruct/edsl"
)
func ExampleStruct() {
s := procstruct.
Struct("Foo").
Header("is a foo.").
Field("Data", reflect.TypeOf(0), "is the data.")
fmt.Println(s)
// Output:
// // Foo is a foo.
// type Foo struct {
// // Data is the data.
// Data int
// }
}
func TestStruct(t *testing.T) {
s := procstruct.
Struct("Foo").
Field(
"Num",
reflect.TypeOf(0),
"is a number",
procstruct.Tag().
Comma("json").
Key("data").
Key("omitempty").
Nil().
Nil(),
).
Field(
"Str",
reflect.PtrTo(reflect.TypeOf("")),
"is a string",
procstruct.Tag().
CommaEqSpace("binding").
Key("required").
Entry("oneof", "todo", "pending", "done").
Nil().
Nil(),
).
Field(
"Float",
reflect.SliceOf(reflect.TypeOf(float64(0))),
"is a float",
procstruct.Tag().
SemiColon("gorm").
Key("not null").
Entry("column", "float").
Nil().
Nil(),
)
fmt.Println(s)
s.ForEach(func(name string, typ reflect.Type, tags []edsl.Tag) {
fmt.Println(name, typ, tags)
})
fmt.Println(s.FieldNames())
fmt.Println(s.TagKeys("json"))
fmt.Println(s.TagValues("gorm", "column"))
fmt.Println(s.TagValues("binding", "oneof"))
fmt.Println(s.Interface())
}
func TestRawTypedField(t *testing.T) {
s := procstruct.Struct("Foo").
RawTypedField(
"Ctx",
&ast.StarExpr{
X: &ast.SelectorExpr{
X: ast.NewIdent("foo"),
Sel: ast.NewIdent("Bar"),
},
},
"is the context",
)
defer func() {
if r := recover(); r == nil {
t.Fatal(s)
}
}()
fmt.Println(s)
fmt.Println(s.Interface())
}
func TestFile(t *testing.T) {
f := procstruct.File("foo").
Header("DO NOT EDIT!").
Imports(
"go/parser",
).
Structs(
procstruct.Struct("Foo").
Field("A", reflect.TypeOf(0), "").
Field("B", reflect.TypeOf(""), ""),
procstruct.Struct("Bar").
Field("A", reflect.TypeOf(float64(0)), "A").
Field(
"B",
reflect.TypeOf(false),
"",
procstruct.Tag().Comma("json"),
),
)
fmt.Println(f)
}
type Data struct {
Total int `json:"total,,,omitempty,,," validate:"required,oneof=1 2,,," gorm:"column:total;not null;;;;"`
}
func TestOf(t *testing.T) {
s := procstruct.File("foo").
Structs(
procstruct.Of(&Data{}).
Field(
"Results",
reflect.TypeOf(nil),
"",
procstruct.Tag().Comma("json").Key("results"),
),
)
fmt.Println(s)
}
func TestStructerOf(t *testing.T) {
s := procstruct.Struct("Item").
Field("Name", reflect.TypeOf(""), "").
Of(&Data{})
fmt.Println(s)
}