Skip to content

Commit 738e06f

Browse files
Merge pull request #33 from sashamelentyev/fix/issue32
fix: convert HTTP method current value to upper
2 parents 773f14e + a5d7611 commit 738e06f

File tree

4 files changed

+132
-2
lines changed

4 files changed

+132
-2
lines changed

pkg/analyzer/analyzer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func lookupFlag(pass *analysis.Pass, name string) bool {
158158
}
159159

160160
func checkHTTPMethod(pass *analysis.Pass, basicLit *ast.BasicLit) {
161-
currentVal := getBasicLitValue(basicLit)
161+
currentVal := strings.ToUpper(getBasicLitValue(basicLit))
162162

163163
if newVal, ok := mapping.HTTPMethod[currentVal]; ok {
164164
report(pass, basicLit.Pos(), currentVal, newVal)

pkg/analyzer/internal/gen.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"log"
1111
"os"
1212
"regexp"
13+
"strings"
1314
"text/template"
1415

1516
"github.com/sashamelentyev/usestdlibvars/pkg/analyzer/internal/mapping"
@@ -21,7 +22,10 @@ var templateDir embed.FS
2122
func main() {
2223
t := template.Must(
2324
template.New("template").
24-
Funcs(map[string]any{"quoteMeta": regexp.QuoteMeta}).
25+
Funcs(map[string]any{
26+
"quoteMeta": regexp.QuoteMeta,
27+
"lower": strings.ToLower,
28+
}).
2529
ParseFS(templateDir, "template/*.tmpl"),
2630
)
2731

@@ -73,6 +77,12 @@ func main() {
7377
templateName: "test-template.go.tmpl",
7478
fileName: "pkg/analyzer/testdata/src/a/time/layout.go",
7579
},
80+
{
81+
mapping: mapping.HTTPMethod,
82+
packageName: "http_test",
83+
templateName: "test-issue32.go.tmpl",
84+
fileName: "pkg/analyzer/testdata/src/a/http/issue32.go",
85+
},
7686
}
7787

7888
for _, operation := range operations {
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+
_ = "{{ lower $key }}"
10+
{{- end }}
11+
)
12+
13+
const (
14+
{{- range $key, $value := .Mapping }}
15+
_ = "{{ lower $key }}"
16+
{{- end }}
17+
)
18+
19+
func _() {
20+
{{- range $key, $value := .Mapping }}
21+
_, _ = http.NewRequest("{{ lower $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, "{{ lower $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: "{{ lower $key }}", // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
35+
}
36+
{{- end }}
37+
}

pkg/analyzer/testdata/src/a/http/issue32.go

Lines changed: 83 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)