Skip to content

Commit ab0ddc6

Browse files
Antonboommaranqz
andauthored
Cosmetic fixes (#7)
* cosmetic fixes * renamed linter * stringsFlag replaced with globsFlag * onlyPackageGlobs renamed to packageGlobsOnly --------- Co-authored-by: Ilia Sergunin <[email protected]>
1 parent 99d4377 commit ab0ddc6

File tree

15 files changed

+108
-104
lines changed

15 files changed

+108
-104
lines changed

.github/workflows/release.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
- v*
77

88
env:
9-
GO_VERSION: "1.21"
9+
GO_VERSION: "1.20"
1010

1111
jobs:
1212

@@ -36,6 +36,6 @@ jobs:
3636
with:
3737
version: latest
3838
args: release --clean
39-
workdir: cmd/go-factory-lint/
39+
workdir: cmd/gofactory/
4040
env:
41-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
41+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.golangci.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ linters-settings:
88
- v
99
- ok
1010
gci:
11-
local-prefixes: github.com/maranqz/go-factory-lint
11+
local-prefixes: github.com/maranqz/gofactory
1212

1313
linters:
1414
enable-all: true
@@ -36,6 +36,7 @@ issues:
3636
- path: lint_test
3737
linters:
3838
- varnamelen
39+
- wrapcheck
3940
- linters:
4041
- lll
4142
source: "^(?: |\t)*// "
@@ -45,4 +46,4 @@ issues:
4546
- linters:
4647
- godot
4748
- lll
48-
source: "// ?TODO "
49+
source: "// ?TODO "

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
.PHONY: fmt lint clean.test test test.clean
2+
13
CVPKG=go list ./... | grep -v mocks | grep -v internal/
24
GO_TEST=go test `$(CVPKG)` -race
35
COVERAGE_FILE="coverage.out"
46

7+
all: fmt lint test install
8+
9+
fmt:
10+
go fmt ./...
11+
12+
lint:
13+
golangci-lint run --fix ./...
14+
515
clean.test:
616
go clean --testcache
717

@@ -12,3 +22,6 @@ test.clean: clean.test test
1222

1323
test.coverage:
1424
$(GO_TEST) -covermode=atomic -coverprofile=$(COVERAGE_FILE)
25+
26+
install:
27+
go install ./cmd/gofactory

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
# Factory linter
22

3-
[![CI](https://github.com/maranqz/go-factory-lint/actions/workflows/ci.yml/badge.svg)](https://github.com/maranqz/go-factory-lint/actions/workflows/ci.yml)
4-
[![Go Report Card](https://goreportcard.com/badge/github.com/maranqz/go-factory-lint)](https://goreportcard.com/report/github.com/maranqz/go-factory-lint?dummy=unused)
3+
[![CI](https://github.com/maranqz/gofactory/actions/workflows/ci.yml/badge.svg)](https://github.com/maranqz/gofactory/actions/workflows/ci.yml)
4+
[![Go Report Card](https://goreportcard.com/badge/github.com/maranqz/gofactory)](https://goreportcard.com/report/github.com/maranqz/gofactory?dummy=unused)
55
[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE)
6-
[![Coverage Status](https://coveralls.io/repos/github/maranqz/go-factory-lint/badge.svg?branch=main)](https://coveralls.io/github/maranqz/go-factory-lint?branch=main)
6+
[![Coverage Status](https://coveralls.io/repos/github/maranqz/gofactory/badge.svg?branch=main)](https://coveralls.io/github/maranqz/gofactory?branch=main)
77

88
The linter checks that the Structures are created by the Factory, and not directly.
99

1010
The checking helps to provide invariants without exclusion and helps avoid creating an invalid object.
1111

1212

13-
## Use
13+
## Usage
1414

15-
Installation
15+
### Installation
1616

17-
go install github.com/maranqz/go-factory-lint/cmd/go-factory-lint@latest
17+
go install github.com/maranqz/gofactory/cmd/gofactory@latest
1818

1919
### Options
2020

21-
- `--packageGlobs` - list of glob packages, which can create structures without factories inside the glob package. By default, all structures from another package should be created by factories, [tests](testdata/src/factory/packageGlobs).
22-
- `onlyPackageGlobs` - use a factory to initiate a structure for glob packages only, [tests](testdata/src/factory/onlyPackageGlobs).
21+
- `--packageGlobs` – list of glob packages, which can create structures without factories inside the glob package.
22+
By default, all structures from another package should be created by factories, [tests](testdata/src/factory/packageGlobs).
23+
- `--packageGlobsOnly` – use a factory to initiate a structure for glob packages only,
24+
[tests](testdata/src/factory/packageGlobsOnly). Doesn't make sense without `--packageGlobs`.
2325

2426
## Example
2527

@@ -128,6 +130,7 @@ Linter doesn't catch some cases.
128130
2. Local initialization, [example](testdata/src/factory/unimplemented/local/).
129131
3. Named return. If you want to block that case, you can use [nonamedreturns](https://github.com/firefart/nonamedreturns) linter, [example](testdata/src/factory/unimplemented/named_return.go).
130132
4. var declaration, `var initilized nested.Struct` gives structure without factory, [example](testdata/src/factory/unimplemented/var.go).
133+
To block that case, you can use [gopublicfield](github.com/maranqz/gopublicfield) to prevent fill of structure fields.
131134

132135
## TODO
133136

cmd/go-factory-lint/main.go renamed to cmd/gofactory/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package main
33
import (
44
"golang.org/x/tools/go/analysis/singlechecker"
55

6-
"github.com/maranqz/go-factory-lint/v2"
6+
"github.com/maranqz/gofactory"
77
)
88

99
func main() {
10-
singlechecker.Main(factory.NewAnalyzer())
10+
singlechecker.Main(gofactory.NewAnalyzer())
1111
}

factory.go

Lines changed: 17 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,38 @@
1-
package factory
1+
package gofactory
22

33
import (
4-
"fmt"
54
"go/ast"
65
"go/types"
76
"log"
8-
"strings"
97

108
"github.com/gobwas/glob"
119
"golang.org/x/tools/go/analysis"
12-
"golang.org/x/tools/go/analysis/passes/inspect"
1310
)
1411

1512
type config struct {
16-
pkgGlobs stringsFlag
13+
pkgGlobs globsFlag
1714
onlyPkgGlobs bool
1815
}
1916

20-
type stringsFlag []string
21-
22-
func (s stringsFlag) String() string {
23-
return strings.Join(s, ", ")
24-
}
25-
26-
func (s *stringsFlag) Set(value string) error {
27-
*s = append(*s, value)
28-
29-
return nil
30-
}
31-
32-
func (s stringsFlag) Value() []string {
33-
res := make([]string, 0, len(s))
34-
35-
for _, str := range s {
36-
res = append(res, strings.TrimSpace(str))
37-
}
38-
39-
return res
40-
}
41-
4217
const (
43-
packageGlobsDesc = "List of glob packages, which can create structures without factories inside the glob package"
44-
onlyPkgGlobsDesc = "Use a factory to initiate a structure for glob packages only."
18+
name = "gofactory"
19+
doc = "Blocks the creation of structures directly, without a factory."
20+
21+
packageGlobsDesc = "list of glob packages, which can create structures without factories inside the glob package"
22+
onlyPkgGlobsDesc = "use a factory to initiate a structure for glob packages only"
4523
)
4624

4725
func NewAnalyzer() *analysis.Analyzer {
4826
analyzer := &analysis.Analyzer{
49-
Name: "gofactory",
50-
Doc: "Blocks the creation of structures directly, without a factory.",
51-
Requires: []*analysis.Analyzer{inspect.Analyzer},
27+
Name: name,
28+
Doc: doc,
5229
}
5330

5431
cfg := config{}
5532

5633
analyzer.Flags.Var(&cfg.pkgGlobs, "packageGlobs", packageGlobsDesc)
5734

58-
analyzer.Flags.BoolVar(&cfg.onlyPkgGlobs, "onlyPackageGlobs", false, onlyPkgGlobsDesc)
35+
analyzer.Flags.BoolVar(&cfg.onlyPkgGlobs, "packageGlobsOnly", false, onlyPkgGlobsDesc)
5936

6037
analyzer.Run = run(&cfg)
6138

@@ -65,17 +42,14 @@ func NewAnalyzer() *analysis.Analyzer {
6542
func run(cfg *config) func(pass *analysis.Pass) (interface{}, error) {
6643
return func(pass *analysis.Pass) (interface{}, error) {
6744
var blockedStrategy blockedStrategy = newAnotherPkg()
68-
if len(cfg.pkgGlobs) > 0 {
45+
46+
pkgGlobs := cfg.pkgGlobs.Value()
47+
if len(pkgGlobs) > 0 {
6948
defaultStrategy := blockedStrategy
7049
if cfg.onlyPkgGlobs {
7150
defaultStrategy = newNilPkg()
7251
}
7352

74-
pkgGlobs, err := compileGlobs(cfg.pkgGlobs.Value())
75-
if err != nil {
76-
return nil, err
77-
}
78-
7953
blockedStrategy = newBlockedPkgs(
8054
pkgGlobs,
8155
defaultStrategy,
@@ -231,18 +205,14 @@ func (v *visitor) checkBrackets(expr ast.Expr, identObj types.Object) {
231205
func (v *visitor) report(node ast.Node, obj types.Object) {
232206
v.pass.Reportf(
233207
node.Pos(),
234-
fmt.Sprintf(`Use factory for %s.%s`, obj.Pkg().Name(), obj.Name()),
208+
"Use factory for %s.%s", obj.Pkg().Name(), obj.Name(),
235209
)
236210
}
237211

238212
func (v *visitor) unexpectedCode(node ast.Node) {
239-
fset := v.pass.Fset
240-
pos := fset.Position(node.Pos())
241-
242-
log.Printf("Unexpected code in %s:%d:%d, please report to the developer with example.\n",
243-
fset.File(node.Pos()).Name(),
244-
pos.Line,
245-
pos.Column,
213+
log.Printf("%s: unexpected code in %s, please report to the developer with example.\n",
214+
name,
215+
v.pass.Fset.Position(node.Pos()),
246216
)
247217
}
248218

@@ -255,18 +225,3 @@ func containsMatchGlob(globs []glob.Glob, el string) bool {
255225

256226
return false
257227
}
258-
259-
func compileGlobs(globs []string) ([]glob.Glob, error) {
260-
compiledGlobs := make([]glob.Glob, len(globs))
261-
262-
for idx, globString := range globs {
263-
glob, err := glob.Compile(globString)
264-
if err != nil {
265-
return nil, fmt.Errorf("unable to compile globs %s: %w", glob, err)
266-
}
267-
268-
compiledGlobs[idx] = glob
269-
}
270-
271-
return compiledGlobs, nil
272-
}

globs_flag.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package gofactory
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/gobwas/glob"
8+
)
9+
10+
type globsFlag struct {
11+
globsString []string
12+
globs []glob.Glob
13+
}
14+
15+
func (g globsFlag) String() string {
16+
return strings.Join(g.globsString, ", ")
17+
}
18+
19+
func (g *globsFlag) Set(globString string) error {
20+
globString = strings.TrimSpace(globString)
21+
22+
compiled, err := glob.Compile(globString)
23+
if err != nil {
24+
return fmt.Errorf("unable to compile globs %s: %w", globString, err)
25+
}
26+
27+
g.globsString = append(g.globsString, globString)
28+
g.globs = append(g.globs, compiled)
29+
30+
return nil
31+
}
32+
33+
func (g globsFlag) Value() []glob.Glob {
34+
return g.globs
35+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/maranqz/go-factory-lint/v2
1+
module github.com/maranqz/gofactory
22

33
go 1.20
44

lint_test.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package factory_test
1+
package gofactory_test
22

33
import (
44
"path/filepath"
@@ -7,7 +7,7 @@ import (
77
"golang.org/x/tools/go/analysis"
88
"golang.org/x/tools/go/analysis/analysistest"
99

10-
"github.com/maranqz/go-factory-lint/v2"
10+
"github.com/maranqz/gofactory"
1111
)
1212

1313
func TestLinterSuite(t *testing.T) {
@@ -17,29 +17,25 @@ func TestLinterSuite(t *testing.T) {
1717

1818
tests := map[string]struct {
1919
pkgs []string
20-
prepare func(t *testing.T, a *analysis.Analyzer)
20+
prepare func(t *testing.T, a *analysis.Analyzer) error
2121
}{
2222
"simple": {pkgs: []string{"simple/..."}},
2323
"casting": {pkgs: []string{"casting/..."}},
2424
"generic": {pkgs: []string{"generic/..."}},
2525
"packageGlobs": {
2626
pkgs: []string{"packageGlobs/..."},
27-
prepare: func(t *testing.T, a *analysis.Analyzer) {
28-
if err := a.Flags.Set("packageGlobs", "factory/packageGlobs/blocked/**"); err != nil {
29-
t.Fatal(err)
30-
}
27+
prepare: func(t *testing.T, a *analysis.Analyzer) error {
28+
return a.Flags.Set("packageGlobs", "factory/packageGlobs/blocked/**")
3129
},
3230
},
33-
"onlyPackageGlobs": {
34-
pkgs: []string{"onlyPackageGlobs/main/..."},
35-
prepare: func(t *testing.T, a *analysis.Analyzer) {
36-
if err := a.Flags.Set("packageGlobs", "factory/onlyPackageGlobs/blocked/**"); err != nil {
37-
t.Fatal(err)
31+
"packageGlobsOnly": {
32+
pkgs: []string{"packageGlobsOnly/main/..."},
33+
prepare: func(t *testing.T, a *analysis.Analyzer) error {
34+
if err := a.Flags.Set("packageGlobs", "factory/packageGlobsOnly/blocked/**"); err != nil {
35+
return err
3836
}
3937

40-
if err := a.Flags.Set("onlyPackageGlobs", "true"); err != nil {
41-
t.Fatal(err)
42-
}
38+
return a.Flags.Set("packageGlobsOnly", "true")
4339
},
4440
},
4541
}
@@ -55,14 +51,15 @@ func TestLinterSuite(t *testing.T) {
5551
dirs = append(dirs, filepath.Join(testdata, "src", "factory", pkg))
5652
}
5753

58-
analyzer := factory.NewAnalyzer()
54+
analyzer := gofactory.NewAnalyzer()
5955

6056
if tt.prepare != nil {
61-
tt.prepare(t, analyzer)
57+
if err := tt.prepare(t, analyzer); err != nil {
58+
t.Fatal(err)
59+
}
6260
}
6361

64-
analysistest.Run(t, testdata,
65-
analyzer, dirs...)
62+
analysistest.Run(t, testdata, analyzer, dirs...)
6663
})
6764
}
6865
}

strategy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package factory
1+
package gofactory
22

33
import (
44
"go/types"

0 commit comments

Comments
 (0)