-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created added graceful shutdown support
Showing
7 changed files
with
100 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package hooks | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
"time" | ||
|
||
"github.com/mitchellh/mapstructure" | ||
) | ||
|
||
func StringToTimeDurationHookFunc() mapstructure.DecodeHookFunc { //nolint: ireturn | ||
return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) { | ||
if f.Kind() != reflect.String { | ||
return data, nil | ||
} | ||
|
||
if t != reflect.TypeOf(time.Second) { | ||
return data, nil | ||
} | ||
|
||
trimmed := strings.ReplaceAll(data.(string), " ", "") //nolint: forcetypeassert | ||
|
||
return time.ParseDuration(trimmed) //nolint:wrapcheck | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package hooks_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/evg4b/uncors/internal/configuration/hooks" | ||
"github.com/evg4b/uncors/testing/testutils" | ||
"github.com/mitchellh/mapstructure" | ||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestStringToTimeDurationHookFunc(t *testing.T) { | ||
const key = "duration" | ||
viperInstance := viper.New() | ||
configOption := viper.DecodeHook(mapstructure.ComposeDecodeHookFunc( | ||
hooks.StringToTimeDurationHookFunc(), | ||
)) | ||
|
||
tests := []struct { | ||
name string | ||
value string | ||
expected time.Duration | ||
}{ | ||
{ | ||
name: "duration with spaces", | ||
value: "1m 4s", | ||
expected: 1*time.Minute + 4*time.Second, | ||
}, | ||
{ | ||
name: "duration without spaces", | ||
value: "3h6m13s", | ||
expected: 3*time.Hour + 6*time.Minute + 13*time.Second, | ||
}, | ||
} | ||
|
||
for _, testCase := range tests { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
viperInstance.Set(key, testCase.value) | ||
|
||
durationValue := time.Duration(0) | ||
err := viperInstance.UnmarshalKey(key, &durationValue, configOption) | ||
testutils.CheckNoError(t, err) | ||
|
||
assert.Equal(t, testCase.expected, durationValue) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package mock | ||
|
||
import "time" | ||
import ( | ||
"time" | ||
) | ||
|
||
type Response struct { | ||
Code int `mapstructure:"code"` | ||
|