Browse Source

Add support for HTML to String

tags/v1.0.0
bep spf13 9 years ago
parent
commit
0a58809502
2 changed files with 5 additions and 0 deletions
  1. +2
    -0
      cast_test.go
  2. +3
    -0
      caste.go

+ 2
- 0
cast_test.go View File

@@ -9,6 +9,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"html/template"
)

func TestToInt(t *testing.T) {
@@ -34,6 +35,7 @@ func TestToString(t *testing.T) {
assert.Equal(t, ToString(8), "8")
assert.Equal(t, ToString(8.12), "8.12")
assert.Equal(t, ToString([]byte("one time")), "one time")
assert.Equal(t, ToString(template.HTML("one time")), "one time")
assert.Equal(t, ToString(foo), "one more time")
assert.Equal(t, ToString(nil), "")
}


+ 3
- 0
caste.go View File

@@ -8,6 +8,7 @@ package cast
import (
"errors"
"fmt"
"html/template"
"reflect"
"strconv"
"time"
@@ -130,6 +131,8 @@ func ToStringE(i interface{}) (string, error) {
return strconv.FormatInt(int64(i.(int)), 10), nil
case []byte:
return string(s), nil
case template.HTML:
return string(s), nil
case nil:
return "", nil
default:


Loading…
Cancel
Save