-
-
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.
- Loading branch information
Showing
3 changed files
with
110 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
var ( | ||
ErrNoToPair = errors.New("`to` values are not set for every `from`") | ||
ErrNoFromPair = errors.New("`from` values are not set for every `to`") | ||
) | ||
|
||
func ReadURLMapping(config *viper.Viper) (map[string]string, error) { | ||
urlMappings := map[string]string{} | ||
from, to := config.GetStringSlice("from"), config.GetStringSlice("to") // nolint: varnamelen | ||
|
||
if len(from) > len(to) { | ||
return nil, ErrNoToPair | ||
} | ||
|
||
if len(to) > len(from) { | ||
return nil, ErrNoFromPair | ||
} | ||
|
||
for index, key := range from { | ||
urlMappings[key] = to[index] | ||
} | ||
|
||
return urlMappings, nil | ||
} |
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,71 @@ | ||
package config_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/evg4b/uncors/internal/config" | ||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestReadURLMapping(t *testing.T) { | ||
t.Run("correctly map pairs", func(t *testing.T) { | ||
viperConfig := viper.New() | ||
viperConfig.Set("from", []string{"host1", "host2", "host3"}) | ||
viperConfig.Set("to", []string{"target-host1", "target-host2", "target-host3"}) | ||
actual, err := config.ReadURLMapping(viperConfig) | ||
|
||
assert.NoError(t, err) | ||
assert.EqualValues(t, map[string]string{ | ||
"host1": "target-host1", | ||
"host2": "target-host2", | ||
"host3": "target-host3", | ||
}, actual) | ||
}) | ||
|
||
t.Run("incorrect pairs", func(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
from []string | ||
to []string | ||
expectedErr string | ||
}{ | ||
{ | ||
name: "from is not set", | ||
from: []string{"host1"}, | ||
to: []string{}, | ||
expectedErr: "`to` values are not set for every `from`", | ||
}, | ||
{ | ||
name: "to is not set", | ||
from: []string{}, | ||
to: []string{"target-host1"}, | ||
expectedErr: "`from` values are not set for every `to`", | ||
}, | ||
{ | ||
name: "count of from values greath then count of to", | ||
from: []string{"host1", "host2"}, | ||
to: []string{"target-host1"}, | ||
expectedErr: "`to` values are not set for every `from`", | ||
}, | ||
{ | ||
name: "count of to values greath then count of from", | ||
from: []string{"host1"}, | ||
to: []string{"target-host1", "target-host2"}, | ||
expectedErr: "`from` values are not set for every `to`", | ||
}, | ||
} | ||
for _, testCase := range tests { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
viperConfig := viper.New() | ||
viperConfig.Set("from", testCase.from) | ||
viperConfig.Set("to", testCase.to) | ||
|
||
actual, err := config.ReadURLMapping(viperConfig) | ||
|
||
assert.Nil(t, actual) | ||
assert.EqualError(t, err, testCase.expectedErr) | ||
}) | ||
} | ||
}) | ||
} |
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