Skip to content

Commit

Permalink
chore: migrate to go-simpler.org
Browse files Browse the repository at this point in the history
  • Loading branch information
tmzane committed Oct 27, 2023
1 parent edef854 commit 2cb367a
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 38 deletions.
9 changes: 0 additions & 9 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,3 @@ archives:
- format_overrides:
- goos: windows
format: zip

brews:
- tap:
owner: tmzane
name: homebrew-tap
token: '{{ .Env.HOMEBREW_TAP_TOKEN }}'
homepage: https://github.com/tmzane/musttag
description: A Go linter that enforces field tags in (un)marshaled structs
license: MPL-2.0
46 changes: 21 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# musttag

[![checks](https://github.com/tmzane/musttag/actions/workflows/checks.yml/badge.svg)](https://github.com/tmzane/musttag/actions/workflows/checks.yml)
[![pkg.go.dev](https://pkg.go.dev/badge/go.tmz.dev/musttag.svg)](https://pkg.go.dev/go.tmz.dev/musttag)
[![goreportcard](https://goreportcard.com/badge/go.tmz.dev/musttag)](https://goreportcard.com/report/go.tmz.dev/musttag)
[![codecov](https://codecov.io/gh/tmzane/musttag/branch/main/graph/badge.svg)](https://codecov.io/gh/tmzane/musttag)
[![checks](https://github.com/go-simpler/musttag/actions/workflows/checks.yml/badge.svg)](https://github.com/go-simpler/musttag/actions/workflows/checks.yml)
[![pkg.go.dev](https://pkg.go.dev/badge/go-simpler.org/musttag.svg)](https://pkg.go.dev/go-simpler.org/musttag)
[![goreportcard](https://goreportcard.com/badge/go-simpler.org/musttag)](https://goreportcard.com/report/go-simpler.org/musttag)
[![codecov](https://codecov.io/gh/go-simpler/musttag/branch/main/graph/badge.svg)](https://codecov.io/gh/go-simpler/musttag)

A Go linter that enforces field tags in (un)marshaled structs
A Go linter that enforces field tags in (un)marshaled structs.

## 📌 About

Expand All @@ -14,13 +14,13 @@ A Go linter that enforces field tags in (un)marshaled structs
```go
// BAD:
var user struct {
Name string
Name string
}
data, err := json.Marshal(user)

// GOOD:
var user struct {
Name string `json:"name"`
Name string `json:"name"`
}
data, err := json.Marshal(user)
```
Expand All @@ -45,9 +45,9 @@ The following packages are supported out of the box:

In addition, any [custom package](#custom-packages) can be added to the list.

## 📋 Usage
## 📦 Install

`musttag` is already integrated into `golangci-lint`, and this is the recommended way to use it.
`musttag` is integrated into [`golangci-lint`][8], and this is the recommended way to use it.

To enable the linter, add the following lines to `.golangci.yml`:

Expand All @@ -57,25 +57,19 @@ linters:
- musttag
```
If you'd rather prefer to use `musttag` standalone, you can install it via `brew`...

```shell
brew install tmzane/tap/musttag
```
Alternatively, you can download a prebuilt binary from the [Releases][9] page to use `musttag` standalone.

...or download a prebuilt binary from the [Releases][9] page.
## 📋 Usage

Then run it either directly or as a `go vet` tool:
Run `golangci-lint` with `musttag` enabled.
See the list of [available options][10] to configure the linter.

```shell
go vet -vettool=$(which musttag) ./...
```
When using `sloglint` standalone, pass the options as flags.

### Custom packages

To enable reporting a custom function, you need to add its description to `.golangci.yml`.

The following is an example of adding support for the `hclsimple.DecodeFile` function from [`github.com/hashicorp/hcl`][8]:
To report a custom function, you need to add its description to `.golangci.yml`.
The following is an example of adding support for the `hclsimple.DecodeFile` function from [`github.com/hashicorp/hcl`][11]:

```yaml
linters-settings:
Expand All @@ -89,7 +83,7 @@ linters-settings:
arg-pos: 2
```

The same can be done via the `-fn=name:tag:arg-pos` flag when using `musttag` standalone:
The same can be done via the `-fn=<name:tag:arg-pos>` flag when using `musttag` standalone:

```shell
musttag -fn="github.com/hashicorp/hcl/v2/hclsimple.DecodeFile:hcl:2" ./...
Expand All @@ -102,5 +96,7 @@ musttag -fn="github.com/hashicorp/hcl/v2/hclsimple.DecodeFile:hcl:2" ./...
[5]: https://github.com/BurntSushi/toml
[6]: https://github.com/mitchellh/mapstructure
[7]: https://github.com/jmoiron/sqlx
[8]: https://github.com/hashicorp/hcl
[9]: https://github.com/tmzane/musttag/releases
[8]: https://golangci-lint.run
[9]: https://github.com/go-simpler/musttag/releases
[10]: https://golangci-lint.run/usage/linters/#musttag
[11]: https://github.com/hashicorp/hcl
2 changes: 1 addition & 1 deletion cmd/musttag/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"runtime"

"go.tmz.dev/musttag"
"go-simpler.org/musttag"
"golang.org/x/tools/go/analysis/singlechecker"
)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module go.tmz.dev/musttag
module go-simpler.org/musttag

go 1.21

Expand Down
2 changes: 1 addition & 1 deletion musttag.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func run(pass *analysis.Pass, mainModule string, funcs map[string]Func) (_ any,
}

if len(call.Args) <= fn.ArgPos {
err = fmt.Errorf("Func.ArgPos cannot be %d: %s accepts only %d argument(s)", fn.ArgPos, fn.Name, len(call.Args))
err = fmt.Errorf("musttag: Func.ArgPos cannot be %d: %s accepts only %d argument(s)", fn.ArgPos, fn.Name, len(call.Args))
return
}

Expand Down
2 changes: 1 addition & 1 deletion musttag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestAnalyzer(t *testing.T) {
Func{Name: "encoding/json.Marshal", Tag: "json", ArgPos: 10},
)
err := analysistest.Run(nopT{}, testdata, analyzer, "tests")[0].Err
assert.Equal[E](t, err.Error(), "Func.ArgPos cannot be 10: encoding/json.Marshal accepts only 1 argument(s)")
assert.Equal[E](t, err.Error(), "musttag: Func.ArgPos cannot be 10: encoding/json.Marshal accepts only 1 argument(s)")
})
}

Expand Down
3 changes: 3 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,20 @@ func getMainModule() (string, error) {
// based on golang.org/x/tools/imports.VendorlessPath
func cutVendor(path string) string {
var prefix string

switch {
case strings.HasPrefix(path, "(*"):
prefix, path = "(*", path[len("(*"):]
case strings.HasPrefix(path, "("):
prefix, path = "(", path[len("("):]
}

if i := strings.LastIndex(path, "/vendor/"); i >= 0 {
return prefix + path[i+len("/vendor/"):]
}
if strings.HasPrefix(path, "vendor/") {
return prefix + path[len("vendor/"):]
}

return prefix + path
}

0 comments on commit 2cb367a

Please sign in to comment.