-
Notifications
You must be signed in to change notification settings - Fork 6
/
collection_batch_methods.go
259 lines (223 loc) · 7.17 KB
/
collection_batch_methods.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package restli
import (
"context"
"net/http"
"github.com/PapaCharlie/go-restli/restli/batchkeyset"
"github.com/PapaCharlie/go-restli/restlicodec"
"github.com/PapaCharlie/go-restli/restlidata"
)
type queryParamsFunc func() (string, error)
func (q queryParamsFunc) EncodeQueryParams() (string, error) {
return q()
}
type batchQueryParamsEncoder[T any] interface {
EncodeQueryParams(set batchkeyset.BatchKeySet[T]) (string, error)
}
type batchQueryParamsDecoder[T, QP any] interface {
NewInstance() QP
DecodeQueryParams(reader restlicodec.QueryParamsReader) ([]T, error)
}
type SliceBatchQueryParams[T any] struct{}
var entityIdsRequiredResponseFields = restlicodec.RequiredFields{batchkeyset.EntityIDsField}
func (s *SliceBatchQueryParams[T]) NewInstance() *SliceBatchQueryParams[T] {
return new(SliceBatchQueryParams[T])
}
func (s *SliceBatchQueryParams[T]) DecodeQueryParams(reader restlicodec.QueryParamsReader) (ids []T, err error) {
err = reader.ReadRecord(entityIdsRequiredResponseFields, func(reader restlicodec.Reader, field string) (err error) {
if field == batchkeyset.EntityIDsField {
ids, err = restlicodec.ReadArray(reader, restlicodec.UnmarshalRestLi[T])
} else {
err = reader.Skip()
}
return err
})
return ids, err
}
type wrappedBatchQueryParamsDecoder[T any, QP batchQueryParamsDecoder[T, QP]] struct {
keys []T
qp QP
}
func (b *wrappedBatchQueryParamsDecoder[T, QP]) NewInstance() *wrappedBatchQueryParamsDecoder[T, QP] {
return new(wrappedBatchQueryParamsDecoder[T, QP])
}
func (b *wrappedBatchQueryParamsDecoder[T, QP]) DecodeQueryParams(reader restlicodec.QueryParamsReader) (err error) {
b.qp = b.qp.NewInstance()
b.keys, err = b.qp.DecodeQueryParams(reader)
return err
}
func batchQueryParams[T any](set batchkeyset.BatchKeySet[T], query batchQueryParamsEncoder[T]) QueryParamsEncoder {
if query != nil {
return queryParamsFunc(func() (string, error) {
return query.EncodeQueryParams(set)
})
} else {
return set
}
}
// BatchCreate executes a batch_create with the given slice of entities
func BatchCreate[K comparable, V restlicodec.Marshaler](
c *Client,
ctx context.Context,
rp ResourcePath,
entities []V,
query QueryParamsEncoder,
readOnlyFields restlicodec.PathSpec,
) (createdEntities []*restlidata.CreatedEntity[K], err error) {
return batchCreate[K, V, *restlidata.CreatedEntity[K]](c, ctx, rp, entities, query, readOnlyFields)
}
func BatchCreateWithReturnEntity[K comparable, V restlicodec.Marshaler](
c *Client,
ctx context.Context,
rp ResourcePath,
entities []V,
query QueryParamsEncoder,
readOnlyFields restlicodec.PathSpec,
) (createdEntities []*restlidata.CreatedAndReturnedEntity[K, V], err error) {
return batchCreate[K, V, *restlidata.CreatedAndReturnedEntity[K, V]](c, ctx, rp, entities, query, readOnlyFields)
}
func batchCreate[K comparable, V restlicodec.Marshaler, R restlicodec.Marshaler](
c *Client,
ctx context.Context,
rp ResourcePath,
entities []V,
query QueryParamsEncoder,
readOnlyFields restlicodec.PathSpec,
) (createdEntities []R, err error) {
req, err := NewCreateRequest(c, ctx, rp, query, Method_batch_create, restlicodec.MarshalerFunc(func(writer restlicodec.Writer) error {
return writer.WriteMap(func(keyWriter func(key string) restlicodec.Writer) (err error) {
return restlicodec.WriteArray(keyWriter(restlidata.ElementsField), entities, V.MarshalRestLi)
})
}), readOnlyFields)
if err != nil {
return nil, err
}
elements, _, err := DoAndUnmarshal(c, req, restlicodec.UnmarshalRestLi[*restlidata.Elements[R]])
if err != nil {
return nil, err
}
return elements.Elements, nil
}
func BatchDelete[K comparable](
c *Client,
ctx context.Context,
rp ResourcePath,
keys []K,
query batchQueryParamsEncoder[K],
) (*restlidata.BatchResponse[K, *restlidata.BatchEntityUpdateResponse], error) {
keySet := batchkeyset.NewBatchKeySet[K]()
err := batchkeyset.AddAllKeys(keySet, keys...)
if err != nil {
return nil, err
}
req, err := NewDeleteRequest(c, ctx, rp, batchQueryParams(keySet, query), Method_batch_delete)
if err != nil {
return nil, err
}
return doBatchQuery[K, *restlidata.BatchEntityUpdateResponse](c, keySet, req)
}
func BatchGet[K comparable, V restlicodec.Marshaler](
c *Client,
ctx context.Context,
rp ResourcePath,
keys []K,
query batchQueryParamsEncoder[K],
) (*restlidata.BatchResponse[K, V], error) {
keySet := batchkeyset.NewBatchKeySet[K]()
err := batchkeyset.AddAllKeys(keySet, keys...)
if err != nil {
return nil, err
}
req, err := NewGetRequest(c, ctx, rp, batchQueryParams(keySet, query), Method_batch_get)
if err != nil {
return nil, err
}
return doBatchQuery[K, V](c, keySet, req)
}
func BatchUpdate[K comparable, V restlicodec.Marshaler](
c *Client,
ctx context.Context,
rp ResourcePath,
entities map[K]V,
query batchQueryParamsEncoder[K],
createAndReadOnlyFields restlicodec.PathSpec,
) (*restlidata.BatchResponse[K, *restlidata.BatchEntityUpdateResponse], error) {
keys := batchkeyset.NewBatchKeySet[K]()
err := batchkeyset.AddAllMapKeys(keys, entities)
if err != nil {
return nil, err
}
req, err := NewJsonRequest(
c,
ctx,
rp,
batchQueryParams(keys, query),
http.MethodPut,
Method_batch_update,
batchEntities[K, V](entities),
createAndReadOnlyFields,
)
if err != nil {
return nil, err
}
return doBatchQuery[K, *restlidata.BatchEntityUpdateResponse](c, keys, req)
}
func BatchPartialUpdate[K comparable, PV restlicodec.Marshaler](
c *Client,
ctx context.Context,
rp ResourcePath,
entities map[K]PV,
query batchQueryParamsEncoder[K],
createAndReadOnlyFields restlicodec.PathSpec,
) (*restlidata.BatchResponse[K, *restlidata.BatchEntityUpdateResponse], error) {
keys := batchkeyset.NewBatchKeySet[K]()
err := batchkeyset.AddAllMapKeys(keys, entities)
if err != nil {
return nil, err
}
req, err := NewJsonRequest(
c,
ctx,
rp,
batchQueryParams(keys, query),
http.MethodPost,
Method_batch_partial_update,
batchEntities[K, PV](entities),
createAndReadOnlyFields,
)
if err != nil {
return nil, err
}
return doBatchQuery[K, *restlidata.BatchEntityUpdateResponse](c, keys, req)
}
func doBatchQuery[K comparable, V restlicodec.Marshaler](
c *Client,
keys batchkeyset.BatchKeySet[K],
req *http.Request,
) (res *restlidata.BatchResponse[K, V], err error) {
data, _, err := c.do(req)
if err != nil {
return nil, err
}
r, err := restlicodec.NewJsonReader(data)
if err != nil {
return nil, err
}
res = new(restlidata.BatchResponse[K, V])
return res, res.UnmarshalWithKeyLocator(r, keys)
}
type batchEntities[K comparable, V restlicodec.Marshaler] map[K]V
func (b batchEntities[K, V]) MarshalRestLi(writer restlicodec.Writer) error {
return writer.WriteMap(func(keyWriter func(key string) restlicodec.Writer) (err error) {
return restlidata.MarshalBatchEntities(b, keyWriter(restlidata.EntitiesField))
})
}
var entitiesRequiredResponseFields = restlicodec.RequiredFields{restlidata.EntitiesField}
func (b batchEntities[K, V]) UnmarshalRestLi(reader restlicodec.Reader) error {
return reader.ReadRecord(entitiesRequiredResponseFields, func(reader restlicodec.Reader, field string) (err error) {
if field == restlidata.EntitiesField {
return restlidata.UnmarshalBatchEntities(b, reader)
} else {
return reader.Skip()
}
})
}