Skip to content

Commit

Permalink
feat: Add GetAppSetting convenience API (#761)
Browse files Browse the repository at this point in the history
close #760

Signed-off-by: lenny <[email protected]>
  • Loading branch information
lenny-goodell authored Mar 25, 2021
1 parent 80a7af3 commit 7158bb1
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
16 changes: 15 additions & 1 deletion internal/app/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,20 @@ func (svc *Service) ApplicationSettings() map[string]string {
return svc.config.ApplicationSettings
}

// GetAppSettingStrings returns the string for the specified App Setting.
func (svc *Service) GetAppSetting(setting string) (string, error) {
if svc.config.ApplicationSettings == nil {
return "", fmt.Errorf("%s setting not found: ApplicationSettings section is missing", setting)
}

settingValue, ok := svc.config.ApplicationSettings[setting]
if !ok {
return "", fmt.Errorf("%s setting not found in ApplicationSettings section", setting)
}

return settingValue, nil
}

// GetAppSettingStrings returns the strings slice for the specified App Setting.
func (svc *Service) GetAppSettingStrings(setting string) ([]string, error) {
if svc.config.ApplicationSettings == nil {
Expand All @@ -331,7 +345,7 @@ func (svc *Service) GetAppSettingStrings(setting string) ([]string, error) {

settingValue, ok := svc.config.ApplicationSettings[setting]
if !ok {
return nil, fmt.Errorf("%s setting not found in ApplicationSettings", setting)
return nil, fmt.Errorf("%s setting not found in ApplicationSettings section", setting)
}

valueStrings := util.DeleteEmptyAndTrim(strings.FieldsFunc(settingValue, util.SplitComma))
Expand Down
27 changes: 27 additions & 0 deletions internal/app/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,33 @@ func TestApplicationSettingsNil(t *testing.T) {
require.Nil(t, appSettings, "returned application settings expected to be nil")
}

func TestGetAppSetting(t *testing.T) {
goodSettingName := "ExportUrl"
expectedGoodValue := "http:/somewhere.com"
badSettingName := "DeviceName"

svc := Service{
config: &common.ConfigurationStruct{
ApplicationSettings: map[string]string{
goodSettingName: expectedGoodValue,
},
},
}

actual, err := svc.GetAppSetting(goodSettingName)
require.NoError(t, err)
assert.EqualValues(t, expectedGoodValue, actual, "actual application setting values not as expected")

_, err = svc.GetAppSetting(badSettingName)
require.Error(t, err)
assert.EqualError(t, err, fmt.Sprintf("%s setting not found in ApplicationSettings section", badSettingName))

svc.config.ApplicationSettings = nil
_, err = svc.GetAppSetting(goodSettingName)
require.Error(t, err)
assert.EqualError(t, err, fmt.Sprintf("%s setting not found: ApplicationSettings section is missing", goodSettingName))
}

func TestGetAppSettingStrings(t *testing.T) {
setting := "DeviceNames"
expected := []string{"dev1", "dev2"}
Expand Down
16 changes: 16 additions & 0 deletions pkg/interfaces/mocks/AppFunctionContext.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions pkg/interfaces/mocks/ApplicationService.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion pkg/interfaces/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@ type ApplicationService interface {
AddRoute(route string, handler func(http.ResponseWriter, *http.Request), methods ...string) error
// ApplicationSettings returns the key/value map of custom settings
ApplicationSettings() map[string]string
// GetAppSetting is a convenience function return a setting from the ApplicationSetting
// section of the service configuration.
// An error is returned if the specified setting is not found.
GetAppSetting(setting string) (string, error)
// GetAppSettingStrings is a convenience function that parses the value for the specified custom
// application setting as a comma separated list. It returns the list of strings.
// An error is returned if the specified setting is no found.
// An error is returned if the specified setting is not found.
GetAppSettingStrings(setting string) ([]string, error)
// SetFunctionsPipeline set the functions pipeline with the specified list of Application Functions.
// Note that the functions are executed in the order provided in the list.
Expand Down

0 comments on commit 7158bb1

Please sign in to comment.