-
Notifications
You must be signed in to change notification settings - Fork 654
/
generate.go
127 lines (105 loc) · 3.24 KB
/
generate.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//go:build ignore
// +build ignore
package main
import (
"fmt"
"log"
"os"
"text/template"
"github.com/aws/smithy-go/ptr"
)
func main() {
types := ptr.GetScalars()
for filename, tmplName := range map[string]string{
"to_ptr.go": "scalar to pointer",
"from_ptr.go": "scalar from pointer",
} {
if err := generateFile(filename, tmplName, types); err != nil {
log.Fatalf("%s file generation failed, %v", filename, err)
}
}
}
func generateFile(filename string, tmplName string, types ptr.Scalars) (err error) {
f, err := os.Create(filename)
if err != nil {
return fmt.Errorf("failed to create %s file, %v", filename, err)
}
defer func() {
closeErr := f.Close()
if err == nil {
err = closeErr
} else if closeErr != nil {
err = fmt.Errorf("close error: %v, original error: %w", closeErr, err)
}
}()
if err = ptrTmpl.ExecuteTemplate(f, tmplName, types); err != nil {
return fmt.Errorf("failed to generate %s file, %v", filename, err)
}
return nil
}
var ptrTmpl = template.Must(template.New("ptrTmpl").Parse(`
{{- define "header" }}
// Code generated by aws/generate.go DO NOT EDIT.
package aws
import (
"github.com/aws/smithy-go/ptr"
{{- range $_, $import := $.Imports }}
"{{ $import.Path }}"
{{- end }}
)
{{- end }}
{{- define "scalar from pointer" }}
{{ template "header" $ }}
{{ range $_, $type := $ }}
{{ template "from pointer func" $type }}
{{ template "from pointers func" $type }}
{{- end }}
{{- end }}
{{- define "scalar to pointer" }}
{{ template "header" $ }}
{{ range $_, $type := $ }}
{{ template "to pointer func" $type }}
{{ template "to pointers func" $type }}
{{- end }}
{{- end }}
{{- define "to pointer func" }}
// {{ $.Name }} returns a pointer value for the {{ $.Symbol }} value passed in.
func {{ $.Name }}(v {{ $.Symbol }}) *{{ $.Symbol }} {
return ptr.{{ $.Name }}(v)
}
{{- end }}
{{- define "to pointers func" }}
// {{ $.Name }}Slice returns a slice of {{ $.Symbol }} pointers from the values
// passed in.
func {{ $.Name }}Slice(vs []{{ $.Symbol }}) []*{{ $.Symbol }} {
return ptr.{{ $.Name }}Slice(vs)
}
// {{ $.Name }}Map returns a map of {{ $.Symbol }} pointers from the values
// passed in.
func {{ $.Name }}Map(vs map[string]{{ $.Symbol }}) map[string]*{{ $.Symbol }} {
return ptr.{{ $.Name }}Map(vs)
}
{{- end }}
{{- define "from pointer func" }}
// To{{ $.Name }} returns {{ $.Symbol }} value dereferenced if the passed
// in pointer was not nil. Returns a {{ $.Symbol }} zero value if the
// pointer was nil.
func To{{ $.Name }}(p *{{ $.Symbol }}) (v {{ $.Symbol }}) {
return ptr.To{{ $.Name }}(p)
}
{{- end }}
{{- define "from pointers func" }}
// To{{ $.Name }}Slice returns a slice of {{ $.Symbol }} values, that are
// dereferenced if the passed in pointer was not nil. Returns a {{ $.Symbol }}
// zero value if the pointer was nil.
func To{{ $.Name }}Slice(vs []*{{ $.Symbol }}) []{{ $.Symbol }} {
return ptr.To{{ $.Name }}Slice(vs)
}
// To{{ $.Name }}Map returns a map of {{ $.Symbol }} values, that are
// dereferenced if the passed in pointer was not nil. The {{ $.Symbol }}
// zero value is used if the pointer was nil.
func To{{ $.Name }}Map(vs map[string]*{{ $.Symbol }}) map[string]{{ $.Symbol }} {
return ptr.To{{ $.Name }}Map(vs)
}
{{- end }}
`))