forked from cloudspannerecosystem/yo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxlength.yo.go
174 lines (145 loc) · 4.88 KB
/
maxlength.yo.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
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
// Code generated by yo. DO NOT EDIT.
// Package customtypes contains the types.
package customtypes
import (
"context"
"fmt"
"cloud.google.com/go/spanner"
"google.golang.org/grpc/codes"
)
// MaxLength represents a row from 'MaxLengths'.
type MaxLength struct {
MaxString string `spanner:"MaxString" json:"MaxString"` // MaxString
MaxBytes []byte `spanner:"MaxBytes" json:"MaxBytes"` // MaxBytes
}
func MaxLengthPrimaryKeys() []string {
return []string{
"MaxString",
}
}
func MaxLengthColumns() []string {
return []string{
"MaxString",
"MaxBytes",
}
}
func MaxLengthWritableColumns() []string {
return []string{
"MaxString",
"MaxBytes",
}
}
func (ml *MaxLength) 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 {
case "MaxString":
ret = append(ret, &ml.MaxString)
case "MaxBytes":
ret = append(ret, &ml.MaxBytes)
default:
return nil, fmt.Errorf("unknown column: %s", col)
}
}
return ret, nil
}
func (ml *MaxLength) columnsToValues(cols []string) ([]interface{}, error) {
ret := make([]interface{}, 0, len(cols))
for _, col := range cols {
switch col {
case "MaxString":
ret = append(ret, ml.MaxString)
case "MaxBytes":
ret = append(ret, ml.MaxBytes)
default:
return nil, fmt.Errorf("unknown column: %s", col)
}
}
return ret, nil
}
// newMaxLength_Decoder returns a decoder which reads a row from *spanner.Row
// into MaxLength. The decoder is not goroutine-safe. Don't use it concurrently.
func newMaxLength_Decoder(cols []string) func(*spanner.Row) (*MaxLength, error) {
customPtrs := map[string]interface{}{}
return func(row *spanner.Row) (*MaxLength, error) {
var ml MaxLength
ptrs, err := ml.columnsToPtrs(cols, customPtrs)
if err != nil {
return nil, err
}
if err := row.Columns(ptrs...); err != nil {
return nil, err
}
return &ml, nil
}
}
// Insert returns a Mutation to insert a row into a table. If the row already
// exists, the write or transaction fails.
func (ml *MaxLength) Insert(ctx context.Context) *spanner.Mutation {
values, _ := ml.columnsToValues(MaxLengthWritableColumns())
return spanner.Insert("MaxLengths", MaxLengthWritableColumns(), values)
}
// Update returns a Mutation to update a row in a table. If the row does not
// already exist, the write or transaction fails.
func (ml *MaxLength) Update(ctx context.Context) *spanner.Mutation {
values, _ := ml.columnsToValues(MaxLengthWritableColumns())
return spanner.Update("MaxLengths", MaxLengthWritableColumns(), 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 (ml *MaxLength) InsertOrUpdate(ctx context.Context) *spanner.Mutation {
values, _ := ml.columnsToValues(MaxLengthWritableColumns())
return spanner.InsertOrUpdate("MaxLengths", MaxLengthWritableColumns(), values)
}
// UpdateColumns returns a Mutation to update specified columns of a row in a table.
func (ml *MaxLength) UpdateColumns(ctx context.Context, cols ...string) (*spanner.Mutation, error) {
// add primary keys to columns to update by primary keys
colsWithPKeys := append(cols, MaxLengthPrimaryKeys()...)
values, err := ml.columnsToValues(colsWithPKeys)
if err != nil {
return nil, newErrorWithCode(codes.InvalidArgument, "MaxLength.UpdateColumns", "MaxLengths", err)
}
return spanner.Update("MaxLengths", colsWithPKeys, values), nil
}
// FindMaxLength gets a MaxLength by primary key
func FindMaxLength(ctx context.Context, db YORODB, maxString string) (*MaxLength, error) {
key := spanner.Key{maxString}
row, err := db.ReadRow(ctx, "MaxLengths", key, MaxLengthColumns())
if err != nil {
return nil, newError("FindMaxLength", "MaxLengths", err)
}
decoder := newMaxLength_Decoder(MaxLengthColumns())
ml, err := decoder(row)
if err != nil {
return nil, newErrorWithCode(codes.Internal, "FindMaxLength", "MaxLengths", err)
}
return ml, nil
}
// ReadMaxLength retrieves multiples rows from MaxLength by KeySet as a slice.
func ReadMaxLength(ctx context.Context, db YORODB, keys spanner.KeySet) ([]*MaxLength, error) {
var res []*MaxLength
decoder := newMaxLength_Decoder(MaxLengthColumns())
rows := db.Read(ctx, "MaxLengths", keys, MaxLengthColumns())
err := rows.Do(func(row *spanner.Row) error {
ml, err := decoder(row)
if err != nil {
return err
}
res = append(res, ml)
return nil
})
if err != nil {
return nil, newErrorWithCode(codes.Internal, "ReadMaxLength", "MaxLengths", err)
}
return res, nil
}
// Delete deletes the MaxLength from the database.
func (ml *MaxLength) Delete(ctx context.Context) *spanner.Mutation {
values, _ := ml.columnsToValues(MaxLengthPrimaryKeys())
return spanner.Delete("MaxLengths", spanner.Key(values))
}