Skip to content

Commit

Permalink
introduce FormatOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
atonks2 committed Jun 28, 2022
1 parent 2d812e9 commit fdb61a9
Show file tree
Hide file tree
Showing 116 changed files with 3,178 additions and 1,484 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@ test/fuzz-reader/corpus/*.tar.gz
/cmd/webui/assets/wire.wasm

# GoLand
/.idea/*
/.idea/*

# Gitleaks
gitleaks.tar.gz
22 changes: 17 additions & 5 deletions accountCreditedDrawdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,19 @@ func (creditDD *AccountCreditedDrawdown) UnmarshalJSON(data []byte) error {
return nil
}

// String writes AccountCreditedDrawdown
func (creditDD *AccountCreditedDrawdown) String(options ...bool) string {
// String returns a fixed-width AccountCreditedDrawdown record
func (creditDD *AccountCreditedDrawdown) String() string {
return creditDD.Format(FormatOptions{
VariableLengthFields: false,
})
}

// Format returns an AccountCreditedDrawdown record formatted according to the FormatOptions
func (creditDD *AccountCreditedDrawdown) Format(options FormatOptions) string {
var buf strings.Builder
buf.Grow(15)
buf.WriteString(creditDD.tag)
buf.WriteString(creditDD.DrawdownCreditAccountNumberField(options...))
buf.WriteString(creditDD.FormatCreditAccountNumber(options))
return buf.String()
}

Expand Down Expand Up @@ -105,6 +112,11 @@ func (creditDD *AccountCreditedDrawdown) fieldInclusion() error {
}

// DrawdownCreditAccountNumberField gets a string of the DrawdownCreditAccountNumber field
func (creditDD *AccountCreditedDrawdown) DrawdownCreditAccountNumberField(options ...bool) string {
return creditDD.alphaVariableField(creditDD.DrawdownCreditAccountNumber, 9, creditDD.parseFirstOption(options))
func (creditDD *AccountCreditedDrawdown) DrawdownCreditAccountNumberField() string {
return creditDD.alphaField(creditDD.DrawdownCreditAccountNumber, 9)
}

// FormatCreditAccountNumber returns DrawdownCreditAccountNumber formatted according to the FormatOptions
func (creditDD *AccountCreditedDrawdown) FormatCreditAccountNumber(options FormatOptions) string {
return creditDD.formatAlphaField(creditDD.DrawdownCreditAccountNumber, 9, options)
}
11 changes: 5 additions & 6 deletions accountCreditedDrawdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestStringAccountCreditedDrawdownVariableLength(t *testing.T) {
require.Equal(t, err, nil)
}

// TestStringAccountCreditedDrawdownOptions validates string() with options
// TestStringAccountCreditedDrawdownOptions validates Format() formatted according to the FormatOptions
func TestStringAccountCreditedDrawdownOptions(t *testing.T) {
var line = "{5400}1*"
r := NewReader(strings.NewReader(line))
Expand All @@ -121,9 +121,8 @@ func TestStringAccountCreditedDrawdownOptions(t *testing.T) {
err := r.parseAccountCreditedDrawdown()
require.Equal(t, err, nil)

str := r.currentFEDWireMessage.AccountCreditedDrawdown.String()
require.Equal(t, str, "{5400}1 ")

str = r.currentFEDWireMessage.AccountCreditedDrawdown.String(true)
require.Equal(t, str, "{5400}1*")
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.String(), acd.Format(FormatOptions{VariableLengthFields: false}))
}
74 changes: 53 additions & 21 deletions accountDebitedDrawdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,27 @@ func (debitDD *AccountDebitedDrawdown) UnmarshalJSON(data []byte) error {
return nil
}

// String writes AccountDebitedDrawdown
func (debitDD *AccountDebitedDrawdown) String(options ...bool) string {
// String returns a fixed-width AccountDebitedDrawdown record
func (debitDD *AccountDebitedDrawdown) String() string {
return debitDD.Format(FormatOptions{
VariableLengthFields: false,
})
}

// Format returns an AccountDebitedDrawdown record formatted according to the FormatOptions
func (debitDD *AccountDebitedDrawdown) Format(options FormatOptions) string {
var buf strings.Builder
buf.Grow(181)

buf.WriteString(debitDD.tag)
buf.WriteString(debitDD.IdentificationCodeField())
buf.WriteString(debitDD.IdentifierField(options...))
buf.WriteString(debitDD.NameField(options...))
buf.WriteString(debitDD.AddressLineOneField(options...))
buf.WriteString(debitDD.AddressLineTwoField(options...))
buf.WriteString(debitDD.AddressLineThreeField(options...))
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))

if debitDD.parseFirstOption(options) {
if options.VariableLengthFields {
return debitDD.stripDelimiters(buf.String())
} else {
return buf.String()
Expand Down Expand Up @@ -183,26 +190,51 @@ func (debitDD *AccountDebitedDrawdown) IdentificationCodeField() string {
}

// IdentifierField gets a string of the Identifier field
func (debitDD *AccountDebitedDrawdown) IdentifierField(options ...bool) string {
return debitDD.alphaVariableField(debitDD.Identifier, 34, debitDD.parseFirstOption(options))
func (debitDD *AccountDebitedDrawdown) IdentifierField() string {
return debitDD.alphaField(debitDD.Identifier, 34)
}

// NameField gets a string of the Name field
func (debitDD *AccountDebitedDrawdown) NameField(options ...bool) string {
return debitDD.alphaVariableField(debitDD.Name, 35, debitDD.parseFirstOption(options))
func (debitDD *AccountDebitedDrawdown) NameField() string {
return debitDD.alphaField(debitDD.Name, 35)
}

// AddressLineOneField gets a string of Address.AddressLineOne field
func (debitDD *AccountDebitedDrawdown) AddressLineOneField() string {
return debitDD.alphaField(debitDD.Address.AddressLineOne, 35)
}

// AddressLineTwoField gets a string of Address.AddressLineTwo field
func (debitDD *AccountDebitedDrawdown) AddressLineTwoField() string {
return debitDD.alphaField(debitDD.Address.AddressLineTwo, 35)
}

// AddressLineThreeField gets a string of Address.AddressLineThree field
func (debitDD *AccountDebitedDrawdown) AddressLineThreeField() string {
return debitDD.alphaField(debitDD.Address.AddressLineThree, 35)
}

// FormatIdentifier returns Identifier formatted according to the FormatOptions
func (debitDD *AccountDebitedDrawdown) FormatIdentifier(options FormatOptions) string {
return debitDD.formatAlphaField(debitDD.Identifier, 34, options)
}

// FormatName returns Name formatted according to the FormatOptions
func (debitDD *AccountDebitedDrawdown) FormatName(options FormatOptions) string {
return debitDD.formatAlphaField(debitDD.Name, 35, options)
}

// AddressLineOneField gets a string of AddressLineOne field
func (debitDD *AccountDebitedDrawdown) AddressLineOneField(options ...bool) string {
return debitDD.alphaVariableField(debitDD.Address.AddressLineOne, 35, debitDD.parseFirstOption(options))
// FormatAddressLineOne returns Address.AddressLineOne formatted according to the FormatOptions
func (debitDD *AccountDebitedDrawdown) FormatAddressLineOne(options FormatOptions) string {
return debitDD.formatAlphaField(debitDD.Address.AddressLineOne, 35, options)
}

// AddressLineTwoField gets a string of AddressLineTwo field
func (debitDD *AccountDebitedDrawdown) AddressLineTwoField(options ...bool) string {
return debitDD.alphaVariableField(debitDD.Address.AddressLineTwo, 35, debitDD.parseFirstOption(options))
// FormatAddressLineTwo returns Address.AddressLineTwo formatted according to the FormatOptions
func (debitDD *AccountDebitedDrawdown) FormatAddressLineTwo(options FormatOptions) string {
return debitDD.formatAlphaField(debitDD.Address.AddressLineTwo, 35, options)
}

// AddressLineThreeField gets a string of AddressLineThree field
func (debitDD *AccountDebitedDrawdown) AddressLineThreeField(options ...bool) string {
return debitDD.alphaVariableField(debitDD.Address.AddressLineThree, 35, debitDD.parseFirstOption(options))
// FormatAddressLineThree returns Address.AddressLineThree formatted according to the FormatOptions
func (debitDD *AccountDebitedDrawdown) FormatAddressLineThree(options FormatOptions) string {
return debitDD.formatAlphaField(debitDD.Address.AddressLineThree, 35, options)
}
11 changes: 5 additions & 6 deletions accountDebitedDrawdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func TestStringAccountDebitedDrawdownVariableLength(t *testing.T) {
require.Equal(t, err, nil)
}

// TestStringDebitedDrawdownOptions validates string() with options
// TestStringDebitedDrawdownOptions validates Format() formatted according to the FormatOptions
func TestStringAccountDebitedDrawdownOptions(t *testing.T) {
var line = "{4400}D2*3*"
r := NewReader(strings.NewReader(line))
Expand All @@ -206,9 +206,8 @@ func TestStringAccountDebitedDrawdownOptions(t *testing.T) {
err := r.parseAccountDebitedDrawdown()
require.Equal(t, err, nil)

str := r.currentFEDWireMessage.AccountDebitedDrawdown.String()
require.Equal(t, str, "{4400}D2 3 ")

str = r.currentFEDWireMessage.AccountDebitedDrawdown.String(true)
require.Equal(t, str, "{4400}D2*3*")
add := r.currentFEDWireMessage.AccountDebitedDrawdown
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}))
}
37 changes: 27 additions & 10 deletions actualAmountPaid.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,21 @@ func (aap *ActualAmountPaid) UnmarshalJSON(data []byte) error {
return nil
}

// String writes ActualAmountPaid
func (aap *ActualAmountPaid) String(options ...bool) string {
// String returns a fixed-width ActualAmountPaid record
func (aap *ActualAmountPaid) String() string {
return aap.Format(FormatOptions{
VariableLengthFields: false,
})
}

// Format returns an ActualAmountPaid record formatted according to the FormatOptions
func (aap *ActualAmountPaid) Format(options FormatOptions) string {
var buf strings.Builder
buf.Grow(28)

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

return buf.String()
}
Expand Down Expand Up @@ -122,12 +129,22 @@ func (aap *ActualAmountPaid) fieldInclusion() error {
return nil
}

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

// AmountField gets a string of the RemittanceAmount.Amount field
func (aap *ActualAmountPaid) AmountField() string {
return aap.alphaField(aap.RemittanceAmount.Amount, 19)
}

// FormatCurrencyCode returns RemittanceAmount.CurrencyCode formatted according to the FormatOptions
func (aap *ActualAmountPaid) FormatCurrencyCode(options FormatOptions) string {
return aap.formatAlphaField(aap.RemittanceAmount.CurrencyCode, 3, options)
}

// AmountField gets a string of the Amount field
func (aap *ActualAmountPaid) AmountField(options ...bool) string {
return aap.alphaVariableField(aap.RemittanceAmount.Amount, 19, aap.parseFirstOption(options))
// FormatAmount returns RemittanceAmount.Amount formatted according to the FormatOptions
func (aap *ActualAmountPaid) FormatAmount(options FormatOptions) string {
return aap.formatAlphaField(aap.RemittanceAmount.Amount, 19, options)
}
11 changes: 5 additions & 6 deletions actualAmountPaid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func TestStringActualAmountPaidVariableLength(t *testing.T) {
require.Equal(t, err, nil)
}

// TestStringActualAmountPaidOptions validates string() with options
// TestStringActualAmountPaidOptions validates Format() formatted according to the FormatOptions
func TestStringActualAmountPaidOptions(t *testing.T) {
var line = "{8450}USD1234.56*"
r := NewReader(strings.NewReader(line))
Expand All @@ -149,9 +149,8 @@ func TestStringActualAmountPaidOptions(t *testing.T) {
err := r.parseActualAmountPaid()
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*")
aap := r.currentFEDWireMessage.ActualAmountPaid
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}))
}
68 changes: 50 additions & 18 deletions adjustment.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,26 @@ func (adj *Adjustment) UnmarshalJSON(data []byte) error {
return nil
}

// String writes Adjustment
func (adj *Adjustment) String(options ...bool) string {
// String returns a fixed-width Adjustment record
func (adj *Adjustment) String() string {
return adj.Format(FormatOptions{
VariableLengthFields: false,
})
}

// Format returns an Adjustment record formatted according to the FormatOptions
func (adj *Adjustment) Format(options FormatOptions) string {
var buf strings.Builder
buf.Grow(168)

buf.WriteString(adj.tag)
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...))
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))

if adj.parseFirstOption(options) {
if options.VariableLengthFields {
return adj.stripDelimiters(buf.String())
} else {
return buf.String()
Expand Down Expand Up @@ -168,26 +175,51 @@ func (adj *Adjustment) fieldInclusion() error {
}

// AdjustmentReasonCodeField gets a string of the AdjustmentReasonCode field
func (adj *Adjustment) AdjustmentReasonCodeField(options ...bool) string {
return adj.alphaVariableField(adj.AdjustmentReasonCode, 2, adj.parseFirstOption(options))
func (adj *Adjustment) AdjustmentReasonCodeField() string {
return adj.alphaField(adj.AdjustmentReasonCode, 2)
}

// CreditDebitIndicatorField gets a string of the CreditDebitIndicator field
func (adj *Adjustment) CreditDebitIndicatorField(options ...bool) string {
return adj.alphaVariableField(adj.CreditDebitIndicator, 4, adj.parseFirstOption(options))
func (adj *Adjustment) CreditDebitIndicatorField() string {
return adj.alphaField(adj.CreditDebitIndicator, 4)
}

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

// AmountField gets a string of the Amount field
func (adj *Adjustment) AmountField(options ...bool) string {
return adj.alphaVariableField(adj.RemittanceAmount.Amount, 19, adj.parseFirstOption(options))
func (adj *Adjustment) AmountField() string {
return adj.alphaField(adj.RemittanceAmount.Amount, 19)
}

// AdditionalInfoField gets a string of the AdditionalInfo field
func (adj *Adjustment) AdditionalInfoField(options ...bool) string {
return adj.alphaVariableField(adj.AdditionalInfo, 140, adj.parseFirstOption(options))
func (adj *Adjustment) AdditionalInfoField() string {
return adj.alphaField(adj.AdditionalInfo, 140)
}

// FormatAdjustmentReasonCode returns AdjustmentReasonCode formatted according to the FormatOptions
func (adj *Adjustment) FormatAdjustmentReasonCode(options FormatOptions) string {
return adj.formatAlphaField(adj.AdjustmentReasonCode, 2, options)
}

// FormatCreditDebitIndicator returns CreditDebitIndicator formatted according to the FormatOptions
func (adj *Adjustment) FormatCreditDebitIndicator(options FormatOptions) string {
return adj.formatAlphaField(adj.CreditDebitIndicator, 4, options)
}

// FormatCurrencyCode returns RemittanceAmount.CurrencyCode formatted according to the FormatOptions
func (adj *Adjustment) FormatCurrencyCode(options FormatOptions) string {
return adj.formatAlphaField(adj.RemittanceAmount.CurrencyCode, 3, options)
}

// FormatAmount returns RemittanceAmount.Amount formatted according to the FormatOptions
func (adj *Adjustment) FormatAmount(options FormatOptions) string {
return adj.formatAlphaField(adj.RemittanceAmount.Amount, 19, options)
}

// FormatAdditionalInfo returns AdditionalInfo formatted according to the FormatOptions
func (adj *Adjustment) FormatAdditionalInfo(options FormatOptions) string {
return adj.formatAlphaField(adj.AdditionalInfo, 140, options)
}
11 changes: 5 additions & 6 deletions adjustment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func TestStringAdjustmentVariableLength(t *testing.T) {
require.Equal(t, err, nil)
}

// TestStringAdjustmentOptions validates string() with options
// TestStringAdjustmentOptions validates Format() formatted according to the FormatOptions
func TestStringAdjustmentOptions(t *testing.T) {
var line = "{8600}01CRDTUSD1234.56 *"
r := NewReader(strings.NewReader(line))
Expand All @@ -184,9 +184,8 @@ func TestStringAdjustmentOptions(t *testing.T) {
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*")
adj := r.currentFEDWireMessage.Adjustment
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}))
}
Loading

0 comments on commit fdb61a9

Please sign in to comment.