Skip to content

Commit

Permalink
Add error conversion (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Philippe Melanson authored and evalphobia committed Jul 11, 2019
1 parent 7275750 commit 989dca7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ func ConvertToValue(p interface{}, tagName string) interface{} {
rv := toValue(p)
switch rv.Kind() {
case reflect.Struct:
if err, ok := p.(error); ok {
return err.Error()
}
return convertFromStruct(rv.Interface(), tagName)
case reflect.Map:
return convertFromMap(rv, tagName)
Expand Down
14 changes: 14 additions & 0 deletions reflect_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logrus_fluent

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -25,6 +26,19 @@ type Creature struct {
Cute bool `fluent:"-"`
}

func TestConvertToValuError(t *testing.T) {
assert := assert.New(t)

err := errors.New("the error")
data := map[string]interface{} {"error": err}

result := ConvertToValue(data, TagName)

r, ok := result.(map[string]interface{})
assert.True(ok)
assert.Equal(err.Error(), r["error"])
}

func TestConvertToValueStruct(t *testing.T) {
assert := assert.New(t)

Expand Down

0 comments on commit 989dca7

Please sign in to comment.