-
Notifications
You must be signed in to change notification settings - Fork 52
/
type.go.tpl
202 lines (175 loc) · 6.36 KB
/
type.go.tpl
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
{{- $short := (shortname .Name "err" "res" "sqlstr" "db" "YOLog") -}}
{{- $table := (.Table.TableName) -}}
// {{ .Name }} represents a row from '{{ $table }}'.
type {{ .Name }} struct {
{{- range .Fields }}
{{- if eq (.Col.DataType) (.Col.ColumnName) }}
{{ .Name }} string `spanner:"{{ .Col.ColumnName }}" json:"{{ .Col.ColumnName }}"` // {{ .Col.ColumnName }} enum
{{- else if .CustomType }}
{{ .Name }} {{ retype .CustomType }} `spanner:"{{ .Col.ColumnName }}" json:"{{ .Col.ColumnName }}"` // {{ .Col.ColumnName }}
{{- else }}
{{ .Name }} {{ .Type }} `spanner:"{{ .Col.ColumnName }}" json:"{{ .Col.ColumnName }}"` // {{ .Col.ColumnName }}
{{- end }}
{{- end }}
}
{{ if .PrimaryKey }}
func {{ .Name }}PrimaryKeys() []string {
return []string{
{{- range .PrimaryKeyFields }}
"{{ colname .Col }}",
{{- end }}
}
}
{{- end }}
func {{ .Name }}Columns() []string {
return []string{
{{- range .Fields }}
"{{ colname .Col }}",
{{- end }}
}
}
func {{ .Name }}WritableColumns() []string {
return []string{
{{- range .Fields }}
{{- if not .Col.IsGenerated }}
"{{ colname .Col }}",
{{- end }}
{{- end }}
}
}
func ({{ $short }} *{{ .Name }}) columnsToPtrs(cols []string, customPtrs map[string]interface{}) ([]interface{}, error) {
ret := make([]interface{}, 0, len(cols))
for _, col := range cols {
if val, ok := customPtrs[col]; ok {
ret = append(ret, val)
continue
}
switch col {
{{- range .Fields }}
case "{{ colname .Col }}":
ret = append(ret, &{{ $short }}.{{ .Name }})
{{- end }}
default:
return nil, fmt.Errorf("unknown column: %s", col)
}
}
return ret, nil
}
func ({{ $short }} *{{ .Name }}) columnsToValues(cols []string) ([]interface{}, error) {
ret := make([]interface{}, 0, len(cols))
for _, col := range cols {
switch col {
{{- range .Fields }}
case "{{ colname .Col }}":
{{- if .CustomType }}
ret = append(ret, {{ .Type }}({{ $short }}.{{ .Name }}))
{{- else }}
ret = append(ret, {{ $short }}.{{ .Name }})
{{- end }}
{{- end }}
default:
return nil, fmt.Errorf("unknown column: %s", col)
}
}
return ret, nil
}
// new{{ .Name }}_Decoder returns a decoder which reads a row from *spanner.Row
// into {{ .Name }}. The decoder is not goroutine-safe. Don't use it concurrently.
func new{{ .Name }}_Decoder(cols []string) func(*spanner.Row) (*{{ .Name }}, error) {
{{- range .Fields }}
{{- if .CustomType }}
var {{ customtypeparam .Name }} {{ .Type }}
{{- end }}
{{- end }}
customPtrs := map[string]interface{}{
{{- range .Fields }}
{{- if .CustomType }}
"{{ colname .Col }}": &{{ customtypeparam .Name }},
{{- end }}
{{- end }}
}
return func(row *spanner.Row) (*{{ .Name }}, error) {
var {{ $short }} {{ .Name }}
ptrs, err := {{ $short }}.columnsToPtrs(cols, customPtrs)
if err != nil {
return nil, err
}
if err := row.Columns(ptrs...); err != nil {
return nil, err
}
{{- range .Fields }}
{{- if .CustomType }}
{{ $short }}.{{ .Name }} = {{ retype .CustomType }}({{ customtypeparam .Name }})
{{- end }}
{{- end }}
return &{{ $short }}, nil
}
}
// Insert returns a Mutation to insert a row into a table. If the row already
// exists, the write or transaction fails.
func ({{ $short }} *{{ .Name }}) Insert(ctx context.Context) *spanner.Mutation {
values, _ := {{ $short }}.columnsToValues({{ .Name }}WritableColumns())
return spanner.Insert("{{ $table }}", {{ .Name }}WritableColumns(), values)
}
{{ if ne (fieldnames .Fields $short .PrimaryKeyFields) "" }}
// Update returns a Mutation to update a row in a table. If the row does not
// already exist, the write or transaction fails.
func ({{ $short }} *{{ .Name }}) Update(ctx context.Context) *spanner.Mutation {
values, _ := {{ $short }}.columnsToValues({{ .Name }}WritableColumns())
return spanner.Update("{{ $table }}", {{ .Name }}WritableColumns(), values)
}
// InsertOrUpdate returns a Mutation to insert a row into a table. If the row
// already exists, it updates it instead. Any column values not explicitly
// written are preserved.
func ({{ $short }} *{{ .Name }}) InsertOrUpdate(ctx context.Context) *spanner.Mutation {
values, _ := {{ $short }}.columnsToValues({{ .Name }}WritableColumns())
return spanner.InsertOrUpdate("{{ $table }}", {{ .Name }}WritableColumns(), values)
}
// UpdateColumns returns a Mutation to update specified columns of a row in a table.
func ({{ $short }} *{{ .Name }}) UpdateColumns(ctx context.Context, cols ...string) (*spanner.Mutation, error) {
// add primary keys to columns to update by primary keys
colsWithPKeys := append(cols, {{ .Name }}PrimaryKeys()...)
values, err := {{ $short }}.columnsToValues(colsWithPKeys)
if err != nil {
return nil, newErrorWithCode(codes.InvalidArgument, "{{ .Name }}.UpdateColumns", "{{ $table }}", err)
}
return spanner.Update("{{ $table }}", colsWithPKeys, values), nil
}
// Find{{ .Name }} gets a {{ .Name }} by primary key
func Find{{ .Name }}(ctx context.Context, db YORODB{{ gocustomparamlist .PrimaryKeyFields true true }}) (*{{ .Name }}, error) {
key := spanner.Key{ {{ gocustomparamlist .PrimaryKeyFields false false }} }
row, err := db.ReadRow(ctx, "{{ $table }}", key, {{ .Name }}Columns())
if err != nil {
return nil, newError("Find{{ .Name }}", "{{ $table }}", err)
}
decoder := new{{ .Name }}_Decoder({{ .Name}}Columns())
{{ $short }}, err := decoder(row)
if err != nil {
return nil, newErrorWithCode(codes.Internal, "Find{{ .Name }}", "{{ $table }}", err)
}
return {{ $short }}, nil
}
// Read{{ .Name }} retrieves multiples rows from {{ .Name }} by KeySet as a slice.
func Read{{ .Name }}(ctx context.Context, db YORODB, keys spanner.KeySet) ([]*{{ .Name }}, error) {
var res []*{{ .Name }}
decoder := new{{ .Name }}_Decoder({{ .Name}}Columns())
rows := db.Read(ctx, "{{ $table }}", keys, {{ .Name }}Columns())
err := rows.Do(func(row *spanner.Row) error {
{{ $short }}, err := decoder(row)
if err != nil {
return err
}
res = append(res, {{ $short }})
return nil
})
if err != nil {
return nil, newErrorWithCode(codes.Internal, "Read{{ .Name }}", "{{ $table }}", err)
}
return res, nil
}
{{ end }}
// Delete deletes the {{ .Name }} from the database.
func ({{ $short }} *{{ .Name }}) Delete(ctx context.Context) *spanner.Mutation {
values, _ := {{ $short }}.columnsToValues({{ .Name }}PrimaryKeys())
return spanner.Delete("{{ $table }}", spanner.Key(values))
}