Skip to content

Commit

Permalink
Fix or workarond lint issues
Browse files Browse the repository at this point in the history
This change mostly fixes golangci-lint's reported issues, except for

- Renaming ObjectId, since it will break existing code
- MapStructureHookFunc, since I'd like to separate larger refactoring
  from this commit.

Signed-off-by: Kazuyoshi Kato <[email protected]>
  • Loading branch information
kzys committed Oct 27, 2021
1 parent c8fcbfb commit c984fbd
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 32 deletions.
4 changes: 2 additions & 2 deletions bson.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
type ObjectId bsonprim.ObjectID //nolint:revive

// NewObjectId creates a ObjectId from a Hex String
func NewObjectId(hex string) ObjectId {
func NewObjectId(hex string) ObjectId { //nolint:revive
oid, err := bsonprim.ObjectIDFromHex(hex)
if err != nil {
panic(err)
Expand Down
8 changes: 8 additions & 0 deletions default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ type testableFormat interface {
}

func testStringFormat(t *testing.T, what testableFormat, format, with string, validSamples, invalidSamples []string) {
t.Helper()

// text encoding interface
b := []byte(with)
err := what.UnmarshalText(b)
Expand Down Expand Up @@ -422,6 +424,8 @@ func testStringFormat(t *testing.T, what testableFormat, format, with string, va
}

func resetValue(t *testing.T, format string, what encoding.TextUnmarshaler) {
t.Helper()

err := what.UnmarshalText([]byte("reset value"))
assert.NoError(t, err)
val := reflect.Indirect(reflect.ValueOf(what))
Expand All @@ -430,13 +434,17 @@ func resetValue(t *testing.T, format string, what encoding.TextUnmarshaler) {
}

func testValid(t *testing.T, name, value string) {
t.Helper()

ok := Default.Validates(name, value)
if !ok {
t.Errorf("expected %q of type %s to be valid", value, name)
}
}

func testInvalid(t *testing.T, name, value string) {
t.Helper()

ok := Default.Validates(name, value)
if ok {
t.Errorf("expected %q of type %s to be invalid", value, name)
Expand Down
4 changes: 4 additions & 0 deletions duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func TestDuration(t *testing.T) {
}

func testDurationParser(t *testing.T, toParse string, expected time.Duration) {
t.Helper()

r, e := ParseDuration(toParse)
assert.NoError(t, e)
assert.Equal(t, expected, r)
Expand All @@ -91,6 +93,8 @@ func TestIsDuration_Failed(t *testing.T) {
}

func testDurationSQLScanner(t *testing.T, dur time.Duration) {
t.Helper()

values := []interface{}{int64(dur), float64(dur)}
for _, value := range values {
var result Duration
Expand Down
59 changes: 32 additions & 27 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,75 +93,80 @@ func NewSeededFormats(seeds []knownFormat, normalizer NameNormalizer) Registry {
}

// MapStructureHookFunc is a decode hook function for mapstructure
func (f *defaultFormats) MapStructureHookFunc() mapstructure.DecodeHookFunc {
return func(from reflect.Type, to reflect.Type, data interface{}) (interface{}, error) {
func (f *defaultFormats) MapStructureHookFunc() mapstructure.DecodeHookFunc { //nolint:gocyclo,cyclop
return func(from reflect.Type, to reflect.Type, obj interface{}) (interface{}, error) {
if from.Kind() != reflect.String {
return data, nil
return obj, nil
}
data, ok := obj.(string)
if !ok {
return nil, fmt.Errorf("failed to cast %+v to string", obj)
}

for _, v := range f.data {
tpe, _ := f.GetType(v.Name)
if to == tpe {
switch v.Name {
case "date":
d, err := time.Parse(RFC3339FullDate, data.(string))
d, err := time.Parse(RFC3339FullDate, data)
if err != nil {
return nil, err
}
return Date(d), nil
case "datetime":
input := data.(string)
input := data
if len(input) == 0 {
return nil, fmt.Errorf("empty string is an invalid datetime format")
}
return ParseDateTime(input)
case "duration":
dur, err := ParseDuration(data.(string))
dur, err := ParseDuration(data)
if err != nil {
return nil, err
}
return Duration(dur), nil
case "uri":
return URI(data.(string)), nil
return URI(data), nil
case "email":
return Email(data.(string)), nil
return Email(data), nil
case "uuid":
return UUID(data.(string)), nil
return UUID(data), nil
case "uuid3":
return UUID3(data.(string)), nil
return UUID3(data), nil
case "uuid4":
return UUID4(data.(string)), nil
return UUID4(data), nil
case "uuid5":
return UUID5(data.(string)), nil
return UUID5(data), nil
case "hostname":
return Hostname(data.(string)), nil
return Hostname(data), nil
case "ipv4":
return IPv4(data.(string)), nil
return IPv4(data), nil
case "ipv6":
return IPv6(data.(string)), nil
return IPv6(data), nil
case "cidr":
return CIDR(data.(string)), nil
return CIDR(data), nil
case "mac":
return MAC(data.(string)), nil
return MAC(data), nil
case "isbn":
return ISBN(data.(string)), nil
return ISBN(data), nil
case "isbn10":
return ISBN10(data.(string)), nil
return ISBN10(data), nil
case "isbn13":
return ISBN13(data.(string)), nil
return ISBN13(data), nil
case "creditcard":
return CreditCard(data.(string)), nil
return CreditCard(data), nil
case "ssn":
return SSN(data.(string)), nil
return SSN(data), nil
case "hexcolor":
return HexColor(data.(string)), nil
return HexColor(data), nil
case "rgbcolor":
return RGBColor(data.(string)), nil
return RGBColor(data), nil
case "byte":
return Base64(data.(string)), nil
return Base64(data), nil
case "password":
return Password(data.(string)), nil
return Password(data), nil
case "ulid":
ulid, err := ParseULID(data.(string))
ulid, err := ParseULID(data)
if err != nil {
return nil, err
}
Expand Down
8 changes: 6 additions & 2 deletions ulid.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (
}

ULIDScanDefaultFunc = func(raw interface{}) (ULID, error) {
var u ULID = NewULIDZero()
u := NewULIDZero()
switch x := raw.(type) {
case nil:
// zerp ulid
Expand Down Expand Up @@ -90,7 +90,11 @@ func NewULIDZero() ULID {

// NewULID generates new unique ULID value and a error if any
func NewULID() (u ULID, err error) {
entropy := ulidEntropyPool.Get().(io.Reader)
obj := ulidEntropyPool.Get()
entropy, ok := obj.(io.Reader)
if !ok {
return u, fmt.Errorf("failed to cast %+v to io.Reader", obj)
}

id, err := ulid.New(ulid.Now(), entropy)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion ulid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func TestFormatULID_Scan(t *testing.T) {
assert.NoError(t, err)

ULIDScanOverrideFunc = func(raw interface{}) (ULID, error) {
var u ULID = NewULIDZero()
u := NewULIDZero()
switch x := raw.(type) {
case [16]byte:
return u, u.ULID.UnmarshalBinary(x[:])
Expand Down

0 comments on commit c984fbd

Please sign in to comment.