Skip to content

Commit

Permalink
Merge pull request #2 from go-viper/lint
Browse files Browse the repository at this point in the history
chore: fix lint violations
  • Loading branch information
sagikazarmark authored Dec 18, 2023
2 parents b356139 + f9487f1 commit c2530db
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 97 deletions.
8 changes: 4 additions & 4 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ linters-settings:
linters:
disable-all: true
enable:
# - gci
# - gofmt
# - gofumpt
# - goimports
- gci
- gofmt
- gofumpt
- goimports
- staticcheck
# - stylecheck
25 changes: 16 additions & 9 deletions decode_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func typedDecodeHook(h DecodeHookFunc) DecodeHookFunc {
// that took reflect.Kind instead of reflect.Type.
func DecodeHookExec(
raw DecodeHookFunc,
from reflect.Value, to reflect.Value) (interface{}, error) {

from reflect.Value, to reflect.Value,
) (interface{}, error) {
switch f := typedDecodeHook(raw).(type) {
case DecodeHookFuncType:
return f(from.Type(), to.Type(), from.Interface())
Expand Down Expand Up @@ -105,7 +105,8 @@ func StringToSliceHookFunc(sep string) DecodeHookFunc {
return func(
f reflect.Kind,
t reflect.Kind,
data interface{}) (interface{}, error) {
data interface{},
) (interface{}, error) {
if f != reflect.String || t != reflect.Slice {
return data, nil
}
Expand All @@ -125,7 +126,8 @@ func StringToTimeDurationHookFunc() DecodeHookFunc {
return func(
f reflect.Type,
t reflect.Type,
data interface{}) (interface{}, error) {
data interface{},
) (interface{}, error) {
if f.Kind() != reflect.String {
return data, nil
}
Expand All @@ -144,7 +146,8 @@ func StringToIPHookFunc() DecodeHookFunc {
return func(
f reflect.Type,
t reflect.Type,
data interface{}) (interface{}, error) {
data interface{},
) (interface{}, error) {
if f.Kind() != reflect.String {
return data, nil
}
Expand All @@ -168,7 +171,8 @@ func StringToIPNetHookFunc() DecodeHookFunc {
return func(
f reflect.Type,
t reflect.Type,
data interface{}) (interface{}, error) {
data interface{},
) (interface{}, error) {
if f.Kind() != reflect.String {
return data, nil
}
Expand All @@ -188,7 +192,8 @@ func StringToTimeHookFunc(layout string) DecodeHookFunc {
return func(
f reflect.Type,
t reflect.Type,
data interface{}) (interface{}, error) {
data interface{},
) (interface{}, error) {
if f.Kind() != reflect.String {
return data, nil
}
Expand All @@ -209,7 +214,8 @@ func StringToTimeHookFunc(layout string) DecodeHookFunc {
func WeaklyTypedHook(
f reflect.Kind,
t reflect.Kind,
data interface{}) (interface{}, error) {
data interface{},
) (interface{}, error) {
dataVal := reflect.ValueOf(data)
switch t {
case reflect.String:
Expand Down Expand Up @@ -262,7 +268,8 @@ func TextUnmarshallerHookFunc() DecodeHookFuncType {
return func(
f reflect.Type,
t reflect.Type,
data interface{}) (interface{}, error) {
data interface{},
) (interface{}, error) {
if f.Kind() != reflect.String {
return data, nil
}
Expand Down
52 changes: 34 additions & 18 deletions decode_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ func TestComposeDecodeHookFunc(t *testing.T) {
f1 := func(
f reflect.Kind,
t reflect.Kind,
data interface{}) (interface{}, error) {
data interface{},
) (interface{}, error) {
return data.(string) + "foo", nil
}

f2 := func(
f reflect.Kind,
t reflect.Kind,
data interface{}) (interface{}, error) {
data interface{},
) (interface{}, error) {
return data.(string) + "bar", nil
}

Expand Down Expand Up @@ -61,14 +63,16 @@ func TestComposeDecodeHookFunc_kinds(t *testing.T) {
f1 := func(
f reflect.Kind,
t reflect.Kind,
data interface{}) (interface{}, error) {
data interface{},
) (interface{}, error) {
return int(42), nil
}

f2 := func(
f reflect.Kind,
t reflect.Kind,
data interface{}) (interface{}, error) {
data interface{},
) (interface{}, error) {
f2From = f
return data, nil
}
Expand All @@ -89,14 +93,16 @@ func TestOrComposeDecodeHookFunc(t *testing.T) {
f1 := func(
f reflect.Kind,
t reflect.Kind,
data interface{}) (interface{}, error) {
data interface{},
) (interface{}, error) {
return data.(string) + "foo", nil
}

f2 := func(
f reflect.Kind,
t reflect.Kind,
data interface{}) (interface{}, error) {
data interface{},
) (interface{}, error) {
return data.(string) + "bar", nil
}

Expand All @@ -116,21 +122,24 @@ func TestOrComposeDecodeHookFunc_correctValueIsLast(t *testing.T) {
f1 := func(
f reflect.Kind,
t reflect.Kind,
data interface{}) (interface{}, error) {
data interface{},
) (interface{}, error) {
return nil, errors.New("f1 error")
}

f2 := func(
f reflect.Kind,
t reflect.Kind,
data interface{}) (interface{}, error) {
data interface{},
) (interface{}, error) {
return nil, errors.New("f2 error")
}

f3 := func(
f reflect.Kind,
t reflect.Kind,
data interface{}) (interface{}, error) {
data interface{},
) (interface{}, error) {
return data.(string) + "bar", nil
}

Expand All @@ -150,14 +159,16 @@ func TestOrComposeDecodeHookFunc_err(t *testing.T) {
f1 := func(
f reflect.Kind,
t reflect.Kind,
data interface{}) (interface{}, error) {
data interface{},
) (interface{}, error) {
return nil, errors.New("f1 error")
}

f2 := func(
f reflect.Kind,
t reflect.Kind,
data interface{}) (interface{}, error) {
data interface{},
) (interface{}, error) {
return nil, errors.New("f2 error")
}

Expand Down Expand Up @@ -281,8 +292,10 @@ func TestStringToTimeHookFunc(t *testing.T) {
result interface{}
err bool
}{
{reflect.ValueOf("2006-01-02T15:04:05Z"), timeValue, time.RFC3339,
time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC), false},
{
reflect.ValueOf("2006-01-02T15:04:05Z"), timeValue, time.RFC3339,
time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC), false,
},
{strValue, timeValue, time.RFC3339, time.Time{}, true},
{strValue, strValue, time.RFC3339, "5", false},
}
Expand All @@ -309,8 +322,10 @@ func TestStringToIPHookFunc(t *testing.T) {
result interface{}
err bool
}{
{reflect.ValueOf("1.2.3.4"), ipValue,
net.IPv4(0x01, 0x02, 0x03, 0x04), false},
{
reflect.ValueOf("1.2.3.4"), ipValue,
net.IPv4(0x01, 0x02, 0x03, 0x04), false,
},
{strValue, ipValue, net.IP{}, true},
{strValue, strValue, "5", false},
}
Expand Down Expand Up @@ -339,11 +354,13 @@ func TestStringToIPNetHookFunc(t *testing.T) {
result interface{}
err bool
}{
{reflect.ValueOf("1.2.3.4/24"), ipNetValue,
{
reflect.ValueOf("1.2.3.4/24"), ipNetValue,
&net.IPNet{
IP: net.IP{0x01, 0x02, 0x03, 0x00},
Mask: net.IPv4Mask(0xff, 0xff, 0xff, 0x00),
}, false},
}, false,
},
{strValue, ipNetValue, nilNet, true},
{strValue, strValue, "5", false},
}
Expand Down Expand Up @@ -538,7 +555,6 @@ func TestStructToMapHookFuncTabled(t *testing.T) {
tc.expected, tc.receiver)
}
})

}
}

Expand Down
Loading

0 comments on commit c2530db

Please sign in to comment.