diff --git a/actualAmountPaid.go b/actualAmountPaid.go index 902d7b99..dc8dea2d 100644 --- a/actualAmountPaid.go +++ b/actualAmountPaid.go @@ -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 } @@ -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() } @@ -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)) } diff --git a/actualAmountPaid_test.go b/actualAmountPaid_test.go index b420b407..51145415 100644 --- a/actualAmountPaid_test.go +++ b/actualAmountPaid_test.go @@ -1,6 +1,7 @@ package wire import ( + "fmt" "strings" "testing" @@ -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 @@ -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*") +}