/
pointer.go
65 lines (52 loc) · 1.65 KB
/
pointer.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
package main
import (
"flag"
"io"
"text/template"
"unicode"
"github.com/micvbang/go-helpy/gen"
"github.com/micvbang/go-helpy/internal/stringsy"
)
type templateData struct {
Type string
TypeName string
PackageName string
Import string
}
func main() {
var d templateData
flag.StringVar(&d.Type, "type", "", "Name of the type")
flag.StringVar(&d.TypeName, "type-name", "", "User friendly name of the type")
flag.StringVar(&d.PackageName, "package-name", "", "Name of the package to place the generated file in")
flag.StringVar(&d.Import, "import", "", "Name of package to import")
flag.Parse()
funcMap := template.FuncMap{
"Title": func(s string) string {
return stringsy.TitleCasing(s, unicode.ToUpper)
},
}
t := template.Must(template.New("pointer").Funcs(funcMap).Parse(pointerTemplate))
gen.GenerateToPackage(d.PackageName, "gen_pointer.go", func(w io.Writer) error {
return t.Execute(w, d)
})
}
const pointerTemplate = `
package {{.PackageName}}
import (
"github.com/micvbang/go-helpy"
{{if .Import}}
"{{.Import}}"
{{end}}
)
// Code generated. DO NOT EDIT.
// Pointer returns a pointer to the given {{.Type}}.
// NOTE: this method is deprecated. Use helpy.Pointer instead.
func Pointer(v {{.Type}}) *{{.Type}} {
return helpy.Pointer(v)
}
// {{if .TypeName}}{{.TypeName}}{{else}}{{.Type | Title}}{{end}}OrDefault returns {{.Type}} if it is not nil, and a pointer to defaultVal otherwise.
// NOTE: this method is deprecated. Use helpy.DerefOrValue instead.
func {{if .TypeName}}{{.TypeName}}{{else}}{{.Type | Title}}{{end}}OrDefault(v *{{.Type}}, defaultVal {{.Type}}) *{{.Type}} {
return helpy.DerefOrValue(v, defaultVal)
}
`