@@ -304,37 +304,79 @@ var (
304
304
_ json.Unmarshaler = (* Rule )(nil )
305
305
)
306
306
307
+ // This is a helper struct used to customizing the JSON marshal/unmarshal methods of `Rule`.
308
+ type rule struct {
309
+ GroupID string `json:"group_id"`
310
+ ID string `json:"id"`
311
+ Index int `json:"index,omitempty"`
312
+ Override bool `json:"override,omitempty"`
313
+ StartKeyHex string `json:"start_key"`
314
+ EndKeyHex string `json:"end_key"`
315
+ Role PeerRoleType `json:"role"`
316
+ IsWitness bool `json:"is_witness"`
317
+ Count int `json:"count"`
318
+ LabelConstraints []LabelConstraint `json:"label_constraints,omitempty"`
319
+ LocationLabels []string `json:"location_labels,omitempty"`
320
+ IsolationLevel string `json:"isolation_level,omitempty"`
321
+ }
322
+
307
323
// MarshalJSON implements `json.Marshaler` interface to make sure we could set the correct start/end key.
308
324
func (r * Rule ) MarshalJSON () ([]byte , error ) {
309
- r .StartKeyHex = hex .EncodeToString (encodeBytes (r .StartKey ))
310
- r .EndKeyHex = hex .EncodeToString (encodeBytes (r .EndKey ))
311
- return json .Marshal (r )
325
+ tempRule := & rule {
326
+ GroupID : r .GroupID ,
327
+ ID : r .ID ,
328
+ Index : r .Index ,
329
+ Override : r .Override ,
330
+ StartKeyHex : r .StartKeyHex ,
331
+ EndKeyHex : r .EndKeyHex ,
332
+ Role : r .Role ,
333
+ IsWitness : r .IsWitness ,
334
+ Count : r .Count ,
335
+ LabelConstraints : r .LabelConstraints ,
336
+ LocationLabels : r .LocationLabels ,
337
+ IsolationLevel : r .IsolationLevel ,
338
+ }
339
+ // Converts the start/end key to hex format if the corresponding hex field is empty.
340
+ if len (r .StartKey ) > 0 && len (r .StartKeyHex ) == 0 {
341
+ tempRule .StartKeyHex = rawKeyToKeyHexStr (r .StartKey )
342
+ }
343
+ if len (r .EndKey ) > 0 && len (r .EndKeyHex ) == 0 {
344
+ tempRule .EndKeyHex = rawKeyToKeyHexStr (r .EndKey )
345
+ }
346
+ return json .Marshal (tempRule )
312
347
}
313
348
314
349
// UnmarshalJSON implements `json.Unmarshaler` interface to make sure we could get the correct start/end key.
315
350
func (r * Rule ) UnmarshalJSON (bytes []byte ) error {
316
- if err := json .Unmarshal (bytes , r ); err != nil {
317
- return err
318
- }
319
-
320
- startKey , err := hex .DecodeString (r .StartKeyHex )
351
+ var tempRule rule
352
+ err := json .Unmarshal (bytes , & tempRule )
321
353
if err != nil {
322
354
return err
323
355
}
324
-
325
- endKey , err := hex .DecodeString (r .EndKeyHex )
356
+ newRule := Rule {
357
+ GroupID : tempRule .GroupID ,
358
+ ID : tempRule .ID ,
359
+ Index : tempRule .Index ,
360
+ Override : tempRule .Override ,
361
+ StartKeyHex : tempRule .StartKeyHex ,
362
+ EndKeyHex : tempRule .EndKeyHex ,
363
+ Role : tempRule .Role ,
364
+ IsWitness : tempRule .IsWitness ,
365
+ Count : tempRule .Count ,
366
+ LabelConstraints : tempRule .LabelConstraints ,
367
+ LocationLabels : tempRule .LocationLabels ,
368
+ IsolationLevel : tempRule .IsolationLevel ,
369
+ }
370
+ newRule .StartKey , err = keyHexStrToRawKey (newRule .StartKeyHex )
326
371
if err != nil {
327
372
return err
328
373
}
329
-
330
- _ , r .StartKey , err = decodeBytes (startKey )
374
+ newRule .EndKey , err = keyHexStrToRawKey (newRule .EndKeyHex )
331
375
if err != nil {
332
376
return err
333
377
}
334
-
335
- _ , r .EndKey , err = decodeBytes (endKey )
336
-
337
- return err
378
+ * r = newRule
379
+ return nil
338
380
}
339
381
340
382
// RuleOpType indicates the operation type
@@ -365,37 +407,87 @@ var (
365
407
_ json.Unmarshaler = (* RuleOp )(nil )
366
408
)
367
409
410
+ // This is a helper struct used to customizing the JSON marshal/unmarshal methods of `RuleOp`.
411
+ type ruleOp struct {
412
+ GroupID string `json:"group_id"`
413
+ ID string `json:"id"`
414
+ Index int `json:"index,omitempty"`
415
+ Override bool `json:"override,omitempty"`
416
+ StartKeyHex string `json:"start_key"`
417
+ EndKeyHex string `json:"end_key"`
418
+ Role PeerRoleType `json:"role"`
419
+ IsWitness bool `json:"is_witness"`
420
+ Count int `json:"count"`
421
+ LabelConstraints []LabelConstraint `json:"label_constraints,omitempty"`
422
+ LocationLabels []string `json:"location_labels,omitempty"`
423
+ IsolationLevel string `json:"isolation_level,omitempty"`
424
+ Action RuleOpType `json:"action"`
425
+ DeleteByIDPrefix bool `json:"delete_by_id_prefix"`
426
+ }
427
+
368
428
// MarshalJSON implements `json.Marshaler` interface to make sure we could set the correct start/end key.
369
429
func (r * RuleOp ) MarshalJSON () ([]byte , error ) {
370
- r .StartKeyHex = hex .EncodeToString (encodeBytes (r .StartKey ))
371
- r .EndKeyHex = hex .EncodeToString (encodeBytes (r .EndKey ))
372
- return json .Marshal (r )
430
+ tempRuleOp := & ruleOp {
431
+ GroupID : r .GroupID ,
432
+ ID : r .ID ,
433
+ Index : r .Index ,
434
+ Override : r .Override ,
435
+ StartKeyHex : r .StartKeyHex ,
436
+ EndKeyHex : r .EndKeyHex ,
437
+ Role : r .Role ,
438
+ IsWitness : r .IsWitness ,
439
+ Count : r .Count ,
440
+ LabelConstraints : r .LabelConstraints ,
441
+ LocationLabels : r .LocationLabels ,
442
+ IsolationLevel : r .IsolationLevel ,
443
+ Action : r .Action ,
444
+ DeleteByIDPrefix : r .DeleteByIDPrefix ,
445
+ }
446
+ // Converts the start/end key to hex format if the corresponding hex field is empty.
447
+ if len (r .StartKey ) > 0 && len (r .StartKeyHex ) == 0 {
448
+ tempRuleOp .StartKeyHex = rawKeyToKeyHexStr (r .StartKey )
449
+ }
450
+ if len (r .EndKey ) > 0 && len (r .EndKeyHex ) == 0 {
451
+ tempRuleOp .EndKeyHex = rawKeyToKeyHexStr (r .EndKey )
452
+ }
453
+ return json .Marshal (tempRuleOp )
373
454
}
374
455
375
456
// UnmarshalJSON implements `json.Unmarshaler` interface to make sure we could get the correct start/end key.
376
457
func (r * RuleOp ) UnmarshalJSON (bytes []byte ) error {
377
- if err := json .Unmarshal (bytes , r ); err != nil {
378
- return err
379
- }
380
-
381
- startKey , err := hex .DecodeString (r .StartKeyHex )
458
+ var tempRuleOp ruleOp
459
+ err := json .Unmarshal (bytes , & tempRuleOp )
382
460
if err != nil {
383
461
return err
384
462
}
385
-
386
- endKey , err := hex .DecodeString (r .EndKeyHex )
463
+ newRuleOp := RuleOp {
464
+ Rule : & Rule {
465
+ GroupID : tempRuleOp .GroupID ,
466
+ ID : tempRuleOp .ID ,
467
+ Index : tempRuleOp .Index ,
468
+ Override : tempRuleOp .Override ,
469
+ StartKeyHex : tempRuleOp .StartKeyHex ,
470
+ EndKeyHex : tempRuleOp .EndKeyHex ,
471
+ Role : tempRuleOp .Role ,
472
+ IsWitness : tempRuleOp .IsWitness ,
473
+ Count : tempRuleOp .Count ,
474
+ LabelConstraints : tempRuleOp .LabelConstraints ,
475
+ LocationLabels : tempRuleOp .LocationLabels ,
476
+ IsolationLevel : tempRuleOp .IsolationLevel ,
477
+ },
478
+ Action : tempRuleOp .Action ,
479
+ DeleteByIDPrefix : tempRuleOp .DeleteByIDPrefix ,
480
+ }
481
+ newRuleOp .StartKey , err = keyHexStrToRawKey (newRuleOp .StartKeyHex )
387
482
if err != nil {
388
483
return err
389
484
}
390
-
391
- _ , r .StartKey , err = decodeBytes (startKey )
485
+ newRuleOp .EndKey , err = keyHexStrToRawKey (newRuleOp .EndKeyHex )
392
486
if err != nil {
393
487
return err
394
488
}
395
-
396
- _ , r .EndKey , err = decodeBytes (endKey )
397
-
398
- return err
489
+ * r = newRuleOp
490
+ return nil
399
491
}
400
492
401
493
// RuleGroup defines properties of a rule group.
0 commit comments