Skip to content

Commit

Permalink
change naming
Browse files Browse the repository at this point in the history
  • Loading branch information
mfdeveloper508 authored and adamdecaf committed Feb 2, 2023
1 parent 87a0562 commit 589e427
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 33 deletions.
16 changes: 4 additions & 12 deletions pkg/lib/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,12 @@ func (r *Account) Validate() error {
return nil
}

func (r *Account) Read(scan *Bai2Scanner, isRead bool) error {

func (r *Account) Read(scan *Bai2Scanner, useCurrentLine bool) error {
if scan == nil {
return errors.New("invalid bai2 scanner")
}

parseAccountIdentifier := func(raw string) error {

if raw == "" {
return nil
}
Expand All @@ -129,32 +127,27 @@ func (r *Account) Read(scan *Bai2Scanner, isRead bool) error {
find := false
isBreak := false

for line := scan.ScanLine(isRead); line != ""; line = scan.ScanLine(isRead) {

for line := scan.ScanLine(useCurrentLine); line != ""; line = scan.ScanLine(useCurrentLine) {
// find record code
if len(line) < 3 {
continue
}

useCurrentLine = false
switch line[:2] {
case util.AccountIdentifierCode:

if find {
isBreak = true
break
}

isRead = false
rawData = line
find = true

case util.ContinuationCode:

isRead = false
rawData = rawData[:len(rawData)-1] + "," + line[3:]

case util.AccountTrailerCode:

if err := parseAccountIdentifier(rawData); err != nil {
return err
} else {
Expand All @@ -173,7 +166,6 @@ func (r *Account) Read(scan *Bai2Scanner, isRead bool) error {
return nil

case util.TransactionDetailCode:

if err := parseAccountIdentifier(rawData); err != nil {
return err
} else {
Expand All @@ -187,7 +179,7 @@ func (r *Account) Read(scan *Bai2Scanner, isRead bool) error {
}

r.Details = append(r.Details, *detail)
isRead = true
useCurrentLine = true

default:
return fmt.Errorf("ERROR parsing file on line %d (unabled to read record type %s)", scan.GetLineIndex(), line[0:2])
Expand Down
9 changes: 3 additions & 6 deletions pkg/lib/detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ func (r *Detail) String(opts ...int64) string {
return (*transactionDetail)(r).string(opts...)
}

func (r *Detail) Read(scan *Bai2Scanner, isRead bool) error {

func (r *Detail) Read(scan *Bai2Scanner, useCurrentLine bool) error {
if scan == nil {
return errors.New("invalid bai2 scanner")
}
Expand All @@ -43,9 +42,8 @@ func (r *Detail) Read(scan *Bai2Scanner, isRead bool) error {
find := false
isBreak := false

for line := scan.ScanLine(isRead); line != ""; line = scan.ScanLine(isRead) {

isRead = false
for line := scan.ScanLine(useCurrentLine); line != ""; line = scan.ScanLine(useCurrentLine) {
useCurrentLine = false

// find record code
if len(line) < 3 {
Expand All @@ -63,7 +61,6 @@ func (r *Detail) Read(scan *Bai2Scanner, isRead bool) error {
find = true

case util.ContinuationCode:

rawData = rawData[:len(rawData)-1] + "," + line[3:]

default:
Expand Down
5 changes: 1 addition & 4 deletions pkg/lib/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,7 @@ func (r *Bai2) Read(scan *Bai2Scanner) error {
}

var err error

isRead := false
for line := scan.ScanLine(isRead); line != ""; line = scan.ScanLine(isRead) {
for line := scan.ScanLine(); line != ""; line = scan.ScanLine() {

// find record code
if len(line) < 3 {
Expand Down Expand Up @@ -186,7 +184,6 @@ func (r *Bai2) Read(scan *Bai2Scanner) error {
}

r.Groups = append(r.Groups, *newGroup)
isRead = false

case util.FileTrailerCode:

Expand Down
12 changes: 3 additions & 9 deletions pkg/lib/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,14 @@ func (r *Group) Validate() error {
return nil
}

func (r *Group) Read(scan *Bai2Scanner, isRead bool) error {

func (r *Group) Read(scan *Bai2Scanner, useCurrentLine bool) error {
if scan == nil {
return errors.New("invalid bai2 scanner")
}

var err error

for line := scan.ScanLine(isRead); line != ""; line = scan.ScanLine(isRead) {

isRead = false
for line := scan.ScanLine(useCurrentLine); line != ""; line = scan.ScanLine(useCurrentLine) {
useCurrentLine = false

// find record code
if len(line) < 3 {
Expand All @@ -134,7 +131,6 @@ func (r *Group) Read(scan *Bai2Scanner, isRead bool) error {

switch line[:2] {
case util.GroupHeaderCode:

newRecord := groupHeader{}
_, err = newRecord.parse(line)
if err != nil {
Expand All @@ -150,7 +146,6 @@ func (r *Group) Read(scan *Bai2Scanner, isRead bool) error {
r.AsOfDateModifier = newRecord.AsOfDateModifier

case util.AccountIdentifierCode:

newAccount := NewAccount()
err = newAccount.Read(scan, true)
if err != nil {
Expand All @@ -160,7 +155,6 @@ func (r *Group) Read(scan *Bai2Scanner, isRead bool) error {
r.Accounts = append(r.Accounts, *newAccount)

case util.GroupTrailerCode:

newRecord := groupTrailer{}
_, err = newRecord.parse(line)
if err != nil {
Expand Down
11 changes: 9 additions & 2 deletions pkg/lib/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@ func (b *Bai2Scanner) GetLine() string {
return strings.TrimSpace(strings.ReplaceAll(b.scan.Text(), "\n", ""))
}

func (b *Bai2Scanner) ScanLine(isRead bool) string {
// ScanLine returns a line from the underlying reader
// arg[0]: useCurrentLine (if false read a new line)
func (b *Bai2Scanner) ScanLine(arg ...bool) string {

if isRead {
useCurrentLine := false
if len(arg) > 0 {
useCurrentLine = arg[0]
}

if useCurrentLine {
return b.GetLine()
}

Expand Down

0 comments on commit 589e427

Please sign in to comment.