diff options
Diffstat (limited to 'libgo/go/text/template/exec.go')
-rw-r--r-- | libgo/go/text/template/exec.go | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/libgo/go/text/template/exec.go b/libgo/go/text/template/exec.go index 8ec8174a162..43b0b266eca 100644 --- a/libgo/go/text/template/exec.go +++ b/libgo/go/text/template/exec.go @@ -201,7 +201,7 @@ func (s *state) walkIfOrWith(typ parse.NodeType, dot reflect.Value, pipe *parse. } } -// isTrue returns whether the value is 'true', in the sense of not the zero of its type, +// isTrue reports whether the value is 'true', in the sense of not the zero of its type, // and whether the value has a meaningful truth value. func isTrue(val reflect.Value) (truth, ok bool) { if !val.IsValid() { @@ -755,12 +755,21 @@ func indirect(v reflect.Value) (rv reflect.Value, isNil bool) { // the template. func (s *state) printValue(n parse.Node, v reflect.Value) { s.at(n) + iface, ok := printableValue(v) + if !ok { + s.errorf("can't print %s of type %s", n, v.Type()) + } + fmt.Fprint(s.wr, iface) +} + +// 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() { - fmt.Fprint(s.wr, "<no value>") - return + return "<no value>", true } if !v.Type().Implements(errorType) && !v.Type().Implements(fmtStringerType) { @@ -769,11 +778,11 @@ func (s *state) printValue(n parse.Node, v reflect.Value) { } else { switch v.Kind() { case reflect.Chan, reflect.Func: - s.errorf("can't print %s of type %s", n, v.Type()) + return nil, false } } } - fmt.Fprint(s.wr, v.Interface()) + return v.Interface(), true } // Types to help sort the keys in a map for reproducible output. |