file: trim down cyclic complexity in SegmentFile
CI failed due to gocyclo
@@ -679,13 +679,15 @@ func (f *File) SegmentFile(sfc *SegmentFileConfiguration) (*File, *File, error)
for i := range f.Batches {
bh := f.Batches[i].GetHeader()
- switch bh.ServiceClassCode {
- case AutomatedAccountingAdvices:
- cbh := createSegmentFileBatchHeader(AutomatedAccountingAdvices, bh)
- creditBatch, _ := NewBatch(cbh)
+ var creditBatch Batcher
+ var debitBatch Batcher
- dbh := createSegmentFileBatchHeader(AutomatedAccountingAdvices, bh)
- debitBatch, _ := NewBatch(dbh)
+ switch bh.ServiceClassCode {
+ case AutomatedAccountingAdvices, MixedDebitsAndCredits:
+ if bh.StandardEntryClassCode == ADV {
+ bh := createSegmentFileBatchHeader(AutomatedAccountingAdvices, bh)
+ creditBatch, _ = NewBatch(bh)
+ debitBatch, _ = NewBatch(bh)
entries := f.Batches[i].GetADVEntries()
for i := range entries {
@@ -696,28 +698,13 @@ func (f *File) SegmentFile(sfc *SegmentFileConfiguration) (*File, *File, error)
debitBatch.AddADVEntry(entries[i])
}
}
- // Add the Entry to its Batch
- if len(creditBatch.GetEntries())+len(creditBatch.GetADVEntries()) > 0 {
- if err := creditBatch.Create(); err != nil {
- return nil, nil, fmt.Errorf("ADV Segment: creditBatch.Create(): %v", err)
- }
- creditFile.AddBatch(creditBatch)
- }
- if len(debitBatch.GetEntries())+len(debitBatch.GetADVEntries()) > 0 {
- if err := debitBatch.Create(); err != nil {
- return nil, nil, fmt.Errorf("ADV Segment: debitBatch.Create(): %v", err)
- }
- debitFile.AddBatch(debitBatch)
- }
-
- case MixedDebitsAndCredits:
+ } else {
cbh := createSegmentFileBatchHeader(CreditsOnly, bh)
- creditBatch, _ := NewBatch(cbh)
+ creditBatch, _ = NewBatch(cbh)
dbh := createSegmentFileBatchHeader(DebitsOnly, bh)
- debitBatch, _ := NewBatch(dbh)
+ debitBatch, _ = NewBatch(dbh)
- // Add entries
entries := f.Batches[i].GetEntries()
for i := range entries {
entries[i].TraceNumber = "" // unset so Batch.build generates a TraceNumber
@@ -729,16 +716,18 @@ func (f *File) SegmentFile(sfc *SegmentFileConfiguration) (*File, *File, error)
debitBatch.AddEntry(entries[i])
}
}
+ }
+
// Add the Entry to its Batch
- if len(creditBatch.GetEntries())+len(creditBatch.GetADVEntries()) > 0 {
+ if creditBatch != nil && len(creditBatch.GetEntries())+len(creditBatch.GetADVEntries()) > 0 {
if err := creditBatch.Create(); err != nil {
- return nil, nil, fmt.Errorf("Segment: creditBatch.Create(): %v", err)
+ return nil, nil, fmt.Errorf("ADV Segment: creditBatch.Create(): %v", err)
}
creditFile.AddBatch(creditBatch)
}
- if len(debitBatch.GetEntries())+len(debitBatch.GetADVEntries()) > 0 {
+ if debitBatch != nil && len(debitBatch.GetEntries())+len(debitBatch.GetADVEntries()) > 0 {
if err := debitBatch.Create(); err != nil {
- return nil, nil, fmt.Errorf("Segment: debitBatch.Create(): %v", err)
+ return nil, nil, fmt.Errorf("ADV Segment: debitBatch.Create(): %v", err)
}
debitFile.AddBatch(debitBatch)
}
