Skip to content

Commit

Permalink
fix: Missing type on struct with package name (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
h4ckm03d authored Sep 5, 2020
1 parent 7c3051f commit 8a229e2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
5 changes: 4 additions & 1 deletion generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ type Tag struct {
DataType string
}

// GetType determine type from expression
func GetType(n ast.Expr) string {
switch x := n.(type) {
case *ast.ArrayType:
return "[]" + GetType(x.Elt)
case *ast.Ident:
return x.Name
case *ast.SelectorExpr:
return fmt.Sprintf("%s.%s", GetType(x.X), GetType(x.Sel))
case *ast.StarExpr:
return "*" + GetType(x.X)
case *ast.MapType:
Expand All @@ -28,7 +31,7 @@ func GetType(n ast.Expr) string {
}
}

// GetTags get tag from existing tags
// GetTags from existing tags
func GetTags(f *ast.File, structName, tag string) []Tag {
if tag == "" {
tag = `opt`
Expand Down
9 changes: 8 additions & 1 deletion generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func TestGetOptTags(t *testing.T) {

field3 = generator.Tag{Name: "Field3",
DataType: "map[byte]float64"}

field4 = generator.Tag{Name: "Field4",
DataType: "http.Header"}
)
type args struct {
sourcePath string
Expand Down Expand Up @@ -72,7 +75,7 @@ func TestGetOptTags(t *testing.T) {
},
"All": {
args: args{sourcePath: ".././testfile/foo.sample", Tag: "_all_", structName: "Thing"},
want: []generator.Tag{field1, field2, field3},
want: []generator.Tag{field1, field2, field3, field4},
},
}
for name, tt := range tests {
Expand All @@ -99,6 +102,10 @@ func Test_getType(t *testing.T) {
args: args{&ast.Ident{Name: "int"}},
want: "int",
},
"header": {
args: args{&ast.Ident{Name: "http.Header"}},
want: "http.Header",
},
"empty": {
args: args{nil},
want: "",
Expand Down
5 changes: 5 additions & 0 deletions testfile/foo.sample
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package foo

import (
"net/http"
)

type Thing struct {
Field1 string `opt`
Field2 []*int `opt`
Field3 map[byte]float64
Field4 http.Header
}

type ThingThong struct {
Expand Down

0 comments on commit 8a229e2

Please sign in to comment.