-
Notifications
You must be signed in to change notification settings - Fork 35
/
fieldTemplate.go
71 lines (60 loc) · 1.71 KB
/
fieldTemplate.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
67
68
69
70
71
package main
import (
"bytes"
//"fmt"
"log"
//"strconv"
"text/template"
)
type FieldDef struct {
XMLName string
XMLNameSpace string
GoName string
GoType string
GoTypeArrayOrPointer string
Length int64
//Foo bool
}
var fieldTemplate *template.Template
var fieldTemplateString = `{{.GoName}} {{.GoTypeArrayOrPointer}}{{.GoType}} ` + "`" + `xml:"{{if notEmpty .XMLNameSpace}}{{.XMLNameSpace}} {{end}}{{.XMLName}},omitempty" json:"{{.XMLName}},omitempty"` + "`" + ``
func render(otd FieldDef) (string, error) {
var err error
// fieldTemplate = template.Must(template.New("fieldTemplate").Funcs(template.FuncMap{
fieldTemplate, err = template.New("fieldTemplate").Funcs(template.FuncMap{
"notEmpty": func(feature string) bool {
return len(feature) > 0
},
}).Parse(fieldTemplateString)
if err != nil {
return "", err
}
var buf bytes.Buffer
//err := t.Execute(os.Stdout, ot)
err = fieldTemplate.Execute(&buf, otd)
if err != nil {
return "", err
}
//fmt.Println(buf.String())
//return "\t" + buf.String() + " // ZZmaxLength=" + strconv.FormatInt(otd.Length, 10), nil
return "\t" + buf.String(), nil
}
func runValidateFieldTemplate(printToLog bool) error {
fieldDef := FieldDef{
XMLName: "xmlName",
XMLNameSpace: "xmlNameSpace",
GoName: "Foobar",
GoType: "string",
GoTypeArrayOrPointer: "[]",
Length: 32,
}
string, err := render(fieldDef)
if printToLog {
log.Println("validateFieldTemplate")
log.Println("Using template:", fieldTemplateString)
log.Println(string)
}
if err != nil {
log.Println("Error with field template:", err)
}
return err
}