]> Cypherpunks repositories - gostls13.git/commitdiff
template: two bug fixes / nits
authorRuss Cox <rsc@golang.org>
Mon, 30 Nov 2009 18:29:14 +0000 (10:29 -0800)
committerRuss Cox <rsc@golang.org>
Mon, 30 Nov 2009 18:29:14 +0000 (10:29 -0800)
  * diagnose template not created with New
    (current code just crashes)
  * write []byte uninterpreted
    (current code writes fmt format: "[65 65 65 65]")

R=r
CC=golang-dev
https://golang.org/cl/161075

src/pkg/template/format.go
src/pkg/template/template.go
src/pkg/template/template_test.go

index e79469e6f18f706a7a31f0590184672713048339..f357ab2a1b44ee23a062174e02a2d5f2d150d0fc 100644 (file)
@@ -18,7 +18,11 @@ import (
 // You can override the default formatter by storing your default
 // under the name "" in your custom formatter map.
 func StringFormatter(w io.Writer, value interface{}, format string) {
-       fmt.Fprint(w, value)
+       if b, ok := value.([]byte); ok {
+               w.Write(b);
+               return;
+       }
+       fmt.Fprint(w, value);
 }
 
 var (
index 0a713de52cfebd5f5ce1429d2e7c58b69d45582d..9bba532c623e2db4e58895234ae3f9bfdd9369e4 100644 (file)
@@ -847,6 +847,9 @@ func validDelim(d []byte) bool {
 // s contains the template text.  If any errors occur, Parse returns
 // the error.
 func (t *Template) Parse(s string) os.Error {
+       if t.elems == nil {
+               return &Error{1, "template not allocated with New"}
+       }
        if !validDelim(t.ldelim) || !validDelim(t.rdelim) {
                return &Error{1, fmt.Sprintf("bad delimiter strings %q %q", t.ldelim, t.rdelim)}
        }
index 7384da9e5b8647e842e4bcb8dd4f47cde227544f..379f0f34250041a87ecd359ef4884efb4869e183 100644 (file)
@@ -9,6 +9,7 @@ import (
        "container/vector";
        "fmt";
        "io";
+       "strings";
        "testing";
 )
 
@@ -41,6 +42,7 @@ type S struct {
        false           bool;
        mp              map[string]string;
        innermap        U;
+       bytes           []byte;
 }
 
 var t1 = T{"ItemNumber1", "ValueNumber1"}
@@ -282,6 +284,12 @@ var tests = []*Test{
                out: "1\n4\n",
        },
 
+       &Test{
+               in: "{bytes}",
+
+               out: "hello",
+       },
+
        // Maps
 
        &Test{
@@ -317,6 +325,7 @@ func TestAll(t *testing.T) {
        s.mp["mapkey"] = "Ahoy!";
        s.innermap.mp = make(map[string]int);
        s.innermap.mp["innerkey"] = 55;
+       s.bytes = strings.Bytes("hello");
 
        var buf bytes.Buffer;
        for _, test := range tests {