Skip to content

Commit 70b1ff9

Browse files
authored
chore(fix): fix make generaate command (#81)
* chore(fix): fix `make generaate` command * dev: adding `maphash.String/Bytes` * dev: updating tests & generator
1 parent 5908922 commit 70b1ff9

File tree

9 files changed

+130
-34
lines changed

9 files changed

+130
-34
lines changed

MIRROR_FUNCS.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
| `func bytes.LastIndexFunc([]byte, func(rune) bool) int` | `func strings.LastIndexFunc(string, func(rune) bool) int` |
2626
| `func bytes.NewBuffer([]byte) *bytes.Buffer` | `func bytes.NewBufferString(string) *bytes.Buffer` |
2727
| `func (*httptest.ResponseRecorder) Write([]byte) (int, error)` | `func (*httptest.ResponseRecorder) WriteString(string) (int, error)` |
28+
| `func maphash.Bytes([]byte) uint64` | `func maphash.String(string) uint64` |
2829
| `func (*maphash.Hash) Write([]byte) (int, error)` | `func (*maphash.Hash) WriteString(string) (int, error)` |
2930
| `func (*os.File) Write([]byte) (int, error)` | `func (*os.File) WriteString(string) (int, error)` |
3031
| `func regexp.Match(string, []byte) (bool, error)` | `func regexp.MatchString(string, string) (bool, error)` |

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ endef
1010

1111
# Generate Artifacts ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1212
generate: ## Generate Assets
13-
$(MAKE)
13+
$(MAKE) generate-tests
14+
$(MAKE) generate-mirror-table
1415

1516
generate-tests: ## Generates Assets at testdata
1617
go run ./cmd/internal/tests/ "$(PWD)/testdata"

analyzer.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ func Run(pass *analysis.Pass, withTests bool) []*checker.Violation {
4444
BytesFunctions, BytesBufferMethods,
4545
RegexpFunctions, RegexpRegexpMethods,
4646
StringFunctions, StringsBuilderMethods,
47+
MaphashMethods, MaphashFunctions,
4748
BufioMethods, HTTPTestMethods,
48-
OsFileMethods, MaphashMethods,
49-
UTF8Functions,
49+
OsFileMethods, UTF8Functions,
5050
)
5151

5252
check.Type = checker.WrapType(pass.TypesInfo)

checkers_maphash.go

+59-28
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,66 @@ package mirror
22

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

5-
var MaphashMethods = []checker.Violation{
6-
{ // (*hash/maphash).Write
7-
Targets: checker.Bytes,
8-
Type: checker.Method,
9-
Package: "hash/maphash",
10-
Struct: "Hash",
11-
Caller: "Write",
12-
Args: []int{0},
13-
AltCaller: "WriteString",
5+
var (
6+
MaphashFunctions = []checker.Violation{
7+
{ // maphash.Bytes
8+
Targets: checker.Bytes,
9+
Type: checker.Function,
10+
Package: "hash/maphash",
11+
Caller: "Bytes",
12+
Args: []int{1},
13+
AltCaller: "String",
1414

15-
Generate: &checker.Generate{
16-
PreCondition: `h := maphash.Hash{}`,
17-
Pattern: `Write($0)`,
18-
Returns: []string{"int", "error"},
15+
Generate: &checker.Generate{
16+
Pattern: `Bytes(maphash.MakeSeed(), $0)`,
17+
Returns: []string{"uint64"},
18+
},
1919
},
20-
},
21-
{ // (*hash/maphash).WriteString
22-
Targets: checker.Strings,
23-
Type: checker.Method,
24-
Package: "hash/maphash",
25-
Struct: "Hash",
26-
Caller: "WriteString",
27-
Args: []int{0},
28-
AltCaller: "Write",
20+
{ // maphash.String
21+
Targets: checker.Strings,
22+
Type: checker.Function,
23+
Package: "hash/maphash",
24+
Caller: "String",
25+
Args: []int{1},
26+
AltCaller: "Bytes",
2927

30-
Generate: &checker.Generate{
31-
PreCondition: `h := maphash.Hash{}`,
32-
Pattern: `WriteString($0)`,
33-
Returns: []string{"int", "error"},
28+
Generate: &checker.Generate{
29+
Pattern: `String(maphash.MakeSeed(), $0)`,
30+
Returns: []string{"uint64"},
31+
},
3432
},
35-
},
36-
}
33+
}
34+
35+
MaphashMethods = []checker.Violation{
36+
{ // (*hash/maphash).Write
37+
Targets: checker.Bytes,
38+
Type: checker.Method,
39+
Package: "hash/maphash",
40+
Struct: "Hash",
41+
Caller: "Write",
42+
Args: []int{0},
43+
AltCaller: "WriteString",
44+
45+
Generate: &checker.Generate{
46+
PreCondition: `h := maphash.Hash{}`,
47+
Pattern: `Write($0)`,
48+
Returns: []string{"int", "error"},
49+
},
50+
},
51+
{ // (*hash/maphash).WriteString
52+
Targets: checker.Strings,
53+
Type: checker.Method,
54+
Package: "hash/maphash",
55+
Struct: "Hash",
56+
Caller: "WriteString",
57+
Args: []int{0},
58+
AltCaller: "Write",
59+
60+
Generate: &checker.Generate{
61+
PreCondition: `h := maphash.Hash{}`,
62+
Pattern: `WriteString($0)`,
63+
Returns: []string{"int", "error"},
64+
},
65+
},
66+
}
67+
)

cmd/internal/mirror-table/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func main() {
1717
mirror.BytesBufferMethods,
1818
mirror.BytesFunctions,
1919
mirror.HTTPTestMethods,
20+
mirror.MaphashFunctions,
2021
mirror.MaphashMethods,
2122
mirror.OsFileMethods,
2223
mirror.RegexpFunctions,

cmd/internal/mirror-table/support.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"os"
56
"regexp"
67
"strings"
78

@@ -80,7 +81,7 @@ func formArgs(v checker.Violation, isAlt bool) string {
8081
f := strings.Split(i, "{")
8182
a = append(a, strings.TrimSpace(f[0]))
8283
default:
83-
fmt.Println(">", i)
84+
fmt.Fprintln(os.Stderr, ">", i)
8485
}
8586
}
8687
}

cmd/internal/tests/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func main() {
6161
{ // hash/maphash
6262

6363
tests := []string{}
64+
tests = append(tests, generateTests("maphash", mirror.MaphashFunctions)...)
6465
tests = append(tests, generateTests("maphash", mirror.MaphashMethods)...)
6566

6667
err := GenerateTestFile(filepath.Join(testdata, "maphash.go"), "hash/maphash", tests)

cmd/internal/tests/templates/case.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
{{if .PreCond}}{{.PreCond}}{{end}}
3-
{{- range $v := .Arguments}}
2+
{{if .PreCond}}{{.PreCond}}{{end -}}
3+
{{range $v := .Arguments}}
44
{{$v}}
55
{{- end}}
66
{{.Returns}} = {{if .Package}}{{.Package}}.{{end}}{{.Func}} {{if .Want}}// want `{{.Want}}`{{end}}

testdata/maphash.go

+60
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)