]> Cypherpunks repositories - gostls13.git/commitdiff
html/template, text/template: add ParseFS
authorRuss Cox <rsc@golang.org>
Mon, 6 Jul 2020 15:28:52 +0000 (11:28 -0400)
committerRuss Cox <rsc@golang.org>
Tue, 20 Oct 2020 18:41:14 +0000 (18:41 +0000)
Now templates can be parsed not just from operating system files
but from arbitrary file systems, including zip files.

For #41190.

Change-Id: I2172001388ddb1f13defa6c5e644e8ec8703ee80
Reviewed-on: https://go-review.googlesource.com/c/go/+/243938
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
src/html/template/multi_test.go
src/html/template/template.go
src/html/template/testdata/fs.zip [new file with mode: 0644]
src/text/template/helper.go
src/text/template/multi_test.go

index 50526c5b65ffeb81b87c5caa0c2003388f1a81c3..6535ab6c0439cfd9954b674d0733a088753347c4 100644 (file)
@@ -7,7 +7,9 @@
 package template
 
 import (
+       "archive/zip"
        "bytes"
+       "os"
        "testing"
        "text/template/parse"
 )
@@ -82,6 +84,35 @@ func TestParseGlob(t *testing.T) {
        testExecute(multiExecTests, template, t)
 }
 
