Skip to content

Commit

Permalink
Merge pull request #4 from wolfogre/dev
Browse files Browse the repository at this point in the history
chore: use goreleaser
  • Loading branch information
wolfogre authored May 5, 2020
2 parents 2f3d4c1 + 9605543 commit 48c2844
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.idea/
/dist/

# Created by https://www.gitignore.io/api/go,intellij,visualstudiocode,vim,macos
# Edit at https://www.gitignore.io/?templates=go,intellij,visualstudiocode,vim,macos
Expand Down
21 changes: 21 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# This is an example goreleaser.yaml file with some sane defaults.
# Make sure to check the documentation at http://goreleaser.com
builds:
-
main: ./cmd/gtag/main.go
env:
- CGO_ENABLED=0
goos:
- linux
- darwin
- windows
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ .Tag }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
40 changes: 5 additions & 35 deletions internal/gtag/gtag.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gtag

import (
"bytes"
"context"
"fmt"
"go/ast"
Expand Down Expand Up @@ -34,42 +33,13 @@ func Generate(ctx context.Context, file, name string) (*GenerateResult, error) {
}
pkg := f.Name.Name

buffer := &bytes.Buffer{}

fmt.Fprintf(buffer, "// Code generated by gtag. DO NOT EDIT.\n")
fmt.Fprintf(buffer, "// See: https://github.com/wolfogre/gtag\n\n")

fmt.Fprintf(buffer, "//go:generate gtag\n")

fmt.Fprintf(buffer, "package %s\n", pkg)

fmt.Fprintf(buffer, "import \"reflect\"\n")

fmt.Fprintf(buffer, "var (\n")
fmt.Fprintf(buffer, "valueOf%s = %s{}\n", name, name)
fmt.Fprintf(buffer, "typeOf%s = reflect.TypeOf(valueOf%s)\n\n", name, name)
for _, field := range fields {
fmt.Fprintf(buffer, "_ = valueOfUser.%s\n", field)
fmt.Fprintf(buffer, "fieldOf%s%s, _ = typeOf%s.FieldByName(\"%s\")\n", name, field, name, field)
fmt.Fprintf(buffer, "tagOf%s%s = fieldOf%s%s.Tag\n\n", name, field, name, field)

}
fmt.Fprintf(buffer, ")\n")

fmt.Fprintf(buffer, "type %sTags struct {\n", name)
for _, field := range fields {
fmt.Fprintf(buffer, "\t%s string\n", field)
}
fmt.Fprintf(buffer, "}\n")

fmt.Fprintf(buffer, "func (%s) Tags(tag string) %sTags {\n", name, name)
fmt.Fprintf(buffer, "return %sTags{\n", name)
for _, field := range fields {
fmt.Fprintf(buffer, "%s: tagOf%s%s.Get(tag),\n", field, name, field)
data := templateData{
Package: pkg,
Type: name,
Fields: fields,
}
fmt.Fprintf(buffer, "}\n}\n")

src, err := format.Source(buffer.Bytes())
src, err := format.Source(execute(data))
if err != nil {
return nil, err
}
Expand Down
60 changes: 60 additions & 0 deletions internal/gtag/template.go

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

71 changes: 71 additions & 0 deletions internal/gtag/template_test.go

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

0 comments on commit 48c2844

Please sign in to comment.