-
Notifications
You must be signed in to change notification settings - Fork 8
/
templates.go
86 lines (77 loc) · 2.21 KB
/
templates.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package cuetsy
import (
"strings"
"text/template"
)
// Generate a typescript type declaration. Inputs:
// .maturity Optional. Maturity level, applied in doc comment
// .export Whether to export the declaration
// .name The name of the typescript type
// .tokens Slice of token strings
var typeCode = tmpl("type", `
{{if .maturity -}}
/**
* {{.maturity}}
*/
{{end -}}
{{if .export}}export {{end -}}
type {{.name}} = {{ Join .tokens " | "}};
{{- if .default }}
{{if .export}}export {{end -}}
const default{{.name}}: {{.name}} = {{.default}};{{end}}`)
// Generate a typescript enum declaration. Inputs:
// .maturity Optional. Maturity level, applied in doc comment
// .export Whether to export the declaration
// .name The name of the typescript enum
// .pairs Slice of {K: string, V: string}
var enumCode = tmpl("enum", `
{{if .maturity -}}
/**
* {{.maturity}}
*/
{{end -}}
{{if .export}}export {{end -}}
enum {{.name}} {
{{- range .pairs}}
{{.K}} = {{.V}},{{end}}
}
{{- if .default }}
{{if .export}}export {{end -}}
const default{{.name}}: {{.name}} = {{.name}}.{{.default}};{{end}}`)
// Generate a typescript interface declaration. Inputs:
// .maturity Optional. Maturity level, applied in doc comment
// .name The name of the typescript enum.
// .pairs Slice of {K: string, V: string}
// .extends Slice of other interface names to extend
// .defaults Whether to generate a default const
var interfaceCode = tmpl("interface", `
{{if .maturity -}}
/**
* {{.maturity}}
*/
{{end -}}
{{if .export}}export {{end -}}
interface {{.name}}{{if ne (len .extends) 0}} extends {{ Join .extends ", "}}{{end}} {
{{- range .pairs}}
{{.K}}: {{.V}};{{end}}
}{{- if .defaults }}
{{if .export}}export {{end -}}
const default{{.name}}: {{.name}} = {
{{- range .pairs}}{{if .Default}}
{{StripQ .K}}: {{.Default}},{{end}}{{end}}
};{{end}}`)
var nestedStructCode = tmpl("nestedstruct", `{
{{- range .pairs}}
{{ range $.level}} {{end}} {{.K}}: {{.V}};{{end}}
{{ range $.level}} {{end}}}`)
func tmpl(name, data string) *template.Template {
t := template.New(name)
t.Funcs(template.FuncMap{
"Join": strings.Join,
"StripQ": func(s string) string {
return strings.TrimSuffix(s, "?")
},
})
t = template.Must(t.Parse(data))
return t
}