mirror of
https://github.com/golang/go
synced 2024-11-22 04:44:39 -07:00
exp/template: rename Parse*File and Parse*Files for clarity
IMPORTANT: Previous usage of *Files will continue to compile fine but misbehave since the interface is compatible. The following functions have been renamed: ParseFiles => ParseGlob ParseFile => ParseFiles ParseSetFiles => ParseSetGlob ParseSetFile => ParseSetFiles ParseTemplateFiles => ParseTemplateGlob ParseTemplateFile => ParseTemplateFiles R=golang-dev, r CC=golang-dev https://golang.org/cl/4867041
This commit is contained in:
parent
c8ee9c60d2
commit
f3625e7087
@ -70,7 +70,7 @@ func (t *Template) parseFileInSet(filename string, set *Set) (*Template, os.Erro
|
|||||||
// SetMust is a helper that wraps a call to a function returning (*Set, os.Error)
|
// SetMust is a helper that wraps a call to a function returning (*Set, os.Error)
|
||||||
// and panics if the error is non-nil. It is intended for use in variable initializations
|
// and panics if the error is non-nil. It is intended for use in variable initializations
|
||||||
// such as
|
// such as
|
||||||
// var s = template.SetMust(template.ParseSetFile("file"))
|
// var s = template.SetMust(template.ParseSetFiles("file"))
|
||||||
func SetMust(s *Set, err os.Error) *Set {
|
func SetMust(s *Set, err os.Error) *Set {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -78,10 +78,10 @@ func SetMust(s *Set, err os.Error) *Set {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseFile parses the named files into a set of named templates.
|
// ParseFiles parses the named files into a set of named templates.
|
||||||
// Each file must be parseable by itself.
|
// Each file must be parseable by itself.
|
||||||
// If an error occurs, parsing stops and the returned set is nil.
|
// If an error occurs, parsing stops and the returned set is nil.
|
||||||
func (s *Set) ParseFile(filenames ...string) (*Set, os.Error) {
|
func (s *Set) ParseFiles(filenames ...string) (*Set, os.Error) {
|
||||||
for _, filename := range filenames {
|
for _, filename := range filenames {
|
||||||
b, err := ioutil.ReadFile(filename)
|
b, err := ioutil.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -95,9 +95,9 @@ func (s *Set) ParseFile(filenames ...string) (*Set, os.Error) {
|
|||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseSetFile creates a new Set and parses the set definition from the
|
// ParseSetFiles creates a new Set and parses the set definition from the
|
||||||
// named files. Each file must be individually parseable.
|
// named files. Each file must be individually parseable.
|
||||||
func ParseSetFile(filenames ...string) (*Set, os.Error) {
|
func ParseSetFiles(filenames ...string) (*Set, os.Error) {
|
||||||
s := new(Set)
|
s := new(Set)
|
||||||
for _, filename := range filenames {
|
for _, filename := range filenames {
|
||||||
b, err := ioutil.ReadFile(filename)
|
b, err := ioutil.ReadFile(filename)
|
||||||
@ -112,11 +112,11 @@ func ParseSetFile(filenames ...string) (*Set, os.Error) {
|
|||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseFiles parses the set definition from the files identified by the
|
// ParseGlob parses the set definition from the files identified by the
|
||||||
// pattern. The pattern is processed by filepath.Glob and must match at
|
// pattern. The pattern is processed by filepath.Glob and must match at
|
||||||
// least one file.
|
// least one file.
|
||||||
// If an error occurs, parsing stops and the returned set is nil.
|
// If an error occurs, parsing stops and the returned set is nil.
|
||||||
func (s *Set) ParseFiles(pattern string) (*Set, os.Error) {
|
func (s *Set) ParseGlob(pattern string) (*Set, os.Error) {
|
||||||
filenames, err := filepath.Glob(pattern)
|
filenames, err := filepath.Glob(pattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -124,14 +124,14 @@ func (s *Set) ParseFiles(pattern string) (*Set, os.Error) {
|
|||||||
if len(filenames) == 0 {
|
if len(filenames) == 0 {
|
||||||
return nil, fmt.Errorf("pattern matches no files: %#q", pattern)
|
return nil, fmt.Errorf("pattern matches no files: %#q", pattern)
|
||||||
}
|
}
|
||||||
return s.ParseFile(filenames...)
|
return s.ParseFiles(filenames...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseSetFiles creates a new Set and parses the set definition from the
|
// ParseSetGlob creates a new Set and parses the set definition from the
|
||||||
// files identified by the pattern. The pattern is processed by filepath.Glob
|
// files identified by the pattern. The pattern is processed by filepath.Glob
|
||||||
// and must match at least one file.
|
// and must match at least one file.
|
||||||
func ParseSetFiles(pattern string) (*Set, os.Error) {
|
func ParseSetGlob(pattern string) (*Set, os.Error) {
|
||||||
set, err := new(Set).ParseFiles(pattern)
|
set, err := new(Set).ParseGlob(pattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -140,17 +140,17 @@ func ParseSetFiles(pattern string) (*Set, os.Error) {
|
|||||||
|
|
||||||
// Functions and methods to parse stand-alone template files into a set.
|
// Functions and methods to parse stand-alone template files into a set.
|
||||||
|
|
||||||
// ParseTemplateFile parses the named template files and adds
|
// ParseTemplateFiles parses the named template files and adds
|
||||||
// them to the set. Each template will named the base name of
|
// them to the set. Each template will be named the base name of
|
||||||
// its file.
|
// its file.
|
||||||
// Unlike with ParseFile, each file should be a stand-alone template
|
// Unlike with ParseFiles, each file should be a stand-alone template
|
||||||
// definition suitable for Template.Parse (not Set.Parse); that is, the
|
// definition suitable for Template.Parse (not Set.Parse); that is, the
|
||||||
// file does not contain {{define}} clauses. ParseTemplateFile is
|
// file does not contain {{define}} clauses. ParseTemplateFiles is
|
||||||
// therefore equivalent to calling the ParseFile function to create
|
// therefore equivalent to calling the ParseFile function to create
|
||||||
// individual templates, which are then added to the set.
|
// individual templates, which are then added to the set.
|
||||||
// Each file must be parseable by itself.
|
// Each file must be parseable by itself.
|
||||||
// If an error occurs, parsing stops and the returned set is nil.
|
// If an error occurs, parsing stops and the returned set is nil.
|
||||||
func (s *Set) ParseTemplateFile(filenames ...string) (*Set, os.Error) {
|
func (s *Set) ParseTemplateFiles(filenames ...string) (*Set, os.Error) {
|
||||||
for _, filename := range filenames {
|
for _, filename := range filenames {
|
||||||
_, err := parseFileInSet(filename, s)
|
_, err := parseFileInSet(filename, s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -160,17 +160,17 @@ func (s *Set) ParseTemplateFile(filenames ...string) (*Set, os.Error) {
|
|||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseTemplateFiles parses the template files matched by the
|
// ParseTemplateGlob parses the template files matched by the
|
||||||
// patern and adds them to the set. Each template will named
|
// patern and adds them to the set. Each template will be named
|
||||||
// the base name of its file.
|
// the base name of its file.
|
||||||
// Unlike with ParseFiles, each file should be a stand-alone template
|
// Unlike with ParseGlob, each file should be a stand-alone template
|
||||||
// definition suitable for Template.Parse (not Set.Parse); that is, the
|
// definition suitable for Template.Parse (not Set.Parse); that is, the
|
||||||
// file does not contain {{define}} clauses. ParseTemplateFiles is
|
// file does not contain {{define}} clauses. ParseTemplateGlob is
|
||||||
// therefore equivalent to calling the ParseFile function to create
|
// therefore equivalent to calling the ParseFile function to create
|
||||||
// individual templates, which are then added to the set.
|
// individual templates, which are then added to the set.
|
||||||
// Each file must be parseable by itself.
|
// Each file must be parseable by itself.
|
||||||
// If an error occurs, parsing stops and the returned set is nil.
|
// If an error occurs, parsing stops and the returned set is nil.
|
||||||
func (s *Set) ParseTemplateFiles(pattern string) (*Set, os.Error) {
|
func (s *Set) ParseTemplateGlob(pattern string) (*Set, os.Error) {
|
||||||
filenames, err := filepath.Glob(pattern)
|
filenames, err := filepath.Glob(pattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -184,17 +184,17 @@ func (s *Set) ParseTemplateFiles(pattern string) (*Set, os.Error) {
|
|||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseTemplateFile creates a set by parsing the named files,
|
// ParseTemplateFiles creates a set by parsing the named files,
|
||||||
// each of which defines a single template. Each template will
|
// each of which defines a single template. Each template will be
|
||||||
// named the base name of its file.
|
// named the base name of its file.
|
||||||
// Unlike with ParseFile, each file should be a stand-alone template
|
// Unlike with ParseFiles, each file should be a stand-alone template
|
||||||
// definition suitable for Template.Parse (not Set.Parse); that is, the
|
// definition suitable for Template.Parse (not Set.Parse); that is, the
|
||||||
// file does not contain {{define}} clauses. ParseTemplateFile is
|
// file does not contain {{define}} clauses. ParseTemplateFiles is
|
||||||
// therefore equivalent to calling the ParseFile function to create
|
// therefore equivalent to calling the ParseFile function to create
|
||||||
// individual templates, which are then added to the set.
|
// individual templates, which are then added to the set.
|
||||||
// Each file must be parseable by itself. Parsing stops if an error is
|
// Each file must be parseable by itself. Parsing stops if an error is
|
||||||
// encountered.
|
// encountered.
|
||||||
func ParseTemplateFile(filenames ...string) (*Set, os.Error) {
|
func ParseTemplateFiles(filenames ...string) (*Set, os.Error) {
|
||||||
set := new(Set)
|
set := new(Set)
|
||||||
set.init()
|
set.init()
|
||||||
for _, filename := range filenames {
|
for _, filename := range filenames {
|
||||||
@ -209,17 +209,17 @@ func ParseTemplateFile(filenames ...string) (*Set, os.Error) {
|
|||||||
return set, nil
|
return set, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseTemplateFiles creates a set by parsing the files matched
|
// ParseTemplateGlob creates a set by parsing the files matched
|
||||||
// by the pattern, each of which defines a single template. Each
|
// by the pattern, each of which defines a single template. Each
|
||||||
// template will named the base name of its file.
|
// template will be named the base name of its file.
|
||||||
// Unlike with ParseFiles, each file should be a stand-alone template
|
// Unlike with ParseGlob, each file should be a stand-alone template
|
||||||
// definition suitable for Template.Parse (not Set.Parse); that is, the
|
// definition suitable for Template.Parse (not Set.Parse); that is, the
|
||||||
// file does not contain {{define}} clauses. ParseTemplateFiles is
|
// file does not contain {{define}} clauses. ParseTemplateGlob is
|
||||||
// therefore equivalent to calling the ParseFile function to create
|
// therefore equivalent to calling the ParseFile function to create
|
||||||
// individual templates, which are then added to the set.
|
// individual templates, which are then added to the set.
|
||||||
// Each file must be parseable by itself. Parsing stops if an error is
|
// Each file must be parseable by itself. Parsing stops if an error is
|
||||||
// encountered.
|
// encountered.
|
||||||
func ParseTemplateFiles(pattern string) (*Set, os.Error) {
|
func ParseTemplateGlob(pattern string) (*Set, os.Error) {
|
||||||
set := new(Set)
|
set := new(Set)
|
||||||
filenames, err := filepath.Glob(pattern)
|
filenames, err := filepath.Glob(pattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -120,42 +120,13 @@ func TestSetExecute(t *testing.T) {
|
|||||||
testExecute(setExecTests, set, t)
|
testExecute(setExecTests, set, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSetParseFile(t *testing.T) {
|
|
||||||
set := new(Set)
|
|
||||||
_, err := set.ParseFile("DOES NOT EXIST")
|
|
||||||
if err == nil {
|
|
||||||
t.Error("expected error for non-existent file; got none")
|
|
||||||
}
|
|
||||||
_, err = set.ParseFile("testdata/file1.tmpl", "testdata/file2.tmpl")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("error parsing files: %v", err)
|
|
||||||
}
|
|
||||||
testExecute(setExecTests, set, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseSetFile(t *testing.T) {
|
|
||||||
set := new(Set)
|
|
||||||
_, err := ParseSetFile("DOES NOT EXIST")
|
|
||||||
if err == nil {
|
|
||||||
t.Error("expected error for non-existent file; got none")
|
|
||||||
}
|
|
||||||
set, err = ParseSetFile("testdata/file1.tmpl", "testdata/file2.tmpl")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("error parsing files: %v", err)
|
|
||||||
}
|
|
||||||
testExecute(setExecTests, set, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSetParseFiles(t *testing.T) {
|
func TestSetParseFiles(t *testing.T) {
|
||||||
_, err := new(Set).ParseFiles("DOES NOT EXIST")
|
set := new(Set)
|
||||||
|
_, err := set.ParseFiles("DOES NOT EXIST")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("expected error for non-existent file; got none")
|
t.Error("expected error for non-existent file; got none")
|
||||||
}
|
}
|
||||||
_, err = new(Set).ParseFiles("[x")
|
_, err = set.ParseFiles("testdata/file1.tmpl", "testdata/file2.tmpl")
|
||||||
if err == nil {
|
|
||||||
t.Error("expected error for bad pattern; got none")
|
|
||||||
}
|
|
||||||
set, err := new(Set).ParseFiles("testdata/file*.tmpl")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("error parsing files: %v", err)
|
t.Fatalf("error parsing files: %v", err)
|
||||||
}
|
}
|
||||||
@ -163,15 +134,44 @@ func TestSetParseFiles(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestParseSetFiles(t *testing.T) {
|
func TestParseSetFiles(t *testing.T) {
|
||||||
|
set := new(Set)
|
||||||
_, err := ParseSetFiles("DOES NOT EXIST")
|
_, err := ParseSetFiles("DOES NOT EXIST")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("expected error for non-existent file; got none")
|
t.Error("expected error for non-existent file; got none")
|
||||||
}
|
}
|
||||||
_, err = ParseSetFiles("[x")
|
set, err = ParseSetFiles("testdata/file1.tmpl", "testdata/file2.tmpl")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error parsing files: %v", err)
|
||||||
|
}
|
||||||
|
testExecute(setExecTests, set, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSetParseGlob(t *testing.T) {
|
||||||
|
_, err := new(Set).ParseGlob("DOES NOT EXIST")
|
||||||
|
if err == nil {
|
||||||
|
t.Error("expected error for non-existent file; got none")
|
||||||
|
}
|
||||||
|
_, err = new(Set).ParseGlob("[x")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("expected error for bad pattern; got none")
|
t.Error("expected error for bad pattern; got none")
|
||||||
}
|
}
|
||||||
set, err := ParseSetFiles("testdata/file*.tmpl")
|
set, err := new(Set).ParseGlob("testdata/file*.tmpl")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error parsing files: %v", err)
|
||||||
|
}
|
||||||
|
testExecute(setExecTests, set, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseSetGlob(t *testing.T) {
|
||||||
|
_, err := ParseSetGlob("DOES NOT EXIST")
|
||||||
|
if err == nil {
|
||||||
|
t.Error("expected error for non-existent file; got none")
|
||||||
|
}
|
||||||
|
_, err = ParseSetGlob("[x")
|
||||||
|
if err == nil {
|
||||||
|
t.Error("expected error for bad pattern; got none")
|
||||||
|
}
|
||||||
|
set, err := ParseSetGlob("testdata/file*.tmpl")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("error parsing files: %v", err)
|
t.Fatalf("error parsing files: %v", err)
|
||||||
}
|
}
|
||||||
@ -182,40 +182,12 @@ var templateFileExecTests = []execTest{
|
|||||||
{"test", `{{template "tmpl1.tmpl"}}{{template "tmpl2.tmpl"}}`, "template1\ntemplate2\n", 0, true},
|
{"test", `{{template "tmpl1.tmpl"}}{{template "tmpl2.tmpl"}}`, "template1\ntemplate2\n", 0, true},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSetParseTemplateFile(t *testing.T) {
|
|
||||||
_, err := ParseTemplateFile("DOES NOT EXIST")
|
|
||||||
if err == nil {
|
|
||||||
t.Error("expected error for non-existent file; got none")
|
|
||||||
}
|
|
||||||
set, err := new(Set).ParseTemplateFile("testdata/tmpl1.tmpl", "testdata/tmpl2.tmpl")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("error parsing files: %v", err)
|
|
||||||
}
|
|
||||||
testExecute(templateFileExecTests, set, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseTemplateFile(t *testing.T) {
|
|
||||||
_, err := ParseTemplateFile("DOES NOT EXIST")
|
|
||||||
if err == nil {
|
|
||||||
t.Error("expected error for non-existent file; got none")
|
|
||||||
}
|
|
||||||
set, err := new(Set).ParseTemplateFile("testdata/tmpl1.tmpl", "testdata/tmpl2.tmpl")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("error parsing files: %v", err)
|
|
||||||
}
|
|
||||||
testExecute(templateFileExecTests, set, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSetParseTemplateFiles(t *testing.T) {
|
func TestSetParseTemplateFiles(t *testing.T) {
|
||||||
_, err := ParseTemplateFiles("DOES NOT EXIST")
|
_, err := ParseTemplateFiles("DOES NOT EXIST")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("expected error for non-existent file; got none")
|
t.Error("expected error for non-existent file; got none")
|
||||||
}
|
}
|
||||||
_, err = new(Set).ParseTemplateFiles("[x")
|
set, err := new(Set).ParseTemplateFiles("testdata/tmpl1.tmpl", "testdata/tmpl2.tmpl")
|
||||||
if err == nil {
|
|
||||||
t.Error("expected error for bad pattern; got none")
|
|
||||||
}
|
|
||||||
set, err := new(Set).ParseTemplateFiles("testdata/tmpl*.tmpl")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("error parsing files: %v", err)
|
t.Fatalf("error parsing files: %v", err)
|
||||||
}
|
}
|
||||||
@ -227,11 +199,39 @@ func TestParseTemplateFiles(t *testing.T) {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("expected error for non-existent file; got none")
|
t.Error("expected error for non-existent file; got none")
|
||||||
}
|
}
|
||||||
_, err = ParseTemplateFiles("[x")
|
set, err := new(Set).ParseTemplateFiles("testdata/tmpl1.tmpl", "testdata/tmpl2.tmpl")
|
||||||
if err == nil {
|
if err != nil {
|
||||||
t.Error("expected error for bad pattern; got none")
|
t.Fatalf("error parsing files: %v", err)
|
||||||
}
|
}
|
||||||
set, err := ParseTemplateFiles("testdata/tmpl*.tmpl")
|
testExecute(templateFileExecTests, set, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSetParseTemplateGlob(t *testing.T) {
|
||||||
|
_, err := ParseTemplateGlob("DOES NOT EXIST")
|
||||||
|
if err == nil {
|
||||||
|
t.Error("expected error for non-existent file; got none")
|
||||||
|
}
|
||||||
|
_, err = new(Set).ParseTemplateGlob("[x")
|
||||||
|
if err == nil {
|
||||||
|
t.Error("expected error for bad pattern; got none")
|
||||||
|
}
|
||||||
|
set, err := new(Set).ParseTemplateGlob("testdata/tmpl*.tmpl")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error parsing files: %v", err)
|
||||||
|
}
|
||||||
|
testExecute(templateFileExecTests, set, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseTemplateGlob(t *testing.T) {
|
||||||
|
_, err := ParseTemplateGlob("DOES NOT EXIST")
|
||||||
|
if err == nil {
|
||||||
|
t.Error("expected error for non-existent file; got none")
|
||||||
|
}
|
||||||
|
_, err = ParseTemplateGlob("[x")
|
||||||
|
if err == nil {
|
||||||
|
t.Error("expected error for bad pattern; got none")
|
||||||
|
}
|
||||||
|
set, err := ParseTemplateGlob("testdata/tmpl*.tmpl")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("error parsing files: %v", err)
|
t.Fatalf("error parsing files: %v", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user