Skip to content

Commit

Permalink
Upgraded linter, go version, dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
bogcon committed Jun 21, 2023
1 parent afb4da8 commit b245479
Show file tree
Hide file tree
Showing 59 changed files with 664 additions and 846 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ jobs:
timeout-minutes: 10
strategy:
matrix:
go-version: [1.18.x, 1.19.x]
go-version: [1.19.x, 1.20.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}

steps:
- name: Install Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}

Expand Down Expand Up @@ -48,7 +48,7 @@ jobs:
timeout-minutes: 10
runs-on: ubuntu-latest

container: golang:1.19.3-bullseye
container: golang:1.20.5-bullseye

steps:
- name: Checkout code
Expand Down
2 changes: 1 addition & 1 deletion .golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ linters:
- goconst
- gocritic
- godot
- lll
- misspell
- nlreturn
- noctx
- whitespace
- lll

issues:
exclude-use-default: false
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
LINTER_VERSION=v1.50.1
LINTER_VERSION=v1.53.3
LINTER=./bin/golangci-lint
ifeq ($(OS),Windows_NT)
LINTER=./bin/golangci-lint.exe
Expand Down Expand Up @@ -39,7 +39,7 @@ setup-test-integration-data: ## Run integration tests data setup scripts.

.PHONY: bench
bench: ## Run benchmarks.
go test -race -run=^# -benchmem -benchtime=5s -bench=.
go test -race -benchmem -benchtime=5s -bench=.

.PHONY: cover
cover: ## Run tests with coverage. Generates "cover.out" profile and its html representation.
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ The main configuration contract this package provides looks like:

