Skip to content

Commit

Permalink
chore(fix): fix make generaate command (#81)
Browse files Browse the repository at this point in the history
* chore(fix): fix `make generaate` command

* dev: adding `maphash.String/Bytes`

* dev: updating tests & generator
  • Loading branch information
butuzov authored Nov 29, 2024
1 parent 5908922 commit 70b1ff9
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 34 deletions.
1 change: 1 addition & 0 deletions MIRROR_FUNCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
| `func bytes.LastIndexFunc([]byte, func(rune) bool) int` | `func strings.LastIndexFunc(string, func(rune) bool) int` |
| `func bytes.NewBuffer([]byte) *bytes.Buffer` | `func bytes.NewBufferString(string) *bytes.Buffer` |
| `func (*httptest.ResponseRecorder) Write([]byte) (int, error)` | `func (*httptest.ResponseRecorder) WriteString(string) (int, error)` |
| `func maphash.Bytes([]byte) uint64` | `func maphash.String(string) uint64` |
| `func (*maphash.Hash) Write([]byte) (int, error)` | `func (*maphash.Hash) WriteString(string) (int, error)` |
| `func (*os.File) Write([]byte) (int, error)` | `func (*os.File) WriteString(string) (int, error)` |
| `func regexp.Match(string, []byte) (bool, error)` | `func regexp.MatchString(string, string) (bool, error)` |
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ endef

# Generate Artifacts ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
generate: ## Generate Assets
$(MAKE)
$(MAKE) generate-tests
$(MAKE) generate-mirror-table

generate-tests: ## Generates Assets at testdata
go run ./cmd/internal/tests/ "$(PWD)/testdata"
Expand Down
4 changes: 2 additions & 2 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ func Run(pass *analysis.Pass, withTests bool) []*checker.Violation {
BytesFunctions, BytesBufferMethods,
RegexpFunctions, RegexpRegexpMethods,
StringFunctions, StringsBuilderMethods,
MaphashMethods, MaphashFunctions,
BufioMethods, HTTPTestMethods,
OsFileMethods, MaphashMethods,
UTF8Functions,
OsFileMethods, UTF8Functions,
)

check.Type = checker.WrapType(pass.TypesInfo)
Expand Down
87 changes: 59 additions & 28 deletions checkers_maphash.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,66 @@ package mirror

import "github.com/butuzov/mirror/internal/checker"

var MaphashMethods = []checker.Violation{
{ // (*hash/maphash).Write
Targets: checker.Bytes,
Type: checker.Method,
Package: "hash/maphash",
Struct: "Hash",
Caller: "Write",
Args: []int{0},
AltCaller: "WriteString",
var (
MaphashFunctions = []checker.Violation{
{ // maphash.Bytes
Targets: checker.Bytes,
Type: checker.Function,
Package: "hash/maphash",
Caller: "Bytes",
Args: []int{1},
AltCaller: "String",

Generate: &checker.Generate{
PreCondition: `h := maphash.Hash{}`,
Pattern: `Write($0)`,
Returns: []string{"int", "error"},
Generate: &checker.Generate{
Pattern: `Bytes(maphash.MakeSeed(), $0)`,
Returns: []string{"uint64"},
},
},
},
{ // (*hash/maphash).WriteString
Targets: checker.Strings,
Type: checker.Method,
Package: "hash/maphash",
Struct: "Hash",
Caller: "WriteString",
Args: []int{0},
AltCaller: "Write",
{ // maphash.String
Targets: checker.Strings,
Type: checker.Function,
Package: "hash/maphash",
Caller: "String",
Args: []int{1},
AltCaller: "Bytes",

Generate: &checker.Generate{
PreCondition: `h := maphash.Hash{}`,
Pattern: `WriteString($0)`,
Returns: []string{"int", "error"},
Generate: &checker.Generate{
Pattern: `String(maphash.MakeSeed(), $0)`,
Returns: []string{"uint64"},
},
},
},
}
}

MaphashMethods = []checker.Violation{
{ // (*hash/maphash).Write
Targets: checker.Bytes,
Type: checker.Method,
Package: "hash/maphash",
Struct: "Hash",
Caller: "Write",
Args: []int{0},
AltCaller: "WriteString",

Generate: &checker.Generate{
PreCondition: `h := maphash.Hash{}`,
Pattern: `Write($0)`,
Returns: []string{"int", "error"},
},
},
{ // (*hash/maphash).WriteString
Targets: checker.Strings,
Type: checker.Method,
Package: "hash/maphash",
Struct: "Hash",
Caller: "WriteString",
Args: []int{0},
AltCaller: "Write",

Generate: &checker.Generate{
PreCondition: `h := maphash.Hash{}`,
Pattern: `WriteString($0)`,
Returns: []string{"int", "error"},
},
},
}
)
1 change: 1 addition & 0 deletions cmd/internal/mirror-table/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func main() {
mirror.BytesBufferMethods,
mirror.BytesFunctions,
mirror.HTTPTestMethods,
mirror.MaphashFunctions,
mirror.MaphashMethods,
mirror.OsFileMethods,
mirror.RegexpFunctions,
Expand Down
3 changes: 2 additions & 1 deletion cmd/internal/mirror-table/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"os"
"regexp"
"strings"

Expand Down Expand Up @@ -80,7 +81,7 @@ func formArgs(v checker.Violation, isAlt bool) string {
f := strings.Split(i, "{")
a = append(a, strings.TrimSpace(f[0]))
default:
fmt.Println(">", i)
fmt.Fprintln(os.Stderr, ">", i)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions cmd/internal/tests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func main() {
{ // hash/maphash

tests := []string{}
tests = append(tests, generateTests("maphash", mirror.MaphashFunctions)...)
tests = append(tests, generateTests("maphash", mirror.MaphashMethods)...)

err := GenerateTestFile(filepath.Join(testdata, "maphash.go"), "hash/maphash", tests)
Expand Down
4 changes: 2 additions & 2 deletions cmd/internal/tests/templates/case.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
{{if .PreCond}}{{.PreCond}}{{end}}
{{- range $v := .Arguments}}
{{if .PreCond}}{{.PreCond}}{{end -}}
{{range $v := .Arguments}}
{{$v}}
{{- end}}
{{.Returns}} = {{if .Package}}{{.Package}}.{{end}}{{.Func}} {{if .Want}}// want `{{.Want}}`{{end}}
Expand Down
60 changes: 60 additions & 0 deletions testdata/maphash.go

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

0 comments on commit 70b1ff9

Please sign in to comment.