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 709520b
Showing
17 changed files
with
1,587 additions
and
252 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
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,50 @@ | ||
package main | ||
|
||
import ( | ||
"embed" | ||
"github.com/sashamelentyev/usestdlibvars/pkg/analyzer/internal/mapping" | ||
"log" | ||
"os" | ||
"regexp" | ||
"text/template" | ||
) | ||
|
||
type tmpl struct { | ||
PackageName string | ||
Mapping map[string]string | ||
} | ||
|
||
//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"), | ||
) | ||
execute("pkg/analyzer/testdata/src/a/crypto/crypto.go", t, "template.tmpl", tmpl{PackageName: "crypto", Mapping: mapping.CryptoHash}) | ||
execute("pkg/analyzer/testdata/src/a/http/method.go", t, "httpmethod.tmpl", tmpl{PackageName: "http", Mapping: mapping.HTTPMethod}) | ||
execute("pkg/analyzer/testdata/src/a/http/statuscode.go", t, "httpstatuscode.tmpl", tmpl{PackageName: "http", Mapping: mapping.HTTPStatusCode}) | ||
execute("pkg/analyzer/testdata/src/a/rpc/rpc.go", t, "template.tmpl", tmpl{PackageName: "rpc", Mapping: mapping.DefaultRPCPath}) | ||
execute("pkg/analyzer/testdata/src/a/time/weekday.go", t, "template.tmpl", tmpl{PackageName: "time", Mapping: mapping.TimeWeekday}) | ||
execute("pkg/analyzer/testdata/src/a/time/month.go", t, "template.tmpl", tmpl{PackageName: "time", Mapping: mapping.TimeMonth}) | ||
execute("pkg/analyzer/testdata/src/a/time/layout.go", t, "template.tmpl", tmpl{PackageName: "time", Mapping: mapping.TimeLayout}) | ||
} | ||
|
||
func execute(fileName string, t *template.Template, templateName string, data any) { | ||
f, err := os.Create(fileName) | ||
mustNil(err) | ||
defer f.Close() | ||
t.ExecuteTemplate( | ||
f, | ||
templateName, | ||
data, | ||
) | ||
} | ||
|
||
func mustNil(err error) { | ||
if err != nil { | ||
log.Panic(err) | ||
} | ||
} |
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 }} | ||
} |
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.