Skip to content

Commit

Permalink
General improvement (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfogre authored Nov 15, 2023
1 parent b817711 commit de69ce5
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 43 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
22 changes: 6 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:

Expand All @@ -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:
Expand All @@ -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 (
Expand Down Expand Up @@ -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:

Expand Down
17 changes: 2 additions & 15 deletions cmd/gtag/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
13 changes: 7 additions & 6 deletions internal/gtag/gtag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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,
Expand All @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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,
})
}
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion internal/gtag/gtag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 7 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.

2 changes: 1 addition & 1 deletion test/internal/regular/user.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 14 additions & 2 deletions test/internal/regular/user_tag.go

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

2 changes: 1 addition & 1 deletion test/internal/tutorial/user.go
Original file line number Diff line number Diff line change
@@ -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"`
Expand Down
9 changes: 9 additions & 0 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 de69ce5

Please sign in to comment.