server: generate string literals instead of escaped strings

When compiling the default templates into the source code, use
string literals instead of escaped strings to reduce merge
conflicts.
This commit is contained in:
Eric Chiang 2016-09-05 18:06:17 -07:00
parent 058de90dde
commit 04912c04e3
3 changed files with 354 additions and 6 deletions

View File

@ -27,6 +27,10 @@ var requiredTmpls = []string{
// TemplateConfig describes. // TemplateConfig describes.
type TemplateConfig struct { type TemplateConfig struct {
// TODO(ericchiang): Asking for a directory with a set of templates doesn't indicate
// what the templates should look like and doesn't allow consumers of this package to
// provide their own templates in memory. In the future clean this up.
// Directory of the templates. If empty, these will be loaded from memory. // Directory of the templates. If empty, these will be loaded from memory.
Dir string `yaml:"dir"` Dir string `yaml:"dir"`

File diff suppressed because one or more lines are too long

View File

@ -32,12 +32,15 @@ func ignoreFile(p string) (ok bool, err error) {
return false, err return false, err
} }
// Maps aren't deterministic, use a struct instead.
type fileData struct { type fileData struct {
name string name string
data string data string
} }
func main() { func main() {
// ReadDir guarentees result in sorted order.
dir, err := ioutil.ReadDir("web/templates") dir, err := ioutil.ReadDir("web/templates")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -57,6 +60,9 @@ func main() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if bytes.Contains(data, []byte{'`'}) {
log.Fatalf("file %s contains escape character '`' and cannot be compiled into go source", p)
}
files = append(files, fileData{file.Name(), string(data)}) files = append(files, fileData{file.Name(), string(data)})
} }
@ -69,7 +75,7 @@ func main() {
fmt.Fprintln(f, "// defaultTemplates is a key for file name to file data of the files in web/templates.") fmt.Fprintln(f, "// defaultTemplates is a key for file name to file data of the files in web/templates.")
fmt.Fprintln(f, "var defaultTemplates = map[string]string{") fmt.Fprintln(f, "var defaultTemplates = map[string]string{")
for _, file := range files { for _, file := range files {
fmt.Fprintf(f, "\t%q: %q,\n", file.name, file.data) fmt.Fprintf(f, "\t%q: `%s`,\n", file.name, file.data)
} }
fmt.Fprintln(f, "}") fmt.Fprintln(f, "}")