-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparse_struct_test.go
More file actions
58 lines (49 loc) · 1.16 KB
/
Copy pathparse_struct_test.go
File metadata and controls
58 lines (49 loc) · 1.16 KB
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
package parseStruct
import (
"testing"
)
type Example1 struct {
// Foo Comments
Foo string `json:"foo"`
}
type Example2 struct {
// Aoo Comments
Aoo int `json:"aoo"`
}
func TestParseStructFromSrc(t *testing.T) {
src := `package main
type Example struct {
// Foo comments
Foo string ` + "`json:\"foo\"`\n" +
"AA int " + "`json:\"aa,string\"`}"
fieldsMap, err := ParseStruct("./test.go", []byte(src), "json")
if err != nil {
t.Fatalf("err:%v", err)
return
}
for structName, fields := range fieldsMap {
t.Logf("structName:%s", structName)
for _, field := range fields {
t.Logf(" FieldName:%s", field.Names[0].Name)
t.Logf(" FieldType:%s", field.Type)
t.Logf(" FieldTag:%s", field.Tag.Value)
}
}
return
}
func TestParseStructFromFile(t *testing.T) {
fieldsMap, err := ParseStruct("parse_struct_test.go", nil, "json")
if err != nil {
t.Fatalf("err:%v", err)
return
}
for structName, fields := range fieldsMap {
t.Logf("structName:%s", structName)
for _, field := range fields {
t.Logf(" FieldName:%s", field.Names[0].Name)
t.Logf(" FieldType:%s", field.Type)
t.Logf(" FieldTag:%s", field.Tag.Value)
}
}
return
}