From 37146370596ad812445b46970a3e77dbcbe51c11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20BIDON?= Date: Sat, 9 Aug 2025 23:05:48 +0200 Subject: [PATCH] chore(lint): update linter config, relinted code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Frédéric BIDON --- .golangci.yml | 13 +++++++++++++ bson.go | 4 ++-- date_test.go | 2 +- default_test.go | 28 ++++++++++++++-------------- duration_test.go | 2 +- format.go | 45 +++++++++++++++++++++++---------------------- time.go | 2 +- time_test.go | 4 ++-- 8 files changed, 57 insertions(+), 43 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 5006306..568ce16 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -16,13 +16,16 @@ linters: - godox - gosmopolitan - inamedparam + - intrange # disabled while < go1.22 - ireturn - lll - musttag - nestif - nlreturn - nonamedreturns + - noinlineerr - paralleltest + - recvcheck - testpackage - thelper - tparallel @@ -31,6 +34,7 @@ linters: - whitespace - wrapcheck - wsl + - wsl_v5 settings: dupl: threshold: 200 @@ -60,3 +64,12 @@ formatters: - third_party$ - builtin$ - examples$ +issues: + # Maximum issues count per one linter. + # Set to 0 to disable. + # Default: 50 + max-issues-per-linter: 0 + # Maximum count of issues with the same text. + # Set to 0 to disable. + # Default: 3 + max-same-issues: 0 diff --git a/bson.go b/bson.go index 685eaf6..78d6e07 100644 --- a/bson.go +++ b/bson.go @@ -39,10 +39,10 @@ func IsBSONObjectID(str string) bool { // ObjectId represents a BSON object ID (alias to go.mongodb.org/mongo-driver/bson/primitive.ObjectID) // // swagger:strfmt bsonobjectid -type ObjectId bsonprim.ObjectID //nolint:revive,stylecheck +type ObjectId bsonprim.ObjectID //nolint:revive // NewObjectId creates a ObjectId from a Hex String -func NewObjectId(hex string) ObjectId { //nolint:revive,stylecheck +func NewObjectId(hex string) ObjectId { //nolint:revive oid, err := bsonprim.ObjectIDFromHex(hex) if err != nil { panic(err) diff --git a/date_test.go b/date_test.go index 6a16b73..bd7c3ca 100644 --- a/date_test.go +++ b/date_test.go @@ -48,7 +48,7 @@ func TestDate(t *testing.T) { err = pp.UnmarshalJSON(bj) require.NoError(t, err) - assert.EqualValues(t, orig, pp.String()) + assert.Equal(t, orig, pp.String()) err = pp.UnmarshalJSON([]byte(`"1972/01/01"`)) require.Error(t, err) diff --git a/default_test.go b/default_test.go index 79316d0..1fcc256 100644 --- a/default_test.go +++ b/default_test.go @@ -265,7 +265,7 @@ func TestFormatUUID3(t *testing.T) { var uuidZero UUID3 err := uuidZero.UnmarshalJSON([]byte(jsonNull)) require.NoError(t, err) - assert.EqualValues(t, UUID3(""), uuidZero) + assert.Equal(t, UUID3(""), uuidZero) } func TestFormatUUID4(t *testing.T) { @@ -296,7 +296,7 @@ func TestFormatUUID4(t *testing.T) { var uuidZero UUID4 err := uuidZero.UnmarshalJSON([]byte(jsonNull)) require.NoError(t, err) - assert.EqualValues(t, UUID4(""), uuidZero) + assert.Equal(t, UUID4(""), uuidZero) } func TestFormatUUID5(t *testing.T) { @@ -327,7 +327,7 @@ func TestFormatUUID5(t *testing.T) { var uuidZero UUID5 err := uuidZero.UnmarshalJSON([]byte(jsonNull)) require.NoError(t, err) - assert.EqualValues(t, UUID5(""), uuidZero) + assert.Equal(t, UUID5(""), uuidZero) } func TestFormatUUID(t *testing.T) { @@ -365,7 +365,7 @@ func TestFormatUUID(t *testing.T) { var uuidZero UUID err := uuidZero.UnmarshalJSON([]byte(jsonNull)) require.NoError(t, err) - assert.EqualValues(t, UUID(""), uuidZero) + assert.Equal(t, UUID(""), uuidZero) } func TestFormatISBN(t *testing.T) { @@ -425,7 +425,7 @@ func TestFormatBase64(t *testing.T) { var subj Base64 err := subj.UnmarshalText([]byte(str)) require.NoError(t, err) - assert.EqualValues(t, expected, subj) + assert.Equal(t, expected, subj) b, err = subj.MarshalText() require.NoError(t, err) @@ -434,7 +434,7 @@ func TestFormatBase64(t *testing.T) { var subj2 Base64 err = subj2.UnmarshalJSON(bj) require.NoError(t, err) - assert.EqualValues(t, expected, subj2) + assert.Equal(t, expected, subj2) b, err = subj2.MarshalJSON() require.NoError(t, err) @@ -456,18 +456,18 @@ func TestFormatBase64(t *testing.T) { require.NoError(t, err) sqlvalueAsString, ok := sqlvalue.(string) if assert.Truef(t, ok, "[%s]Value: expected driver value to be a string", "byte") { - assert.EqualValuesf(t, str, sqlvalueAsString, "[%s]Value: expected %v and %v to be equal", "byte", sqlvalue, str) + assert.Equal(t, str, sqlvalueAsString, "[%s]Value: expected %v and %v to be equal", "byte", sqlvalue, str) } // Scanner interface var subj3 Base64 err = subj3.Scan([]byte(str)) require.NoError(t, err) - assert.EqualValues(t, str, subj3.String()) + assert.Equal(t, str, subj3.String()) var subj4 Base64 err = subj4.Scan(str) require.NoError(t, err) - assert.EqualValues(t, str, subj4.String()) + assert.Equal(t, str, subj4.String()) err = subj4.Scan(123) require.Error(t, err) @@ -511,7 +511,7 @@ func testStringFormat(t *testing.T, what testableFormat, format, with string, va require.NoError(t, err) val = reflect.Indirect(reflect.ValueOf(what)) strVal = val.String() - assert.EqualValuesf(t, with, strVal, "[%s]UnmarshalJSON: expected %v and %v to be value equal", format, strVal, with) + assert.Equal(t, with, strVal, "[%s]UnmarshalJSON: expected %v and %v to be value equal", format, strVal, with) b, err = what.MarshalJSON() require.NoError(t, err) @@ -527,7 +527,7 @@ func testStringFormat(t *testing.T, what testableFormat, format, with string, va require.NoError(t, err) val = reflect.Indirect(reflect.ValueOf(what)) strVal = val.String() - assert.EqualValuesf(t, with, strVal, "[%s]bson.Unmarshal: expected %v and %v to be equal (reset value) ", format, what, with) + assert.Equal(t, with, strVal, "[%s]bson.Unmarshal: expected %v and %v to be equal (reset value) ", format, what, with) // Scanner interface resetValue(t, format, what) @@ -535,13 +535,13 @@ func testStringFormat(t *testing.T, what testableFormat, format, with string, va require.NoError(t, err) val = reflect.Indirect(reflect.ValueOf(what)) strVal = val.String() - assert.EqualValuesf(t, with, strVal, "[%s]Scan: expected %v and %v to be value equal", format, strVal, with) + assert.Equal(t, with, strVal, "[%s]Scan: expected %v and %v to be value equal", format, strVal, with) err = what.Scan([]byte(with)) require.NoError(t, err) val = reflect.Indirect(reflect.ValueOf(what)) strVal = val.String() - assert.EqualValuesf(t, with, strVal, "[%s]Scan: expected %v and %v to be value equal", format, strVal, with) + assert.Equal(t, with, strVal, "[%s]Scan: expected %v and %v to be value equal", format, strVal, with) err = what.Scan(123) require.Error(t, err) @@ -551,7 +551,7 @@ func testStringFormat(t *testing.T, what testableFormat, format, with string, va require.NoError(t, err) sqlvalueAsString, ok := sqlvalue.(string) if assert.Truef(t, ok, "[%s]Value: expected driver value to be a string", format) { - assert.EqualValuesf(t, with, sqlvalueAsString, "[%s]Value: expected %v and %v to be equal", format, sqlvalue, with) + assert.Equal(t, with, sqlvalueAsString, "[%s]Value: expected %v and %v to be equal", format, sqlvalue, with) } // validation with Registry diff --git a/duration_test.go b/duration_test.go index cfee1bc..de6a5f5 100644 --- a/duration_test.go +++ b/duration_test.go @@ -50,7 +50,7 @@ func TestDuration(t *testing.T) { err = pp.UnmarshalJSON(bj) require.NoError(t, err) - assert.EqualValues(t, orig, pp.String()) + assert.Equal(t, orig, pp.String()) err = pp.UnmarshalJSON([]byte("yada")) require.Error(t, err) diff --git a/format.go b/format.go index 75d4b60..73f83af 100644 --- a/format.go +++ b/format.go @@ -49,10 +49,29 @@ type Registry interface { GetType(string) (reflect.Type, bool) ContainsName(string) bool Validates(string, string) bool - Parse(string, string) (interface{}, error) + Parse(string, string) (any, error) MapStructureHookFunc() mapstructure.DecodeHookFunc } +// NewFormats creates a new formats registry seeded with the values from the default +func NewFormats() Registry { + //nolint:forcetypeassert + return NewSeededFormats(Default.(*defaultFormats).data, nil) +} + +// NewSeededFormats creates a new formats registry +func NewSeededFormats(seeds []knownFormat, normalizer NameNormalizer) Registry { + if normalizer == nil { + normalizer = DefaultNameNormalizer + } + // copy here, don't modify original + d := append([]knownFormat(nil), seeds...) + return &defaultFormats{ + data: d, + normalizeName: normalizer, + } +} + type knownFormat struct { Name string OrigName string @@ -70,32 +89,14 @@ func DefaultNameNormalizer(name string) string { type defaultFormats struct { sync.Mutex + data []knownFormat normalizeName NameNormalizer } -// NewFormats creates a new formats registry seeded with the values from the default -func NewFormats() Registry { - //nolint:forcetypeassert - return NewSeededFormats(Default.(*defaultFormats).data, nil) -} - -// NewSeededFormats creates a new formats registry -func NewSeededFormats(seeds []knownFormat, normalizer NameNormalizer) Registry { - if normalizer == nil { - normalizer = DefaultNameNormalizer - } - // copy here, don't modify original - d := append([]knownFormat(nil), seeds...) - return &defaultFormats{ - data: d, - normalizeName: normalizer, - } -} - // MapStructureHookFunc is a decode hook function for mapstructure func (f *defaultFormats) MapStructureHookFunc() mapstructure.DecodeHookFunc { - return func(from reflect.Type, to reflect.Type, obj interface{}) (interface{}, error) { + return func(from reflect.Type, to reflect.Type, obj any) (any, error) { if from.Kind() != reflect.String { return obj, nil } @@ -306,7 +307,7 @@ func (f *defaultFormats) Validates(name, data string) bool { // Parse a string into the appropriate format representation type. // // E.g. parsing a string a "date" will return a Date type. -func (f *defaultFormats) Parse(name, data string) (interface{}, error) { +func (f *defaultFormats) Parse(name, data string) (any, error) { f.Lock() defer f.Unlock() nme := f.normalizeName(name) diff --git a/time.go b/time.go index 7952aba..e5da7a3 100644 --- a/time.go +++ b/time.go @@ -159,7 +159,7 @@ func (t DateTime) IsZero() bool { // IsUnixZerom returns whether the date time is equivalent to time.Unix(0, 0).UTC(). func (t DateTime) IsUnixZero() bool { - return time.Time(t) == UnixZero + return time.Time(t).Equal(UnixZero) } // MarshalText implements the text marshaller interface diff --git a/time_test.go b/time_test.go index 212f592..e95f13f 100644 --- a/time_test.go +++ b/time_test.go @@ -217,7 +217,7 @@ func TestDateTime_MarshalJSON(t *testing.T) { dt := DateTime(example.time) bb, err := dt.MarshalJSON() require.NoError(t, err) - assert.EqualValues(t, esc([]byte(example.str)), bb) + assert.Equal(t, esc([]byte(example.str)), bb) } } func TestDateTime_MarshalJSON_Override(t *testing.T) { @@ -234,7 +234,7 @@ func TestDateTime_MarshalJSON_Override(t *testing.T) { dt := DateTime(example.time.UTC()) bb, err := dt.MarshalJSON() require.NoError(t, err) - assert.EqualValues(t, esc([]byte(example.utcStr)), bb) + assert.Equal(t, esc([]byte(example.utcStr)), bb) } }