forked from GoComply/xsd2go
/
template.go
66 lines (55 loc) · 1.36 KB
/
template.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package template
import (
"bytes"
"fmt"
"go/format"
"io/ioutil"
"os"
"path/filepath"
"text/template"
"github.com/elliotcourant/xsd2go/pkg/xsd"
"github.com/markbates/pkger"
)
func GenerateTypes(schema *xsd.Schema, outputDir string) error {
t, err := newTemplate(outputDir)
if err != nil {
return err
}
packageName := schema.GoPackageName()
dir := filepath.Join(outputDir, packageName)
err = os.MkdirAll(dir, os.FileMode(0722))
if err != nil {
return err
}
goFile := filepath.Clean(filepath.Join(dir, "models.go"))
fmt.Printf("\tGenerating '%s'\n", goFile)
f, err := os.Create(goFile)
if err != nil {
return fmt.Errorf("Could not create '%s': %v", goFile, err)
}
var buf bytes.Buffer
if err := t.Execute(&buf, schema); err != nil {
return fmt.Errorf("Could not execute template: %v", err)
}
p, err := format.Source(buf.Bytes())
if err != nil {
return fmt.Errorf("Could not gofmt output file\nError was: '%v'\nFile was:\n%s\n", err, buf.String())
}
_, err = f.Write(p)
if err != nil {
return err
}
return nil
}
func newTemplate(outputDir string) (*template.Template, error) {
in, err := pkger.Open("/pkg/template/types.tmpl")
if err != nil {
return nil, err
}
defer in.Close()
tempText, err := ioutil.ReadAll(in)
if err != nil {
return nil, err
}
return template.New("types.tmpl").Funcs(template.FuncMap{}).Parse(string(tempText))
}