Skip to content

Commit

Permalink
patched map detection
Browse files Browse the repository at this point in the history
  • Loading branch information
adranwit committed Mar 29, 2021
1 parent 8bca9fe commit 37dc0a1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
15 changes: 12 additions & 3 deletions fileset_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func toFunctionInfos(source *ast.FieldList, owner *FileInfo) []*FunctionInfo {
//Visit visits ast node to extract struct details from the passed file
func (f *FileInfo) Visit(node ast.Node) ast.Visitor {
if node != nil {

//TODO refactor this mess !!!!
switch value := node.(type) {
case *ast.TypeSpec:
typeName := value.Name.Name
Expand Down Expand Up @@ -403,15 +403,23 @@ func (f *FileInfo) Visit(node ast.Node) ast.Visitor {
}
case *ast.MapType:
f.currentTypInfo.IsMap = true

if keyTypeID, ok := value.Key.(*ast.Ident); ok {
f.currentTypInfo.KeyTypeName = keyTypeID.Name
}
switch v := value.Value.(type) {
case *ast.Ident:
f.currentTypInfo.ValueTypeName = v.Name
case *ast.ArrayType:
case *ast.StarExpr:
componentTypeName := "*"
switch y :=v.X.(type) {
case *ast.Ident:
componentTypeName += y.Name
case *ast.SelectorExpr:
componentTypeName += y.X.(*ast.Ident).Name + "." + y.Sel.Name
}
f.currentTypInfo.ValueTypeName = componentTypeName

case *ast.ArrayType:
componentTypeName := ""
switch compType := v.Elt.(type) {
case *ast.StarExpr:
Expand All @@ -426,6 +434,7 @@ func (f *FileInfo) Visit(node ast.Node) ast.Visitor {
componentTypeName = compType.Name
}
f.currentTypInfo.ValueTypeName = "[]" + componentTypeName

}
case *ast.FuncType:

Expand Down
21 changes: 17 additions & 4 deletions fileset_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestNewFileSetInfoInfo(t *testing.T) {
assert.False(t, fileInfo.HasType("F"))
assert.True(t, fileInfo.HasType("User"))

assert.Equal(t, 9, len(fileInfo.Types()))
assert.Equal(t, 10, len(fileInfo.Types()))

address := fileSetInfo.Type("Address")
assert.NotNil(t, address)
Expand All @@ -48,7 +48,7 @@ func TestNewFileSetInfoInfo(t *testing.T) {
assert.True(t, userInfo.HasField("Name"))
assert.False(t, userInfo.HasField("FF"))

assert.Equal(t, 13, len(userInfo.Fields()))
assert.Equal(t, 14, len(userInfo.Fields()))

idInfo := userInfo.Field("ID")
assert.True(t, idInfo.IsPointer)
Expand All @@ -67,9 +67,9 @@ func TestNewFileSetInfoInfo(t *testing.T) {
assert.Equal(t, "Address", addressPointer.TypeName)


aMapField := userInfo.Field("AMap")
aMapField := userInfo.Field("AMap1")
assert.NotNil(t, aMapField)
assert.EqualValues(t, "AMap", aMapField.TypeName)
assert.EqualValues(t, "AMap1", aMapField.TypeName)
aMapType := fileSetInfo.Type(aMapField.TypeName)
assert.NotNil(t, aMapType)
assert.True(t, aMapType.IsMap)
Expand All @@ -86,6 +86,19 @@ func TestNewFileSetInfoInfo(t *testing.T) {
assert.EqualValues(t, "string", aMapType2.KeyTypeName)
assert.EqualValues(t, "[]*Country", aMapType2.ValueTypeName)


aMapField3 := userInfo.Field("AMap3")
assert.NotNil(t, aMapField3)
assert.EqualValues(t, "AMap3", aMapField3.TypeName)
aMapType3 := fileSetInfo.Type(aMapField3.TypeName)
assert.NotNil(t, aMapType3)
assert.True(t, aMapType3.IsMap)
assert.EqualValues(t, "string", aMapType3.KeyTypeName)
assert.EqualValues(t, "*Country", aMapType3.ValueTypeName)




cInfo := userInfo.Field("C")
assert.True(t, cInfo.IsChannel)

Expand Down
7 changes: 5 additions & 2 deletions test/fileset_info/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ type Address struct {
City string
}

type AMap map[string][]int
type AMap1 map[string][]int

type AMap2 map[string][]*Country

type AMap3 map[string]*Country

type A Address

//User represents a test struct
Expand All @@ -40,8 +42,9 @@ type User struct { //my comments
M map[string][]string
C chan *bool
Appointments []time.Time
AMap1
AMap2
AMap
AMap3
}

//Test represents a test method
Expand Down

0 comments on commit 37dc0a1

Please sign in to comment.