From 925ea1151600e209b607c881646030cc159caf91 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Wed, 6 May 2020 16:14:50 +0800 Subject: [PATCH 1/3] fix: generate commands --- internal/gtag/gtag.go | 2 +- test/internal/regular/empty_tag.go | 2 +- test/internal/regular/user_tag.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/gtag/gtag.go b/internal/gtag/gtag.go index 923bf52..3616129 100644 --- a/internal/gtag/gtag.go +++ b/internal/gtag/gtag.go @@ -25,7 +25,7 @@ func (r *GenerateResult) String() string { } func Generate(ctx context.Context, dir string, types []string) ([]*GenerateResult, error) { - cmd := fmt.Sprintf("gtag -types %s %s", strings.Join(types, ","), dir) + cmd := fmt.Sprintf("gtag -types %s .", strings.Join(types, ",")) types = types[:uniq.Strings(types)] diff --git a/test/internal/regular/empty_tag.go b/test/internal/regular/empty_tag.go index cc47358..496e9cb 100644 --- a/test/internal/regular/empty_tag.go +++ b/test/internal/regular/empty_tag.go @@ -1,7 +1,7 @@ // Code generated by gtag. DO NOT EDIT. // See: https://github.com/wolfogre/gtag -//go:generate gtag -types User,Empty,UserName,UserName ../../test/internal/regular/ +//go:generate gtag -types User,Empty,UserName,UserName . package regular import "reflect" diff --git a/test/internal/regular/user_tag.go b/test/internal/regular/user_tag.go index f5f7172..ed92ae9 100644 --- a/test/internal/regular/user_tag.go +++ b/test/internal/regular/user_tag.go @@ -1,7 +1,7 @@ // Code generated by gtag. DO NOT EDIT. // See: https://github.com/wolfogre/gtag -//go:generate gtag -types User,Empty,UserName,UserName ../../test/internal/regular/ +//go:generate gtag -types User,Empty,UserName,UserName . package regular import "reflect" From 971ae48ede0d33a5a60671515e734ff0ded8f295 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Wed, 6 May 2020 16:38:48 +0800 Subject: [PATCH 2/3] feat: support preset tags --- cmd/gtag/main.go | 13 ++++++++++--- internal/gtag/gtag.go | 13 ++++++++++--- internal/gtag/gtag_test.go | 4 +++- internal/gtag/template.go | 14 ++++++++++++++ internal/gtag/template_test.go | 24 ++++++++++++++++++++++++ test/internal/regular/empty_tag.go | 8 ++++++++ test/internal/regular/user_tag.go | 16 ++++++++++++++++ 7 files changed, 85 insertions(+), 7 deletions(-) diff --git a/cmd/gtag/main.go b/cmd/gtag/main.go index 355a322..38a5817 100644 --- a/cmd/gtag/main.go +++ b/cmd/gtag/main.go @@ -11,20 +11,27 @@ import ( ) var ( - types = flag.String("types", "", "struct types") + Types = flag.String("types", "", "struct types") + Tags = flag.String("tags", "", "preset tags") ) func main() { flag.Parse() args := flag.Args() - if *types == "" || len(args) != 1 { + if *Types == "" || len(args) != 1 { printUsages() return } dir := args[0] - _, err := gtag.Generate(context.Background(), dir, strings.Split(*types, ",")) + types := strings.Split(*Types, ",") + var tags []string + if *Tags != "" { + tags = strings.Split(*Tags, ",") + } + + _, err := gtag.Generate(context.Background(), dir, types, tags) if err != nil { fmt.Println(err) os.Exit(1) diff --git a/internal/gtag/gtag.go b/internal/gtag/gtag.go index 3616129..c2f8a99 100644 --- a/internal/gtag/gtag.go +++ b/internal/gtag/gtag.go @@ -24,7 +24,7 @@ func (r *GenerateResult) String() string { return fmt.Sprintf("%s\n%s", r.Output, r.Content) } -func Generate(ctx context.Context, dir string, types []string) ([]*GenerateResult, error) { +func Generate(ctx context.Context, dir string, types []string, tags []string) ([]*GenerateResult, error) { cmd := fmt.Sprintf("gtag -types %s .", strings.Join(types, ",")) types = types[:uniq.Strings(types)] @@ -47,7 +47,7 @@ func Generate(ctx context.Context, dir string, types []string) ([]*GenerateResul var ret []*GenerateResult for _, file := range files { - result, err := generateFile(ctx, cmd, file, types) + result, err := generateFile(ctx, cmd, file, types, tags) if err != nil { return nil, err } @@ -63,7 +63,7 @@ func Generate(ctx context.Context, dir string, types []string) ([]*GenerateResul return ret, nil } -func generateFile(ctx context.Context, cmd, file string, types []string) (*GenerateResult, error) { +func generateFile(ctx context.Context, cmd, file string, types []string, tags []string) (*GenerateResult, error) { f, err := loadFile(file) if err != nil { return nil, err @@ -100,6 +100,13 @@ func generateFile(ctx context.Context, cmd, file string, types []string) (*Gener } } + for _, tag := range tags { + data.Tags = append(data.Tags, templateDataTag{ + Name: strings.Title(strings.ReplaceAll(tag, "_", " ")), + Value: tag, + }) + } + src, err := format.Source(execute(data)) if err != nil { return nil, err diff --git a/internal/gtag/gtag_test.go b/internal/gtag/gtag_test.go index db07e89..32451c5 100644 --- a/internal/gtag/gtag_test.go +++ b/internal/gtag/gtag_test.go @@ -12,6 +12,7 @@ func TestGenerate(t *testing.T) { ctx context.Context dir string types []string + tags []string } tests := []struct { name string @@ -25,6 +26,7 @@ func TestGenerate(t *testing.T) { ctx: context.Background(), dir: testDir + "regular/", types: []string{"User", "Empty", "UserName", "UserName"}, + tags: []string{"json", "bson"}, }, want: nil, wantErr: false, @@ -32,7 +34,7 @@ func TestGenerate(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := Generate(tt.args.ctx, tt.args.dir, tt.args.types) + got, err := Generate(tt.args.ctx, tt.args.dir, tt.args.types, tt.args.tags) if (err != nil) != tt.wantErr { t.Errorf("Generate() error = %v, wantErr %v", err, tt.wantErr) return diff --git a/internal/gtag/template.go b/internal/gtag/template.go index 8cb15f3..3b3e884 100644 --- a/internal/gtag/template.go +++ b/internal/gtag/template.go @@ -9,6 +9,7 @@ type templateData struct { Package string Command string Types []templateDataType + Tags []templateDataTag } type templateDataType struct { @@ -16,6 +17,11 @@ type templateDataType struct { Fields []string } +type templateDataTag struct { + Name string + Value string +} + const templateLayout = ` // Code generated by gtag. DO NOT EDIT. // See: https://github.com/wolfogre/gtag @@ -25,6 +31,7 @@ package {{.Package}} import "reflect" +{{$tags := .Tags}} {{- range .Types}} var ( @@ -53,6 +60,13 @@ func ({{$type}}) Tags(tag string) {{$type}}Tags { } } +{{range $tags}} +func (v {{$type}}) Tags{{.Name}}() {{$type}}Tags { + return v.Tags("{{.Value}}") +} + +{{end}} + {{- end}} ` diff --git a/internal/gtag/template_test.go b/internal/gtag/template_test.go index 42fd5be..51b8929 100644 --- a/internal/gtag/template_test.go +++ b/internal/gtag/template_test.go @@ -26,6 +26,16 @@ func Test_execute(t *testing.T) { Fields: []string{"A", "b"}, }, }, + Tags: []templateDataTag{ + { + Name: "Json", + Value: "json", + }, + { + Name: "Bson", + Value: "bson", + }, + }, }, }, want: ` @@ -37,6 +47,8 @@ package test import "reflect" + + var ( valueOftest = test{} typeOftest = reflect.TypeOf(valueOftest) @@ -64,6 +76,18 @@ func (test) Tags(tag string) testTags { } } + +func (v test) TagsJson() testTags { + return v.Tags("json") +} + + +func (v test) TagsBson() testTags { + return v.Tags("bson") +} + + + `, }, } diff --git a/test/internal/regular/empty_tag.go b/test/internal/regular/empty_tag.go index 496e9cb..b75deda 100644 --- a/test/internal/regular/empty_tag.go +++ b/test/internal/regular/empty_tag.go @@ -17,3 +17,11 @@ type EmptyTags struct { func (Empty) Tags(tag string) EmptyTags { return EmptyTags{} } + +func (v Empty) TagsJson() EmptyTags { + return v.Tags("json") +} + +func (v Empty) TagsBson() EmptyTags { + return v.Tags("bson") +} diff --git a/test/internal/regular/user_tag.go b/test/internal/regular/user_tag.go index ed92ae9..0fd5fe1 100644 --- a/test/internal/regular/user_tag.go +++ b/test/internal/regular/user_tag.go @@ -43,6 +43,14 @@ func (User) Tags(tag string) UserTags { } } +func (v User) TagsJson() UserTags { + return v.Tags("json") +} + +func (v User) TagsBson() UserTags { + return v.Tags("bson") +} + var ( valueOfUserName = UserName{} typeOfUserName = reflect.TypeOf(valueOfUserName) @@ -67,3 +75,11 @@ func (UserName) Tags(tag string) UserNameTags { Last: tagOfUserNameLast.Get(tag), } } + +func (v UserName) TagsJson() UserNameTags { + return v.Tags("json") +} + +func (v UserName) TagsBson() UserNameTags { + return v.Tags("bson") +} From e6fe1b4a290ebc80673335910577670038674efa Mon Sep 17 00:00:00 2001 From: Jason Song Date: Wed, 6 May 2020 16:45:35 +0800 Subject: [PATCH 3/3] test: add cases --- test/internal/regular/empty_tag_test.go | 40 +++++++ test/internal/regular/user_tag_test.go | 132 ++++++++++++++++++++++++ 2 files changed, 172 insertions(+) diff --git a/test/internal/regular/empty_tag_test.go b/test/internal/regular/empty_tag_test.go index e45e5b1..a3aad28 100644 --- a/test/internal/regular/empty_tag_test.go +++ b/test/internal/regular/empty_tag_test.go @@ -31,3 +31,43 @@ func TestEmpty_Tags(t *testing.T) { }) } } + +func TestEmpty_TagsJson(t *testing.T) { + tests := []struct { + name string + want EmptyTags + }{ + { + name: "regular", + want: EmptyTags{}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + v := Empty{} + if got := v.TagsJson(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("TagsJson() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestEmpty_TagsBson(t *testing.T) { + tests := []struct { + name string + want EmptyTags + }{ + { + name: "regular", + want: EmptyTags{}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + v := Empty{} + if got := v.TagsBson(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("TagsBson() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/test/internal/regular/user_tag_test.go b/test/internal/regular/user_tag_test.go index 37dc436..425d69f 100644 --- a/test/internal/regular/user_tag_test.go +++ b/test/internal/regular/user_tag_test.go @@ -99,3 +99,135 @@ func TestUserName_Tags(t *testing.T) { }) } } + +func TestUserName_TagsJson(t *testing.T) { + type fields struct { + First string + Last string + } + tests := []struct { + name string + fields fields + want UserNameTags + }{ + { + name: "regular", + fields: fields{}, + want: UserNameTags{ + First: "first", + Last: "last", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + v := UserName{ + First: tt.fields.First, + Last: tt.fields.Last, + } + if got := v.TagsJson(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("TagsJson() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestUserName_TagsBson(t *testing.T) { + type fields struct { + First string + Last string + } + tests := []struct { + name string + fields fields + want UserNameTags + }{ + { + name: "regular", + fields: fields{}, + want: UserNameTags{}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + v := UserName{ + First: tt.fields.First, + Last: tt.fields.Last, + } + if got := v.TagsBson(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("TagsBson() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestUser_TagsJson(t *testing.T) { + type fields struct { + Id int + Name UserName + Email string + age int + } + tests := []struct { + name string + fields fields + want UserTags + }{ + { + name: "regular", + fields: fields{}, + want: UserTags{ + Id: "id", + Name: "name", + Email: "email", + age: "", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + v := User{ + Id: tt.fields.Id, + Name: tt.fields.Name, + Email: tt.fields.Email, + age: tt.fields.age, + } + if got := v.TagsJson(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("TagsJson() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestUser_TagsBson(t *testing.T) { + type fields struct { + Id int + Name UserName + Email string + age int + } + tests := []struct { + name string + fields fields + want UserTags + }{ + { + name: "regular", + fields: fields{}, + want: UserTags{}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + v := User{ + Id: tt.fields.Id, + Name: tt.fields.Name, + Email: tt.fields.Email, + age: tt.fields.age, + } + if got := v.TagsBson(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("TagsBson() = %v, want %v", got, tt.want) + } + }) + } +}