Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support convert #19

Merged
merged 4 commits into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,37 @@ and you will get file user_tag.go:
//go:generate gtag -types User -tags bson .
package tutorial

import "reflect"
import (
"reflect"
"strings"
)

var (
// ...
)

// UserTags indicate tags of type User
type UserTags struct {
Id string
Name string
Email string
Id string // `bson:"_id"`
Name string // `bson:"name"`
Email string // `bson:"email"`
}

func (User) Tags(tag string) UserTags {
// Tags return specified tags of User
func (User) Tags(tag string, convert ...func(string) string) UserTags {
conv := func(in string) string { return strings.TrimSpace(strings.Split(in, ",")[0]) }
if len(convert) > 0 && convert[0] != nil {
conv = convert[0]
}
_ = conv
return UserTags{
Id: tagOfUserId.Get(tag),
Name: tagOfUserName.Get(tag),
Email: tagOfUserEmail.Get(tag),
Id: conv(tagOfUserId.Get(tag)),
Name: conv(tagOfUserName.Get(tag)),
Email: conv(tagOfUserEmail.Get(tag)),
}
}

// TagsBson is alias of Tags("bson")
func (v User) TagsBson() UserTags {
return v.Tags("bson")
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ go 1.14

require (
github.com/gochore/uniq v1.0.0
github.com/pmezard/go-difflib v1.0.0
github.com/google/go-cmp v0.4.0
golang.org/x/tools v0.0.0-20200505023115-26f46d2f7ef8
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/gochore/uniq v1.0.0 h1:qBA8Jm9GhSojZ8vsCI2h5FJGonmItjY9NLBPUaciOYk=
github.com/gochore/uniq v1.0.0/go.mod h1:zxZBO4/6PcgNuYMrlJCUKOEEpxSejbD6R53GUpSHelk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
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
}
32 changes: 24 additions & 8 deletions internal/gtag/template.go

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

47 changes: 30 additions & 17 deletions internal/gtag/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package gtag
import (
"testing"

"github.com/pmezard/go-difflib/difflib"
"github.com/google/go-cmp/cmp"
)

func Test_execute(t *testing.T) {
Expand All @@ -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 All @@ -45,7 +54,10 @@ func Test_execute(t *testing.T) {
//go:generate
package test

import "reflect"
import (
"reflect"
"strings"
)



Expand All @@ -66,15 +78,23 @@ 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
func (test) Tags(tag string) testTags {
func (test) Tags(tag string, convert ...func(string) string) testTags {
conv := func(in string) string { return strings.TrimSpace(strings.Split(in, ",")[0]) }
if len(convert) > 0 {
conv = convert[0]
}
if conv == nil {
conv = func(in string) string { return in }
}
_ = conv
return testTags{
A: tagOftestA.Get(tag),
b: tagOftestb.Get(tag),
A: conv(tagOftestA.Get(tag)),
b: conv(tagOftestb.Get(tag)),
}
}

Expand All @@ -99,14 +119,7 @@ func (v test) TagsBson() testTags {
t.Run(tt.name, func(t *testing.T) {
if got := string(execute(tt.args.data)); got != tt.want {
t.Errorf("execute() = %v, want %v", got, tt.want)
diff := difflib.UnifiedDiff{
A: difflib.SplitLines(got),
B: difflib.SplitLines(tt.want),
FromFile: "got",
ToFile: "want",
}
text, _ := difflib.GetUnifiedDiffString(diff)
t.Log(text)
t.Error(cmp.Diff(got, tt.want))
}
})
}
Expand Down
15 changes: 13 additions & 2 deletions test/internal/regular/empty_tag.go

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

22 changes: 20 additions & 2 deletions test/internal/regular/empty_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package regular

import (
"reflect"
"strings"
"testing"
)

func TestEmpty_Tags(t *testing.T) {
type args struct {
tag string
tag string
convert []func(string) string
}
tests := []struct {
name string
Expand All @@ -21,11 +23,27 @@ func TestEmpty_Tags(t *testing.T) {
},
want: EmptyTags{},
},
{
name: "convert ToUpper",
args: args{
tag: "json",
convert: []func(string) string{strings.ToUpper},
},
want: EmptyTags{},
},
{
name: "convert nil",
args: args{
tag: "json",
convert: []func(string) string{nil},
},
want: EmptyTags{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
em := Empty{}
if got := em.Tags(tt.args.tag); !reflect.DeepEqual(got, tt.want) {
if got := em.Tags(tt.args.tag, tt.args.convert...); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Tags() = %v, want %v", got, tt.want)
}
})
Expand Down
2 changes: 1 addition & 1 deletion test/internal/regular/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package regular

type User struct {
Id int `json:"id"`
Name UserName `json:"name"`
Name UserName `json:"name,omitempty"`
Email string `json:"email"`
age int
}
Expand Down
Loading