Skip to content

Commit 2e439dd

Browse files
cooperbenson-qzldez
authored andcommitted
feat: Add swaggo/swag formatter
1 parent af60e9f commit 2e439dd

File tree

15 files changed

+184
-5
lines changed

15 files changed

+184
-5
lines changed

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ require (
108108
github.com/ssgreg/nlreturn/v2 v2.2.1
109109
github.com/stbenjam/no-sprintf-host-port v0.2.0
110110
github.com/stretchr/testify v1.10.0
111+
github.com/swaggo/swag v1.16.4
111112
github.com/tdakkota/asciicheck v0.4.1
112113
github.com/tetafro/godot v1.5.1
113114
github.com/timakin/bodyclose v0.0.0-20241222091800-1db5c5ca4d67
@@ -138,7 +139,10 @@ require (
138139
)
139140

140141
require (
142+
github.com/KyleBanks/depth v1.2.1 // indirect
141143
github.com/Masterminds/semver/v3 v3.3.1 // indirect
144+
github.com/PuerkitoBio/purell v1.1.1 // indirect
145+
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
142146
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
143147
github.com/beorn7/perks v1.0.1 // indirect
144148
github.com/ccojocar/zxcvbn-go v1.0.2 // indirect
@@ -156,6 +160,10 @@ require (
156160
github.com/fatih/structtag v1.2.0 // indirect
157161
github.com/fsnotify/fsnotify v1.5.4 // indirect
158162
github.com/go-ole/go-ole v1.2.6 // indirect
163+
github.com/go-openapi/jsonpointer v0.19.5 // indirect
164+
github.com/go-openapi/jsonreference v0.19.6 // indirect
165+
github.com/go-openapi/spec v0.20.4 // indirect
166+
github.com/go-openapi/swag v0.19.15 // indirect
159167
github.com/go-toolsmith/astcast v1.1.0 // indirect
160168
github.com/go-toolsmith/astcopy v1.1.0 // indirect
161169
github.com/go-toolsmith/astequal v1.2.0 // indirect
@@ -173,9 +181,11 @@ require (
173181
github.com/hashicorp/hcl v1.0.0 // indirect
174182
github.com/hexops/gotextdiff v1.0.3 // indirect
175183
github.com/inconshreveable/mousetrap v1.1.0 // indirect
184+
github.com/josharian/intern v1.0.0 // indirect
176185
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
177186
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
178187
github.com/magiconair/properties v1.8.6 // indirect
188+
github.com/mailru/easyjson v0.7.6 // indirect
179189
github.com/mattn/go-isatty v0.0.20 // indirect
180190
github.com/mattn/go-runewidth v0.0.16 // indirect
181191
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
@@ -209,6 +219,7 @@ require (
209219
go.uber.org/zap v1.24.0 // indirect
210220
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
211221
golang.org/x/exp/typeparams v0.0.0-20250210185358-939b2ce775ac // indirect
222+
golang.org/x/net v0.39.0 // indirect
212223
golang.org/x/sync v0.13.0 // indirect
213224
golang.org/x/text v0.24.0 // indirect
214225
google.golang.org/protobuf v1.36.6 // indirect

go.sum

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jsonschema/golangci.next.jsonschema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,8 @@
836836
"gofmt",
837837
"gofumpt",
838838
"goimports",
839-
"golines"
839+
"golines",
840+
"swaggo"
840841
]
841842
},
842843
"settings": {

pkg/config/linters.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,5 @@ func (l *Linters) validateNoFormatters() error {
4949
}
5050

5151
func getAllFormatterNames() []string {
52-
return []string{"gci", "gofmt", "gofumpt", "goimports", "golines"}
52+
return []string{"gci", "gofmt", "gofumpt", "goimports", "golines", "swaggo"}
5353
}

pkg/goformatters/meta_formatter.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/golangci/golangci-lint/v2/pkg/goformatters/gofumpt"
1313
"github.com/golangci/golangci-lint/v2/pkg/goformatters/goimports"
1414
"github.com/golangci/golangci-lint/v2/pkg/goformatters/golines"
15+
"github.com/golangci/golangci-lint/v2/pkg/goformatters/swaggo"
1516
"github.com/golangci/golangci-lint/v2/pkg/logutils"
1617
)
1718

@@ -41,6 +42,10 @@ func NewMetaFormatter(log logutils.Log, cfg *config.Formatters, runCfg *config.R
4142
m.formatters = append(m.formatters, goimports.New(&cfg.Settings.GoImports))
4243
}
4344

45+
if slices.Contains(cfg.Enable, swaggo.Name) {
46+
m.formatters = append(m.formatters, swaggo.New())
47+
}
48+
4449
// gci is a last because the only goal of gci is to handle imports.
4550
if slices.Contains(cfg.Enable, gci.Name) {
4651
formatter, err := gci.New(&cfg.Settings.Gci)
@@ -86,5 +91,5 @@ func (m *MetaFormatter) Format(filename string, src []byte) []byte {
8691
}
8792

8893
func IsFormatter(name string) bool {
89-
return slices.Contains([]string{gofmt.Name, gofumpt.Name, goimports.Name, gci.Name, golines.Name}, name)
94+
return slices.Contains([]string{gofmt.Name, gofumpt.Name, goimports.Name, gci.Name, golines.Name, swaggo.Name}, name)
9095
}

pkg/goformatters/swaggo/swaggo.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package swaggo
2+
3+
import "github.com/swaggo/swag"
4+
5+
const Name = "swaggo"
6+
7+
type Formatter struct {
8+
formatter *swag.Formatter
9+
}
10+
11+
func New() *Formatter {
12+
return &Formatter{
13+
formatter: swag.NewFormatter(),
14+
}
15+
}
16+
17+
func (*Formatter) Name() string {
18+
return Name
19+
}
20+
21+
func (f *Formatter) Format(path string, src []byte) ([]byte, error) {
22+
return f.formatter.Format(path, src)
23+
}

pkg/golinters/swaggo/swaggo.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package swaggo
2+
3+
import (
4+
"github.com/golangci/golangci-lint/v2/pkg/goanalysis"
5+
"github.com/golangci/golangci-lint/v2/pkg/goformatters"
6+
"github.com/golangci/golangci-lint/v2/pkg/goformatters/swaggo"
7+
"github.com/golangci/golangci-lint/v2/pkg/golinters/internal"
8+
"golang.org/x/tools/go/analysis"
9+
)
10+
11+
const linterName = "swaggo"
12+
13+
func New() *goanalysis.Linter {
14+
a := goformatters.NewAnalyzer(
15+
internal.LinterLogger.Child(linterName),
16+
"Checks if swaggo comments are formatted",
17+
swaggo.New(),
18+
)
19+
20+
return goanalysis.NewLinter(a.Name, a.Doc, []*analysis.Analyzer{a}, nil).
21+
WithLoadMode(goanalysis.LoadModeSyntax)
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package swaggo
2+
3+
import (
4+
"testing"
5+
6+
"github.com/golangci/golangci-lint/v2/test/testshared/integration"
7+
)
8+
9+
func TestFromTestdata(t *testing.T) {
10+
integration.RunTestdata(t)
11+
}
12+
13+
func TestFix(t *testing.T) {
14+
integration.RunFix(t)
15+
}
16+
17+
func TestFixPathPrefix(t *testing.T) {
18+
integration.RunFixPathPrefix(t)
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//golangcitest:config_path testdata/swaggo.yml
2+
//golangcitest:expected_exitcode 0
3+
package api
4+
5+
import "net/http"
6+
7+
// @Summary Add a new pet to the store
8+
// @Description get string by ID
9+
// @ID get-string-by-int
10+
// @Accept json
11+
// @Produce json
12+
// @Param some_id path int true "Some ID" Format(int64)
13+
// @Param some_id body web.Pet true "Some ID"
14+
// @Success 200 {string} string "ok"
15+
// @Failure 400 {object} web.APIError "We need ID!!"
16+
// @Failure 404 {object} web.APIError "Can not find ID"
17+
// @Router /testapi/get-string-by-int/{some_id} [get]
18+
func GetStringByInt(w http.ResponseWriter, r *http.Request) {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//golangcitest:config_path testdata/swaggo.yml
2+
//golangcitest:expected_exitcode 0
3+
package api
4+
5+
import "net/http"
6+
7+
// @Summary Add a new pet to the store
8+
// @Description get string by ID
9+
// @ID get-string-by-int
10+
// @Accept json
11+
// @Produce json
12+
// @Param some_id path int true "Some ID" Format(int64)
13+
// @Param some_id body web.Pet true "Some ID"
14+
// @Success 200 {string} string "ok"
15+
// @Failure 400 {object} web.APIError "We need ID!!"
16+
// @Failure 404 {object} web.APIError "Can not find ID"
17+
// @Router /testapi/get-string-by-int/{some_id} [get]
18+
func GetStringByInt(w http.ResponseWriter, r *http.Request) {}

0 commit comments

Comments
 (0)