| 1 |
package generator |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"go/ast" |
| 6 |
"go/token" |
| 7 |
) |
| 8 |
|
| 9 |
const segmentElementsMethodIdentifier = "elements" |
| 10 |
const segmentElementsMethodReturnType = "[]element.DataElement" |
| 11 |
|
| 12 |
type elementVisitor struct { |
| 13 |
receiverName string |
| 14 |
fileSet *token.FileSet |
| 15 |
object *ast.Object |
| 16 |
fields []field |
| 17 |
err error |
| 18 |
} |
| 19 |
|
| 20 |
func (e *elementVisitor) Visit(node ast.Node) ast.Visitor { |
| 21 |
if funcDecl, ok := node.(*ast.FuncDecl); ok { |
| 22 |
if e.isElementsMethod(funcDecl) { |
| 23 |
bodyStatements := funcDecl.Body.List |
| 24 |
if len(bodyStatements) == 1 { |
| 25 |
if ret, ok := bodyStatements[0].(*ast.ReturnStmt); ok { |
| 26 |
if len(ret.Results) == 1 { |
| 27 |
if res, ok := ret.Results[0].(*ast.CompositeLit); ok { |
| 28 |
resType := nodeToString(res.Type, e.fileSet) |
| 29 |
if resType == segmentElementsMethodReturnType { |
| 30 |
for _, element := range res.Elts { |
| 31 |
if sel, ok := element.(*ast.SelectorExpr); ok { |
| 32 |
pos := sel.Pos() |
| 33 |
position := e.fileSet.Position(pos) |
| 34 |
elemField := field{Name: sel.Sel.Name, Line: position.Line} |
| 35 |
if _, ok := sel.X.(*ast.Ident); ok { |
| 36 |
fieldVisitor := &structFieldVisitor{fileSet: e.fileSet, fieldName: sel.Sel.Name} |
| 37 |
ast.Walk(fieldVisitor, e.object.Decl.(*ast.TypeSpec)) |
| 38 |
if fieldVisitor.err != nil { |
| 39 |
e.err = fieldVisitor.err |
| 40 |
return nil |
| 41 |
} |
| 42 |
elemField.TypeDecl = fieldVisitor.fieldTypeDecl |
| 43 |
} else { |
| 44 |
e.err = fmt.Errorf("Unexpected Selector: %q", nodeToString(sel, e.fileSet)) |
| 45 |
return nil |
| 46 |
} |
| 47 |
if elemField.TypeDecl == "" { |
| 48 |
e.err = fmt.Errorf("No type declaration found for element %q", sel.Sel.Name) |
| 49 |
return nil |
| 50 |
} |
| 51 |
e.fields = append(e.fields, elemField) |
| 52 |
} else { |
| 53 |
e.err = fmt.Errorf("Unsupported element in elements method: %q", nodeToString(element, e.fileSet)) |
| 54 |
return nil |
| 55 |
} |
| 56 |
} |
| 57 |
return nil |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
return e |
| 66 |
} |
| 67 |
|
| 68 |
func (e *elementVisitor) isElementsMethod(funcDecl *ast.FuncDecl) bool { |
| 69 |
if funcDecl.Name.Name == segmentElementsMethodIdentifier { |
| 70 |
if funcDecl.Recv != nil { |
| 71 |
if fields := funcDecl.Recv.List; fields != nil { |
| 72 |
if len(fields) == 1 { |
| 73 |
if typ, ok := fields[0].Type.(*ast.StarExpr); ok { |
| 74 |
if ident, ok := typ.X.(*ast.Ident); ok { |
| 75 |
if ident.Name == e.receiverName { |
| 76 |
return true |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
return false |
| 85 |
} |
| 86 |
|
| 87 |
type structFieldVisitor struct { |
| 88 |
fileSet *token.FileSet |
| 89 |
fieldName string |
| 90 |
fieldTypeDecl string |
| 91 |
err error |
| 92 |
} |
| 93 |
|
| 94 |
func (s *structFieldVisitor) Visit(node ast.Node) ast.Visitor { |
| 95 |
if structType, ok := node.(*ast.StructType); ok { |
| 96 |
if fields := structType.Fields; fields != nil { |
| 97 |
for _, f := range fields.List { |
| 98 |
var fieldName string |
| 99 |
if names := f.Names; names != nil { |
| 100 |
fieldName = nodeToString(names[0], s.fileSet) |
| 101 |
if fieldName != s.fieldName { |
| 102 |
continue // not the field we're looking for |
| 103 |
} |
| 104 |
} else { |
| 105 |
continue // anonymous field |
| 106 |
} |
| 107 |
if starExpr, ok := f.Type.(*ast.StarExpr); ok { |
| 108 |
s.fieldTypeDecl = nodeToString(starExpr.X, s.fileSet) |
| 109 |
} else { |
| 110 |
s.err = fmt.Errorf("Unexpected type found: %q", nodeToString(f.Type, s.fileSet)) |
| 111 |
return nil |
| 112 |
} |
| 113 |
} |
| 114 |
return nil |
| 115 |
} |
| 116 |
} |
| 117 |
return s |
| 118 |
} |