Skip to content

Commit 773f14e

Browse files
Merge pull request #29 from sashamelentyev/feat/gen
feat: add gen
2 parents 3619a08 + 9b7cae2 commit 773f14e

File tree

19 files changed

+1651
-260
lines changed

19 files changed

+1651
-260
lines changed

.github/workflows/ci.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ jobs:
3434
- name: Go Mod
3535
run: go mod download
3636

37-
- name: Go Generate
38-
run: go generate ./... && git diff --exit-code
39-
4037
- name: Go Build
4138
run: go build -v ./...
4239

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ module github.com/sashamelentyev/usestdlibvars
22

33
go 1.19
44

5-
require golang.org/x/tools v0.1.11
5+
require golang.org/x/tools v0.1.12
66

77
require (
88
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
9-
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
9+
golang.org/x/sys v0.0.0-20220804214406-8e32c043e418 // indirect
1010
)

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
22
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
3-
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0=
4-
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5-
golang.org/x/tools v0.1.11 h1:loJ25fNOEhSXfHrpoGj91eCUThwdNX6u24rO1xnNteY=
6-
golang.org/x/tools v0.1.11/go.mod h1:SgwaegtQh8clINPpECJMqnxLv9I09HLqnW3RMqW0CA4=
3+
golang.org/x/sys v0.0.0-20220804214406-8e32c043e418 h1:9vYwv7OjYaky/tlAeD7C4oC9EsPTlaFl1H2jS++V+ME=
4+
golang.org/x/sys v0.0.0-20220804214406-8e32c043e418/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5+
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
6+
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"github.com/sashamelentyev/usestdlibvars/pkg/analyzer"
77
)
88

9+
//go:generate go run pkg/analyzer/internal/gen.go
10+
911
func main() {
1012
singlechecker.Main(analyzer.New())
1113
}

pkg/analyzer/analyzer.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"golang.org/x/tools/go/analysis"
1010
"golang.org/x/tools/go/analysis/passes/inspect"
1111
"golang.org/x/tools/go/ast/inspector"
12+
13+
"github.com/sashamelentyev/usestdlibvars/pkg/analyzer/internal/mapping"
1214
)
1315

