-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.go
65 lines (59 loc) · 1.56 KB
/
output.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
package aster
import (
"go/ast"
"reflect"
)
func resultFromDecl(decl ast.Decl) (Result, error) {
r := Result{}
switch reflect.TypeOf(decl).Elem().Name() {
case "GenDecl":
genDecl := decl.(*ast.GenDecl)
r.Comments = genDecl.Doc.Text()
switch reflect.TypeOf(genDecl.Specs[0]).Elem().Name() {
case "TypeSpec":
typeSpec := genDecl.Specs[0].(*ast.TypeSpec)
r.Name = typeSpec.Name.Name
switch reflect.TypeOf(typeSpec.Type).Elem().Name() {
case "StructType":
structType := typeSpec.Type.(*ast.StructType)
r.Type = "struct"
for _, f := range structType.Fields.List {
fld := Field{
Name: f.Names[0].Name,
Comments: f.Doc.Text(),
Type: f.Type.(*ast.Ident).Name,
}
r.Fields = append(r.Fields, fld)
}
}
}
case "FuncDecl":
funcDecl := decl.(*ast.FuncDecl)
r.Comments = funcDecl.Doc.Text()
r.Name = funcDecl.Name.Name
r.Type = "func"
}
return r, nil
}
// Result is an abstracted / simplified view of the matching Go objects,
// their attached comments, and, if applicable, fields. The idea here is to
// hide the complexity of the underlying 'ast' package structures.
type Result struct {
// The name of the object
Name string
// The type of the object, accessible via the 'Name()' method
Type string
// Comments associated with the object
Comments string
// Fields for a struct or func
Fields []Field
}
// A Field for a struct or funtion
type Field struct {
// The name of the field
Name string
// The type of the field
Type string
// Comments associated with the field
Comments string
}