Skip to content

Commit

Permalink
implemted new field parser functions, updated record parse, format fu…
Browse files Browse the repository at this point in the history
…nctions
  • Loading branch information
mfdeveloper508 committed Jun 26, 2023
1 parent dd7dc70 commit a3212b2
Show file tree
Hide file tree
Showing 114 changed files with 848 additions and 721 deletions.
4 changes: 2 additions & 2 deletions accountCreditedDrawdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (creditDD *AccountCreditedDrawdown) Parse(record string) error {
creditDD.tag = record[:6]
length := 6

value, read, err := creditDD.parseVariableStringField(record[length:], 9)
value, read, err := creditDD.parseFixedStringField(record[length:], 9)
if err != nil {
return fieldError("DrawdownCreditAccountNumber", err)
}
Expand Down Expand Up @@ -83,7 +83,7 @@ func (creditDD *AccountCreditedDrawdown) Format(options FormatOptions) string {
var buf strings.Builder
buf.Grow(15)
buf.WriteString(creditDD.tag)
buf.WriteString(creditDD.FormatCreditAccountNumber(options))
buf.WriteString(creditDD.DrawdownCreditAccountNumberField())
return buf.String()
}

Expand Down
16 changes: 12 additions & 4 deletions accountCreditedDrawdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,28 @@ func TestStringAccountCreditedDrawdownVariableLength(t *testing.T) {
r.line = line

err = r.parseAccountCreditedDrawdown()
expected = r.parseError(fieldError("DrawdownCreditAccountNumber", ErrFieldRequired)).Error()
expected = r.parseError(fieldError("DrawdownCreditAccountNumber", ErrValidLength)).Error()
require.EqualError(t, err, expected)

line = "{5400}1*"
r = NewReader(strings.NewReader(line))
r.line = line

err = r.parseAccountCreditedDrawdown()
require.Equal(t, err, nil)
expected = r.parseError(fieldError("DrawdownCreditAccountNumber", ErrValidLength)).Error()
require.EqualError(t, err, expected)

line = "{5400}1 *"
r = NewReader(strings.NewReader(line))
r.line = line

err = r.parseAccountCreditedDrawdown()
require.NoError(t, err)
}

// TestStringAccountCreditedDrawdownOptions validates Format() formatted according to the FormatOptions
func TestStringAccountCreditedDrawdownOptions(t *testing.T) {
var line = "{5400}1*"
var line = "{5400}1 "
r := NewReader(strings.NewReader(line))
r.line = line

Expand All @@ -124,6 +132,6 @@ func TestStringAccountCreditedDrawdownOptions(t *testing.T) {

acd := r.currentFEDWireMessage.AccountCreditedDrawdown
require.Equal(t, acd.String(), "{5400}1 ")
require.Equal(t, acd.Format(FormatOptions{VariableLengthFields: true}), "{5400}1*")
require.Equal(t, acd.Format(FormatOptions{VariableLengthFields: true}), "{5400}1 ")
require.Equal(t, acd.String(), acd.Format(FormatOptions{VariableLengthFields: false}))
}
10 changes: 5 additions & 5 deletions accountDebitedDrawdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ func (debitDD *AccountDebitedDrawdown) Format(options FormatOptions) string {

buf.WriteString(debitDD.tag)
buf.WriteString(debitDD.IdentificationCodeField())
buf.WriteString(debitDD.FormatIdentifier(options))
buf.WriteString(debitDD.FormatName(options))
buf.WriteString(debitDD.FormatAddressLineOne(options))
buf.WriteString(debitDD.FormatAddressLineTwo(options))
buf.WriteString(debitDD.FormatAddressLineThree(options))
buf.WriteString(debitDD.FormatIdentifier(options) + Delimiter)
buf.WriteString(debitDD.FormatName(options) + Delimiter)
buf.WriteString(debitDD.FormatAddressLineOne(options) + Delimiter)
buf.WriteString(debitDD.FormatAddressLineTwo(options) + Delimiter)
buf.WriteString(debitDD.FormatAddressLineThree(options) + Delimiter)

if options.VariableLengthFields {
return debitDD.stripDelimiters(buf.String())
Expand Down
13 changes: 6 additions & 7 deletions accountDebitedDrawdown_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package wire

import (
"errors"
"strings"
"testing"

Expand Down Expand Up @@ -129,18 +128,18 @@ func TestIdentificationCodeBogus(t *testing.T) {

// TestParseAccountDebitedDrawdownWrongLength parses a wrong AccountDebitedDrawdown record length
func TestParseAccountDebitedDrawdownWrongLength(t *testing.T) {
var line = "{4400}D123456789 debitDD Name Address One Address Two Address Three "
var line = "{4400}D123456789 *debitDD Name *Address One *Address Two *Address Three "
r := NewReader(strings.NewReader(line))
r.line = line

err := r.parseAccountDebitedDrawdown()

require.EqualError(t, err, r.parseError(fieldError("AddressLineThree", ErrValidLength)).Error())
require.EqualError(t, err, r.parseError(fieldError("AddressLineThree", ErrRequireDelimiter)).Error())
}

// TestParseAccountDebitedDrawdownReaderParseError parses a wrong AccountDebitedDrawdown reader parse error
func TestParseAccountDebitedDrawdownReaderParseError(t *testing.T) {
var line = "{4400}D123456789 debitDD ®ame Address One Address Two Address Three "
var line = "{4400}D123456789 *debitDD ®ame *Address One *Address Two *Address Three *"
r := NewReader(strings.NewReader(line))
r.line = line

Expand Down Expand Up @@ -175,12 +174,12 @@ func TestStringAccountDebitedDrawdownVariableLength(t *testing.T) {
expected := r.parseError(NewTagMinLengthErr(9, len(r.line))).Error()
require.EqualError(t, err, expected)

line = "{4400}D123456789 debitDD Name Address One Address Two Address Three NNNN"
line = "{4400}D123456789 *debitDD Name *Address One *Address Two *Address Three NNNNNNNN*"
r = NewReader(strings.NewReader(line))
r.line = line

err = r.parseAccountDebitedDrawdown()
require.ErrorContains(t, err, r.parseError(NewTagMaxLengthErr(errors.New(""))).Error())
require.NoError(t, err)

line = "{4400}***"
r = NewReader(strings.NewReader(line))
Expand Down Expand Up @@ -208,7 +207,7 @@ func TestStringAccountDebitedDrawdownOptions(t *testing.T) {
require.Equal(t, err, nil)

add := r.currentFEDWireMessage.AccountDebitedDrawdown
require.Equal(t, add.String(), "{4400}D2 3 ")
require.Equal(t, add.String(), "{4400}D2 *3 * * * *")
require.Equal(t, add.Format(FormatOptions{VariableLengthFields: true}), "{4400}D2*3*")
require.Equal(t, add.String(), add.Format(FormatOptions{VariableLengthFields: false}))
}
6 changes: 3 additions & 3 deletions actualAmountPaid.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (aap *ActualAmountPaid) Parse(record string) error {
aap.tag = record[:6]
length := 6

value, read, err := aap.parseVariableStringField(record[length:], 3)
value, read, err := aap.parseFixedStringField(record[length:], 3)
if err != nil {
return fieldError("CurrencyCode", err)
}
Expand Down Expand Up @@ -91,8 +91,8 @@ func (aap *ActualAmountPaid) Format(options FormatOptions) string {
buf.Grow(28)

buf.WriteString(aap.tag)
buf.WriteString(aap.FormatCurrencyCode(options))
buf.WriteString(aap.FormatAmount(options))
buf.WriteString(aap.CurrencyCodeField())
buf.WriteString(aap.FormatAmount(options) + Delimiter)

return buf.String()
}
Expand Down
18 changes: 8 additions & 10 deletions actualAmountPaid_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package wire

import (
"errors"
"strings"
"testing"

Expand Down Expand Up @@ -65,18 +64,17 @@ func TestActualAmountPaidCurrencyCodeValid(t *testing.T) {

// TestParseActualAmountPaidWrongLength parses a wrong ActualAmountPaid record length
func TestParseActualAmountPaidWrongLength(t *testing.T) {
var line = "{8450}USD1234.56 "
var line = "{8450}USD1234.56 *"
r := NewReader(strings.NewReader(line))
r.line = line

err := r.parseActualAmountPaid()

require.EqualError(t, err, r.parseError(fieldError("Amount", ErrValidLength)).Error())
require.NoError(t, err)
}

// TestParseActualAmountPaidReaderParseError parses a wrong ActualAmountPaid reader parse error
func TestParseActualAmountPaidReaderParseError(t *testing.T) {
var line = "{8450}USD1234.56Z "
var line = "{8450}USD1234.56Z *"
r := NewReader(strings.NewReader(line))
r.line = line

Expand Down Expand Up @@ -111,21 +109,21 @@ func TestStringActualAmountPaidVariableLength(t *testing.T) {
expected := r.parseError(NewTagMinLengthErr(8, len(r.line))).Error()
require.EqualError(t, err, expected)

line = "{8450}USD1234.56 NNN"
line = "{8450}USD1234.56 NNN*"
r = NewReader(strings.NewReader(line))
r.line = line

err = r.parseActualAmountPaid()
require.ErrorContains(t, err, r.parseError(NewTagMaxLengthErr(errors.New(""))).Error())
require.ErrorContains(t, err, ErrNonAmount.Error())

line = "{8450}****"
r = NewReader(strings.NewReader(line))
r.line = line

err = r.parseActualAmountPaid()
require.ErrorContains(t, err, r.parseError(NewTagMaxLengthErr(errors.New(""))).Error())
require.ErrorContains(t, err, ErrValidLength.Error())

line = "{8450}**"
line = "{8450}USD*"
r = NewReader(strings.NewReader(line))
r.line = line

Expand All @@ -151,7 +149,7 @@ func TestStringActualAmountPaidOptions(t *testing.T) {
require.Equal(t, err, nil)

aap := r.currentFEDWireMessage.ActualAmountPaid
require.Equal(t, aap.String(), "{8450}USD1234.56 ")
require.Equal(t, aap.String(), "{8450}USD1234.56 *")
require.Equal(t, aap.Format(FormatOptions{VariableLengthFields: true}), "{8450}USD1234.56*")
require.Equal(t, aap.String(), aap.Format(FormatOptions{VariableLengthFields: false}))
}
18 changes: 9 additions & 9 deletions adjustment.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ func (adj *Adjustment) Parse(record string) error {
adj.tag = record[:6]
length := 6

value, read, err := adj.parseVariableStringField(record[length:], 2)
value, read, err := adj.parseFixedStringField(record[length:], 2)
if err != nil {
return fieldError("AdjustmentReasonCode", err)
}
adj.AdjustmentReasonCode = value
length += read

value, read, err = adj.parseVariableStringField(record[length:], 4)
value, read, err = adj.parseFixedStringField(record[length:], 4)
if err != nil {
return fieldError("CreditDebitIndicator", err)
}
adj.CreditDebitIndicator = value
length += read

value, read, err = adj.parseVariableStringField(record[length:], 3)
value, read, err = adj.parseFixedStringField(record[length:], 3)
if err != nil {
return fieldError("CurrencyCode", err)
}
Expand All @@ -72,7 +72,7 @@ func (adj *Adjustment) Parse(record string) error {

value, read, err = adj.parseVariableStringField(record[length:], 19)
if err != nil {
return fieldError("CurrencyCode", err)
return fieldError("Amount", err)
}
adj.RemittanceAmount.Amount = value
length += read
Expand Down Expand Up @@ -118,11 +118,11 @@ func (adj *Adjustment) Format(options FormatOptions) string {
buf.Grow(168)

buf.WriteString(adj.tag)
buf.WriteString(adj.FormatAdjustmentReasonCode(options))
buf.WriteString(adj.FormatCreditDebitIndicator(options))
buf.WriteString(adj.FormatCurrencyCode(options))
buf.WriteString(adj.FormatAmount(options))
buf.WriteString(adj.FormatAdditionalInfo(options))
buf.WriteString(adj.AdjustmentReasonCodeField())
buf.WriteString(adj.CreditDebitIndicatorField())
buf.WriteString(adj.CurrencyCodeField())
buf.WriteString(adj.FormatAmount(options) + Delimiter)
buf.WriteString(adj.FormatAdditionalInfo(options) + Delimiter)

if options.VariableLengthFields {
return adj.stripDelimiters(buf.String())
Expand Down
11 changes: 5 additions & 6 deletions adjustment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,17 @@ func TestAdjustmentCurrencyCodeRequired(t *testing.T) {

// TestParseAdjustmentWrongLength parses a wrong Adjustment record length
func TestParseAdjustmentWrongLength(t *testing.T) {
var line = "{8600}01CRDTUSD1234.56Z Adjustment Additional Information "
var line = "{8600}01CRDTUSD1234.56 *Additional Information *"
r := NewReader(strings.NewReader(line))
r.line = line

err := r.parseAdjustment()

require.EqualError(t, err, r.parseError(fieldError("AdditionalInfo", ErrValidLength)).Error())
require.NoError(t, err)
}

// TestParseAdjustmentReaderParseError parses a wrong Adjustment reader parse error
func TestParseAdjustmentReaderParseError(t *testing.T) {
var line = "{8600}01CRDTUSD1234.56Z Adjustment Additional Information "
var line = "{8600}01CRDTUSD1234.56Z *Additional Information *"
r := NewReader(strings.NewReader(line))
r.line = line

Expand Down Expand Up @@ -159,7 +158,7 @@ func TestStringAdjustmentVariableLength(t *testing.T) {
r.line = line

err = r.parseAdjustment()
require.ErrorContains(t, err, r.parseError(NewTagMaxLengthErr(errors.New(""))).Error())
require.ErrorContains(t, err, ErrRequireDelimiter.Error())

line = "{8600}01CRDTUSD1234.56****"
r = NewReader(strings.NewReader(line))
Expand All @@ -186,7 +185,7 @@ func TestStringAdjustmentOptions(t *testing.T) {
require.Equal(t, err, nil)

adj := r.currentFEDWireMessage.Adjustment
require.Equal(t, adj.String(), "{8600}01CRDTUSD1234.56 ")
require.Equal(t, adj.String(), "{8600}01CRDTUSD1234.56 * *")
require.Equal(t, adj.Format(FormatOptions{VariableLengthFields: true}), "{8600}01CRDTUSD1234.56*")
require.Equal(t, adj.String(), adj.Format(FormatOptions{VariableLengthFields: false}))
}
6 changes: 3 additions & 3 deletions amountNegotiatedDiscount.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (nd *AmountNegotiatedDiscount) Parse(record string) error {
nd.tag = record[:6]
length := 6

value, read, err := nd.parseVariableStringField(record[length:], 3)
value, read, err := nd.parseFixedStringField(record[length:], 3)
if err != nil {
return fieldError("CurrencyCode", err)
}
Expand Down Expand Up @@ -91,8 +91,8 @@ func (nd *AmountNegotiatedDiscount) Format(options FormatOptions) string {
buf.Grow(28)

buf.WriteString(nd.tag)
buf.WriteString(nd.FormatCurrencyCode(options))
buf.WriteString(nd.FormatAmount(options))
buf.WriteString(nd.CurrencyCodeField())
buf.WriteString(nd.FormatAmount(options) + Delimiter)

return buf.String()
}
Expand Down
8 changes: 4 additions & 4 deletions amountNegotiatedDiscount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ func TestParseAmountNegotiatedDiscountWrongLength(t *testing.T) {

err := r.parseAmountNegotiatedDiscount()

require.EqualError(t, err, r.parseError(fieldError("Amount", ErrValidLength)).Error())
require.EqualError(t, err, r.parseError(fieldError("Amount", ErrRequireDelimiter)).Error())
}

// TestParseAmountNegotiatedDiscountReaderParseError parses a wrong AmountNegotiatedDiscount reader parse error
func TestParseAmountNegotiatedDiscountReaderParseError(t *testing.T) {
var line = "{8550}USD1234.56Z "
var line = "{8550}USD1234.56Z *"
r := NewReader(strings.NewReader(line))
r.line = line

Expand Down Expand Up @@ -116,7 +116,7 @@ func TestStringAmountNegotiatedDiscountVariableLength(t *testing.T) {
r.line = line

err = r.parseAmountNegotiatedDiscount()
require.ErrorContains(t, err, r.parseError(NewTagMaxLengthErr(errors.New(""))).Error())
require.ErrorContains(t, err, ErrRequireDelimiter.Error())

line = "{8550}USD1234.56***"
r = NewReader(strings.NewReader(line))
Expand All @@ -143,7 +143,7 @@ func TestStringAmountNegotiatedDiscountOptions(t *testing.T) {
require.Equal(t, err, nil)

and := r.currentFEDWireMessage.AmountNegotiatedDiscount
require.Equal(t, and.String(), "{8550}USD1234.56 ")
require.Equal(t, and.String(), "{8550}USD1234.56 *")
require.Equal(t, and.Format(FormatOptions{VariableLengthFields: true}), "{8550}USD1234.56*")
require.Equal(t, and.String(), and.Format(FormatOptions{VariableLengthFields: false}))
}
10 changes: 5 additions & 5 deletions beneficiary.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ func (ben *Beneficiary) Format(options FormatOptions) string {

buf.WriteString(ben.tag)
buf.WriteString(ben.IdentificationCodeField())
buf.WriteString(ben.FormatIdentifier(options))
buf.WriteString(ben.FormatName(options))
buf.WriteString(ben.FormatAddressLineOne(options))
buf.WriteString(ben.FormatAddressLineTwo(options))
buf.WriteString(ben.FormatAddressLineThree(options))
buf.WriteString(ben.FormatIdentifier(options) + Delimiter)
buf.WriteString(ben.FormatName(options) + Delimiter)
buf.WriteString(ben.FormatAddressLineOne(options) + Delimiter)
buf.WriteString(ben.FormatAddressLineTwo(options) + Delimiter)
buf.WriteString(ben.FormatAddressLineThree(options) + Delimiter)

if options.VariableLengthFields {
return ben.stripDelimiters(buf.String())
Expand Down
12 changes: 6 additions & 6 deletions beneficiaryCustomer.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ func (bc *BeneficiaryCustomer) Format(options FormatOptions) string {
buf.Grow(186)

buf.WriteString(bc.tag)
buf.WriteString(bc.FormatSwiftFieldTag(options))
buf.WriteString(bc.FormatSwiftLineOne(options))
buf.WriteString(bc.FormatSwiftLineTwo(options))
buf.WriteString(bc.FormatSwiftLineThree(options))
buf.WriteString(bc.FormatSwiftLineFour(options))
buf.WriteString(bc.FormatSwiftLineFive(options))
buf.WriteString(bc.FormatSwiftFieldTag(options) + Delimiter)
buf.WriteString(bc.FormatSwiftLineOne(options) + Delimiter)
buf.WriteString(bc.FormatSwiftLineTwo(options) + Delimiter)
buf.WriteString(bc.FormatSwiftLineThree(options) + Delimiter)
buf.WriteString(bc.FormatSwiftLineFour(options) + Delimiter)
buf.WriteString(bc.FormatSwiftLineFive(options) + Delimiter)

if options.VariableLengthFields {
return bc.stripDelimiters(buf.String())
Expand Down
Loading

0 comments on commit a3212b2

Please sign in to comment.