Skip to content

Commit ebd3be2

Browse files
committed
Reorganize project structure
Signed-off-by: timflannagan <timflannagan@gmail.com>
1 parent 5b58085 commit ebd3be2

9 files changed

Lines changed: 102 additions & 44 deletions

File tree

README.md

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,29 @@ krtequals is designed to catch common bugs in `Equals()` method implementations,
88

99
## Installation
1010

11+
### Standalone CLI
12+
13+
```bash
14+
go install github.com/kgateway-dev/krtequals/cmd/krtequals@latest
15+
```
16+
17+
### As a Library
18+
19+
```bash
20+
go get github.com/kgateway-dev/krtequals
21+
```
22+
23+
## Usage
24+
25+
### Standalone
26+
1127
```bash
12-
go install github.com/kgateway-dev/krtequals@latest
28+
krtequals ./...
1329
```
1430

15-
## Usage with golangci-lint
31+
### With golangci-lint
1632

17-
krtequals is designed to be used as a [golangci-lint](https://golangci-lint.run/) custom plugin. Add it to your `.golangci.yaml`:
33+
krtequals can be used as a [golangci-lint](https://golangci-lint.run/) custom plugin. Add it to your `.golangci.yaml`:
1834

1935
```yaml
2036
linters-settings:
@@ -26,6 +42,20 @@ linters-settings:
2642
deepEqual: true # Enable reflect.DeepEqual detection (optional)
2743
```
2844
45+
### Programmatic Usage
46+
47+
```go
48+
import "github.com/kgateway-dev/krtequals/pkg/analyzer"
49+
50+
// Use the default analyzer
51+
a := analyzer.Analyzer
52+
53+
// Or create a custom configured analyzer
54+
a := analyzer.NewAnalyzer(&analyzer.Config{
55+
DeepEqual: true,
56+
})
57+
```
58+
2959
## What It Checks
3060

3161
### Missing Field Comparisons
@@ -87,6 +117,19 @@ type MyStruct struct {
87117
|--------|------|---------|-------------|
88118
| `deepEqual` | bool | `false` | Enable detection of `reflect.DeepEqual` usage in `Equals()` methods |
89119

120+
## Project Structure
121+
122+
```
123+
.
124+
├── cmd/krtequals/ # Standalone CLI
125+
│ └── main.go
126+
├── pkg/analyzer/ # Core analyzer library
127+
│ ├── analyzer.go # Main analysis logic
128+
│ ├── plugin.go # golangci-lint plugin integration
129+
│ └── testdata/ # Test fixtures
130+
└── README.md
131+
```
132+
90133
## Development
91134

92135
### Running Tests
@@ -98,7 +141,7 @@ go test -v ./...
98141
### Building
99142

100143
```bash
101-
go build ./...
144+
go build ./cmd/krtequals
102145
```
103146

104147
## License

analyzer_test.go

Lines changed: 0 additions & 25 deletions
This file was deleted.

cmd/krtequals/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Command krtequals runs the krtequals analyzer as a standalone tool.
2+
package main
3+
4+
import (
5+
"github.com/kgateway-dev/krtequals/pkg/analyzer"
6+
"golang.org/x/tools/go/analysis/singlechecker"
7+
)
8+
9+
func main() {
10+
singlechecker.Main(analyzer.Analyzer)
11+
}

analyzer.go renamed to pkg/analyzer/analyzer.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package krtequals
1+
// Package analyzer provides a static analysis tool that checks Equals() method
2+
// implementations for KRT-style semantic equality issues.
3+
package analyzer
24

35
import (
46
"go/ast"
@@ -11,25 +13,23 @@ import (
1113
"golang.org/x/tools/go/ast/inspector"
1214
)
1315

14-
// TODO(tim): Determine if we need to add a way for devs that define an Equals() method that's
15-
// unrelated to KRT collection to opt out of the analyzer.
16-
17-
// TODO(tim): [krtEqualsNone, krtEqualsTODO] the right naming for these markers?
18-
1916
// Config controls optional checks performed by the krtequals analyzer.
2017
type Config struct {
2118
// DeepEqual toggles the rule that flags usage of reflect.DeepEqual inside Equals methods.
2219
// This check is disabled by default for incremental rollout.
2320
DeepEqual bool `json:"deepEqual"`
24-
// TODO(time): time.Equals() for direct time.Time comparisons
2521
}
2622

27-
type analyzer struct {
28-
cfg Config
29-
}
23+
// Analyzer is the default analyzer instance with all checks disabled.
24+
// Use NewAnalyzer for custom configuration.
25+
var Analyzer = NewAnalyzer(&Config{})
3026

31-
func newAnalyzer(cfg *Config) *analysis.Analyzer {
32-
a := &analyzer{cfg: *cfg}
27+
// NewAnalyzer creates a new krtequals analyzer with the given configuration.
28+
func NewAnalyzer(cfg *Config) *analysis.Analyzer {
29+
if cfg == nil {
30+
cfg = &Config{}
31+
}
32+
a := &analyzerImpl{cfg: *cfg}
3333
return &analysis.Analyzer{
3434
Name: "krtequals",
3535
Doc: "Checks Equals() implementations for KRT-style semantic equality issues",
@@ -38,6 +38,10 @@ func newAnalyzer(cfg *Config) *analysis.Analyzer {
3838
}
3939
}
4040

41+
type analyzerImpl struct {
42+
cfg Config
43+
}
44+
4145
type structInfo struct {
4246
name string
4347
fields map[string]*fieldInfo
@@ -51,7 +55,7 @@ type fieldInfo struct {
5155
todo bool
5256
}
5357

54-
func (a *analyzer) Run(pass *analysis.Pass) (any, error) {
58+
func (a *analyzerImpl) Run(pass *analysis.Pass) (any, error) {
5559
ins := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
5660

5761
structs := collectStructs(ins)

pkg/analyzer/analyzer_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package analyzer
2+
3+
import (
4+
"testing"
5+
6+
"golang.org/x/tools/go/analysis/analysistest"
7+
)
8+
9+
func TestDeepEqualCheckEnabled(t *testing.T) {
10+
testdata := analysistest.TestData()
11+
a := NewAnalyzer(&Config{DeepEqual: true})
12+
analysistest.Run(t, testdata, a, "deepequalon")
13+
}
14+
15+
func TestDeepEqualCheckDisabled(t *testing.T) {
16+
testdata := analysistest.TestData()
17+
a := NewAnalyzer(&Config{DeepEqual: false})
18+
analysistest.Run(t, testdata, a, "deepequaloff")
19+
}
20+
21+
func TestMarkers(t *testing.T) {
22+
testdata := analysistest.TestData()
23+
a := NewAnalyzer(&Config{})
24+
analysistest.Run(t, testdata, a, "markers")
25+
}

plugin.go renamed to pkg/analyzer/plugin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package krtequals
1+
package analyzer
22

33
import (
44
"github.com/golangci/plugin-module-register/register"
@@ -25,7 +25,7 @@ func New(settings any) (register.LinterPlugin, error) {
2525
}
2626

2727
func (p *plugin) BuildAnalyzers() ([]*analysis.Analyzer, error) {
28-
return []*analysis.Analyzer{newAnalyzer(&p.cfg)}, nil
28+
return []*analysis.Analyzer{NewAnalyzer(&p.cfg)}, nil
2929
}
3030

3131
func (p *plugin) GetLoadMode() string {
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)