Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return Error from ParseLevel #83

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions level.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package log

import "strings"
import (
"fmt"
"strings"
)

// Level is a logging level.
type Level int32
Expand Down Expand Up @@ -38,20 +41,24 @@ func (l Level) String() string {
}
}

func parseLevelError(level string) error {
return fmt.Errorf("invalid level %s", strings.ToLower(level))
}

dezren39 marked this conversation as resolved.
Show resolved Hide resolved
// ParseLevel converts level in string to Level type. Default level is InfoLevel.
func ParseLevel(level string) Level {
func ParseLevel(level string) (Level, error) {
switch strings.ToLower(level) {
case DebugLevel.String():
return DebugLevel
return DebugLevel, nil
case InfoLevel.String():
return InfoLevel
return InfoLevel, nil
case WarnLevel.String():
return WarnLevel
return WarnLevel, nil
case ErrorLevel.String():
return ErrorLevel
return ErrorLevel, nil
case FatalLevel.String():
return FatalLevel
return FatalLevel, nil
default:
return InfoLevel
return InfoLevel, parseLevelError(level)
dezren39 marked this conversation as resolved.
Show resolved Hide resolved
}
}
57 changes: 32 additions & 25 deletions level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,59 @@ func TestDefaultLevel(t *testing.T) {
var level Level
assert.Equal(t, InfoLevel, level)
}

type parseLevelResult struct {
level Level
err error
}

dezren39 marked this conversation as resolved.
Show resolved Hide resolved
func TestParseLevel(t *testing.T) {
testCases := []struct {
name string
level string
expLevel Level
name string
input string
result parseLevelResult
dezren39 marked this conversation as resolved.
Show resolved Hide resolved
}{
{
name: "Parse debug",
level: "debug",
expLevel: DebugLevel,
name: "Parse debug",
input: "debug",
result: parseLevelResult{DebugLevel, nil},
},
{
name: "Parse info",
level: "Info",
expLevel: InfoLevel,
name: "Parse info",
input: "Info",
result: parseLevelResult{InfoLevel, nil},
},
{
name: "Parse warn",
level: "WARN",
expLevel: WarnLevel,
name: "Parse warn",
input: "WARN",
result: parseLevelResult{WarnLevel, nil},
},
{
name: "Parse error",
level: "error",
expLevel: ErrorLevel,
name: "Parse error",
input: "error",
result: parseLevelResult{ErrorLevel, nil},
},
{
name: "Parse fatal",
level: "FATAL",
expLevel: FatalLevel,
name: "Parse fatal",
input: "FATAL",
result: parseLevelResult{FatalLevel, nil},
},
{
name: "Default",
level: "",
expLevel: InfoLevel,
name: "Default",
input: "",
result: parseLevelResult{InfoLevel, parseLevelError("")},
},
{
name: "Wrong level, set INFO",
level: "WRONG_LEVEL",
expLevel: InfoLevel,
name: "Wrong level, set INFO",
input: "WRONG_LEVEL",
result: parseLevelResult{InfoLevel, parseLevelError("WRONG_LEVEL")},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.expLevel, ParseLevel(tc.level))
lvl, err := ParseLevel(tc.input)
assert.Equal(t, tc.result, parseLevelResult{lvl, err})
})
}
}