-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Moved some common level name methods/types from
native/level
to …
…`level`. 2. Introduced a `LoggerFacade` interface with some more utility methods.
- Loading branch information
Showing
13 changed files
with
189 additions
and
112 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
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,41 @@ | ||
package level | ||
|
||
// Names is used to make readable names out of level.Level or the other way | ||
// around. | ||
type Names interface { | ||
// ToName converts a given level.Level to a human-readable name. If this | ||
// level is unknown by this instance an error is returned. Most likely | ||
// ErrIllegalLevel. | ||
ToName(Level) (string, error) | ||
|
||
// ToLevel converts a given human-readable name to a level.Level. If this | ||
// name is unknown by this instance an error is returned. Most likely | ||
// ErrIllegalLevel. | ||
ToLevel(string) (Level, error) | ||
} | ||
|
||
// NamesAware represents an object that is aware of Names. | ||
type NamesAware interface { | ||
// GetLevelNames returns an instance of level.Names that support by | ||
// formatting levels in a human-readable format. | ||
GetLevelNames() Names | ||
} | ||
|
||
// NewNamesFacade creates a facade of Names using the given provider. | ||
func NewNamesFacade(provider func() Names) Names { | ||
return namesFacade(provider) | ||
} | ||
|
||
type namesFacade func() Names | ||
|
||
func (instance namesFacade) ToName(lvl Level) (string, error) { | ||
return instance.Unwrap().ToName(lvl) | ||
} | ||
|
||
func (instance namesFacade) ToLevel(name string) (Level, error) { | ||
return instance.Unwrap().ToLevel(name) | ||
} | ||
|
||
func (instance namesFacade) Unwrap() Names { | ||
return instance() | ||
} |
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,67 @@ | ||
package level | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/echocat/slf4g/internal/test/assert" | ||
) | ||
|
||
func Test_NewNamesFacade(t *testing.T) { | ||
givenNames := &mockNames{} | ||
|
||
actual := NewNamesFacade(func() Names { | ||
return givenNames | ||
}) | ||
|
||
assert.ToBeSame(t, givenNames, actual.(namesFacade)()) | ||
} | ||
|
||
func Test_namesFacade_ToName(t *testing.T) { | ||
givenLevel := Warn | ||
givenError := errors.New("foo") | ||
givenNames := &mockNames{onToName: func(actualLevel Level) (string, error) { | ||
assert.ToBeEqual(t, givenLevel, actualLevel) | ||
return "bar", givenError | ||
}} | ||
instance := namesFacade(func() Names { return givenNames }) | ||
|
||
actual, actualErr := instance.ToName(givenLevel) | ||
|
||
assert.ToBeEqual(t, "bar", actual) | ||
assert.ToBeSame(t, givenError, actualErr) | ||
} | ||
|
||
func Test_namesFacade_ToLevel(t *testing.T) { | ||
givenLevel := Warn | ||
givenError := errors.New("foo") | ||
givenNames := &mockNames{onToLevel: func(actualName string) (Level, error) { | ||
assert.ToBeEqual(t, "bar", actualName) | ||
return givenLevel, givenError | ||
}} | ||
instance := namesFacade(func() Names { return givenNames }) | ||
|
||
actual, actualErr := instance.ToLevel("bar") | ||
|
||
assert.ToBeEqual(t, givenLevel, actual) | ||
assert.ToBeSame(t, givenError, actualErr) | ||
} | ||
|
||
type mockNames struct { | ||
onToName func(lvl Level) (string, error) | ||
onToLevel func(name string) (Level, error) | ||
} | ||
|
||
func (instance *mockNames) ToName(l Level) (string, error) { | ||
if v := instance.onToName; v != nil { | ||
return v(l) | ||
} | ||
panic("not implemented") | ||
} | ||
|
||
func (instance *mockNames) ToLevel(s string) (Level, error) { | ||
if v := instance.onToLevel; v != nil { | ||
return v(s) | ||
} | ||
panic("not implemented") | ||
} |
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
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
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
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
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
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
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
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
Oops, something went wrong.