diff --git a/actualAmountPaid_test.go b/actualAmountPaid_test.go index 51145415..8290661b 100644 --- a/actualAmountPaid_test.go +++ b/actualAmountPaid_test.go @@ -1,7 +1,6 @@ package wire import ( - "fmt" "strings" "testing" @@ -148,7 +147,6 @@ func TestStringActualAmountPaidOptions(t *testing.T) { r.line = line err := r.parseActualAmountPaid() - fmt.Println(err) require.Equal(t, err, nil) str := r.currentFEDWireMessage.ActualAmountPaid.String() diff --git a/adjustment.go b/adjustment.go index a3084ec7..56f72a69 100644 --- a/adjustment.go +++ b/adjustment.go @@ -42,15 +42,45 @@ func NewAdjustment() *Adjustment { // Parse provides no guarantee about all fields being filled in. Callers should make a Validate() call to confirm // successful parsing and data validity. func (adj *Adjustment) Parse(record string) error { - if utf8.RuneCountInString(record) != 174 { - return NewTagWrongLengthErr(174, len(record)) + if utf8.RuneCountInString(record) < 10 { + return NewTagMinLengthErr(10, len(record)) } + adj.tag = record[:6] - adj.AdjustmentReasonCode = adj.parseStringField(record[6:8]) - adj.CreditDebitIndicator = adj.parseStringField(record[8:12]) - adj.RemittanceAmount.CurrencyCode = adj.parseStringField(record[12:15]) - adj.RemittanceAmount.Amount = adj.parseStringField(record[15:34]) - adj.AdditionalInfo = adj.parseStringField(record[34:174]) + + var err error + length := 6 + read := 0 + + if adj.AdjustmentReasonCode, read, err = adj.parseVariableStringField(record[length:], 2); err != nil { + return fieldError("AdjustmentReasonCode", err) + } + length += read + + if adj.CreditDebitIndicator, read, err = adj.parseVariableStringField(record[length:], 4); err != nil { + return fieldError("CreditDebitIndicator", err) + } + length += read + + if adj.RemittanceAmount.CurrencyCode, read, err = adj.parseVariableStringField(record[length:], 3); err != nil { + return fieldError("CurrencyCode", err) + } + length += read + + if adj.RemittanceAmount.Amount, read, err = adj.parseVariableStringField(record[length:], 19); err != nil { + return fieldError("Amount", err) + } + length += read + + if adj.AdditionalInfo, read, err = adj.parseVariableStringField(record[length:], 140); err != nil { + return fieldError("AdditionalInfo", err) + } + length += read + + if len(record) != length { + return NewTagMaxLengthErr() + } + return nil } @@ -69,16 +99,22 @@ func (adj *Adjustment) UnmarshalJSON(data []byte) error { } // String writes Adjustment -func (adj *Adjustment) String() string { +func (adj *Adjustment) String(options ...bool) string { var buf strings.Builder buf.Grow(168) + buf.WriteString(adj.tag) - buf.WriteString(adj.AdjustmentReasonCodeField()) - buf.WriteString(adj.CreditDebitIndicatorField()) - buf.WriteString(adj.CurrencyCodeField()) - buf.WriteString(adj.AmountField()) - buf.WriteString(adj.AdditionalInfoField()) - return buf.String() + buf.WriteString(adj.AdjustmentReasonCodeField(options...)) + buf.WriteString(adj.CreditDebitIndicatorField(options...)) + buf.WriteString(adj.CurrencyCodeField(options...)) + buf.WriteString(adj.AmountField(options...)) + buf.WriteString(adj.AdditionalInfoField(options...)) + + if adj.parseFirstOption(options) { + return adj.stripDelimiters(buf.String()) + } else { + return buf.String() + } } // Validate performs WIRE format rule checks on Adjustment and returns an error if not Validated @@ -125,26 +161,26 @@ func (adj *Adjustment) fieldInclusion() error { } // AdjustmentReasonCodeField gets a string of the AdjustmentReasonCode field -func (adj *Adjustment) AdjustmentReasonCodeField() string { - return adj.alphaField(adj.AdjustmentReasonCode, 2) +func (adj *Adjustment) AdjustmentReasonCodeField(options ...bool) string { + return adj.alphaVariableField(adj.AdjustmentReasonCode, 2, adj.parseFirstOption(options)) } // CreditDebitIndicatorField gets a string of the CreditDebitIndicator field -func (adj *Adjustment) CreditDebitIndicatorField() string { - return adj.alphaField(adj.CreditDebitIndicator, 4) +func (adj *Adjustment) CreditDebitIndicatorField(options ...bool) string { + return adj.alphaVariableField(adj.CreditDebitIndicator, 4, adj.parseFirstOption(options)) } // CurrencyCodeField gets a string of the CurrencyCode field -func (adj *Adjustment) CurrencyCodeField() string { - return adj.alphaField(adj.RemittanceAmount.CurrencyCode, 3) +func (adj *Adjustment) CurrencyCodeField(options ...bool) string { + return adj.alphaVariableField(adj.RemittanceAmount.CurrencyCode, 3, adj.parseFirstOption(options)) } // AmountField gets a string of the Amount field -func (adj *Adjustment) AmountField() string { - return adj.alphaField(adj.RemittanceAmount.Amount, 19) +func (adj *Adjustment) AmountField(options ...bool) string { + return adj.alphaVariableField(adj.RemittanceAmount.Amount, 19, adj.parseFirstOption(options)) } // AdditionalInfoField gets a string of the AdditionalInfo field -func (adj *Adjustment) AdditionalInfoField() string { - return adj.alphaField(adj.AdditionalInfo, 140) +func (adj *Adjustment) AdditionalInfoField(options ...bool) string { + return adj.alphaVariableField(adj.AdditionalInfo, 140, adj.parseFirstOption(options)) } diff --git a/adjustment_test.go b/adjustment_test.go index 541f9855..cedadd78 100644 --- a/adjustment_test.go +++ b/adjustment_test.go @@ -113,7 +113,7 @@ func TestParseAdjustmentWrongLength(t *testing.T) { err := r.parseAdjustment() - require.EqualError(t, err, r.parseError(NewTagWrongLengthErr(174, len(r.line))).Error()) + require.EqualError(t, err, r.parseError(fieldError("AdditionalInfo", ErrValidLengthSize)).Error()) } // TestParseAdjustmentReaderParseError parses a wrong Adjustment reader parse error @@ -142,3 +142,51 @@ func TestAdjustmentTagError(t *testing.T) { require.EqualError(t, err, fieldError("tag", ErrValidTagForType, adj.tag).Error()) } + +// TestStringAdjustmentVariableLength parses using variable length +func TestStringAdjustmentVariableLength(t *testing.T) { + var line = "{8600}" + r := NewReader(strings.NewReader(line)) + r.line = line + + err := r.parseAdjustment() + expected := r.parseError(NewTagMinLengthErr(10, len(r.line))).Error() + require.EqualError(t, err, expected) + + line = "{8600}01CRDTUSD1234.56 NNN" + r = NewReader(strings.NewReader(line)) + r.line = line + + err = r.parseAdjustment() + require.EqualError(t, err, r.parseError(NewTagMaxLengthErr()).Error()) + + line = "{8600}01CRDTUSD1234.56***" + r = NewReader(strings.NewReader(line)) + r.line = line + + err = r.parseAdjustment() + require.EqualError(t, err, r.parseError(NewTagMaxLengthErr()).Error()) + + line = "{8600}01CRDTUSD1234.56*" + r = NewReader(strings.NewReader(line)) + r.line = line + + err = r.parseAdjustment() + require.Equal(t, err, nil) +} + +// TestStringAdjustmentOptions validates string() with options +func TestStringAdjustmentOptions(t *testing.T) { + var line = "{8600}01CRDTUSD1234.56 *" + r := NewReader(strings.NewReader(line)) + r.line = line + + err := r.parseAdjustment() + require.Equal(t, err, nil) + + str := r.currentFEDWireMessage.Adjustment.String() + require.Equal(t, str, "{8600}01CRDTUSD1234.56 ") + + str = r.currentFEDWireMessage.Adjustment.String(true) + require.Equal(t, str, "{8600}01CRDTUSD1234.56*") +} diff --git a/amountNegotiatedDiscount.go b/amountNegotiatedDiscount.go index d004f088..3eb1fe7f 100644 --- a/amountNegotiatedDiscount.go +++ b/amountNegotiatedDiscount.go @@ -36,12 +36,30 @@ func NewAmountNegotiatedDiscount() *AmountNegotiatedDiscount { // Parse provides no guarantee about all fields being filled in. Callers should make a Validate() call to confirm // successful parsing and data validity. func (nd *AmountNegotiatedDiscount) Parse(record string) error { - if utf8.RuneCountInString(record) != 28 { - return NewTagWrongLengthErr(28, len(record)) + if utf8.RuneCountInString(record) < 8 { + return NewTagMinLengthErr(8, len(record)) } + nd.tag = record[:6] - nd.RemittanceAmount.CurrencyCode = nd.parseStringField(record[6:9]) - nd.RemittanceAmount.Amount = nd.parseStringField(record[9:28]) + + var err error + length := 6 + read := 0 + + if nd.RemittanceAmount.CurrencyCode, read, err = nd.parseVariableStringField(record[length:], 3); err != nil { + return fieldError("CurrencyCode", err) + } + length += read + + if nd.RemittanceAmount.Amount, read, err = nd.parseVariableStringField(record[length:], 19); err != nil { + return fieldError("Amount", err) + } + length += read + + if len(record) != length { + return NewTagMaxLengthErr() + } + return nil } @@ -60,12 +78,14 @@ func (nd *AmountNegotiatedDiscount) UnmarshalJSON(data []byte) error { } // String writes AmountNegotiatedDiscount -func (nd *AmountNegotiatedDiscount) String() string { +func (nd *AmountNegotiatedDiscount) String(options ...bool) string { var buf strings.Builder buf.Grow(28) + buf.WriteString(nd.tag) - buf.WriteString(nd.CurrencyCodeField()) - buf.WriteString(nd.AmountField()) + buf.WriteString(nd.CurrencyCodeField(options...)) + buf.WriteString(nd.AmountField(options...)) + return buf.String() } @@ -100,11 +120,11 @@ func (nd *AmountNegotiatedDiscount) fieldInclusion() error { } // CurrencyCodeField gets a string of the CurrencyCode field -func (nd *AmountNegotiatedDiscount) CurrencyCodeField() string { - return nd.alphaField(nd.RemittanceAmount.CurrencyCode, 3) +func (nd *AmountNegotiatedDiscount) CurrencyCodeField(options ...bool) string { + return nd.alphaVariableField(nd.RemittanceAmount.CurrencyCode, 3, nd.parseFirstOption(options)) } // AmountField gets a string of the Amount field -func (nd *AmountNegotiatedDiscount) AmountField() string { - return nd.alphaField(nd.RemittanceAmount.Amount, 19) +func (nd *AmountNegotiatedDiscount) AmountField(options ...bool) string { + return nd.alphaVariableField(nd.RemittanceAmount.Amount, 19, nd.parseFirstOption(options)) } diff --git a/amountNegotiatedDiscount_test.go b/amountNegotiatedDiscount_test.go index 2261c3ba..dc655225 100644 --- a/amountNegotiatedDiscount_test.go +++ b/amountNegotiatedDiscount_test.go @@ -70,7 +70,7 @@ func TestParseAmountNegotiatedDiscountWrongLength(t *testing.T) { err := r.parseAmountNegotiatedDiscount() - require.EqualError(t, err, r.parseError(NewTagWrongLengthErr(28, len(r.line))).Error()) + require.EqualError(t, err, r.parseError(fieldError("Amount", ErrValidLengthSize)).Error()) } // TestParseAmountNegotiatedDiscountReaderParseError parses a wrong AmountNegotiatedDiscount reader parse error @@ -99,3 +99,51 @@ func TestAmountNegotiatedDiscountTagError(t *testing.T) { require.EqualError(t, err, fieldError("tag", ErrValidTagForType, nd.tag).Error()) } + +// TestStringAmountNegotiatedDiscountVariableLength parses using variable length +func TestStringAmountNegotiatedDiscountVariableLength(t *testing.T) { + var line = "{8600}" + r := NewReader(strings.NewReader(line)) + r.line = line + + err := r.parseAmountNegotiatedDiscount() + expected := r.parseError(NewTagMinLengthErr(8, len(r.line))).Error() + require.EqualError(t, err, expected) + + line = "{8550}USD1234.56 NNN" + r = NewReader(strings.NewReader(line)) + r.line = line + + err = r.parseAmountNegotiatedDiscount() + require.EqualError(t, err, r.parseError(NewTagMaxLengthErr()).Error()) + + line = "{8550}USD1234.56**" + r = NewReader(strings.NewReader(line)) + r.line = line + + err = r.parseAmountNegotiatedDiscount() + require.EqualError(t, err, r.parseError(NewTagMaxLengthErr()).Error()) + + line = "{8550}USD1234.56*" + r = NewReader(strings.NewReader(line)) + r.line = line + + err = r.parseAmountNegotiatedDiscount() + require.Equal(t, err, nil) +} + +// TestStringAmountNegotiatedDiscountOptions validates string() with options +func TestStringAmountNegotiatedDiscountOptions(t *testing.T) { + var line = "{8550}USD1234.56*" + r := NewReader(strings.NewReader(line)) + r.line = line + + err := r.parseAmountNegotiatedDiscount() + require.Equal(t, err, nil) + + str := r.currentFEDWireMessage.AmountNegotiatedDiscount.String() + require.Equal(t, str, "{8550}USD1234.56 ") + + str = r.currentFEDWireMessage.AmountNegotiatedDiscount.String(true) + require.Equal(t, str, "{8550}USD1234.56*") +} diff --git a/beneficiary.go b/beneficiary.go index 0252dded..954dbf40 100644 --- a/beneficiary.go +++ b/beneficiary.go @@ -36,16 +36,46 @@ func NewBeneficiary() *Beneficiary { // Parse provides no guarantee about all fields being filled in. Callers should make a Validate() call to confirm // successful parsing and data validity. func (ben *Beneficiary) Parse(record string) error { - if utf8.RuneCountInString(record) != 181 { - return NewTagWrongLengthErr(181, len(record)) + if utf8.RuneCountInString(record) < 7 { + return NewTagMinLengthErr(7, len(record)) } + ben.tag = record[:6] ben.Personal.IdentificationCode = ben.parseStringField(record[6:7]) - ben.Personal.Identifier = ben.parseStringField(record[7:41]) - ben.Personal.Name = ben.parseStringField(record[41:76]) - ben.Personal.Address.AddressLineOne = ben.parseStringField(record[76:111]) - ben.Personal.Address.AddressLineTwo = ben.parseStringField(record[111:146]) - ben.Personal.Address.AddressLineThree = ben.parseStringField(record[146:181]) + + var err error + length := 7 + read := 0 + + if ben.Personal.Identifier, read, err = ben.parseVariableStringField(record[length:], 34); err != nil { + return fieldError("Identifier", err) + } + length += read + + if ben.Personal.Name, read, err = ben.parseVariableStringField(record[length:], 35); err != nil { + return fieldError("Name", err) + } + length += read + + if ben.Personal.Address.AddressLineOne, read, err = ben.parseVariableStringField(record[length:], 35); err != nil { + return fieldError("AddressLineOne", err) + } + length += read + + if ben.Personal.Address.AddressLineTwo, read, err = ben.parseVariableStringField(record[length:], 35); err != nil { + return fieldError("AddressLineTwo", err) + } + length += read + + if ben.Personal.Address.AddressLineThree, read, err = ben.parseVariableStringField(record[length:], 35); err != nil { + return fieldError("AddressLineThree", err) + } + length += read + + if len(record) != length { + return NewTagMaxLengthErr() + } + return nil } @@ -64,17 +94,23 @@ func (ben *Beneficiary) UnmarshalJSON(data []byte) error { } // String writes Beneficiary -func (ben *Beneficiary) String() string { +func (ben *Beneficiary) String(options ...bool) string { var buf strings.Builder buf.Grow(181) + buf.WriteString(ben.tag) buf.WriteString(ben.IdentificationCodeField()) - buf.WriteString(ben.IdentifierField()) - buf.WriteString(ben.NameField()) - buf.WriteString(ben.AddressLineOneField()) - buf.WriteString(ben.AddressLineTwoField()) - buf.WriteString(ben.AddressLineThreeField()) - return buf.String() + buf.WriteString(ben.IdentifierField(options...)) + buf.WriteString(ben.NameField(options...)) + buf.WriteString(ben.AddressLineOneField(options...)) + buf.WriteString(ben.AddressLineTwoField(options...)) + buf.WriteString(ben.AddressLineThreeField(options...)) + + if ben.parseFirstOption(options) { + return ben.stripDelimiters(buf.String()) + } else { + return buf.String() + } } // Validate performs WIRE format rule checks on Beneficiary and returns an error if not Validated @@ -127,26 +163,26 @@ func (ben *Beneficiary) IdentificationCodeField() string { } // IdentifierField gets a string of the Identifier field -func (ben *Beneficiary) IdentifierField() string { - return ben.alphaField(ben.Personal.Identifier, 34) +func (ben *Beneficiary) IdentifierField(options ...bool) string { + return ben.alphaVariableField(ben.Personal.Identifier, 34, ben.parseFirstOption(options)) } // NameField gets a string of the Name field -func (ben *Beneficiary) NameField() string { - return ben.alphaField(ben.Personal.Name, 35) +func (ben *Beneficiary) NameField(options ...bool) string { + return ben.alphaVariableField(ben.Personal.Name, 35, ben.parseFirstOption(options)) } // AddressLineOneField gets a string of AddressLineOne field -func (ben *Beneficiary) AddressLineOneField() string { - return ben.alphaField(ben.Personal.Address.AddressLineOne, 35) +func (ben *Beneficiary) AddressLineOneField(options ...bool) string { + return ben.alphaVariableField(ben.Personal.Address.AddressLineOne, 35, ben.parseFirstOption(options)) } // AddressLineTwoField gets a string of AddressLineTwo field -func (ben *Beneficiary) AddressLineTwoField() string { - return ben.alphaField(ben.Personal.Address.AddressLineTwo, 35) +func (ben *Beneficiary) AddressLineTwoField(options ...bool) string { + return ben.alphaVariableField(ben.Personal.Address.AddressLineTwo, 35, ben.parseFirstOption(options)) } // AddressLineThreeField gets a string of AddressLineThree field -func (ben *Beneficiary) AddressLineThreeField() string { - return ben.alphaField(ben.Personal.Address.AddressLineThree, 35) +func (ben *Beneficiary) AddressLineThreeField(options ...bool) string { + return ben.alphaVariableField(ben.Personal.Address.AddressLineThree, 35, ben.parseFirstOption(options)) } diff --git a/beneficiaryCustomer.go b/beneficiaryCustomer.go index 08c5e597..91c29092 100644 --- a/beneficiaryCustomer.go +++ b/beneficiaryCustomer.go @@ -36,16 +36,50 @@ func NewBeneficiaryCustomer() *BeneficiaryCustomer { // Parse provides no guarantee about all fields being filled in. Callers should make a Validate() call to confirm // successful parsing and data validity. func (bc *BeneficiaryCustomer) Parse(record string) error { - if utf8.RuneCountInString(record) != 186 { - return NewTagWrongLengthErr(186, len(record)) + if utf8.RuneCountInString(record) < 6 { + return NewTagMinLengthErr(6, len(record)) } + bc.tag = record[:6] - bc.CoverPayment.SwiftFieldTag = bc.parseStringField(record[6:11]) - bc.CoverPayment.SwiftLineOne = bc.parseStringField(record[11:46]) - bc.CoverPayment.SwiftLineTwo = bc.parseStringField(record[46:81]) - bc.CoverPayment.SwiftLineThree = bc.parseStringField(record[81:116]) - bc.CoverPayment.SwiftLineFour = bc.parseStringField(record[116:151]) - bc.CoverPayment.SwiftLineFive = bc.parseStringField(record[151:186]) + + var err error + length := 6 + read := 0 + + if bc.CoverPayment.SwiftFieldTag, read, err = bc.parseVariableStringField(record[length:], 5); err != nil { + return fieldError("SwiftFieldTag", err) + } + length += read + + if bc.CoverPayment.SwiftLineOne, read, err = bc.parseVariableStringField(record[length:], 35); err != nil { + return fieldError("SwiftLineOne", err) + } + length += read + + if bc.CoverPayment.SwiftLineTwo, read, err = bc.parseVariableStringField(record[length:], 35); err != nil { + return fieldError("SwiftLineTwo", err) + } + length += read + + if bc.CoverPayment.SwiftLineThree, read, err = bc.parseVariableStringField(record[length:], 35); err != nil { + return fieldError("SwiftLineThree", err) + } + length += read + + if bc.CoverPayment.SwiftLineFour, read, err = bc.parseVariableStringField(record[length:], 35); err != nil { + return fieldError("SwiftLineFour", err) + } + length += read + + if bc.CoverPayment.SwiftLineFive, read, err = bc.parseVariableStringField(record[length:], 35); err != nil { + return fieldError("SwiftLineFive", err) + } + length += read + + if len(record) != length { + return NewTagMaxLengthErr() + } + return nil } @@ -64,17 +98,23 @@ func (bc *BeneficiaryCustomer) UnmarshalJSON(data []byte) error { } // String writes BeneficiaryCustomer -func (bc *BeneficiaryCustomer) String() string { +func (bc *BeneficiaryCustomer) String(options ...bool) string { var buf strings.Builder buf.Grow(186) + buf.WriteString(bc.tag) - buf.WriteString(bc.SwiftFieldTagField()) - buf.WriteString(bc.SwiftLineOneField()) - buf.WriteString(bc.SwiftLineTwoField()) - buf.WriteString(bc.SwiftLineThreeField()) - buf.WriteString(bc.SwiftLineFourField()) - buf.WriteString(bc.SwiftLineFiveField()) - return buf.String() + buf.WriteString(bc.SwiftFieldTagField(options...)) + buf.WriteString(bc.SwiftLineOneField(options...)) + buf.WriteString(bc.SwiftLineTwoField(options...)) + buf.WriteString(bc.SwiftLineThreeField(options...)) + buf.WriteString(bc.SwiftLineFourField(options...)) + buf.WriteString(bc.SwiftLineFiveField(options...)) + + if bc.parseFirstOption(options) { + return bc.stripDelimiters(buf.String()) + } else { + return buf.String() + } } // Validate performs WIRE format rule checks on BeneficiaryCustomer and returns an error if not Validated @@ -117,31 +157,31 @@ func (bc *BeneficiaryCustomer) fieldInclusion() error { } // SwiftFieldTagField gets a string of the SwiftFieldTag field -func (bc *BeneficiaryCustomer) SwiftFieldTagField() string { - return bc.alphaField(bc.CoverPayment.SwiftFieldTag, 5) +func (bc *BeneficiaryCustomer) SwiftFieldTagField(options ...bool) string { + return bc.alphaVariableField(bc.CoverPayment.SwiftFieldTag, 5, bc.parseFirstOption(options)) } // SwiftLineOneField gets a string of the SwiftLineOne field -func (bc *BeneficiaryCustomer) SwiftLineOneField() string { - return bc.alphaField(bc.CoverPayment.SwiftLineOne, 35) +func (bc *BeneficiaryCustomer) SwiftLineOneField(options ...bool) string { + return bc.alphaVariableField(bc.CoverPayment.SwiftLineOne, 35, bc.parseFirstOption(options)) } // SwiftLineTwoField gets a string of the SwiftLineTwo field -func (bc *BeneficiaryCustomer) SwiftLineTwoField() string { - return bc.alphaField(bc.CoverPayment.SwiftLineTwo, 35) +func (bc *BeneficiaryCustomer) SwiftLineTwoField(options ...bool) string { + return bc.alphaVariableField(bc.CoverPayment.SwiftLineTwo, 35, bc.parseFirstOption(options)) } // SwiftLineThreeField gets a string of the SwiftLineThree field -func (bc *BeneficiaryCustomer) SwiftLineThreeField() string { - return bc.alphaField(bc.CoverPayment.SwiftLineThree, 35) +func (bc *BeneficiaryCustomer) SwiftLineThreeField(options ...bool) string { + return bc.alphaVariableField(bc.CoverPayment.SwiftLineThree, 35, bc.parseFirstOption(options)) } // SwiftLineFourField gets a string of the SwiftLineFour field -func (bc *BeneficiaryCustomer) SwiftLineFourField() string { - return bc.alphaField(bc.CoverPayment.SwiftLineFour, 35) +func (bc *BeneficiaryCustomer) SwiftLineFourField(options ...bool) string { + return bc.alphaVariableField(bc.CoverPayment.SwiftLineFour, 35, bc.parseFirstOption(options)) } // SwiftLineFiveField gets a string of the SwiftLineFive field -func (bc *BeneficiaryCustomer) SwiftLineFiveField() string { - return bc.alphaField(bc.CoverPayment.SwiftLineFive, 35) +func (bc *BeneficiaryCustomer) SwiftLineFiveField(options ...bool) string { + return bc.alphaVariableField(bc.CoverPayment.SwiftLineFive, 35, bc.parseFirstOption(options)) } diff --git a/beneficiaryCustomer_test.go b/beneficiaryCustomer_test.go index 742bf2a5..49b9b016 100644 --- a/beneficiaryCustomer_test.go +++ b/beneficiaryCustomer_test.go @@ -104,12 +104,12 @@ func TestParseBeneficiaryCustomerWrongLength(t *testing.T) { err := r.parseBeneficiaryCustomer() - require.EqualError(t, err, r.parseError(NewTagWrongLengthErr(186, len(r.line))).Error()) + require.EqualError(t, err, r.parseError(fieldError("SwiftLineFive", ErrValidLengthSize)).Error()) } // TestParseBeneficiaryCustomerReaderParseError parses a wrong BeneficiaryCustomer reader parse error func TestParseBeneficiaryCustomerReaderParseError(t *testing.T) { - var line = "{7059}SwiftSwift ®ine One Swift Line Two Swift Line Three Swift Line Four Swift Line Five " + var line = "{7059}SwiftSwift ®ine One Swift Line Two Swift Line Three Swift Line Four Swift Line Five " r := NewReader(strings.NewReader(line)) r.line = line @@ -133,3 +133,50 @@ func TestBeneficiaryCustomerTagError(t *testing.T) { require.EqualError(t, err, fieldError("tag", ErrValidTagForType, bc.tag).Error()) } + +// TestStringBeneficiaryCustomerVariableLength parses using variable length +func TestStringBeneficiaryCustomerVariableLength(t *testing.T) { + var line = "{7059}" + r := NewReader(strings.NewReader(line)) + r.line = line + + err := r.parseBeneficiaryCustomer() + require.Nil(t, err) + + line = "{7059}SwiftSwift ®ine One Swift Line Two Swift Line Three Swift Line Four Swift Line Five NN" + r = NewReader(strings.NewReader(line)) + r.line = line + + err = r.parseBeneficiaryCustomer() + require.EqualError(t, err, r.parseError(NewTagMaxLengthErr()).Error()) + + line = "{7059}*******" + r = NewReader(strings.NewReader(line)) + r.line = line + + err = r.parseBeneficiaryCustomer() + require.EqualError(t, err, r.parseError(NewTagMaxLengthErr()).Error()) + + line = "{7059}******" + r = NewReader(strings.NewReader(line)) + r.line = line + + err = r.parseBeneficiaryCustomer() + require.Equal(t, err, nil) +} + +// TestStringBeneficiaryCustomerOptions validates string() with options +func TestStringBeneficiaryCustomerOptions(t *testing.T) { + var line = "{7059}*" + r := NewReader(strings.NewReader(line)) + r.line = line + + err := r.parseBeneficiaryCustomer() + require.Equal(t, err, nil) + + str := r.currentFEDWireMessage.BeneficiaryCustomer.String() + require.Equal(t, str, "{7059} ") + + str = r.currentFEDWireMessage.BeneficiaryCustomer.String(true) + require.Equal(t, str, "{7059}*") +} \ No newline at end of file diff --git a/beneficiaryFI.go b/beneficiaryFI.go index 4c6728dd..d2656059 100644 --- a/beneficiaryFI.go +++ b/beneficiaryFI.go @@ -36,16 +36,46 @@ func NewBeneficiaryFI() *BeneficiaryFI { // Parse provides no guarantee about all fields being filled in. Callers should make a Validate() call to confirm // successful parsing and data validity. func (bfi *BeneficiaryFI) Parse(record string) error { - if utf8.RuneCountInString(record) != 181 { - return NewTagWrongLengthErr(181, len(record)) + if utf8.RuneCountInString(record) < 7 { + return NewTagMinLengthErr(7, len(record)) } + bfi.tag = record[:6] bfi.FinancialInstitution.IdentificationCode = bfi.parseStringField(record[6:7]) - bfi.FinancialInstitution.Identifier = bfi.parseStringField(record[7:41]) - bfi.FinancialInstitution.Name = bfi.parseStringField(record[41:76]) - bfi.FinancialInstitution.Address.AddressLineOne = bfi.parseStringField(record[76:111]) - bfi.FinancialInstitution.Address.AddressLineTwo = bfi.parseStringField(record[111:146]) - bfi.FinancialInstitution.Address.AddressLineThree = bfi.parseStringField(record[146:181]) + + var err error + length := 7 + read := 0 + + if bfi.FinancialInstitution.Identifier, read, err = bfi.parseVariableStringField(record[length:], 34); err != nil { + return fieldError("Identifier", err) + } + length += read + + if bfi.FinancialInstitution.Name, read, err = bfi.parseVariableStringField(record[length:], 35); err != nil { + return fieldError("Name", err) + } + length += read + + if bfi.FinancialInstitution.Address.AddressLineOne, read, err = bfi.parseVariableStringField(record[length:], 35); err != nil { + return fieldError("AddressLineOne", err) + } + length += read + + if bfi.FinancialInstitution.Address.AddressLineTwo, read, err = bfi.parseVariableStringField(record[length:], 35); err != nil { + return fieldError("AddressLineTwo", err) + } + length += read + + if bfi.FinancialInstitution.Address.AddressLineThree, read, err = bfi.parseVariableStringField(record[length:], 35); err != nil { + return fieldError("AddressLineThree", err) + } + length += read + + if len(record) != length { + return NewTagMaxLengthErr() + } + return nil } @@ -64,17 +94,23 @@ func (bfi *BeneficiaryFI) UnmarshalJSON(data []byte) error { } // String writes BeneficiaryFI -func (bfi *BeneficiaryFI) String() string { +func (bfi *BeneficiaryFI) String(options ...bool) string { var buf strings.Builder buf.Grow(181) + buf.WriteString(bfi.tag) buf.WriteString(bfi.IdentificationCodeField()) - buf.WriteString(bfi.IdentifierField()) - buf.WriteString(bfi.NameField()) - buf.WriteString(bfi.AddressLineOneField()) - buf.WriteString(bfi.AddressLineTwoField()) - buf.WriteString(bfi.AddressLineThreeField()) - return buf.String() + buf.WriteString(bfi.IdentifierField(options...)) + buf.WriteString(bfi.NameField(options...)) + buf.WriteString(bfi.AddressLineOneField(options...)) + buf.WriteString(bfi.AddressLineTwoField(options...)) + buf.WriteString(bfi.AddressLineThreeField(options...)) + + if bfi.parseFirstOption(options) { + return bfi.stripDelimiters(buf.String()) + } else { + return buf.String() + } } // Validate performs WIRE format rule checks on BeneficiaryFI and returns an error if not Validated @@ -136,26 +172,26 @@ func (bfi *BeneficiaryFI) IdentificationCodeField() string { } // IdentifierField gets a string of the Identifier field -func (bfi *BeneficiaryFI) IdentifierField() string { - return bfi.alphaField(bfi.FinancialInstitution.Identifier, 34) +func (bfi *BeneficiaryFI) IdentifierField(options ...bool) string { + return bfi.alphaVariableField(bfi.FinancialInstitution.Identifier, 34, bfi.parseFirstOption(options)) } // NameField gets a string of the Name field -func (bfi *BeneficiaryFI) NameField() string { - return bfi.alphaField(bfi.FinancialInstitution.Name, 35) +func (bfi *BeneficiaryFI) NameField(options ...bool) string { + return bfi.alphaVariableField(bfi.FinancialInstitution.Name, 35, bfi.parseFirstOption(options)) } // AddressLineOneField gets a string of AddressLineOne field -func (bfi *BeneficiaryFI) AddressLineOneField() string { - return bfi.alphaField(bfi.FinancialInstitution.Address.AddressLineOne, 35) +func (bfi *BeneficiaryFI) AddressLineOneField(options ...bool) string { + return bfi.alphaVariableField(bfi.FinancialInstitution.Address.AddressLineOne, 35, bfi.parseFirstOption(options)) } // AddressLineTwoField gets a string of AddressLineTwo field -func (bfi *BeneficiaryFI) AddressLineTwoField() string { - return bfi.alphaField(bfi.FinancialInstitution.Address.AddressLineTwo, 35) +func (bfi *BeneficiaryFI) AddressLineTwoField(options ...bool) string { + return bfi.alphaVariableField(bfi.FinancialInstitution.Address.AddressLineTwo, 35, bfi.parseFirstOption(options)) } // AddressLineThreeField gets a string of AddressLineThree field -func (bfi *BeneficiaryFI) AddressLineThreeField() string { - return bfi.alphaField(bfi.FinancialInstitution.Address.AddressLineThree, 35) +func (bfi *BeneficiaryFI) AddressLineThreeField(options ...bool) string { + return bfi.alphaVariableField(bfi.FinancialInstitution.Address.AddressLineThree, 35, bfi.parseFirstOption(options)) } diff --git a/beneficiaryFI_test.go b/beneficiaryFI_test.go index 263c6b2a..23cb06b3 100644 --- a/beneficiaryFI_test.go +++ b/beneficiaryFI_test.go @@ -124,12 +124,12 @@ func TestParseBeneficiaryFIWrongLength(t *testing.T) { err := r.parseBeneficiaryFI() - require.EqualError(t, err, r.parseError(NewTagWrongLengthErr(181, len(r.line))).Error()) + require.EqualError(t, err, r.parseError(fieldError("AddressLineThree", ErrValidLengthSize)).Error()) } // TestParseBeneficiaryFIReaderParseError parses a wrong BeneficiaryFI reader parse error func TestParseBeneficiaryFIReaderParseError(t *testing.T) { - var line = "{4100}D123456789 F® Name Address One Address Two Address Three " + var line = "{4100}D123456789 F® Name Address One Address Two Address Three " r := NewReader(strings.NewReader(line)) r.line = line @@ -153,3 +153,51 @@ func TestBeneficiaryFITagError(t *testing.T) { require.EqualError(t, err, fieldError("tag", ErrValidTagForType, bfi.tag).Error()) } + +// TestStringBeneficiaryFIVariableLength parses using variable length +func TestStringBeneficiaryFIVariableLength(t *testing.T) { + var line = "{4100}" + r := NewReader(strings.NewReader(line)) + r.line = line + + err := r.parseBeneficiaryFI() + expected := r.parseError(NewTagMinLengthErr(7, len(r.line))).Error() + require.EqualError(t, err, expected) + + line = "{4100}D123456789 FI Name Address One Address Two Address Three NNN" + r = NewReader(strings.NewReader(line)) + r.line = line + + err = r.parseBeneficiaryFI() + require.EqualError(t, err, r.parseError(NewTagMaxLengthErr()).Error()) + + line = "{4100}D123456789******" + r = NewReader(strings.NewReader(line)) + r.line = line + + err = r.parseBeneficiaryFI() + require.EqualError(t, err, r.parseError(NewTagMaxLengthErr()).Error()) + + line = "{4100}D123456789****" + r = NewReader(strings.NewReader(line)) + r.line = line + + err = r.parseBeneficiaryFI() + require.Equal(t, err, nil) +} + +// TestStringBeneficiaryFIOptions validates string() with options +func TestStringBeneficiaryFIOptions(t *testing.T) { + var line = "{4100}D123456789*" + r := NewReader(strings.NewReader(line)) + r.line = line + + err := r.parseBeneficiaryFI() + require.Equal(t, err, nil) + + str := r.currentFEDWireMessage.BeneficiaryFI.String() + require.Equal(t, str, "{4100}D123456789 ") + + str = r.currentFEDWireMessage.BeneficiaryFI.String(true) + require.Equal(t, str, "{4100}D123456789*") +} diff --git a/beneficiaryIntermediaryFI.go b/beneficiaryIntermediaryFI.go index 249b8a73..76ab1015 100644 --- a/beneficiaryIntermediaryFI.go +++ b/beneficiaryIntermediaryFI.go @@ -36,16 +36,46 @@ func NewBeneficiaryIntermediaryFI() *BeneficiaryIntermediaryFI { // Parse provides no guarantee about all fields being filled in. Callers should make a Validate() call to confirm // successful parsing and data validity. func (bifi *BeneficiaryIntermediaryFI) Parse(record string) error { - if utf8.RuneCountInString(record) != 181 { - return NewTagWrongLengthErr(181, len(record)) + if utf8.RuneCountInString(record) < 7 { + return NewTagMinLengthErr(7, len(record)) } + bifi.tag = record[:6] bifi.FinancialInstitution.IdentificationCode = bifi.parseStringField(record[6:7]) - bifi.FinancialInstitution.Identifier = bifi.parseStringField(record[7:41]) - bifi.FinancialInstitution.Name = bifi.parseStringField(record[41:76]) - bifi.FinancialInstitution.Address.AddressLineOne = bifi.parseStringField(record[76:111]) - bifi.FinancialInstitution.Address.AddressLineTwo = bifi.parseStringField(record[111:146]) - bifi.FinancialInstitution.Address.AddressLineThree = bifi.parseStringField(record[146:181]) + + var err error + length := 7 + read := 0 + + if bifi.FinancialInstitution.Identifier, read, err = bifi.parseVariableStringField(record[length:], 34); err != nil { + return fieldError("Identifier", err) + } + length += read + + if bifi.FinancialInstitution.Name, read, err = bifi.parseVariableStringField(record[length:], 35); err != nil { + return fieldError("Name", err) + } + length += read + + if bifi.FinancialInstitution.Address.AddressLineOne, read, err = bifi.parseVariableStringField(record[length:], 35); err != nil { + return fieldError("AddressLineOne", err) + } + length += read + + if bifi.FinancialInstitution.Address.AddressLineTwo, read, err = bifi.parseVariableStringField(record[length:], 35); err != nil { + return fieldError("AddressLineTwo", err) + } + length += read + + if bifi.FinancialInstitution.Address.AddressLineThree, read, err = bifi.parseVariableStringField(record[length:], 35); err != nil { + return fieldError("AddressLineThree", err) + } + length += read + + if len(record) != length { + return NewTagMaxLengthErr() + } + return nil } @@ -64,17 +94,23 @@ func (bifi *BeneficiaryIntermediaryFI) UnmarshalJSON(data []byte) error { } // String writes BeneficiaryIntermediaryFI -func (bifi *BeneficiaryIntermediaryFI) String() string { +func (bifi *BeneficiaryIntermediaryFI) String(options ...bool) string { var buf strings.Builder buf.Grow(181) + buf.WriteString(bifi.tag) buf.WriteString(bifi.IdentificationCodeField()) - buf.WriteString(bifi.IdentifierField()) - buf.WriteString(bifi.NameField()) - buf.WriteString(bifi.AddressLineOneField()) - buf.WriteString(bifi.AddressLineTwoField()) - buf.WriteString(bifi.AddressLineThreeField()) - return buf.String() + buf.WriteString(bifi.IdentifierField(options...)) + buf.WriteString(bifi.NameField(options...)) + buf.WriteString(bifi.AddressLineOneField(options...)) + buf.WriteString(bifi.AddressLineTwoField(options...)) + buf.WriteString(bifi.AddressLineThreeField(options...)) + + if bifi.parseFirstOption(options) { + return bifi.stripDelimiters(buf.String()) + } else { + return buf.String() + } } // Validate performs WIRE format rule checks on BeneficiaryIntermediaryFI and returns an error if not Validated @@ -133,26 +169,26 @@ func (bifi *BeneficiaryIntermediaryFI) IdentificationCodeField() string { } // IdentifierField gets a string of the Identifier field -func (bifi *BeneficiaryIntermediaryFI) IdentifierField() string { - return bifi.alphaField(bifi.FinancialInstitution.Identifier, 34) +func (bifi *BeneficiaryIntermediaryFI) IdentifierField(options ...bool) string { + return bifi.alphaVariableField(bifi.FinancialInstitution.Identifier, 34, bifi.parseFirstOption(options)) } // NameField gets a string of the Name field -func (bifi *BeneficiaryIntermediaryFI) NameField() string { - return bifi.alphaField(bifi.FinancialInstitution.Name, 35) +func (bifi *BeneficiaryIntermediaryFI) NameField(options ...bool) string { + return bifi.alphaVariableField(bifi.FinancialInstitution.Name, 35, bifi.parseFirstOption(options)) } // AddressLineOneField gets a string of AddressLineOne field -func (bifi *BeneficiaryIntermediaryFI) AddressLineOneField() string { - return bifi.alphaField(bifi.FinancialInstitution.Address.AddressLineOne, 35) +func (bifi *BeneficiaryIntermediaryFI) AddressLineOneField(options ...bool) string { + return bifi.alphaVariableField(bifi.FinancialInstitution.Address.AddressLineOne, 35, bifi.parseFirstOption(options)) } // AddressLineTwoField gets a string of AddressLineTwo field -func (bifi *BeneficiaryIntermediaryFI) AddressLineTwoField() string { - return bifi.alphaField(bifi.FinancialInstitution.Address.AddressLineTwo, 35) +func (bifi *BeneficiaryIntermediaryFI) AddressLineTwoField(options ...bool) string { + return bifi.alphaVariableField(bifi.FinancialInstitution.Address.AddressLineTwo, 35, bifi.parseFirstOption(options)) } // AddressLineThreeField gets a string of AddressLineThree field -func (bifi *BeneficiaryIntermediaryFI) AddressLineThreeField() string { - return bifi.alphaField(bifi.FinancialInstitution.Address.AddressLineThree, 35) +func (bifi *BeneficiaryIntermediaryFI) AddressLineThreeField(options ...bool) string { + return bifi.alphaVariableField(bifi.FinancialInstitution.Address.AddressLineThree, 35, bifi.parseFirstOption(options)) } diff --git a/beneficiaryIntermediaryFI_test.go b/beneficiaryIntermediaryFI_test.go index d540b610..65d12bc0 100644 --- a/beneficiaryIntermediaryFI_test.go +++ b/beneficiaryIntermediaryFI_test.go @@ -124,12 +124,12 @@ func TestParseBeneficiaryIntermediaryFIWrongLength(t *testing.T) { err := r.parseBeneficiaryIntermediaryFI() - require.EqualError(t, err, r.parseError(NewTagWrongLengthErr(181, len(r.line))).Error()) + require.EqualError(t, err, r.parseError(fieldError("AddressLineThree", ErrValidLengthSize)).Error()) } // TestParseBeneficiaryIntermediaryFIReaderParseError parses a wrong BeneficiaryIntermediaryFI reader parse error func TestParseBeneficiaryIntermediaryFIReaderParseError(t *testing.T) { - var line = "{4000}D123456789 F® Name Address One Address Two Address Three " + var line = "{4000}D123456789 F® Name Address One Address Two Address Three " r := NewReader(strings.NewReader(line)) r.line = line fwm := new(FEDWireMessage) @@ -156,3 +156,51 @@ func TestBeneficiaryIntermediaryFITagError(t *testing.T) { require.EqualError(t, err, fieldError("tag", ErrValidTagForType, bifi.tag).Error()) } + +// TestStringBeneficiaryIntermediaryFIVariableLength parses using variable length +func TestStringBeneficiaryIntermediaryFIVariableLength(t *testing.T) { + var line = "{4000}" + r := NewReader(strings.NewReader(line)) + r.line = line + + err := r.parseBeneficiaryIntermediaryFI() + expected := r.parseError(NewTagMinLengthErr(7, len(r.line))).Error() + require.EqualError(t, err, expected) + + line = "{4000}D123456789 FI Name Address One Address Two Address Three NNN" + r = NewReader(strings.NewReader(line)) + r.line = line + + err = r.parseBeneficiaryIntermediaryFI() + require.EqualError(t, err, r.parseError(NewTagMaxLengthErr()).Error()) + + line = "{4000}D123456789******" + r = NewReader(strings.NewReader(line)) + r.line = line + + err = r.parseBeneficiaryIntermediaryFI() + require.EqualError(t, err, r.parseError(NewTagMaxLengthErr()).Error()) + + line = "{4000}D123456789****" + r = NewReader(strings.NewReader(line)) + r.line = line + + err = r.parseBeneficiaryIntermediaryFI() + require.Equal(t, err, nil) +} + +// TestStringBeneficiaryIntermediaryFIOptions validates string() with options +func TestStringBeneficiaryIntermediaryFIOptions(t *testing.T) { + var line = "{4000}D123456789*" + r := NewReader(strings.NewReader(line)) + r.line = line + + err := r.parseBeneficiaryIntermediaryFI() + require.Equal(t, err, nil) + + str := r.currentFEDWireMessage.BeneficiaryIntermediaryFI.String() + require.Equal(t, str, "{4000}D123456789 ") + + str = r.currentFEDWireMessage.BeneficiaryIntermediaryFI.String(true) + require.Equal(t, str, "{4000}D123456789*") +} diff --git a/beneficiaryReference.go b/beneficiaryReference.go index bfbc9c56..543a32d9 100644 --- a/beneficiaryReference.go +++ b/beneficiaryReference.go @@ -36,11 +36,25 @@ func NewBeneficiaryReference() *BeneficiaryReference { // Parse provides no guarantee about all fields being filled in. Callers should make a Validate() call to confirm // successful parsing and data validity. func (br *BeneficiaryReference) Parse(record string) error { - if utf8.RuneCountInString(record) != 22 { - return NewTagWrongLengthErr(22, len(record)) + if utf8.RuneCountInString(record) < 6 { + return NewTagMinLengthErr(6, len(record)) } + br.tag = record[:6] - br.BeneficiaryReference = br.parseStringField(record[6:22]) + + var err error + length := 6 + read := 0 + + if br.BeneficiaryReference, read, err = br.parseVariableStringField(record[length:], 16); err != nil { + return fieldError("BeneficiaryReference", err) + } + length += read + + if len(record) != length { + return NewTagMaxLengthErr() + } + return nil } @@ -59,11 +73,13 @@ func (br *BeneficiaryReference) UnmarshalJSON(data []byte) error { } // String writes BeneficiaryReference -func (br *BeneficiaryReference) String() string { +func (br *BeneficiaryReference) String(options ...bool) string { var buf strings.Builder buf.Grow(22) + buf.WriteString(br.tag) - buf.WriteString(br.BeneficiaryReferenceField()) + buf.WriteString(br.BeneficiaryReferenceField(options...)) + return buf.String() } @@ -80,6 +96,6 @@ func (br *BeneficiaryReference) Validate() error { } // BeneficiaryReferenceField gets a string of the BeneficiaryReference field -func (br *BeneficiaryReference) BeneficiaryReferenceField() string { - return br.alphaField(br.BeneficiaryReference, 16) +func (br *BeneficiaryReference) BeneficiaryReferenceField(options ...bool) string { + return br.alphaVariableField(br.BeneficiaryReference, 16, br.parseFirstOption(options)) } diff --git a/beneficiaryReference_test.go b/beneficiaryReference_test.go index bbe899ab..4e8c4876 100644 --- a/beneficiaryReference_test.go +++ b/beneficiaryReference_test.go @@ -39,12 +39,12 @@ func TestParseBeneficiaryReferenceWrongLength(t *testing.T) { err := r.parseBeneficiaryReference() - require.EqualError(t, err, r.parseError(NewTagWrongLengthErr(22, len(r.line))).Error()) + require.EqualError(t, err, r.parseError(fieldError("BeneficiaryReference", ErrValidLengthSize)).Error()) } // TestParseBeneficiaryReferenceReaderParseError parses a wrong BeneficiaryReference reader parse error func TestParseBeneficiaryReferenceReaderParseError(t *testing.T) { - var line = "{4320}Reference® " + var line = "{4320}Reference® " r := NewReader(strings.NewReader(line)) r.line = line @@ -68,3 +68,51 @@ func TestBeneficiaryReferenceTagError(t *testing.T) { require.EqualError(t, err, fieldError("tag", ErrValidTagForType, br.tag).Error()) } + +// TestStringBeneficiaryReferenceVariableLength parses using variable length +func TestStringBeneficiaryReferenceVariableLength(t *testing.T) { + var line = "{4320}" + r := NewReader(strings.NewReader(line)) + r.line = line + + err := r.parseBeneficiaryReference() + require.Nil(t, err) + + line = "{4320}Reference NN" + r = NewReader(strings.NewReader(line)) + r.line = line + + err = r.parseBeneficiaryReference() + require.EqualError(t, err, r.parseError(NewTagMaxLengthErr()).Error()) + + line = "{4320}**" + r = NewReader(strings.NewReader(line)) + r.line = line + + err = r.parseBeneficiaryReference() + require.EqualError(t, err, r.parseError(NewTagMaxLengthErr()).Error()) + + line = "{4320}*" + r = NewReader(strings.NewReader(line)) + r.line = line + + err = r.parseBeneficiaryReference() + require.Equal(t, err, nil) +} + +// TestStringBeneficiaryReferenceOptions validates string() with options +func TestStringBeneficiaryReferenceOptions(t *testing.T) { + var line = "{4320}Reference*" + r := NewReader(strings.NewReader(line)) + r.line = line + + err := r.parseBeneficiaryReference() + require.Equal(t, err, nil) + + str := r.currentFEDWireMessage.BeneficiaryReference.String() + require.Equal(t, str, "{4320}Reference ") + + str = r.currentFEDWireMessage.BeneficiaryReference.String(true) + require.Equal(t, str, "{4320}Reference*") +} + diff --git a/beneficiary_test.go b/beneficiary_test.go index 4cd9101f..2708a1a3 100644 --- a/beneficiary_test.go +++ b/beneficiary_test.go @@ -114,12 +114,12 @@ func TestParseBeneficiaryWrongLength(t *testing.T) { err := r.parseBeneficiary() - require.EqualError(t, err, r.parseError(NewTagWrongLengthErr(181, len(r.line))).Error()) + require.EqualError(t, err, r.parseError(fieldError("AddressLineThree", ErrValidLengthSize)).Error()) } // TestParseBeneficiaryReaderParseError parses a wrong Beneficiary reader parse error func TestParseBeneficiaryReaderParseError(t *testing.T) { - var line = "{4200}31234 Na®e Address One Address Two Address Three " + var line = "{4200}31234 Na®e Address One Address Two Address Three " r := NewReader(strings.NewReader(line)) r.line = line @@ -143,3 +143,51 @@ func TestBeneficiaryTagError(t *testing.T) { require.EqualError(t, err, fieldError("tag", ErrValidTagForType, ben.tag).Error()) } + +// TestStringBeneficiaryVariableLength parses using variable length +func TestStringBeneficiaryVariableLength(t *testing.T) { + var line = "{4200}" + r := NewReader(strings.NewReader(line)) + r.line = line + + err := r.parseBeneficiary() + expected := r.parseError(NewTagMinLengthErr(7, len(r.line))).Error() + require.EqualError(t, err, expected) + + line = "{4200}31234 Na®e Address One Address Two Address Three NNN" + r = NewReader(strings.NewReader(line)) + r.line = line + + err = r.parseBeneficiary() + require.EqualError(t, err, r.parseError(NewTagMaxLengthErr()).Error()) + + line = "{4200}31234******" + r = NewReader(strings.NewReader(line)) + r.line = line + + err = r.parseBeneficiary() + require.EqualError(t, err, r.parseError(NewTagMaxLengthErr()).Error()) + + line = "{4200}31234*****" + r = NewReader(strings.NewReader(line)) + r.line = line + + err = r.parseBeneficiary() + require.Equal(t, err, nil) +} + +// TestStringBeneficiaryOptions validates string() with options +func TestStringBeneficiaryOptions(t *testing.T) { + var line = "{4200}31234*" + r := NewReader(strings.NewReader(line)) + r.line = line + + err := r.parseBeneficiary() + require.Equal(t, err, nil) + + str := r.currentFEDWireMessage.Beneficiary.String() + require.Equal(t, str, "{4200}31234 ") + + str = r.currentFEDWireMessage.Beneficiary.String(true) + require.Equal(t, str, "{4200}31234*") +} diff --git a/converters.go b/converters.go index 1854471b..392e1b2b 100644 --- a/converters.go +++ b/converters.go @@ -5,7 +5,6 @@ package wire import ( - "fmt" "strconv" "strings" ) @@ -65,7 +64,6 @@ func (c *converters) parseVariableStringField(r string, maxLen int) (s string, r // Omit field? if len(r) == 0 { - fmt.Println("parseVariableStringField return") return }