-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3619a08
commit 9b7cae2
Showing
19 changed files
with
1,651 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= | ||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= | ||
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0= | ||
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/tools v0.1.11 h1:loJ25fNOEhSXfHrpoGj91eCUThwdNX6u24rO1xnNteY= | ||
golang.org/x/tools v0.1.11/go.mod h1:SgwaegtQh8clINPpECJMqnxLv9I09HLqnW3RMqW0CA4= | ||
golang.org/x/sys v0.0.0-20220804214406-8e32c043e418 h1:9vYwv7OjYaky/tlAeD7C4oC9EsPTlaFl1H2jS++V+ME= | ||
golang.org/x/sys v0.0.0-20220804214406-8e32c043e418/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= | ||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
//go:build ignore | ||
// +build ignore | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"embed" | ||
"go/format" | ||
"log" | ||
"os" | ||
"regexp" | ||
"text/template" | ||
|
||
"github.com/sashamelentyev/usestdlibvars/pkg/analyzer/internal/mapping" | ||
) | ||
|
||
//go:embed template/* | ||
var templateDir embed.FS | ||
|
||
func main() { | ||
t := template.Must( | ||
template.New("template"). | ||
Funcs(map[string]any{"quoteMeta": regexp.QuoteMeta}). | ||
ParseFS(templateDir, "template/*.tmpl"), | ||
) | ||
|
||
operations := []struct { | ||
mapping map[string]string | ||
packageName string | ||
templateName string | ||
fileName string | ||
}{ | ||
{ | ||
mapping: mapping.CryptoHash, | ||
packageName: "crypto_test", | ||
templateName: "test-template.go.tmpl", | ||
fileName: "pkg/analyzer/testdata/src/a/crypto/crypto.go", | ||
}, | ||
{ | ||
mapping: mapping.HTTPMethod, | ||
packageName: "http_test", | ||
templateName: "test-httpmethod.go.tmpl", | ||
fileName: "pkg/analyzer/testdata/src/a/http/method.go", | ||
}, | ||
{ | ||
mapping: mapping.HTTPStatusCode, | ||
packageName: "http_test", | ||
templateName: "test-httpstatuscode.go.tmpl", | ||
fileName: "pkg/analyzer/testdata/src/a/http/statuscode.go", | ||
}, | ||
{ | ||
mapping: mapping.DefaultRPCPath, | ||
packageName: "rpc_test", | ||
templateName: "test-template.go.tmpl", | ||
fileName: "pkg/analyzer/testdata/src/a/rpc/rpc.go", | ||
}, | ||
{ | ||
mapping: mapping.TimeWeekday, | ||
packageName: "time_test", | ||
templateName: "test-template.go.tmpl", | ||
fileName: "pkg/analyzer/testdata/src/a/time/weekday.go", | ||
}, | ||
{ | ||
mapping: mapping.TimeMonth, | ||
packageName: "time_test", | ||
templateName: "test-template.go.tmpl", | ||
fileName: "pkg/analyzer/testdata/src/a/time/month.go", | ||
}, | ||
{ | ||
mapping: mapping.TimeLayout, | ||
packageName: "time_test", | ||
templateName: "test-template.go.tmpl", | ||
fileName: "pkg/analyzer/testdata/src/a/time/layout.go", | ||
}, | ||
} | ||
|
||
for _, operation := range operations { | ||
data := map[string]any{ | ||
"PackageName": operation.packageName, | ||
"Mapping": operation.mapping, | ||
} | ||
|
||
if err := execute(t, operation.templateName, data, operation.fileName); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
} | ||
|
||
func execute(t *template.Template, templateName string, data any, fileName string) error { | ||
var builder bytes.Buffer | ||
|
||
if err := t.ExecuteTemplate(&builder, templateName, data); err != nil { | ||
return err | ||
} | ||
|
||
sourceData, err := format.Source(builder.Bytes()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := os.WriteFile(fileName, sourceData, os.ModePerm); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Code generated by usestdlibvars, DO NOT EDIT. | ||
|
||
package {{ .PackageName }} | ||
|
||
import "net/http" | ||
|
||
var ( | ||
{{- range $key, $value := .Mapping }} | ||
_ = "{{ $key }}" | ||
{{- end }} | ||
) | ||
|
||
const ( | ||
{{- range $key, $value := .Mapping }} | ||
_ = "{{ $key }}" | ||
{{- end }} | ||
) | ||
|
||
func _() { | ||
{{- range $key, $value := .Mapping }} | ||
_, _ = http.NewRequest("{{ $key }}", "", nil) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}` | ||
{{- end }} | ||
} | ||
|
||
func _() { | ||
{{- range $key, $value := .Mapping }} | ||
_, _ = http.NewRequestWithContext(nil, "{{ $key }}", "", nil) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}` | ||
{{- end }} | ||
} | ||
|
||
func _() { | ||
{{- range $key, $value := .Mapping }} | ||
_ = &http.Request{ | ||
Method: "{{ $key }}", // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}` | ||
} | ||
{{- end }} | ||
} |
31 changes: 31 additions & 0 deletions
31
pkg/analyzer/internal/template/test-httpstatuscode.go.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Code generated by usestdlibvars, DO NOT EDIT. | ||
|
||
package {{ .PackageName }} | ||
|
||
import "net/http" | ||
|
||
var ( | ||
{{- range $key, $value := .Mapping }} | ||
_ = {{ $key }} | ||
{{- end }} | ||
) | ||
|
||
const ( | ||
{{- range $key, $value := .Mapping }} | ||
_ = {{ $key }} | ||
{{- end }} | ||
) | ||
{{ range $key, $value := .Mapping }} | ||
func _() { | ||
var w http.ResponseWriter | ||
w.WriteHeader({{ $key }}) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}` | ||
} | ||
{{ end -}} | ||
|
||
{{ range $key, $value := .Mapping }} | ||
func _() { | ||
_ = &http.Response{ | ||
StatusCode: {{ $key }}, // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}` | ||
} | ||
} | ||
{{ end -}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Code generated by usestdlibvars, DO NOT EDIT. | ||
|
||
package {{ .PackageName }} | ||
|
||
import "fmt" | ||
|
||
var ( | ||
{{- range $key, $value := .Mapping }} | ||
_ = "{{ $key }}" // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}` | ||
{{- end }} | ||
) | ||
|
||
const ( | ||
{{- range $key, $value := .Mapping }} | ||
_ = "{{ $key }}" // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}` | ||
{{- end }} | ||
) | ||
|
||
func _() { | ||
{{- range $key, $value := .Mapping }} | ||
_ = func(s string)string{return s}("{{ $key }}") // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}` | ||
_ = func(s string)string{return s}("text before key {{ $key }}") | ||
_ = func(s string)string{return s}("{{ $key }} text after key") | ||
{{- end }} | ||
} | ||
|
||
func _() { | ||
{{- range $key, $value := .Mapping }} | ||
_ = fmt.Sprint("{{ $key }}") // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}` | ||
_ = fmt.Sprint("text before key {{ $key }}") | ||
_ = fmt.Sprint("{{ $key }} text after key") | ||
{{- end }} | ||
} |
Oops, something went wrong.