Skip to content

refactor: drop dependency on github.com/fatih/structtag#1650

Open
alexandear wants to merge 7 commits intomgechev:masterfrom
alexandear:refactor/remove-fatih-structtag
Open

refactor: drop dependency on github.com/fatih/structtag#1650
alexandear wants to merge 7 commits intomgechev:masterfrom
alexandear:refactor/remove-fatih-structtag

Conversation

@alexandear
Copy link
Collaborator

@alexandear alexandear commented Feb 23, 2026

This PR drops one dependency.
Because https://github.com/fatih/structtag seems unmaintained, and we do not utilize all the functions from the library. We need only Parse.

Our implementation is a little bit faster:

go test -bench . -benchmem -count=1 -run=^$ ./internal/structtag/...
goos: darwin
goarch: arm64
pkg: github.com/mgechev/revive/internal/structtag
cpu: Apple M4 Pro
BenchmarkParseCompare/single/revive-12          23213113                51.08 ns/op           88 B/op          3 allocs/op
BenchmarkParseCompare/single/fatih-12           18595112                68.07 ns/op          112 B/op          4 allocs/op
BenchmarkParseCompare/with_options/revive-12    16504845                72.00 ns/op          120 B/op          3 allocs/op
BenchmarkParseCompare/with_options/fatih-12     13443501                87.82 ns/op          144 B/op          4 allocs/op
BenchmarkParseCompare/multiple/revive-12         7166770               165.9 ns/op           296 B/op          7 allocs/op
BenchmarkParseCompare/multiple/fatih-12          6108170               195.6 ns/op           352 B/op         10 allocs/op
BenchmarkParseCompare/complex/revive-12          5118066               232.5 ns/op           432 B/op          9 allocs/op
BenchmarkParseCompare/complex/fatih-12           4201783               269.2 ns/op           480 B/op         12 allocs/op
PASS
bench_test.go

package structtag_test

import (
	"testing"

	fatihst "github.com/fatih/structtag"

	"github.com/mgechev/revive/internal/structtag"
)

func BenchmarkParseCompare(b *testing.B) {
	tests := []struct {
		name string
		tag  string
	}{
		{
			name: "single",
			tag:  `json:"foo"`,
		},
		{
			name: "with_options",
			tag:  `json:"foo,omitempty,string"`,
		},
		{
			name: "multiple",
			tag:  `json:"foo,omitempty" xml:"bar" db:"baz,primarykey"`,
		},
		{
			name: "complex",
			tag:  `json:"id,omitempty,string" xml:"id,attr" db:"id,primarykey" validate:"required,uuid4"`,
		},
	}

	for _, tt := range tests {
		b.Run(tt.name+"/revive", func(b *testing.B) {
			for b.Loop() {
				_, _ = structtag.Parse(tt.tag)
			}
		})
		b.Run(tt.name+"/fatih", func(b *testing.B) {
			for b.Loop() {
				_, _ = fatihst.Parse(tt.tag)
			}
		})
	}
}

Inspired by https://cs.opensource.google/go/x/tools/+/refs/tags/v0.42.0:go/analysis/passes/structtag/structtag.go

@alexandear alexandear force-pushed the refactor/remove-fatih-structtag branch from beda91b to f8fe9b6 Compare February 23, 2026 14:20
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR removes the dependency on github.com/fatih/structtag by introducing an internal internal/structtag package that implements the needed struct-tag parsing functionality, and updates the struct-tag rule to use it.

Changes:

  • Added internal/structtag with a Parse function and Tag helpers (Value, String).
  • Updated rule/struct_tag.go to use the new internal parser.
  • Removed github.com/fatih/structtag from go.mod/go.sum and added tests for the new parser.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
rule/struct_tag.go Switches the struct-tag rule to the new internal parser API.
internal/structtag/structtag.go New internal struct tag parser and Tag implementation.
internal/structtag/structtag_test.go New unit tests validating parsing and Tag.Value().
go.mod Drops the github.com/fatih/structtag dependency.
go.sum Removes checksums for the dropped dependency.

alexandear and others added 2 commits February 23, 2026 06:23
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 5 changed files in this pull request and generated no new comments.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 5 changed files in this pull request and generated no new comments.

Copy link
Collaborator

@denisvmedia denisvmedia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, with a couple of nit comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants