Skip to content

Commit

Permalink
added support for array type in parser
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotoffia committed Sep 27, 2021
1 parent f5d25e7 commit a8c33fc
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
23 changes: 23 additions & 0 deletions goparser/internal_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,29 @@ func parseFile(
Decl: string(source[decl.Pos()-1 : decl.End()-1]),
}

goFile.CustomTypes = append(goFile.CustomTypes, goCustomType)
case (*ast.ArrayType):

var length string
if typeSpecType.Len == nil {
length = ""
} else {
length = typeSpecType.Len.(*ast.BasicLit).Value
}

goCustomType := &GoCustomType{
File: goFile,
Name: genSpecType.Name.Name,
Exported: isExported(genSpecType.Name.Name),

Type: fmt.Sprintf(
"[%s]%s", length, typeSpecType.Elt.(*ast.Ident).Name,
),

Doc: extractDocs(declType.Doc),
Decl: string(source[decl.Pos()-1 : decl.End()-1]),
}

goFile.CustomTypes = append(goFile.CustomTypes, goCustomType)
case (*ast.MapType):

Expand Down
36 changes: 36 additions & 0 deletions goparser/issue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package goparser

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// Contains all issues, reported on GitHub, for the goparser

// Issue15 is a bug that notes that ArrayType is not implemented
// `not-implemented typeSpec.Type.(type) = *ast.ArrayType`.
func TestIssue15(t *testing.T) {
src := `package foo
type Color string
type Label struct {
Name string
Description string
color Color
}
// LabelSet is a custom slice type
type LabelSet []Label`

m := dummyModule()
f, err := ParseInlineFile(m, m.Base+"/mypkg/file.go", src)
require.NoError(t, err)
require.NotNil(t, f)

assert.Equal(t, "[]Label", f.CustomTypes[1].Type)
assert.Equal(t, "type LabelSet []Label", f.CustomTypes[1].Decl)
assert.Equal(t, "LabelSet is a custom slice type", f.CustomTypes[1].Doc)

}
23 changes: 23 additions & 0 deletions goparser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,3 +631,26 @@ type ATime time.Time`

assert.Equal(t, `import "time"`, f.DeclImports())
}

func TestCustomArray(t *testing.T) {
src := `package foo
type Color string
type Label struct {
Name string
Description string
color Color
}
// LabelSet is a custom type
type LabelSet [14]Label`

m := dummyModule()
f, err := ParseInlineFile(m, m.Base+"/mypkg/file.go", src)
require.NoError(t, err)
require.NotNil(t, f)

assert.Equal(t, "[14]Label", f.CustomTypes[1].Type)
assert.Equal(t, "type LabelSet [14]Label", f.CustomTypes[1].Decl)
assert.Equal(t, "LabelSet is a custom type", f.CustomTypes[1].Doc)
}
Empty file removed test-docs.adoc
Empty file.

0 comments on commit a8c33fc

Please sign in to comment.