github.com/unionj-cloud/go-doudou/v2@v2.3.5/toolkit/astutils/structcollector_test.go (about) 1 package astutils 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "github.com/stretchr/testify/assert" 7 "github.com/unionj-cloud/go-doudou/v2/toolkit/pathutils" 8 "go/ast" 9 "go/parser" 10 "go/token" 11 "regexp" 12 "testing" 13 ) 14 15 func TestStruct(t *testing.T) { 16 file := pathutils.Abs("testdata/vo.go") 17 fset := token.NewFileSet() 18 root, err := parser.ParseFile(fset, file, nil, parser.ParseComments) 19 if err != nil { 20 panic(err) 21 } 22 sc := NewStructCollector(ExprString) 23 ast.Walk(sc, root) 24 fmt.Println(sc.Structs) 25 } 26 27 func TestInter(t *testing.T) { 28 file := pathutils.Abs("testdata/svc.go") 29 fset := token.NewFileSet() 30 root, err := parser.ParseFile(fset, file, nil, parser.ParseComments) 31 if err != nil { 32 panic(err) 33 } 34 sc := NewInterfaceCollector(ExprString) 35 ast.Walk(sc, root) 36 fmt.Println(sc.Interfaces) 37 } 38 39 func TestStructFuncDecl(t *testing.T) { 40 file := pathutils.Abs("testdata/cat.go") 41 fset := token.NewFileSet() 42 root, err := parser.ParseFile(fset, file, nil, parser.ParseComments) 43 if err != nil { 44 panic(err) 45 } 46 sc := NewStructCollector(ExprString) 47 ast.Walk(sc, root) 48 methods, exists := sc.Methods["Cat"] 49 if !exists { 50 t.Error("Cat should has methods") 51 } 52 if len(methods) != 1 { 53 t.Error("Cat should has only one method") 54 } 55 } 56 57 func TestRegex(t *testing.T) { 58 re := regexp.MustCompile(`anonystruct«(.*)»`) 59 a := `[]anonystruct«{"Name":"","Fields":[{"Name":"Name","Type":"string","Tag":"","Comments":null,"IsExport":true,"DocName":"Name"},{"Name":"Addr","Type":"anonystruct«{\"Name\":\"\",\"Fields\":[{\"Name\":\"Zip\",\"Type\":\"string\",\"Tag\":\"\",\"Comments\":null,\"IsExport\":true,\"DocName\":\"Zip\"},{\"Name\":\"Block\",\"Type\":\"string\",\"Tag\":\"\",\"Comments\":null,\"IsExport\":true,\"DocName\":\"Block\"},{\"Name\":\"Full\",\"Type\":\"string\",\"Tag\":\"\",\"Comments\":null,\"IsExport\":true,\"DocName\":\"Full\"}],\"Comments\":null,\"Methods\":null,\"IsExport\":false}»","Tag":"","Comments":null,"IsExport":true,"DocName":"Addr"}],"Comments":null,"Methods":null,"IsExport":false}»` 60 result := re.FindStringSubmatch(a) 61 fmt.Println(result[1]) 62 63 j := result[1] 64 var structmeta StructMeta 65 json.Unmarshal([]byte(j), &structmeta) 66 } 67 68 func TestStructCollector_DocFlatEmbed(t *testing.T) { 69 file := pathutils.Abs("testdata/embed.go") 70 fset := token.NewFileSet() 71 root, err := parser.ParseFile(fset, file, nil, parser.ParseComments) 72 if err != nil { 73 panic(err) 74 } 75 sc := NewStructCollector(ExprString) 76 ast.Walk(sc, root) 77 structs := sc.DocFlatEmbed() 78 for _, item := range structs { 79 if item.Name == "TestEmbed" { 80 var fields []string 81 var docFields []string 82 for _, fieldMeta := range item.Fields { 83 fields = append(fields, fieldMeta.Name) 84 docFields = append(docFields, fieldMeta.DocName) 85 } 86 assert.ElementsMatch(t, fields, []string{"Fields", "Type", "Index"}) 87 assert.ElementsMatch(t, docFields, []string{"fields", "type", "index"}) 88 } 89 } 90 } 91 92 func TestStructCollector_DocFlatEmbed1(t *testing.T) { 93 file := pathutils.Abs("testdata/embed1.go") 94 fset := token.NewFileSet() 95 root, err := parser.ParseFile(fset, file, nil, parser.ParseComments) 96 if err != nil { 97 panic(err) 98 } 99 sc := NewStructCollector(ExprString) 100 ast.Walk(sc, root) 101 structs := sc.DocFlatEmbed() 102 for _, item := range structs { 103 if item.Name == "TestEmbed1" { 104 var fields []string 105 var docFields []string 106 for _, fieldMeta := range item.Fields { 107 fields = append(fields, fieldMeta.Name) 108 docFields = append(docFields, fieldMeta.DocName) 109 } 110 assert.ElementsMatch(t, fields, []string{"Fields", "TestBase1"}) 111 assert.ElementsMatch(t, docFields, []string{"fields", "test_base_1"}) 112 } 113 } 114 } 115 116 func TestStructCollector_DocFlatEmbed_ExcludeUnexportedFields(t *testing.T) { 117 file := pathutils.Abs("testdata/embed2.go") 118 fset := token.NewFileSet() 119 root, err := parser.ParseFile(fset, file, nil, parser.ParseComments) 120 if err != nil { 121 panic(err) 122 } 123 sc := NewStructCollector(ExprString) 124 ast.Walk(sc, root) 125 structs := sc.DocFlatEmbed() 126 for _, item := range structs { 127 if item.Name == "TestEmbed2" { 128 var fields []string 129 var docFields []string 130 for _, fieldMeta := range item.Fields { 131 fields = append(fields, fieldMeta.Name) 132 docFields = append(docFields, fieldMeta.DocName) 133 } 134 assert.ElementsMatch(t, fields, []string{"Fields", "Index", "Type"}) 135 assert.ElementsMatch(t, docFields, []string{"fields", "index", "type"}) 136 } 137 } 138 } 139 140 func TestStructCollector_DocFlatEmbed_ExcludeUnexportedFields2(t *testing.T) { 141 file := pathutils.Abs("testdata/embed3.go") 142 fset := token.NewFileSet() 143 root, err := parser.ParseFile(fset, file, nil, parser.ParseComments) 144 if err != nil { 145 panic(err) 146 } 147 sc := NewStructCollector(ExprString) 148 ast.Walk(sc, root) 149 structs := sc.DocFlatEmbed() 150 for _, item := range structs { 151 if item.Name == "TestEmbed3" { 152 var fields []string 153 var docFields []string 154 for _, fieldMeta := range item.Fields { 155 fields = append(fields, fieldMeta.Name) 156 docFields = append(docFields, fieldMeta.DocName) 157 } 158 assert.ElementsMatch(t, fields, []string{"Fields", "Type"}) 159 assert.ElementsMatch(t, docFields, []string{"fields", "type"}) 160 } 161 } 162 } 163 164 func TestStructCollector_DocFlatEmbed_ExcludeUnexportedFields3(t *testing.T) { 165 file := pathutils.Abs("testdata/embed4.go") 166 fset := token.NewFileSet() 167 root, err := parser.ParseFile(fset, file, nil, parser.ParseComments) 168 if err != nil { 169 panic(err) 170 } 171 sc := NewStructCollector(ExprString) 172 ast.Walk(sc, root) 173 structs := sc.DocFlatEmbed() 174 for _, item := range structs { 175 if item.Name == "TestEmbed4" { 176 var fields []string 177 var docFields []string 178 for _, fieldMeta := range item.Fields { 179 fields = append(fields, fieldMeta.Name) 180 docFields = append(docFields, fieldMeta.DocName) 181 } 182 assert.ElementsMatch(t, fields, []string{"Fields", "TestBase1"}) 183 assert.ElementsMatch(t, docFields, []string{"fields", "test_base_1"}) 184 } 185 if item.Name == "TestBase4" { 186 var fields []string 187 var docFields []string 188 for _, fieldMeta := range item.Fields { 189 fields = append(fields, fieldMeta.Name) 190 docFields = append(docFields, fieldMeta.DocName) 191 } 192 assert.ElementsMatch(t, fields, []string{"Type"}) 193 assert.ElementsMatch(t, docFields, []string{"Type"}) 194 } 195 } 196 } 197 198 func TestStructCollector_DocFlatEmbed_ExcludeUnexportedFields4(t *testing.T) { 199 file := pathutils.Abs("testdata/embed5.go") 200 fset := token.NewFileSet() 201 root, err := parser.ParseFile(fset, file, nil, parser.ParseComments) 202 if err != nil { 203 panic(err) 204 } 205 sc := NewStructCollector(ExprString) 206 ast.Walk(sc, root) 207 structs := sc.DocFlatEmbed() 208 for _, item := range structs { 209 if item.Name == "TestEmbed5" { 210 var fields []string 211 var docFields []string 212 for _, fieldMeta := range item.Fields { 213 fields = append(fields, fieldMeta.Name) 214 docFields = append(docFields, fieldMeta.DocName) 215 } 216 assert.ElementsMatch(t, fields, []string{"Fields", "testBase5"}) 217 assert.ElementsMatch(t, docFields, []string{"fields", "testBase"}) 218 } 219 } 220 } 221 222 func TestStructCollector_Alias(t *testing.T) { 223 file := pathutils.Abs("testdata/alias.go") 224 fset := token.NewFileSet() 225 root, err := parser.ParseFile(fset, file, nil, parser.ParseComments) 226 if err != nil { 227 panic(err) 228 } 229 //spew.Dump(root) 230 sc := NewStructCollector(ExprString) 231 ast.Walk(sc, root) 232 structs := sc.DocFlatEmbed() 233 for _, item := range structs { 234 if item.Name == "TestAlias" { 235 fmt.Println(item) 236 } 237 } 238 } 239 240 func TestStructCollector_Entity(t *testing.T) { 241 file := pathutils.Abs("testdata/entity/purchase.go") 242 fset := token.NewFileSet() 243 root, err := parser.ParseFile(fset, file, nil, parser.ParseComments) 244 if err != nil { 245 panic(err) 246 } 247 //spew.Dump(root) 248 sc := NewStructCollector(ExprString) 249 ast.Walk(sc, root) 250 fmt.Println(sc) 251 }