فهرست منبع

Added New() - shortcut to NewTemplate() if the given template has no errors

Aliaksandr Valialkin 10 سال پیش
والد
کامیت
c4c1cb9291
4فایلهای تغییر یافته به همراه35 افزوده شده و 68 حذف شده
  1. 1 4
      README.md
  2. 1 4
      example_test.go
  3. 16 0
      template.go
  4. 17 60
      template_test.go

+ 1 - 4
README.md

@@ -40,10 +40,7 @@ Usage
 
 ```go
 	template := "http://{{host}}/?q={{query}}&foo={{bar}}{{bar}}"
-	t, err := fasttemplate.NewTemplate(template, "{{", "}}")
-	if err != nil {
-		log.Fatalf("unexpected error when parsing template: %s", err)
-	}
+	t := fasttemplate.New(template, "{{", "}}")
 	s := t.ExecuteString(map[string]interface{}{
 		"host":  "google.com",
 		"query": url.QueryEscape("hello=world"),

+ 1 - 4
example_test.go

@@ -9,10 +9,7 @@ import (
 
 func ExampleTemplate() {
 	template := "http://{{host}}/?foo={{bar}}{{bar}}&q={{query}}&baz={{baz}}"
-	t, err := NewTemplate(template, "{{", "}}")
-	if err != nil {
-		log.Fatalf("unexpected error when parsing template: %s", err)
-	}
+	t := New(template, "{{", "}}")
 
 	// Substitution map.
 	// Since "baz" tag is missing in the map, it will be substituted

+ 16 - 0
template.go

@@ -21,6 +21,22 @@ type Template struct {
 	bytesBufferPool sync.Pool
 }
 
+// New parses the given template using the given startTag and endTag
+// as tag start and tag end.
+//
+// The returned template can be executed by concurrently running goroutines
+// using Execute* methods.
+//
+// New panics if the given template cannot be parsed. Use NewTemplate instead
+// if template may contain errors.
+func New(template, startTag, endTag string) *Template {
+	t, err := NewTemplate(template, startTag, endTag)
+	if err != nil {
+		panic(err)
+	}
+	return t
+}
+
 // NewTemplate parses the given template using the given startTag and endTag
 // as tag start and tag end.
 //

+ 17 - 60
template_test.go

@@ -6,10 +6,7 @@ import (
 )
 
 func TestEmptyTemplate(t *testing.T) {
-	tpl, err := NewTemplate("", "[", "]")
-	if err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
+	tpl := New("", "[", "]")
 
 	s := tpl.ExecuteString(map[string]interface{}{"foo": "bar", "aaa": "bbb"})
 	if s != "" {
@@ -27,10 +24,7 @@ func TestEmptyTagEnd(t *testing.T) {
 
 func TestNoTags(t *testing.T) {
 	template := "foobar"
-	tpl, err := NewTemplate(template, "[", "]")
-	if err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
+	tpl := New(template, "[", "]")
 
 	s := tpl.ExecuteString(map[string]interface{}{"foo": "bar", "aaa": "bbb"})
 	if s != template {
@@ -40,10 +34,7 @@ func TestNoTags(t *testing.T) {
 
 func TestEmptyTagName(t *testing.T) {
 	template := "foo[]bar"
-	tpl, err := NewTemplate(template, "[", "]")
-	if err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
+	tpl := New(template, "[", "]")
 
 	s := tpl.ExecuteString(map[string]interface{}{"": "111", "aaa": "bbb"})
 	result := "foo111bar"
@@ -54,10 +45,7 @@ func TestEmptyTagName(t *testing.T) {
 
 func TestOnlyTag(t *testing.T) {
 	template := "[foo]"
-	tpl, err := NewTemplate(template, "[", "]")
-	if err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
+	tpl := New(template, "[", "]")
 
 	s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
 	result := "111"
@@ -68,10 +56,7 @@ func TestOnlyTag(t *testing.T) {
 
 func TestStartWithTag(t *testing.T) {
 	template := "[foo]barbaz"
-	tpl, err := NewTemplate(template, "[", "]")
-	if err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
+	tpl := New(template, "[", "]")
 
 	s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
 	result := "111barbaz"
@@ -82,10 +67,7 @@ func TestStartWithTag(t *testing.T) {
 
 func TestEndWithTag(t *testing.T) {
 	template := "foobar[foo]"
-	tpl, err := NewTemplate(template, "[", "]")
-	if err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
+	tpl := New(template, "[", "]")
 
 	s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
 	result := "foobar111"
@@ -96,10 +78,7 @@ func TestEndWithTag(t *testing.T) {
 
 func TestDuplicateTags(t *testing.T) {
 	template := "[foo]bar[foo][foo]baz"
-	tpl, err := NewTemplate(template, "[", "]")
-	if err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
+	tpl := New(template, "[", "]")
 
 	s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
 	result := "111bar111111baz"
@@ -110,10 +89,7 @@ func TestDuplicateTags(t *testing.T) {
 
 func TestMultipleTags(t *testing.T) {
 	template := "foo[foo]aa[aaa]ccc"
-	tpl, err := NewTemplate(template, "[", "]")
-	if err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
+	tpl := New(template, "[", "]")
 
 	s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
 	result := "foo111aabbbccc"
@@ -124,10 +100,7 @@ func TestMultipleTags(t *testing.T) {
 
 func TestLongDelimiter(t *testing.T) {
 	template := "foo{{{foo}}}bar"
-	tpl, err := NewTemplate(template, "{{{", "}}}")
-	if err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
+	tpl := New(template, "{{{", "}}}")
 
 	s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
 	result := "foo111bar"
@@ -138,10 +111,7 @@ func TestLongDelimiter(t *testing.T) {
 
 func TestIdenticalDelimiter(t *testing.T) {
 	template := "foo@foo@foo@aaa@"
-	tpl, err := NewTemplate(template, "@", "@")
-	if err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
+	tpl := New(template, "@", "@")
 
 	s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
 	result := "foo111foobbb"
@@ -152,10 +122,7 @@ func TestIdenticalDelimiter(t *testing.T) {
 
 func TestDlimitersWithDistinctSize(t *testing.T) {
 	template := "foo<?phpaaa?>bar<?phpzzz?>"
-	tpl, err := NewTemplate(template, "<?php", "?>")
-	if err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
+	tpl := New(template, "<?php", "?>")
 
 	s := tpl.ExecuteString(map[string]interface{}{"zzz": "111", "aaa": "bbb"})
 	result := "foobbbbar111"
@@ -166,10 +133,7 @@ func TestDlimitersWithDistinctSize(t *testing.T) {
 
 func TestEmptyValue(t *testing.T) {
 	template := "foobar[foo]"
-	tpl, err := NewTemplate(template, "[", "]")
-	if err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
+	tpl := New(template, "[", "]")
 
 	s := tpl.ExecuteString(map[string]interface{}{"foo": "", "aaa": "bbb"})
 	result := "foobar"
@@ -180,10 +144,7 @@ func TestEmptyValue(t *testing.T) {
 
 func TestNoValue(t *testing.T) {
 	template := "foobar[foo]x[aaa]"
-	tpl, err := NewTemplate(template, "[", "]")
-	if err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
+	tpl := New(template, "[", "]")
 
 	s := tpl.ExecuteString(map[string]interface{}{"aaa": "bbb"})
 	result := "foobarxbbb"
@@ -198,14 +159,13 @@ func TestNoEndDelimiter(t *testing.T) {
 	if err == nil {
 		t.Fatalf("expected non-nil error. got nil")
 	}
+
+	expectPanic(t, func() { New(template, "[", "]") })
 }
 
 func TestUnsupportedValue(t *testing.T) {
 	template := "foobar[foo]"
-	tpl, err := NewTemplate(template, "[", "]")
-	if err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
+	tpl := New(template, "[", "]")
 
 	expectPanic(t, func() {
 		tpl.ExecuteString(map[string]interface{}{"foo": 123, "aaa": "bbb"})
@@ -214,10 +174,7 @@ func TestUnsupportedValue(t *testing.T) {
 
 func TestMixedValues(t *testing.T) {
 	template := "foo[foo]bar[bar]baz[baz]"
-	tpl, err := NewTemplate(template, "[", "]")
-	if err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
+	tpl := New(template, "[", "]")
 
 	s := tpl.ExecuteString(map[string]interface{}{
 		"foo": "111",