Skip to content

Commit

Permalink
make logLevel and logPath strings, not pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-knozderko committed Oct 12, 2023
1 parent a899c78 commit 1f5d0d5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
18 changes: 9 additions & 9 deletions client_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type ClientConfig struct {

// ClientConfigCommonProps properties from "common" section
type ClientConfigCommonProps struct {
LogLevel *string `json:"log_level"`
LogPath *string `json:"log_path"`
LogLevel string `json:"log_level,omitempty"`
LogPath string `json:"log_path,omitempty"`
}

func parseClientConfiguration(filePath string) (*ClientConfig, error) {
Expand Down Expand Up @@ -62,26 +62,26 @@ func validateClientConfiguration(clientConfig *ClientConfig) error {
if clientConfig.Common == nil {
return errors.New("common section in client config not found")
}
return validateLogLevel(clientConfig)
return validateLogLevel(*clientConfig)
}

func validateLogLevel(clientConfig *ClientConfig) error {
func validateLogLevel(clientConfig ClientConfig) error {
var logLevel = clientConfig.Common.LogLevel
if logLevel != nil && *logLevel != "" {
_, error := toLogLevel(*logLevel)
if logLevel != "" {
_, error := toLogLevel(logLevel)
if error != nil {
return error
}
}
return nil
}

func toLogLevel(logLevelString string) (*string, error) {
func toLogLevel(logLevelString string) (string, error) {
var logLevel = strings.ToUpper(logLevelString)
switch logLevel {
case Off, Error, Warn, Info, Debug, Trace:
return &logLevel, nil
return logLevel, nil
default:
return nil, errors.New("unknown log level: " + logLevelString)
return "", errors.New("unknown log level: " + logLevelString)
}
}
23 changes: 9 additions & 14 deletions client_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func TestParseConfiguration(t *testing.T) {
Name string
FileName string
FileContents string
ExpectedLogLevel *string
ExpectedLogPath *string
ExpectedLogLevel string
ExpectedLogPath string
}{
{
Name: "TestWithLogLevelUpperCase",
Expand All @@ -29,8 +29,8 @@ func TestParseConfiguration(t *testing.T) {
"log_path" : "/some-path/some-directory"
}
}`,
ExpectedLogLevel: toStringPointer("INFO"),
ExpectedLogPath: toStringPointer("/some-path/some-directory"),
ExpectedLogLevel: "INFO",
ExpectedLogPath: "/some-path/some-directory",
},
{
Name: "TestWithLogLevelLowerCase",
Expand All @@ -41,17 +41,17 @@ func TestParseConfiguration(t *testing.T) {
"log_path" : "/some-path/some-directory"
}
}`,
ExpectedLogLevel: toStringPointer("info"),
ExpectedLogPath: toStringPointer("/some-path/some-directory"),
ExpectedLogLevel: "info",
ExpectedLogPath: "/some-path/some-directory",
},
{
Name: "TestWithMissingValues",
FileName: "config_3.json",
FileContents: `{
"common": {}
}`,
ExpectedLogLevel: nil,
ExpectedLogPath: nil,
ExpectedLogLevel: "",
ExpectedLogPath: "",
},
}
for _, tc := range testCases {
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestParseAllLogLevels(t *testing.T) {

assert := assert.New(t)
assert.Equal(nil, err, "Error should be nil")
assert.Equal(logLevel, *config.Common.LogLevel, "Log level should be as expected")
assert.Equal(logLevel, config.Common.LogLevel, "Log level should be as expected")
})
}
}
Expand Down Expand Up @@ -155,11 +155,6 @@ func TestParseConfigurationFails(t *testing.T) {
}
}

func toStringPointer(value string) *string {
var copyOfValue = value
return &copyOfValue
}

func CreateFile(t *testing.T, fileName string, fileContents string, directory string) string {
fullFileName := path.Join(directory, fileName)
writeErr := os.WriteFile(fullFileName, []byte(fileContents), 0644)
Expand Down

0 comments on commit 1f5d0d5

Please sign in to comment.