forked from nspcc-dev/neofs-api-go
/
convert.go
464 lines (365 loc) · 9.07 KB
/
convert.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package acl
import (
acl "github.com/cthulhu-rider/neofs-api-go/v2/acl/grpc"
"github.com/cthulhu-rider/neofs-api-go/v2/refs"
)
// RoleToGRPCField converts unified role enum into grpc enum.
func RoleToGRPCField(t Role) acl.Role {
switch t {
case RoleUser:
return acl.Role_USER
case RoleSystem:
return acl.Role_SYSTEM
case RoleOthers:
return acl.Role_OTHERS
default:
return acl.Role_ROLE_UNSPECIFIED
}
}
// RoleFromGRPCField converts grpc enum into unified role enum.
func RoleFromGRPCField(t acl.Role) Role {
switch t {
case acl.Role_USER:
return RoleUser
case acl.Role_SYSTEM:
return RoleSystem
case acl.Role_OTHERS:
return RoleOthers
default:
return RoleUnknown
}
}
// OperationToGRPCField converts unified operation enum into grpc enum.
func OperationToGRPCField(t Operation) acl.Operation {
switch t {
case OperationPut:
return acl.Operation_PUT
case OperationDelete:
return acl.Operation_DELETE
case OperationGet:
return acl.Operation_GET
case OperationHead:
return acl.Operation_HEAD
case OperationSearch:
return acl.Operation_SEARCH
case OperationRange:
return acl.Operation_GETRANGE
case OperationRangeHash:
return acl.Operation_GETRANGEHASH
default:
return acl.Operation_OPERATION_UNSPECIFIED
}
}
// OperationFromGRPCField converts grpc enum into unified operation enum.
func OperationFromGRPCField(t acl.Operation) Operation {
switch t {
case acl.Operation_PUT:
return OperationPut
case acl.Operation_DELETE:
return OperationDelete
case acl.Operation_GET:
return OperationGet
case acl.Operation_HEAD:
return OperationHead
case acl.Operation_SEARCH:
return OperationSearch
case acl.Operation_GETRANGE:
return OperationRange
case acl.Operation_GETRANGEHASH:
return OperationRangeHash
default:
return OperationUnknown
}
}
// ActionToGRPCField converts unified action enum into grpc enum.
func ActionToGRPCField(t Action) acl.Action {
switch t {
case ActionDeny:
return acl.Action_DENY
case ActionAllow:
return acl.Action_ALLOW
default:
return acl.Action_ACTION_UNSPECIFIED
}
}
// ActionFromGRPCField converts grpc enum into unified action enum.
func ActionFromGRPCField(t acl.Action) Action {
switch t {
case acl.Action_DENY:
return ActionDeny
case acl.Action_ALLOW:
return ActionAllow
default:
return ActionUnknown
}
}
// HeaderTypeToGRPCField converts unified header type enum into grpc enum.
func HeaderTypeToGRPCField(t HeaderType) acl.HeaderType {
switch t {
case HeaderTypeRequest:
return acl.HeaderType_REQUEST
case HeaderTypeObject:
return acl.HeaderType_OBJECT
default:
return acl.HeaderType_HEADER_UNSPECIFIED
}
}
// HeaderTypeFromGRPCField converts grpc enum into unified header type enum.
func HeaderTypeFromGRPCField(t acl.HeaderType) HeaderType {
switch t {
case acl.HeaderType_REQUEST:
return HeaderTypeRequest
case acl.HeaderType_OBJECT:
return HeaderTypeObject
default:
return HeaderTypeUnknown
}
}
// MatchTypeToGRPCField converts unified match type enum into grpc enum.
func MatchTypeToGRPCField(t MatchType) acl.MatchType {
switch t {
case MatchTypeStringEqual:
return acl.MatchType_STRING_EQUAL
case MatchTypeStringNotEqual:
return acl.MatchType_STRING_NOT_EQUAL
default:
return acl.MatchType_MATCH_TYPE_UNSPECIFIED
}
}
// MatchTypeFromGRPCField converts grpc enum into unified match type enum.
func MatchTypeFromGRPCField(t acl.MatchType) MatchType {
switch t {
case acl.MatchType_STRING_EQUAL:
return MatchTypeStringEqual
case acl.MatchType_STRING_NOT_EQUAL:
return MatchTypeStringNotEqual
default:
return MatchTypeUnknown
}
}
// HeaderFilterToGRPCMessage converts unified header filter struct into grpc struct.
func HeaderFilterToGRPCMessage(f *HeaderFilter) *acl.EACLRecord_Filter {
if f == nil {
return nil
}
m := new(acl.EACLRecord_Filter)
m.SetHeader(
HeaderTypeToGRPCField(f.GetHeaderType()),
)
m.SetMatchType(
MatchTypeToGRPCField(f.GetMatchType()),
)
m.SetKey(f.GetKey())
m.SetValue(f.GetValue())
return m
}
// HeaderFilterFromGRPCMessage converts grpc struct into unified header filter struct.
func HeaderFilterFromGRPCMessage(m *acl.EACLRecord_Filter) *HeaderFilter {
if m == nil {
return nil
}
f := new(HeaderFilter)
f.SetHeaderType(
HeaderTypeFromGRPCField(m.GetHeaderType()),
)
f.SetMatchType(
MatchTypeFromGRPCField(m.GetMatchType()),
)
f.SetKey(m.GetKey())
f.SetValue(m.GetValue())
return f
}
// TargetToGRPCMessage converts unified role info struct into grpc struct.
func TargetToGRPCMessage(t *Target) *acl.EACLRecord_Target {
if t == nil {
return nil
}
m := new(acl.EACLRecord_Target)
m.SetRole(
RoleToGRPCField(t.GetRole()),
)
m.SetKeys(t.GetKeys())
return m
}
// TargetInfoFromGRPCMessage converts grpc struct into unified role info struct.
func TargetInfoFromGRPCMessage(m *acl.EACLRecord_Target) *Target {
if m == nil {
return nil
}
t := new(Target)
t.SetRole(
RoleFromGRPCField(m.GetRole()),
)
t.SetKeys(m.GetKeys())
return t
}
// RecordToGRPCMessage converts unified acl record struct into grpc struct.
func RecordToGRPCMessage(r *Record) *acl.EACLRecord {
if r == nil {
return nil
}
m := new(acl.EACLRecord)
m.SetOperation(
OperationToGRPCField(r.GetOperation()),
)
m.SetAction(
ActionToGRPCField(r.GetAction()),
)
filters := r.GetFilters()
filterMsg := make([]*acl.EACLRecord_Filter, 0, len(filters))
for i := range filters {
filterMsg = append(filterMsg, HeaderFilterToGRPCMessage(filters[i]))
}
m.SetFilters(filterMsg)
targets := r.GetTargets()
targetMsg := make([]*acl.EACLRecord_Target, 0, len(targets))
for i := range targets {
targetMsg = append(targetMsg, TargetToGRPCMessage(targets[i]))
}
m.SetTargets(targetMsg)
return m
}
// RecordFromGRPCMessage converts grpc struct into unified acl record struct.
func RecordFromGRPCMessage(m *acl.EACLRecord) *Record {
if m == nil {
return nil
}
r := new(Record)
r.SetOperation(
OperationFromGRPCField(m.GetOperation()),
)
r.SetAction(
ActionFromGRPCField(m.GetAction()),
)
filterMsg := m.GetFilters()
filters := make([]*HeaderFilter, 0, len(filterMsg))
for i := range filterMsg {
filters = append(filters, HeaderFilterFromGRPCMessage(filterMsg[i]))
}
r.SetFilters(filters)
targetMsg := m.GetTargets()
targets := make([]*Target, 0, len(targetMsg))
for i := range targetMsg {
targets = append(targets, TargetInfoFromGRPCMessage(targetMsg[i]))
}
r.SetTargets(targets)
return r
}
// TableToGRPCMessage converts unified acl table struct into grpc struct.
func TableToGRPCMessage(t *Table) *acl.EACLTable {
if t == nil {
return nil
}
m := new(acl.EACLTable)
m.SetVersion(
refs.VersionToGRPCMessage(t.GetVersion()),
)
m.SetContainerId(
refs.ContainerIDToGRPCMessage(t.GetContainerID()),
)
records := t.GetRecords()
recordMsg := make([]*acl.EACLRecord, 0, len(records))
for i := range records {
recordMsg = append(recordMsg, RecordToGRPCMessage(records[i]))
}
m.SetRecords(recordMsg)
return m
}
// TableFromGRPCMessage converts grpc struct into unified acl table struct.
func TableFromGRPCMessage(m *acl.EACLTable) *Table {
if m == nil {
return nil
}
t := new(Table)
t.SetVersion(
refs.VersionFromGRPCMessage(m.GetVersion()),
)
t.SetContainerID(
refs.ContainerIDFromGRPCMessage(m.GetContainerId()),
)
recordMsg := m.GetRecords()
records := make([]*Record, 0, len(recordMsg))
for i := range recordMsg {
records = append(records, RecordFromGRPCMessage(recordMsg[i]))
}
t.SetRecords(records)
return t
}
func TokenLifetimeToGRPCMessage(tl *TokenLifetime) *acl.BearerToken_Body_TokenLifetime {
if tl == nil {
return nil
}
m := new(acl.BearerToken_Body_TokenLifetime)
m.SetExp(tl.GetExp())
m.SetNbf(tl.GetNbf())
m.SetIat(tl.GetIat())
return m
}
func TokenLifetimeFromGRPCMessage(m *acl.BearerToken_Body_TokenLifetime) *TokenLifetime {
if m == nil {
return nil
}
tl := new(TokenLifetime)
tl.SetExp(m.GetExp())
tl.SetNbf(m.GetNbf())
tl.SetIat(m.GetIat())
return tl
}
func BearerTokenBodyToGRPCMessage(v *BearerTokenBody) *acl.BearerToken_Body {
if v == nil {
return nil
}
m := new(acl.BearerToken_Body)
m.SetEaclTable(
TableToGRPCMessage(v.GetEACL()),
)
m.SetOwnerId(
refs.OwnerIDToGRPCMessage(v.GetOwnerID()),
)
m.SetLifetime(
TokenLifetimeToGRPCMessage(v.GetLifetime()),
)
return m
}
func BearerTokenBodyFromGRPCMessage(m *acl.BearerToken_Body) *BearerTokenBody {
if m == nil {
return nil
}
bt := new(BearerTokenBody)
bt.SetEACL(
TableFromGRPCMessage(m.GetEaclTable()),
)
bt.SetOwnerID(
refs.OwnerIDFromGRPCMessage(m.GetOwnerId()),
)
bt.SetLifetime(
TokenLifetimeFromGRPCMessage(m.GetLifetime()),
)
return bt
}
func BearerTokenToGRPCMessage(t *BearerToken) *acl.BearerToken {
if t == nil {
return nil
}
m := new(acl.BearerToken)
m.SetBody(
BearerTokenBodyToGRPCMessage(t.GetBody()),
)
m.SetSignature(
refs.SignatureToGRPCMessage(t.GetSignature()),
)
return m
}
func BearerTokenFromGRPCMessage(m *acl.BearerToken) *BearerToken {
if m == nil {
return nil
}
bt := new(BearerToken)
bt.SetBody(
BearerTokenBodyFromGRPCMessage(m.GetBody()),
)
bt.SetSignature(
refs.SignatureFromGRPCMessage(m.GetSignature()),
)
return bt
}