Skip to content

Commit

Permalink
modified actual amount paid segment
Browse files Browse the repository at this point in the history
  • Loading branch information
mfdeveloper508 committed May 31, 2022
1 parent 160dd3d commit 07aed9f
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 12 deletions.
42 changes: 31 additions & 11 deletions actualAmountPaid.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,30 @@ func NewActualAmountPaid() *ActualAmountPaid {
// Parse provides no guarantee about all fields being filled in. Callers should make a Validate() call to confirm
// successful parsing and data validity.
func (aap *ActualAmountPaid) Parse(record string) error {
if utf8.RuneCountInString(record) != 28 {
return NewTagWrongLengthErr(28, len(record))
if utf8.RuneCountInString(record) < 8 {
return NewTagMinLengthErr(8, len(record))
}

aap.tag = record[:6]
aap.RemittanceAmount.CurrencyCode = aap.parseStringField(record[6:9])
aap.RemittanceAmount.Amount = aap.parseStringField(record[9:28])

var err error
length := 6
read := 0

if aap.RemittanceAmount.CurrencyCode, read, err = aap.parseVariableStringField(record[length:], 3); err != nil {
return fieldError("CurrencyCode", err)
}
length += read

if aap.RemittanceAmount.Amount, read, err = aap.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 (aap *ActualAmountPaid) UnmarshalJSON(data []byte) error {
}

// String writes ActualAmountPaid
func (aap *ActualAmountPaid) String() string {
func (aap *ActualAmountPaid) String(options ...bool) string {
var buf strings.Builder
buf.Grow(28)

buf.WriteString(aap.tag)
buf.WriteString(aap.CurrencyCodeField())
buf.WriteString(aap.AmountField())
buf.WriteString(aap.CurrencyCodeField(options...))
buf.WriteString(aap.AmountField(options...))

return buf.String()
}

Expand Down Expand Up @@ -102,11 +122,11 @@ func (aap *ActualAmountPaid) fieldInclusion() error {
}

// CurrencyCodeField gets a string of the CurrencyCode field
func (aap *ActualAmountPaid) CurrencyCodeField() string {
return aap.alphaField(aap.RemittanceAmount.CurrencyCode, 3)
func (aap *ActualAmountPaid) CurrencyCodeField(options ...bool) string {
return aap.alphaVariableField(aap.RemittanceAmount.CurrencyCode, 3, aap.parseFirstOption(options))
}

// AmountField gets a string of the Amount field
func (aap *ActualAmountPaid) AmountField() string {
return aap.alphaField(aap.RemittanceAmount.Amount, 19)
func (aap *ActualAmountPaid) AmountField(options ...bool) string {
return aap.alphaVariableField(aap.RemittanceAmount.Amount, 19, aap.parseFirstOption(options))
}
60 changes: 59 additions & 1 deletion actualAmountPaid_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wire

import (
"fmt"
"strings"
"testing"

Expand Down Expand Up @@ -70,7 +71,7 @@ func TestParseActualAmountPaidWrongLength(t *testing.T) {

err := r.parseActualAmountPaid()

require.EqualError(t, err, r.parseError(NewTagWrongLengthErr(28, len(r.line))).Error())
require.EqualError(t, err, r.parseError(fieldError("Amount", ErrValidLengthSize)).Error())
}

// TestParseActualAmountPaidReaderParseError parses a wrong ActualAmountPaid reader parse error
Expand Down Expand Up @@ -99,3 +100,60 @@ func TestActualAmountPaidTagError(t *testing.T) {

require.EqualError(t, err, fieldError("tag", ErrValidTagForType, aap.tag).Error())
}

// TestStringActualAmountPaidVariableLength parses using variable length
func TestStringActualAmountPaidVariableLength(t *testing.T) {
var line = "{8450}"
r := NewReader(strings.NewReader(line))
r.line = line

err := r.parseActualAmountPaid()
expected := r.parseError(NewTagMinLengthErr(8, len(r.line))).Error()
require.EqualError(t, err, expected)

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

err = r.parseActualAmountPaid()
require.EqualError(t, err, r.parseError(NewTagMaxLengthErr()).Error())

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

err = r.parseActualAmountPaid()
require.EqualError(t, err, r.parseError(NewTagMaxLengthErr()).Error())

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

err = r.parseActualAmountPaid()
expected = r.parseError(fieldError("Amount", ErrFieldRequired)).Error()
require.EqualError(t, err, expected)

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

err = r.parseActualAmountPaid()
require.Equal(t, err, nil)
}

// TestStringActualAmountPaidOptions validates string() with options
func TestStringActualAmountPaidOptions(t *testing.T) {
var line = "{8450}USD1234.56*"
r := NewReader(strings.NewReader(line))
r.line = line

err := r.parseActualAmountPaid()
fmt.Println(err)
require.Equal(t, err, nil)

str := r.currentFEDWireMessage.ActualAmountPaid.String()
require.Equal(t, str, "{8450}USD1234.56 ")

str = r.currentFEDWireMessage.ActualAmountPaid.String(true)
require.Equal(t, str, "{8450}USD1234.56*")
}

0 comments on commit 07aed9f

Please sign in to comment.