Skip to content

Commit

Permalink
Merge pull request #10 from wolfogre/dev
Browse files Browse the repository at this point in the history
support preset tags
  • Loading branch information
wolfogre authored May 6, 2020
2 parents 5194393 + e6fe1b4 commit 3ed0ec8
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 10 deletions.
13 changes: 10 additions & 3 deletions cmd/gtag/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 11 additions & 4 deletions internal/gtag/gtag.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ 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) {
cmd := fmt.Sprintf("gtag -types %s %s", strings.Join(types, ","), dir)
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)]

Expand All @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion internal/gtag/gtag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func TestGenerate(t *testing.T) {
ctx context.Context
dir string
types []string
tags []string
}
tests := []struct {
name string
Expand All @@ -25,14 +26,15 @@ 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,
},
}
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
Expand Down
14 changes: 14 additions & 0 deletions internal/gtag/template.go

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

24 changes: 24 additions & 0 deletions internal/gtag/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: `
Expand All @@ -37,6 +47,8 @@ package test
import "reflect"
var (
valueOftest = test{}
typeOftest = reflect.TypeOf(valueOftest)
Expand Down Expand Up @@ -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")
}
`,
},
}
Expand Down
10 changes: 9 additions & 1 deletion test/internal/regular/empty_tag.go

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

40 changes: 40 additions & 0 deletions test/internal/regular/empty_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
18 changes: 17 additions & 1 deletion test/internal/regular/user_tag.go

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

Loading

0 comments on commit 3ed0ec8

Please sign in to comment.