Skip to content

Commit

Permalink
chore: migrate to go-simpler.org (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmzane authored Oct 27, 2023
1 parent faf6877 commit a101578
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 53 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
68 changes: 32 additions & 36 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 @@ -36,18 +36,18 @@ The rational from [Uber Style Guide][1]:

The following packages are supported out of the box:

* [`encoding/json`][2]
* [`encoding/xml`][3]
* [`gopkg.in/yaml.v3`][4]
* [`github.com/BurntSushi/toml`][5]
* [`github.com/mitchellh/mapstructure`][6]
* [`github.com/jmoiron/sqlx`][7]
* [encoding/json][2]
* [encoding/xml][3]
* [gopkg.in/yaml.v3][4]
* [github.com/BurntSushi/toml][5]
* [github.com/mitchellh/mapstructure][6]
* [github.com/jmoiron/sqlx][7]

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,39 +57,33 @@ 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 [`hclsimple.Decode`][11]:

```yaml
linters-settings:
musttag:
functions:
# The full name of the function, including the package.
- name: github.com/hashicorp/hcl/v2/hclsimple.DecodeFile
- name: github.com/hashicorp/hcl/v2/hclsimple.Decode
# The struct tag whose presence should be ensured.
tag: hcl
# The position of the argument to check.
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 @@ -98,9 +92,11 @@ musttag -fn="github.com/hashicorp/hcl/v2/hclsimple.DecodeFile:hcl:2" ./...
[1]: https://github.com/uber-go/guide/blob/master/style.md#use-field-tags-in-marshaled-structs
[2]: https://pkg.go.dev/encoding/json
[3]: https://pkg.go.dev/encoding/xml
[4]: https://github.com/go-yaml/yaml
[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
[4]: https://pkg.go.dev/gopkg.in/yaml.v3
[5]: https://pkg.go.dev/github.com/BurntSushi/toml
[6]: https://pkg.go.dev/github.com/mitchellh/mapstructure
[7]: https://pkg.go.dev/github.com/jmoiron/sqlx
[8]: https://golangci-lint.run
[9]: https://github.com/go-simpler/musttag/releases
[10]: https://golangci-lint.run/usage/linters/#musttag
[11]: https://pkg.go.dev/github.com/hashicorp/hcl/v2/hclsimple#Decode
8 changes: 4 additions & 4 deletions builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var builtins = []Func{
ifaceWhitelist: []string{"encoding/xml.Unmarshaler", "encoding.TextUnmarshaler"},
},

// https://github.com/go-yaml/yaml
// https://pkg.go.dev/gopkg.in/yaml.v3
{
Name: "gopkg.in/yaml.v3.Marshal", Tag: "yaml", ArgPos: 0,
ifaceWhitelist: []string{"gopkg.in/yaml.v3.Marshaler"},
Expand All @@ -72,7 +72,7 @@ var builtins = []Func{
ifaceWhitelist: []string{"gopkg.in/yaml.v3.Unmarshaler"},
},

// https://github.com/BurntSushi/toml
// https://pkg.go.dev/github.com/BurntSushi/toml
{
Name: "github.com/BurntSushi/toml.Unmarshal", Tag: "toml", ArgPos: 1,
ifaceWhitelist: []string{"github.com/BurntSushi/toml.Unmarshaler", "encoding.TextUnmarshaler"},
Expand All @@ -98,13 +98,13 @@ var builtins = []Func{
ifaceWhitelist: []string{"github.com/BurntSushi/toml.Unmarshaler", "encoding.TextUnmarshaler"},
},

// https://github.com/mitchellh/mapstructure
// https://pkg.go.dev/github.com/mitchellh/mapstructure
{Name: "github.com/mitchellh/mapstructure.Decode", Tag: "mapstructure", ArgPos: 1},
{Name: "github.com/mitchellh/mapstructure.DecodeMetadata", Tag: "mapstructure", ArgPos: 1},
{Name: "github.com/mitchellh/mapstructure.WeakDecode", Tag: "mapstructure", ArgPos: 1},
{Name: "github.com/mitchellh/mapstructure.WeakDecodeMetadata", Tag: "mapstructure", ArgPos: 1},

// https://github.com/jmoiron/sqlx
// https://pkg.go.dev/github.com/jmoiron/sqlx
{Name: "github.com/jmoiron/sqlx.Get", Tag: "db", ArgPos: 1},
{Name: "github.com/jmoiron/sqlx.GetContext", Tag: "db", ArgPos: 2},
{Name: "github.com/jmoiron/sqlx.Select", Tag: "db", ArgPos: 1},
Expand Down
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.20

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 a101578

Please sign in to comment.