Skip to content

Commit

Permalink
feat: show tag value in comments
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfogre committed May 12, 2020
1 parent 7df2e0f commit b379370
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 30 deletions.
29 changes: 18 additions & 11 deletions internal/gtag/gtag.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func generateFile(ctx context.Context, cmd, file string, types []string, tags []
}
pkg := f.Name.Name

fieldM := map[string][]string{}
fieldM := map[string][]*ast.Field{}
for _, typ := range types {
fields, ok := parseStructField(f, typ)
if ok {
Expand All @@ -95,9 +95,23 @@ func generateFile(ctx context.Context, cmd, file string, types []string, tags []

for _, typ := range types {
if fields, ok := fieldM[typ]; ok {
var tmpFields []templateDataTypeField
for _, field := range fields {
tag := ""
if field.Tag != nil {
tag = field.Tag.Value
}
for _, name := range field.Names {
tmpFields = append(tmpFields, templateDataTypeField{
Name: name.Name,
Tag: tag,
})
}
}

data.Types = append(data.Types, templateDataType{
Name: typ,
Fields: fields,
Fields: tmpFields,
})
}
}
Expand Down Expand Up @@ -140,7 +154,7 @@ func loadFile(name string) (*ast.File, error) {
return parser.ParseFile(token.NewFileSet(), name, f, 0)
}

func parseStructField(f *ast.File, name string) ([]string, bool) {
func parseStructField(f *ast.File, name string) ([]*ast.Field, bool) {
var fields []*ast.Field
found := false

Expand All @@ -166,12 +180,5 @@ func parseStructField(f *ast.File, name string) ([]string, bool) {
return nil, found
}

var ret []string
for _, field := range fields {
for _, v := range field.Names {
ret = append(ret, v.Name)
}
}

return ret, found
return fields, found
}
17 changes: 11 additions & 6 deletions internal/gtag/template.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions internal/gtag/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,17 @@ func Test_execute(t *testing.T) {
Package: "test",
Types: []templateDataType{
{
Name: "test",
Fields: []string{"A", "b"},
Name: "test",
Fields: []templateDataTypeField{
{
Name: "A",
Tag: `json:"a"`,
},
{
Name: "b",
Tag: ``,
},
},
},
},
Tags: []templateDataTag{
Expand Down Expand Up @@ -69,8 +78,8 @@ var (
// testTags indicate tags of type test
type testTags struct {
A string
b string
A string // json:"a"
b string //
}
// Tags return specified tags of test
Expand Down
12 changes: 6 additions & 6 deletions test/internal/regular/user_tag.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions test/internal/tutorial/user_tag.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b379370

Please sign in to comment.