1416
const (
@@ -158,45 +160,45 @@ func lookupFlag(pass *analysis.Pass, name string) bool {
158160
func checkHTTPMethod(pass *analysis.Pass, basicLit *ast.BasicLit) {
159161
currentVal := getBasicLitValue(basicLit)
160162

161-
if newVal, ok := httpMethod[currentVal]; ok {
163+
if newVal, ok := mapping.HTTPMethod[currentVal]; ok {
162164
report(pass, basicLit.Pos(), currentVal, newVal)
163165
}
164166
}
165167

166168
func checkHTTPStatusCode(pass *analysis.Pass, basicLit *ast.BasicLit) {
167169
currentVal := getBasicLitValue(basicLit)
168170

169-
if newVal, ok := httpStatusCode[currentVal]; ok {
171+
if newVal, ok := mapping.HTTPStatusCode[currentVal]; ok {
170172
report(pass, basicLit.Pos(), currentVal, newVal)
171173
}
172174
}
173175

174176
func checkTimeWeekday(pass *analysis.Pass, pos token.Pos, currentVal string) {
175-
if newVal, ok := timeWeekday[currentVal]; ok {
177+
if newVal, ok := mapping.TimeWeekday[currentVal]; ok {
176178
report(pass, pos, currentVal, newVal)
177179
}
178180
}
179181

180182
func checkTimeMonth(pass *analysis.Pass, pos token.Pos, currentVal string) {
181-
if newVal, ok := timeMonth[currentVal]; ok {
183+
if newVal, ok := mapping.TimeMonth[currentVal]; ok {
182184
report(pass, pos, currentVal, newVal)
183185
}
184186
}
185187

186188
func checkTimeLayout(pass *analysis.Pass, pos token.Pos, currentVal string) {
187-
if newVal, ok := timeLayout[currentVal]; ok {
189+
if newVal, ok := mapping.TimeLayout[currentVal]; ok {
188190
report(pass, pos, currentVal, newVal)
189191
}
190192
}
191193

192194
func checkCryptoHash(pass *analysis.Pass, pos token.Pos, currentVal string) {
193-
if newVal, ok := cryptoHash[currentVal]; ok {
195+
if newVal, ok := mapping.CryptoHash[currentVal]; ok {
194196
report(pass, pos, currentVal, newVal)
195197
}
196198
}
197199

198200
func checkDefaultRPCPath(pass *analysis.Pass, pos token.Pos, currentVal string) {
199-
if newVal, ok := defaultRPCPath[currentVal]; ok {
201+
if newVal, ok := mapping.DefaultRPCPath[currentVal]; ok {
200202
report(pass, pos, currentVal, newVal)
201203
}
202204
}

pkg/analyzer/internal/gen.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//go:build ignore
2+
// +build ignore
3+
4+
package main
5+
6+
import (
7+
"bytes"
8+
"embed"
9+
"go/format"
10+
"log"
11+
"os"
12+
"regexp"
13+
"text/template"
14+
15+
"github.com/sashamelentyev/usestdlibvars/pkg/analyzer/internal/mapping"
16+
)
17+
18+
//go:embed template/*
19+
var templateDir embed.FS
20+
21+
func main() {
22+
t := template.Must(
23+
template.New("template").
24+
Funcs(map[string]any{"quoteMeta": regexp.QuoteMeta}).
25+
ParseFS(templateDir, "template/*.tmpl"),
26+
)
27+
28+
operations := []struct {
29+
mapping map[string]string
30+
packageName string
31+
templateName string
32+
fileName string
33+
}{
34+
{
35+
mapping: mapping.CryptoHash,
36+
packageName: "crypto_test",
37+
templateName: "test-template.go.tmpl",
38+
fileName: "pkg/analyzer/testdata/src/a/crypto/crypto.go",
39+
},
40+
{
41+
mapping: mapping.HTTPMethod,
42+
packageName: "http_test",
43+
templateName: "test-httpmethod.go.tmpl",
44+
fileName: "pkg/analyzer/testdata/src/a/http/method.go",
45+
},
46+
{
47+
mapping: mapping.HTTPStatusCode,
48+
packageName: "http_test",
49+
templateName: "test-httpstatuscode.go.tmpl",
50+
fileName: "pkg/analyzer/testdata/src/a/http/statuscode.go",
51+
},
52+
{
53+
mapping: mapping.DefaultRPCPath,
54+
packageName: "rpc_test",
55+
templateName: "test-template.go.tmpl",
56+
fileName: "pkg/analyzer/testdata/src/a/rpc/rpc.go",
57+
},
58+
{
59+
mapping: mapping.TimeWeekday,
60+
packageName: "time_test",
61+
templateName: "test-template.go.tmpl",
62+
fileName: "pkg/analyzer/testdata/src/a/time/weekday.go",
63+
},
64+
{
65+
mapping: mapping.TimeMonth,
66+
packageName: "time_test",
67+
templateName: "test-template.go.tmpl",
68+
fileName: "pkg/analyzer/testdata/src/a/time/month.go",
69+
},
70+
{
71+
mapping: mapping.TimeLayout,
72+
packageName: "time_test",
73+
templateName: "test-template.go.tmpl",
74+
fileName: "pkg/analyzer/testdata/src/a/time/layout.go",
75+
},
76+
}
77+
78+
for _, operation := range operations {
79+
data := map[string]any{
80+
"PackageName": operation.packageName,
81+
"Mapping": operation.mapping,
82+
}
83+
84+
if err := execute(t, operation.templateName, data, operation.fileName); err != nil {
85+
log.Fatal(err)
86+
}
87+
}
88+
}
89+
90+
func execute(t *template.Template, templateName string, data any, fileName string) error {
91+
var builder bytes.Buffer
92+
93+
if err := t.ExecuteTemplate(&builder, templateName, data); err != nil {
94+
return err
95+
}
96+
97+
sourceData, err := format.Source(builder.Bytes())
98+
if err != nil {
99+
return err
100+
}
101+
102+
if err := os.WriteFile(fileName, sourceData, os.ModePerm); err != nil {
103+
return err
104+
}
105+
106+
return nil
107+
}

pkg/analyzer/mapping.go renamed to pkg/analyzer/internal/mapping/mapping.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package analyzer
1+
package mapping
22

33
import (
44
"crypto"
@@ -8,7 +8,7 @@ import (
88
"time"
99
)
1010

11-
var cryptoHash = map[string]string{
11+
var CryptoHash = map[string]string{
1212
crypto.MD4.String(): "crypto.MD4.String()",
1313
crypto.MD5.String(): "crypto.MD5.String()",
1414
crypto.SHA1.String(): "crypto.SHA1.String()",
@@ -30,7 +30,7 @@ var cryptoHash = map[string]string{
3030
crypto.BLAKE2b_512.String(): "crypto.BLAKE2b_512.String()",
3131
}
3232

33-
var httpMethod = map[string]string{
33+
var HTTPMethod = map[string]string{
3434
http.MethodGet: "http.MethodGet",
3535
http.MethodHead: "http.MethodHead",
3636
http.MethodPost: "http.MethodPost",
@@ -42,7 +42,7 @@ var httpMethod = map[string]string{
4242
http.MethodTrace: "http.MethodTrace",
4343
}
4444

45-
var httpStatusCode = map[string]string{
45+
var HTTPStatusCode = map[string]string{
4646
strconv.Itoa(http.StatusContinue): "http.StatusContinue",
4747
strconv.Itoa(http.StatusSwitchingProtocols): "http.StatusSwitchingProtocols",
4848
strconv.Itoa(http.StatusProcessing): "http.StatusProcessing",
@@ -111,12 +111,12 @@ var httpStatusCode = map[string]string{
111111
strconv.Itoa(http.StatusNetworkAuthenticationRequired): "http.StatusNetworkAuthenticationRequired",
112112
}
113113

114-
var defaultRPCPath = map[string]string{
114+
var DefaultRPCPath = map[string]string{
115115
rpc.DefaultRPCPath: "rpc.DefaultRPCPath",
116116
rpc.DefaultDebugPath: "rpc.DefaultDebugPath",
117117
}
118118

119-
var timeWeekday = map[string]string{
119+
var TimeWeekday = map[string]string{
120120
time.Sunday.String(): "time.Sunday.String()",
121121
time.Monday.String(): "time.Monday.String()",
122122
time.Tuesday.String(): "time.Tuesday.String()",
@@ -126,7 +126,7 @@ var timeWeekday = map[string]string{
126126
time.Saturday.String(): "time.Saturday.String()",
127127
}
128128

129-
var timeMonth = map[string]string{
129+
var TimeMonth = map[string]string{
130130
time.January.String(): "time.January.String()",
131131
time.February.String(): "time.February.String()",
132132
time.March.String(): "time.March.String()",
@@ -141,7 +141,7 @@ var timeMonth = map[string]string{
141141
time.December.String(): "time.December.String()",
142142
}
143143

144-
var timeLayout = map[string]string{
144+
var TimeLayout = map[string]string{
145145
time.Layout: "time.Layout",
146146
time.ANSIC: "time.ANSIC",
147147
time.UnixDate: "time.UnixDate",
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Code generated by usestdlibvars, DO NOT EDIT.
2+
3+
package {{ .PackageName }}
4+
5+
import "net/http"
6+
7+
var (
8+
{{- range $key, $value := .Mapping }}
9+
_ = "{{ $key }}"
10+
{{- end }}
11+
)
12+
13+
const (
14+
{{- range $key, $value := .Mapping }}
15+
_ = "{{ $key }}"
16+
{{- end }}
17+
)
18+
19+
func _() {
20+
{{- range $key, $value := .Mapping }}
21+
_, _ = http.NewRequest("{{ $key }}", "", nil) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
22+
{{- end }}
23+
}
24+
25+
func _() {
26+
{{- range $key, $value := .Mapping }}
27+
_, _ = http.NewRequestWithContext(nil, "{{ $key }}", "", nil) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
28+
{{- end }}
29+
}
30+
31+
func _() {
32+
{{- range $key, $value := .Mapping }}
33+
_ = &http.Request{
34+
Method: "{{ $key }}", // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
35+
}
36+
{{- end }}
37+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Code generated by usestdlibvars, DO NOT EDIT.
2+
3+
package {{ .PackageName }}
4+
5+
import "net/http"
6+
7+
var (
8+
{{- range $key, $value := .Mapping }}
9+
_ = {{ $key }}
10+
{{- end }}
11+
)
12+
13+
const (
14+
{{- range $key, $value := .Mapping }}
15+
_ = {{ $key }}
16+
{{- end }}
17+
)
18+
{{ range $key, $value := .Mapping }}
19+
func _() {
20+
var w http.ResponseWriter
21+
w.WriteHeader({{ $key }}) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
22+
}
23+
{{ end -}}
24+
25+
{{ range $key, $value := .Mapping }}
26+
func _() {
27+
_ = &http.Response{
28+
StatusCode: {{ $key }}, // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
29+
}
30+
}
31+
{{ end -}}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Code generated by usestdlibvars, DO NOT EDIT.
2+
3+
package {{ .PackageName }}
4+
5+
import "fmt"
6+
7+
var (
8+
{{- range $key, $value := .Mapping }}
9+
_ = "{{ $key }}" // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
10+
{{- end }}
11+
)
12+
13+
const (
14+
{{- range $key, $value := .Mapping }}
15+
_ = "{{ $key }}" // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
16+
{{- end }}
17+
)
18+
19+
func _() {
20+
{{- range $key, $value := .Mapping }}
21+
_ = func(s string)string{return s}("{{ $key }}") // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
22+
_ = func(s string)string{return s}("text before key {{ $key }}")
23+
_ = func(s string)string{return s}("{{ $key }} text after key")
24+
{{- end }}
25+
}
26+
27+
func _() {
28+
{{- range $key, $value := .Mapping }}
29+
_ = fmt.Sprint("{{ $key }}") // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
30+
_ = fmt.Sprint("text before key {{ $key }}")
31+
_ = fmt.Sprint("{{ $key }} text after key")
32+
{{- end }}
33+
}

0 commit comments

Comments
 (0)