Skip to content

Commit

Permalink
Cyclo reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
abice committed Aug 19, 2017
1 parent d191aad commit 08e9df8
Showing 1 changed file with 56 additions and 34 deletions.
90 changes: 56 additions & 34 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,7 @@ func getEnumDeclFromComments(comments []*ast.Comment) string {
parts := []string{}
store := false
for _, comment := range comments {
lines := []string{}
text := comment.Text
if strings.HasPrefix(text, `/*`) {
// deal with multi line comment
multiline := strings.TrimSuffix(strings.TrimPrefix(text, `/*`), `*/`)
lines = append(lines, strings.Split(multiline, "\n")...)
} else {
lines = append(lines, strings.TrimPrefix(text, `//`))
}
lines := breakCommentIntoLines(comment)

// Go over all the lines in this comment block
for _, line := range lines {
Expand Down Expand Up @@ -270,6 +262,21 @@ func getEnumDeclFromComments(comments []*ast.Comment) string {
return joined
}

// breakCommentIntoLines takes the comment and since single line comments are already broken into lines
// we break multiline comments into separate lines for processing.
func breakCommentIntoLines(comment *ast.Comment) []string {
lines := []string{}
text := comment.Text
if strings.HasPrefix(text, `/*`) {
// deal with multi line comment
multiline := strings.TrimSuffix(strings.TrimPrefix(text, `/*`), `*/`)
lines = append(lines, strings.Split(multiline, "\n")...)
} else {
lines = append(lines, strings.TrimPrefix(text, `//`))
}
return lines
}

// trimAllTheThings takes off all the cruft of a line that we don't need.
func trimAllTheThings(thing string) string {
return strings.TrimSpace(strings.TrimSuffix(strings.TrimSuffix(strings.TrimSpace(thing), `,`), `)`))
Expand All @@ -284,22 +291,7 @@ func (g *Generator) inspect(f *ast.File) map[string]*ast.TypeSpec {
ast.Inspect(f, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.GenDecl:
// Copy the doc spec to the type or value spec
// cause they missed this... whoops
if x.Doc != nil {
for _, spec := range x.Specs {
switch s := spec.(type) {
case *ast.TypeSpec:
if s.Doc == nil {
s.Doc = x.Doc
}
case *ast.ValueSpec:
if s.Doc == nil {
s.Doc = x.Doc
}
}
}
}
copyGenDeclCommentsToSpecs(x)
case *ast.Ident:
if x.Obj != nil {
// fmt.Printf("Node: %#v\n", x.Obj)
Expand All @@ -308,15 +300,7 @@ func (g *Generator) inspect(f *ast.File) map[string]*ast.TypeSpec {
// Make sure it's a spec (Type Identifiers can be throughout the code)
if ts, ok := x.Obj.Decl.(*ast.TypeSpec); ok {
// fmt.Printf("Type: %+v\n", ts)
isEnum := false
if ts.Doc != nil {
for _, comment := range ts.Doc.List {
if strings.Contains(comment.Text, `ENUM(`) {
isEnum = true
}
// fmt.Printf("Doc: %s\n", comment.Text)
}
}
isEnum := isTypeSpecEnum(ts)
// Only store documented enums
if isEnum {
// fmt.Printf("EnumType: %T\n", ts.Type)
Expand All @@ -332,3 +316,41 @@ func (g *Generator) inspect(f *ast.File) map[string]*ast.TypeSpec {

return enums
}

// copyDocsToSpecs will take the GenDecl level documents and copy them
// to the children Type and Value specs. I think this is actually working
// around a bug in the AST, but it works for now.
func copyGenDeclCommentsToSpecs(x *ast.GenDecl) {
// Copy the doc spec to the type or value spec
// cause they missed this... whoops
if x.Doc != nil {
for _, spec := range x.Specs {
switch s := spec.(type) {
case *ast.TypeSpec:
if s.Doc == nil {
s.Doc = x.Doc
}
case *ast.ValueSpec:
if s.Doc == nil {
s.Doc = x.Doc
}
}
}
}

}

// isTypeSpecEnum checks the comments on the type spec to determine if there is an enum
// declaration for the type.
func isTypeSpecEnum(ts *ast.TypeSpec) bool {
isEnum := false
if ts.Doc != nil {
for _, comment := range ts.Doc.List {
if strings.Contains(comment.Text, `ENUM(`) {
isEnum = true
}
}
}

return isEnum
}

0 comments on commit 08e9df8

Please sign in to comment.