Skip to content

Commit

Permalink
Merge pull request #3 from gostaticanalysis/add-pkg
Browse files Browse the repository at this point in the history
Add zapvet package
  • Loading branch information
tenntenn authored Jul 12, 2021
2 parents 8f7ad9e + 4342744 commit b11e963
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 6 deletions.
40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,47 @@

* [fieldtype](./passes/fieldtype): fieldtype finds confliction type of field

```sh
## Install

You can get `zapvet` by `go install` command (Go 1.16 and higher).

```bash
$ go install github.com/gostaticanalysis/zapvet@latest
$ go vet -vettool=`zapvet` ./...
```

## How to use

`zapvet` run with `go vet` as below when Go is 1.12 and higher.

```bash
$ go vet -vettool=$(which zapvet) ./...
```

## Analyzers

### fieldtype

[fieldtype](./passes/fieldtype) finds confliction type of field.

```go
package a

import "go.uber.org/zap"

func f() {
zap.String("id", "100")
zap.Int("id", 100) // want `"id" conflict type Int vs String`
zap.Any("id", "100") // OK - ignore
zap.Reflect("id", "100") // OK - ignore
zap.String("xxx", "100") // OK
}
```

## Analyze with golang.org/x/tools/go/analysis

You can get analyzers of zapvet from [zapvet.Analyzers](https://pkg.go.dev/github.com/gostaticanalysis/zapvet/#Analyzers).
And you can use them with [unitchecker](https://golang.org/x/tools/go/analysis/unitchecker).

<!-- links -->
[gopkg]: https://pkg.go.dev/github.com/gostaticanalysis/zapvet
[gopkg-badge]: https://pkg.go.dev/badge/github.com/gostaticanalysis/zapvet?status.svg
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/gostaticanalysis/zapvet
go 1.16

require (
github.com/google/go-cmp v0.5.4
github.com/gostaticanalysis/analysisutil v0.7.1
github.com/gostaticanalysis/testutil v0.4.0
golang.org/x/tools v0.1.4
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"github.com/gostaticanalysis/zapvet/passes/fieldtype"
"github.com/gostaticanalysis/zapvet/zapvet"
"golang.org/x/tools/go/analysis/unitchecker"
)

func main() { unitchecker.Main(fieldtype.Analyzer) }
func main() { unitchecker.Main(zapvet.Analyzers()...) }
4 changes: 2 additions & 2 deletions passes/fieldtype/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import "go.uber.org/zap"

func f() {
zap.String("id", "100")
zap.Int("id", 100) // want `"id" conflict type Int vs String`
zap.Any("id", "100") // OK - ignore
zap.Int("id", 100) // want `"id" conflict type Int vs String`
zap.Any("id", "100") // OK - ignore
zap.Reflect("id", "100") // OK - ignore
zap.String("xxx", "100") // OK
}
13 changes: 13 additions & 0 deletions zapvet/analyzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package zapvet

import (
"github.com/gostaticanalysis/zapvet/passes/fieldtype"
"golang.org/x/tools/go/analysis"
)

// Analyzers returns analyzers of zapvet.
func Analyzers() []*analysis.Analyzer {
return []*analysis.Analyzer{
fieldtype.Analyzer,
}
}
37 changes: 37 additions & 0 deletions zapvet/analyzer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package zapvet_test

import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/gostaticanalysis/zapvet/zapvet"
"golang.org/x/tools/go/packages"
)

func TestAnalyzers(t *testing.T) {
want := analyzerNames(t)
got := zapvet.Analyzers()
gotNames := make([]string, len(got))
for i := range got {
gotNames[i] = got[i].Name
}
if diff := cmp.Diff(want, gotNames); diff != "" {
t.Error(diff)
}
}

func analyzerNames(t *testing.T) []string {
t.Helper()
cfg := &packages.Config{Mode: packages.NeedName}
pkgs, err := packages.Load(cfg, "github.com/gostaticanalysis/zapvet/passes/...")
if err != nil {
t.Fatal("unexpected error:", err)
}

names := make([]string, len(pkgs))
for i := range pkgs {
names[i] = pkgs[i].Name
}

return names
}

0 comments on commit b11e963

Please sign in to comment.