From de69ce53ebf8535dbc5756439c83b40824cca9a1 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Wed, 15 Nov 2023 18:31:40 +0800 Subject: [PATCH] General improvement (#29) --- .github/workflows/release.yaml | 31 ++++++++++++++++++++++++++++++ README.md | 22 ++++++--------------- cmd/gtag/main.go | 17 ++-------------- go.mod | 1 + go.sum | 1 + internal/gtag/gtag.go | 13 +++++++------ internal/gtag/gtag_test.go | 2 +- test/internal/regular/empty_tag.go | 8 +++++++- test/internal/regular/user.go | 2 +- test/internal/regular/user_tag.go | 16 +++++++++++++-- test/internal/tutorial/user.go | 2 +- test/internal/tutorial/user_tag.go | 9 +++++++++ 12 files changed, 81 insertions(+), 43 deletions(-) create mode 100644 .github/workflows/release.yaml diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..1a16410 --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,31 @@ +name: Release + +on: + push: + tags: [ "v*" ] + +permissions: + contents: write + +jobs: + goreleaser: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: 'stable' + + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v5 + with: + distribution: goreleaser + version: latest + args: release --clean + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/README.md b/README.md index 6dc19b5..10c4ef6 100644 --- a/README.md +++ b/README.md @@ -11,19 +11,9 @@ Help you to get golang struct's tags elegantly. -## Installing +## Quick start -Install gtag by running: - -```bash -go get -u github.com/wolfogre/gtag/cmd/gtag -``` - -and ensuring that `$GOPATH/bin` is added to your `$PATH`. - -## Tutorial - -### 1. define your struct +### 1. Define your struct A source file `user.go`: @@ -37,12 +27,12 @@ type User struct { } ``` -## 2. run gtag +## 2. Run gtag Run ```bash -gtag -types User -tags bson . +go run github.com/wolfogre/gtag/cmd/gtag -types User -tags bson . ``` and you will get file user_tag.go: @@ -51,7 +41,7 @@ and you will get file user_tag.go: // Code generated by gtag. DO NOT EDIT. // See: https://github.com/wolfogre/gtag -//go:generate gtag -types User -tags bson . +//go:generate go run github.com/wolfogre/gtag/cmd/gtag -types User -tags bson . package tutorial import ( @@ -93,7 +83,7 @@ func (*User) TagsBson() UserTags { } ``` -## 3. use it +## 3. Use it Now you can use the generated code to get tags elegantly: diff --git a/cmd/gtag/main.go b/cmd/gtag/main.go index 7ee696e..d3bfa7d 100644 --- a/cmd/gtag/main.go +++ b/cmd/gtag/main.go @@ -11,26 +11,13 @@ import ( ) var ( - version = "dev" - commit = "none" - date = "unknown" - builtBy = "unknown" -) - -var ( - Types = flag.String("types", "", "struct types") - Tags = flag.String("tags", "", "preset tags") - Version = flag.Bool("version", false, " show version") + Types = flag.String("types", "", "struct types") + Tags = flag.String("tags", "", "preset tags") ) func main() { flag.Parse() - if *Version { - fmt.Printf("gtag %s, commit %s, built at %s by %s\n", version, commit, date, builtBy) - return - } - args := flag.Args() if *Types == "" || len(args) != 1 { printUsages() diff --git a/go.mod b/go.mod index 28e1638..50a027e 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,6 @@ go 1.15 require ( github.com/gochore/uniq v1.1.0 github.com/google/go-cmp v0.6.0 + golang.org/x/text v0.14.0 golang.org/x/tools v0.15.0 ) diff --git a/go.sum b/go.sum index 9ab35c0..12d6e33 100644 --- a/go.sum +++ b/go.sum @@ -47,6 +47,7 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= diff --git a/internal/gtag/gtag.go b/internal/gtag/gtag.go index a66d782..b7debc3 100644 --- a/internal/gtag/gtag.go +++ b/internal/gtag/gtag.go @@ -7,11 +7,12 @@ import ( "go/format" "go/parser" "go/token" - "io/ioutil" "os" "strings" "github.com/gochore/uniq" + "golang.org/x/text/cases" + "golang.org/x/text/language" "golang.org/x/tools/go/packages" ) @@ -29,7 +30,7 @@ func Generate(ctx context.Context, dir string, types []string, tags []string) ([ types = types[:uniq.Strings(types)] tags = tags[:uniq.Strings(tags)] - cmd := fmt.Sprintf("gtag -types %s -tags %s .", strings.Join(types, ","), strings.Join(tags, ",")) + cmd := fmt.Sprintf("go run github.com/wolfogre/gtag/cmd/gtag -types %s -tags %s .", strings.Join(types, ","), strings.Join(tags, ",")) pkgs, err := packages.Load(&packages.Config{ Mode: packages.NeedFiles, @@ -49,7 +50,7 @@ func Generate(ctx context.Context, dir string, types []string, tags []string) ([ var ret []*GenerateResult for _, file := range files { - result, err := generateFile(ctx, cmd, file, types, tags) + result, err := generateFile(cmd, file, types, tags) if err != nil { return nil, err } @@ -65,7 +66,7 @@ func Generate(ctx context.Context, dir string, types []string, tags []string) ([ return ret, nil } -func generateFile(ctx context.Context, cmd, file string, types []string, tags []string) (*GenerateResult, error) { +func generateFile(cmd, file string, types []string, tags []string) (*GenerateResult, error) { f, err := loadFile(file) if err != nil { return nil, err @@ -118,7 +119,7 @@ func generateFile(ctx context.Context, cmd, file string, types []string, tags [] for _, tag := range tags { data.Tags = append(data.Tags, templateDataTag{ - Name: strings.Title(strings.ReplaceAll(tag, "_", " ")), + Name: strings.ReplaceAll(cases.Title(language.English).String(strings.ReplaceAll(tag, "_", " ")), " ", ""), Value: tag, }) } @@ -141,7 +142,7 @@ func (r *GenerateResult) Commit() error { if len(r.Content) == 0 { return nil } - return ioutil.WriteFile(r.Output, r.Content, 0666) + return os.WriteFile(r.Output, r.Content, 0o644) } func loadFile(name string) (*ast.File, error) { diff --git a/internal/gtag/gtag_test.go b/internal/gtag/gtag_test.go index 32451c5..5ec94b1 100644 --- a/internal/gtag/gtag_test.go +++ b/internal/gtag/gtag_test.go @@ -26,7 +26,7 @@ func TestGenerate(t *testing.T) { ctx: context.Background(), dir: testDir + "regular/", types: []string{"User", "Empty", "UserName", "UserName"}, - tags: []string{"json", "bson"}, + tags: []string{"json", "bson", "json_x"}, }, want: nil, wantErr: false, diff --git a/test/internal/regular/empty_tag.go b/test/internal/regular/empty_tag.go index 64d771c..597c350 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 Empty,User,UserName -tags bson,json . +//go:generate go run github.com/wolfogre/gtag/cmd/gtag -types Empty,User,UserName -tags bson,json,json_x . package regular import ( @@ -46,3 +46,9 @@ func (*Empty) TagsJson() EmptyTags { var v *Empty return v.Tags("json") } + +// TagsJsonX is alias of Tags("json_x") +func (*Empty) TagsJsonX() EmptyTags { + var v *Empty + return v.Tags("json_x") +} diff --git a/test/internal/regular/user.go b/test/internal/regular/user.go index 34c1956..4c99751 100644 --- a/test/internal/regular/user.go +++ b/test/internal/regular/user.go @@ -1,7 +1,7 @@ package regular type User struct { - Id int `json:"id"` + Id int `json:"id" json_x:"id_x"` Name UserName `json:"name,omitempty"` Email string `json:"email"` age int diff --git a/test/internal/regular/user_tag.go b/test/internal/regular/user_tag.go index 722c7cb..5c27d30 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 Empty,User,UserName -tags bson,json . +//go:generate go run github.com/wolfogre/gtag/cmd/gtag -types Empty,User,UserName -tags bson,json,json_x . package regular import ( @@ -32,7 +32,7 @@ var ( // UserTags indicate tags of type User type UserTags struct { - Id string // `json:"id"` + Id string // `json:"id" json_x:"id_x"` Name string // `json:"name,omitempty"` Email string // `json:"email"` age string // @@ -77,6 +77,12 @@ func (*User) TagsJson() UserTags { return v.Tags("json") } +// TagsJsonX is alias of Tags("json_x") +func (*User) TagsJsonX() UserTags { + var v *User + return v.Tags("json_x") +} + var ( valueOfUserName = UserName{} typeOfUserName = reflect.TypeOf(valueOfUserName) @@ -130,3 +136,9 @@ func (*UserName) TagsJson() UserNameTags { var v *UserName return v.Tags("json") } + +// TagsJsonX is alias of Tags("json_x") +func (*UserName) TagsJsonX() UserNameTags { + var v *UserName + return v.Tags("json_x") +} diff --git a/test/internal/tutorial/user.go b/test/internal/tutorial/user.go index 2682b60..c99cc0a 100644 --- a/test/internal/tutorial/user.go +++ b/test/internal/tutorial/user.go @@ -1,6 +1,6 @@ -// user.go package tutorial +//go:generate go run github.com/wolfogre/gtag/cmd/gtag -types User -tags bson . type User struct { Id int `bson:"_id"` Name string `bson:"name"` diff --git a/test/internal/tutorial/user_tag.go b/test/internal/tutorial/user_tag.go index 3b639d5..0d0f29d 100644 --- a/test/internal/tutorial/user_tag.go +++ b/test/internal/tutorial/user_tag.go @@ -33,6 +33,15 @@ type UserTags struct { Email string // `bson:"email"` } +// Values return all tags of User as slice +func (t *UserTags) Values() []string { + return []string{ + t.Id, + t.Name, + t.Email, + } +} + // 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]) }