+func TestParseFS(t *testing.T) {
+       fs := os.DirFS("testdata")
+
+       {
+               _, err := ParseFS(fs, "DOES NOT EXIST")
+               if err == nil {
+                       t.Error("expected error for non-existent file; got none")
+               }
+       }
+
+       {
+               template := New("root")
+               _, err := template.ParseFS(fs, "file1.tmpl", "file2.tmpl")
+               if err != nil {
+                       t.Fatalf("error parsing files: %v", err)
+               }
+               testExecute(multiExecTests, template, t)
+       }
+
+       {
+               template := New("root")
+               _, err := template.ParseFS(fs, "file*.tmpl")
+               if err != nil {
+                       t.Fatalf("error parsing files: %v", err)
+               }
+               testExecute(multiExecTests, template, t)
+       }
+}
+
 // In these tests, actual content (not just template definitions) comes from the parsed files.
 
 var templateFileExecTests = []execTest{
@@ -104,6 +135,18 @@ func TestParseGlobWithData(t *testing.T) {
        testExecute(templateFileExecTests, template, t)
 }
 
+func TestParseZipFS(t *testing.T) {
+       z, err := zip.OpenReader("testdata/fs.zip")
+       if err != nil {
+               t.Fatalf("error parsing zip: %v", err)
+       }
+       template, err := New("root").ParseFS(z, "tmpl*.tmpl")
+       if err != nil {
+               t.Fatalf("error parsing files: %v", err)
+       }
+       testExecute(templateFileExecTests, template, t)
+}
+
 const (
        cloneText1 = `{{define "a"}}{{template "b"}}{{template "c"}}{{end}}`
        cloneText2 = `{{define "b"}}b{{end}}`
index 75437879e2e6b26866fe25a572a431ad6b1d7eb7..bc960afe5f1dde9c78d0f1a27a91001feaed5006 100644 (file)
@@ -7,7 +7,9 @@ package template
 import (
        "fmt"
        "io"
+       "io/fs"
        "io/ioutil"
+       "path"
        "path/filepath"
        "sync"
        "text/template"
@@ -384,7 +386,7 @@ func Must(t *Template, err error) *Template {
 // For instance, ParseFiles("a/foo", "b/foo") stores "b/foo" as the template
 // named "foo", while "a/foo" is unavailable.
 func ParseFiles(filenames ...string) (*Template, error) {
-       return parseFiles(nil, filenames...)
+       return parseFiles(nil, readFileOS, filenames...)
 }
 
 // ParseFiles parses the named files and associates the resulting templates with
@@ -396,12 +398,12 @@ func ParseFiles(filenames ...string) (*Template, error) {
 //
 // ParseFiles returns an error if t or any associated template has already been executed.
 func (t *Template) ParseFiles(filenames ...string) (*Template, error) {
-       return parseFiles(t, filenames...)
+       return parseFiles(t, readFileOS, filenames...)
 }
 
 // parseFiles is the helper for the method and function. If the argument
 // template is nil, it is created from the first file.
-func parseFiles(t *Template, filenames ...string) (*Template, error) {
+func parseFiles(t *Template, readFile func(string) (string, []byte, error), filenames ...string) (*Template, error) {
        if err := t.checkCanParse(); err != nil {
                return nil, err
        }
@@ -411,12 +413,11 @@ func parseFiles(t *Template, filenames ...string) (*Template, error) {
                return nil, fmt.Errorf("html/template: no files named in call to ParseFiles")
        }
        for _, filename := range filenames {
-               b, err := ioutil.ReadFile(filename)
+               name, b, err := readFile(filename)
                if err != nil {
                        return nil, err
                }
                s := string(b)
-               name := filepath.Base(filename)
                // First template becomes return value if not already defined,
                // and we use that one for subsequent New calls to associate
                // all the templates together. Also, if this file has the same name
@@ -479,7 +480,7 @@ func parseGlob(t *Template, pattern string) (*Template, error) {
        if len(filenames) == 0 {
                return nil, fmt.Errorf("html/template: pattern matches no files: %#q", pattern)
        }
-       return parseFiles(t, filenames...)
+       return parseFiles(t, readFileOS, filenames...)
 }
 
 // IsTrue reports whether the value is 'true', in the sense of not the zero of its type,
@@ -488,3 +489,48 @@ func parseGlob(t *Template, pattern string) (*Template, error) {
 func IsTrue(val interface{}) (truth, ok bool) {
        return template.IsTrue(val)
 }
+
+// ParseFS is like ParseFiles or ParseGlob but reads from the file system fs
+// instead of the host operating system's file system.
+// It accepts a list of glob patterns.
+// (Note that most file names serve as glob patterns matching only themselves.)
+func ParseFS(fs fs.FS, patterns ...string) (*Template, error) {
+       return parseFS(nil, fs, patterns)
+}
+
+// ParseFS is like ParseFiles or ParseGlob but reads from the file system fs
+// instead of the host operating system's file system.
+// It accepts a list of glob patterns.
+// (Note that most file names serve as glob patterns matching only themselves.)
+func (t *Template) ParseFS(fs fs.FS, patterns ...string) (*Template, error) {
+       return parseFS(t, fs, patterns)
+}
+
+func parseFS(t *Template, fsys fs.FS, patterns []string) (*Template, error) {
+       var filenames []string
+       for _, pattern := range patterns {
+               list, err := fs.Glob(fsys, pattern)
+               if err != nil {
+                       return nil, err
+               }
+               if len(list) == 0 {
+                       return nil, fmt.Errorf("template: pattern matches no files: %#q", pattern)
+               }
+               filenames = append(filenames, list...)
+       }
+       return parseFiles(t, readFileFS(fsys), filenames...)
+}
+
+func readFileOS(file string) (name string, b []byte, err error) {
+       name = filepath.Base(file)
+       b, err = ioutil.ReadFile(file)
+       return
+}
+
+func readFileFS(fsys fs.FS) func(string) (string, []byte, error) {
+       return func(file string) (name string, b []byte, err error) {
+               name = path.Base(file)
+               b, err = fs.ReadFile(fsys, file)
+               return
+       }
+}
diff --git a/src/html/template/testdata/fs.zip b/src/html/template/testdata/fs.zip
new file mode 100644 (file)
index 0000000..8581313
Binary files /dev/null and b/src/html/template/testdata/fs.zip differ
index c9e890078c4ea03c002ae7b5449273e4da384685..8269fa28c50d3d0b9c94898969a49f0f3d15369c 100644 (file)
@@ -8,7 +8,9 @@ package template
 
 import (
        "fmt"
+       "io/fs"
        "io/ioutil"
+       "path"
        "path/filepath"
 )
 
@@ -35,7 +37,7 @@ func Must(t *Template, err error) *Template {
 // For instance, ParseFiles("a/foo", "b/foo") stores "b/foo" as the template
 // named "foo", while "a/foo" is unavailable.
 func ParseFiles(filenames ...string) (*Template, error) {
-       return parseFiles(nil, filenames...)
+       return parseFiles(nil, readFileOS, filenames...)
 }
 
 // ParseFiles parses the named files and associates the resulting templates with
@@ -51,23 +53,22 @@ func ParseFiles(filenames ...string) (*Template, error) {
 // the last one mentioned will be the one that results.
 func (t *Template) ParseFiles(filenames ...string) (*Template, error) {
        t.init()
-       return parseFiles(t, filenames...)
+       return parseFiles(t, readFileOS, filenames...)
 }
 
 // parseFiles is the helper for the method and function. If the argument
 // template is nil, it is created from the first file.
-func parseFiles(t *Template, filenames ...string) (*Template, error) {
+func parseFiles(t *Template, readFile func(string) (string, []byte, error), filenames ...string) (*Template, error) {
        if len(filenames) == 0 {
                // Not really a problem, but be consistent.
                return nil, fmt.Errorf("template: no files named in call to ParseFiles")
        }
        for _, filename := range filenames {
-               b, err := ioutil.ReadFile(filename)
+               name, b, err := readFile(filename)
                if err != nil {
                        return nil, err
                }
                s := string(b)
-               name := filepath.Base(filename)
                // First template becomes return value if not already defined,
                // and we use that one for subsequent New calls to associate
                // all the templates together. Also, if this file has the same name
@@ -126,5 +127,51 @@ func parseGlob(t *Template, pattern string) (*Template, error) {
        if len(filenames) == 0 {
                return nil, fmt.Errorf("template: pattern matches no files: %#q", pattern)
        }
-       return parseFiles(t, filenames...)
+       return parseFiles(t, readFileOS, filenames...)
+}
+
+// ParseFS is like ParseFiles or ParseGlob but reads from the file system fsys
+// instead of the host operating system's file system.
+// It accepts a list of glob patterns.
+// (Note that most file names serve as glob patterns matching only themselves.)
+func ParseFS(fsys fs.FS, patterns ...string) (*Template, error) {
+       return parseFS(nil, fsys, patterns)
+}
+
+// ParseFS is like ParseFiles or ParseGlob but reads from the file system fsys
+// instead of the host operating system's file system.
+// It accepts a list of glob patterns.
+// (Note that most file names serve as glob patterns matching only themselves.)
+func (t *Template) ParseFS(fsys fs.FS, patterns ...string) (*Template, error) {
+       t.init()
+       return parseFS(t, fsys, patterns)
+}
+
+func parseFS(t *Template, fsys fs.FS, patterns []string) (*Template, error) {
+       var filenames []string
+       for _, pattern := range patterns {
+               list, err := fs.Glob(fsys, pattern)
+               if err != nil {
+                       return nil, err
+               }
+               if len(list) == 0 {
+                       return nil, fmt.Errorf("template: pattern matches no files: %#q", pattern)
+               }
+               filenames = append(filenames, list...)
+       }
+       return parseFiles(t, readFileFS(fsys), filenames...)
+}
+
+func readFileOS(file string) (name string, b []byte, err error) {
+       name = filepath.Base(file)
+       b, err = ioutil.ReadFile(file)
+       return
+}
+
+func readFileFS(fsys fs.FS) func(string) (string, []byte, error) {
+       return func(file string) (name string, b []byte, err error) {
+               name = path.Base(file)
+               b, err = fs.ReadFile(fsys, file)
+               return
+       }
 }
index 34d2378e387b02fccc8239b52445e1a136a48f45..b543ab5c47c6d3f06c57b20ce5993f57f63df32f 100644 (file)
@@ -9,6 +9,7 @@ package template
 import (
        "bytes"
        "fmt"
+       "os"
        "testing"
        "text/template/parse"
 )
@@ -153,6 +154,35 @@ func TestParseGlob(t *testing.T) {
        testExecute(multiExecTests, template, t)
 }
 
+func TestParseFS(t *testing.T) {
+       fs := os.DirFS("testdata")
+
+       {
+               _, err := ParseFS(fs, "DOES NOT EXIST")
+               if err == nil {
+                       t.Error("expected error for non-existent file; got none")
+               }
+       }
+
+       {
+               template := New("root")
+               _, err := template.ParseFS(fs, "file1.tmpl", "file2.tmpl")
+               if err != nil {
+                       t.Fatalf("error parsing files: %v", err)
+               }
+               testExecute(multiExecTests, template, t)
+       }
+
+       {
+               template := New("root")
+               _, err := template.ParseFS(fs, "file*.tmpl")
+               if err != nil {
+                       t.Fatalf("error parsing files: %v", err)
+               }
+               testExecute(multiExecTests, template, t)
+       }
+}
+
 // In these tests, actual content (not just template definitions) comes from the parsed files.
 
 var templateFileExecTests = []execTest{