Skip to content

Commit

Permalink
added variable length to some segments
Browse files Browse the repository at this point in the history
  • Loading branch information
mfdeveloper508 committed Jun 1, 2022
1 parent 07aed9f commit 54fc580
Show file tree
Hide file tree
Showing 16 changed files with 709 additions and 158 deletions.
2 changes: 0 additions & 2 deletions actualAmountPaid_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package wire

import (
"fmt"
"strings"
"testing"

Expand Down Expand Up @@ -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()
Expand Down
84 changes: 60 additions & 24 deletions adjustment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down Expand Up @@ -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))
}
50 changes: 49 additions & 1 deletion adjustment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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*")
}
42 changes: 31 additions & 11 deletions amountNegotiatedDiscount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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()
}

Expand Down Expand Up @@ -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))
}
50 changes: 49 additions & 1 deletion amountNegotiatedDiscount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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*")
}
Loading

0 comments on commit 54fc580

Please sign in to comment.