-
-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #292 from hairyhenderson/add-conv-tostring-func
Adding conv.ToString function
- Loading branch information
Showing
5 changed files
with
129 additions
and
15 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
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,45 @@ | ||
package conv | ||
|
||
// stolen from the Go standard library, text/template package. | ||
import ( | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
var ( | ||
errorType = reflect.TypeOf((*error)(nil)).Elem() | ||
fmtStringerType = reflect.TypeOf((*fmt.Stringer)(nil)).Elem() | ||
) | ||
|
||
// printableValue returns the, possibly indirected, interface value inside v that | ||
// is best for a call to formatted printer. | ||
func printableValue(v reflect.Value) (interface{}, bool) { | ||
if v.Kind() == reflect.Ptr { | ||
v, _ = indirect(v) // fmt.Fprint handles nil. | ||
} | ||
if !v.IsValid() { | ||
return "<no value>", true | ||
} | ||
|
||
if !v.Type().Implements(errorType) && !v.Type().Implements(fmtStringerType) { | ||
if v.CanAddr() && (reflect.PtrTo(v.Type()).Implements(errorType) || reflect.PtrTo(v.Type()).Implements(fmtStringerType)) { | ||
v = v.Addr() | ||
} else { | ||
switch v.Kind() { | ||
case reflect.Chan, reflect.Func: | ||
return nil, false | ||
} | ||
} | ||
} | ||
return v.Interface(), true | ||
} | ||
|
||
// indirect returns the item at the end of indirection, and a bool to indicate if it's nil. | ||
func indirect(v reflect.Value) (rv reflect.Value, isNil bool) { | ||
for ; v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface; v = v.Elem() { | ||
if v.IsNil() { | ||
return v, true | ||
} | ||
} | ||
return v, false | ||
} |
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