-
Notifications
You must be signed in to change notification settings - Fork 36
/
printf.go
44 lines (38 loc) · 904 Bytes
/
printf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package templatesystem
import (
"fmt"
"github.com/ungerik/go-start/errs"
"io"
"io/ioutil"
"path/filepath"
"reflect"
"strings"
)
type PrintfTemplate struct {
text string
}
func (self *PrintfTemplate) Render(out io.Writer, context interface{}) (err error) {
var str string
switch reflect.TypeOf(context).Kind() {
case reflect.Slice, reflect.Array:
panic("todo implementation")
default:
str = fmt.Sprintf(self.text, context)
}
if strings.HasPrefix(str, "%!") {
return errs.Format(str)
}
_, err = out.Write([]byte(str))
return err
}
type Printf struct{}
func (self *Printf) ParseFile(filename string) (Template, error) {
text, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return self.ParseString(string(text), filepath.Base(filename))
}
func (self *Printf) ParseString(text, name string) (Template, error) {
return &PrintfTemplate{text: text}, nil
}