@@ -664,3 +664,88 @@ func (e *Encoder) DecodeAccountCurrency( // nolint:gocognit
664
664
665
665
return nil
666
666
}
667
+
668
+
669
+ func (e * Encoder ) EncodeBalanceSeq ( // nolint:gocognit
670
+ balanceSeq * types.BalanceSeq ,
671
+ ) ([]byte , error ) {
672
+ output := e .pool .Get ()
673
+ if _ , err := output .WriteString (balanceSeq .Amount .Value ); err != nil {
674
+ return nil , fmt .Errorf ("%w: %s" , errors .ErrObjectEncodeFailed , err .Error ())
675
+ }
676
+ if balanceSeq .SeqNumSupport == nil || ! balanceSeq .SeqNumSupport .SupportSeq {
677
+ return output .Bytes (), nil
678
+ }
679
+
680
+ if _ , err := output .WriteRune (unicodeRecordSeparator ); err != nil {
681
+ return nil , fmt .Errorf ("%w: %s" , errors .ErrObjectEncodeFailed , err .Error ())
682
+ }
683
+ if _ , err := output .WriteString (strconv .FormatInt (int64 (balanceSeq .Seq ), 10 ),); err != nil {
684
+ return nil , fmt .Errorf ("%w: %s" , errors .ErrObjectEncodeFailed , err .Error ())
685
+ }
686
+
687
+ return output .Bytes (), nil
688
+ }
689
+
690
+ func (e * Encoder ) DecodeBalanceSeq (
691
+ b []byte ,
692
+ balanceSeq * types.BalanceSeq ,
693
+ reclaimInput bool ,
694
+ ) error {
695
+ // Indices of encoded BalanceSeq struct
696
+ const (
697
+ balanceValue = iota
698
+ seq
699
+ )
700
+
701
+ count := 0
702
+ currentBytes := b
703
+ for {
704
+ nextRune := bytes .IndexRune (currentBytes , unicodeRecordSeparator )
705
+ if nextRune == - 1 {
706
+ if count != balanceValue && count != seq {
707
+ return fmt .Errorf ("%w: next rune is -1 at %d" , errors .ErrRawDecodeFailed , count )
708
+ }
709
+
710
+ nextRune = len (currentBytes )
711
+ }
712
+
713
+ val := currentBytes [:nextRune ]
714
+ if len (val ) == 0 {
715
+ goto handleNext
716
+ }
717
+
718
+ switch count {
719
+ case balanceValue :
720
+ balanceSeq .Amount = & types.Amount {Value : string (val )}
721
+ case seq :
722
+ strVal := string (val )
723
+ i , err := strconv .ParseInt (strVal , 10 , 32 )
724
+ if err != nil {
725
+ return fmt .Errorf ("%w: %s" , errors .ErrRawDecodeFailed , err .Error ())
726
+ }
727
+
728
+ balanceSeq .Seq = int32 (i )
729
+ balanceSeq .SeqNumSupport = & types.SequenceNumSupport {
730
+ SupportSeq : true ,
731
+ }
732
+ default :
733
+ return fmt .Errorf ("%w: count %d > end" , errors .ErrRawDecodeFailed , count )
734
+ }
735
+
736
+ handleNext:
737
+ if nextRune == len (currentBytes ) &&
738
+ (count == balanceValue || count == seq ) {
739
+ break
740
+ }
741
+
742
+ currentBytes = currentBytes [nextRune + 1 :]
743
+ count ++
744
+ }
745
+
746
+ if reclaimInput {
747
+ e .pool .PutByteSlice (b )
748
+ }
749
+
750
+ return nil
751
+ }
0 commit comments