```go
type Config interface {
Get(key string, def ...interface{}) interface{}
Get(key string, def ...any) any
}
```
with a default implementation obtained with:
Expand Down Expand Up @@ -144,7 +144,7 @@ const (
type RedisClient interface {
Ping() error
Get(key string) (string, error)
Set(key string, value interface{}, expiration time.Duration) (string, error)
Set(key string, value any, expiration time.Duration) (string, error)
Close() error
}

Expand Down Expand Up @@ -246,7 +246,7 @@ func main() {
}
}`
dbConfig DBConfig // the struct to populate with configuration
dbConfigMap map[string]interface{} // the configuration map for "db" key
dbConfigMap map[string]any // the configuration map for "db" key
loader = xconf.JSONReaderLoader(bytes.NewReader([]byte(jsonConfig)))
)

Expand All @@ -255,7 +255,7 @@ func main() {
if err != nil {
panic(err)
}
dbConfigMap = configMap["db"].(map[string]interface{})
dbConfigMap = configMap["db"].(map[string]any)
if err := mapstructure.Decode(dbConfigMap, &dbConfig); err != nil {
panic(err)
}
Expand All @@ -266,7 +266,7 @@ func main() {
if err != nil {
panic(err)
}
dbConfigMap = config.Get("db").(map[string]interface{})
dbConfigMap = config.Get("db").(map[string]any)
if err := mapstructure.Decode(dbConfigMap, &dbConfig); err != nil {
panic(err)
}
Expand All @@ -283,7 +283,7 @@ Things that can be added to package, extended:
- Add also a writer/persister functionality (currently you can only read configurations) to different sources and formats (JSONFileWriter/YAMLFileWriter/EtcdWriter/ConsulWriter/...) implementing a common contract like:
```go
type ConfigWriter interface {
Write(configMap map[string]interface{}) error
Write(configMap map[string]any) error
}
```
- Add a typed struct with methods like `GetString`, `GetInt`...
Expand Down
10 changes: 5 additions & 5 deletions assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

// assertEqual checks if 2 values are equal.
// Returns successful assertion status.
func assertEqual(t *testing.T, expected interface{}, actual interface{}) bool {
func assertEqual(t *testing.T, expected any, actual any) bool {
t.Helper()
if !reflect.DeepEqual(expected, actual) {
t.Errorf(
Expand All @@ -32,7 +32,7 @@ func assertEqual(t *testing.T, expected interface{}, actual interface{}) bool {

// assertNotNil checks if value passed is not nil.
// Returns successful assertion status.
func assertNotNil(t *testing.T, actual interface{}) bool {
func assertNotNil(t *testing.T, actual any) bool {
t.Helper()
if isNil(actual) {
t.Error("should not be nil")
Expand All @@ -45,7 +45,7 @@ func assertNotNil(t *testing.T, actual interface{}) bool {

// assertNil checks if value passed is nil.
// Returns successful assertion status.
func assertNil(t *testing.T, actual interface{}) bool {
func assertNil(t *testing.T, actual any) bool {
t.Helper()
if !isNil(actual) {
t.Errorf("expected nil, but got %+v", actual)
Expand All @@ -57,7 +57,7 @@ func assertNil(t *testing.T, actual interface{}) bool {
}

// requireNil fails the test immediately if passed value is not nil.
func requireNil(t *testing.T, actual interface{}) {
func requireNil(t *testing.T, actual any) {
t.Helper()
if !isNil(actual) {
t.Errorf("expected nil, but got %+v", actual)
Expand All @@ -79,7 +79,7 @@ func assertTrue(t *testing.T, actual bool) bool {
}

// isNil checks an interface if it is nil.
func isNil(object interface{}) bool {
func isNil(object any) bool {
if object == nil {
return true
}
Expand Down
14 changes: 7 additions & 7 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Config interface {
// value in case key is not found. It also has a role in inferring
// the type of key's value (if it exists) and thus key's value
// will be casted to default's value type.
Get(key string, def ...interface{}) interface{}
Get(key string, def ...any) any
}

// DefaultConfig is the default implementation for the Config contract.
Expand All @@ -38,7 +38,7 @@ type defaultConfig struct {
// loader to retrieve configuration from.
loader Loader
// configMap the loaded key-value configuration map.
configMap map[string]interface{}
configMap map[string]any
// observers contain the list of registered observers for changed keys.
observers []ConfigObserver
// refreshInterval represents the interval to reload the configMap.
Expand Down Expand Up @@ -100,7 +100,7 @@ func NewDefaultConfig(loader Loader, opts ...DefaultConfigOption) (*DefaultConfi
// Only basic types (string, bool, int, uint, float, and their flavours),
// time.Duration, time.Time, []int, []string are covered.
// If a cast error occurs, the defaultValue is returned.
func (cfg *defaultConfig) Get(key string, def ...interface{}) interface{} {
func (cfg *defaultConfig) Get(key string, def ...any) any {
if cfg.ignoreCaseSensitivity {
key = strings.ToUpper(key)
}
Expand Down Expand Up @@ -161,7 +161,7 @@ func (cfg *defaultConfig) setConfigMap() error {

// notifyObservers computes changed (updated/deleted/new) keys on a config reload,
// and notifies registered observers about them, if there are any changed keys and observers.
func (cfg *defaultConfig) notifyObservers(oldConfigMap, newConfigMap map[string]interface{}) {
func (cfg *defaultConfig) notifyObservers(oldConfigMap, newConfigMap map[string]any) {
cfg.mu.RLock()
defer cfg.mu.RUnlock()

Expand Down Expand Up @@ -232,9 +232,9 @@ func (cfg *DefaultConfig) Close() error {
// Only basic types (string, bool, int, uint, float, and their flavours),
// time.Duration, time.Time, []int, []string are covered.
// If a cast error occurs, the defaultValue is returned.
func castValueByDefault(value, defaultValue interface{}) interface{} {
func castValueByDefault(value, defaultValue any) any {
var (
castValue interface{}
castValue any
castErr error
)
switch defaultValue.(type) {
Expand Down Expand Up @@ -286,7 +286,7 @@ func castValueByDefault(value, defaultValue interface{}) interface{} {
}

// toUppercaseConfigMap transforms all (first level) keys to uppercase.
func toUppercaseConfigMap(configMap map[string]interface{}) {
func toUppercaseConfigMap(configMap map[string]any) {
for key, value := range configMap {
delete(configMap, key)
// Note: here if a duplicate key exists, it will get overwritten.
Expand Down
16 changes: 8 additions & 8 deletions config_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
type MockConfig struct {
cfg *DefaultConfig
getCallsCnt uint32
getCallback func(key string, def ...interface{})
configMap map[string]interface{}
getCallback func(key string, def ...any)
configMap map[string]any
mu *sync.Mutex
}

Expand All @@ -28,9 +28,9 @@ type MockConfig struct {
// "foo", "bar",
// "year", 2022,
// )
func NewMockConfig(kv ...interface{}) *MockConfig {
func NewMockConfig(kv ...any) *MockConfig {
mock := &MockConfig{
configMap: make(map[string]interface{}),
configMap: make(map[string]any),
mu: new(sync.Mutex),
}
mock.SetKeyValues(kv...)
Expand All @@ -39,7 +39,7 @@ func NewMockConfig(kv ...interface{}) *MockConfig {
}

// Get mock logic.
func (mock *MockConfig) Get(key string, def ...interface{}) interface{} {
func (mock *MockConfig) Get(key string, def ...any) any {
atomic.AddUint32(&mock.getCallsCnt, 1)
if mock.getCallback != nil {
mock.getCallback(key, def...)
Expand All @@ -50,7 +50,7 @@ func (mock *MockConfig) Get(key string, def ...interface{}) interface{} {

// SetKeyValues sets/resets given key-values.
// Make sure you pass an even number of elements and that the keys are strings.
func (mock *MockConfig) SetKeyValues(kv ...interface{}) {
func (mock *MockConfig) SetKeyValues(kv ...any) {
kvLen := len(kv)
if len(kv)%2 == 1 {
kvLen-- // skip last element
Expand All @@ -75,7 +75,7 @@ func (mock *MockConfig) SetKeyValues(kv ...interface{}) {
//
// Usage example:
//
// mock.SetGetCallback(func(key string, def ...interface{}) {
// mock.SetGetCallback(func(key string, def ...any) {
// switch mock.GetCallsCount() {
// case 1:
// if key != "expectedKeyAtCall1" {
Expand All @@ -87,7 +87,7 @@ func (mock *MockConfig) SetKeyValues(kv ...interface{}) {
// }
// }
// })
func (mock *MockConfig) SetGetCallback(callback func(key string, def ...interface{})) {
func (mock *MockConfig) SetGetCallback(callback func(key string, def ...any)) {
mock.getCallback = callback
}

Expand Down
2 changes: 1 addition & 1 deletion config_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestMockConfig(t *testing.T) {
100, "not a string key", // test that this is skipped, as key is not string.
"odd number of elements", // test that this is skipped, as elements no. is odd.
)
subject.SetGetCallback(func(key string, def ...interface{}) {
subject.SetGetCallback(func(key string, def ...any) {
switch subject.GetCallsCount() {
case 1:
assertEqual(t, "foo", key)
Expand Down
2 changes: 1 addition & 1 deletion config_nop.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package xconf
type NopConfig struct{}

// Get returns default value, if present, or nil.
func (NopConfig) Get(_ string, def ...interface{}) interface{} {
func (NopConfig) Get(_ string, def ...any) any {
if len(def) > 0 {
return def[0]
}
Expand Down
Loading

0 comments on commit b245479

Please sign in to comment.