diff --git a/writer.go b/writer.go index e7a1e4c6..f0613566 100644 --- a/writer.go +++ b/writer.go @@ -16,28 +16,53 @@ import ( // Writer struct type Writer struct { - w *bufio.Writer - lineNum int //current line being written + w *bufio.Writer + lineNum int //current line being written + variableLength bool + newlineChar string +} + +type OptionFunc func(*Writer) + +// VariableLengthFields specify to support variable length +func VariableLengthFields(variableLength bool) OptionFunc { + return func(w *Writer) { + w.variableLength = variableLength + } +} + +// NewlineCharacter specify new line character +func NewlineCharacter(newline string) OptionFunc { + return func(w *Writer) { + w.newlineChar = newline + } } // NewWriter returns a new Writer that writes to w. -func NewWriter(w io.Writer) *Writer { - return &Writer{ - w: bufio.NewWriter(w), +func NewWriter(w io.Writer, opts ...OptionFunc) *Writer { + writer := &Writer{ + w: bufio.NewWriter(w), + newlineChar: "\n", + } + + for _, opt := range opts { + opt(writer) } + + return writer } // Writer writes a single FEDWireMessage record to w // options // first bool : has variable length // second bool : has not new line -func (w *Writer) Write(file *File, options ...bool) error { +func (w *Writer) Write(file *File) error { if err := file.Validate(); err != nil { return err } w.lineNum = 0 // Iterate over all records in the file - if err := w.writeFEDWireMessage(file, options...); err != nil { + if err := w.writeFEDWireMessage(file); err != nil { return err } w.lineNum++ @@ -55,88 +80,79 @@ func (w *Writer) Flush() error { // options // first bool : has variable length // second bool : has not new line -func (w *Writer) writeFEDWireMessage(file *File, options ...bool) error { - - newLine := "\n" - if hasNotNewLine(options) { - newLine = "" - } +func (w *Writer) writeFEDWireMessage(file *File) error { fwm := file.FEDWireMessage - if err := w.writeMandatory(fwm, options...); err != nil { + + if err := w.writeMandatory(fwm); err != nil { return err } - if err := w.writeOtherTransferInfo(fwm, options...); err != nil { + if err := w.writeOtherTransferInfo(fwm); err != nil { return err } - if err := w.writeBeneficiary(fwm, options...); err != nil { + if err := w.writeBeneficiary(fwm); err != nil { return err } - if err := w.writeOriginator(fwm, options...); err != nil { + if err := w.writeOriginator(fwm); err != nil { return err } - if err := w.writeFinancialInstitution(fwm, options...); err != nil { + if err := w.writeFinancialInstitution(fwm); err != nil { return err } - if err := w.writeCoverPayment(fwm, options...); err != nil { + if err := w.writeCoverPayment(fwm); err != nil { return err } if fwm.UnstructuredAddenda != nil { - if _, err := w.w.WriteString(fwm.UnstructuredAddenda.String() + newLine); err != nil { + if _, err := w.w.WriteString(fwm.UnstructuredAddenda.String() + w.newlineChar); err != nil { return err } } - if err := w.writeRemittance(fwm, options...); err != nil { + if err := w.writeRemittance(fwm); err != nil { return err } if fwm.ServiceMessage != nil { - if _, err := w.w.WriteString(fwm.ServiceMessage.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.ServiceMessage.String(w.variableLength) + w.newlineChar); err != nil { return err } } - if err := w.writeFedAppended(fwm, options...); err != nil { + if err := w.writeFedAppended(fwm); err != nil { return err } return nil } -func (w *Writer) writeFedAppended(fwm FEDWireMessage, options ...bool) error { - - newLine := "\n" - if hasNotNewLine(options) { - newLine = "" - } +func (w *Writer) writeFedAppended(fwm FEDWireMessage) error { if fwm.MessageDisposition != nil { - if _, err := w.w.WriteString(fwm.MessageDisposition.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.MessageDisposition.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.ReceiptTimeStamp != nil { - if _, err := w.w.WriteString(fwm.ReceiptTimeStamp.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.ReceiptTimeStamp.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.OutputMessageAccountabilityData != nil { - if _, err := w.w.WriteString(fwm.OutputMessageAccountabilityData.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.OutputMessageAccountabilityData.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.ErrorWire != nil { - if _, err := w.w.WriteString(fwm.ErrorWire.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.ErrorWire.String(w.variableLength) + w.newlineChar); err != nil { return err } } @@ -144,15 +160,10 @@ func (w *Writer) writeFedAppended(fwm FEDWireMessage, options ...bool) error { return nil } -func (w *Writer) writeMandatory(fwm FEDWireMessage, options ...bool) error { - - newLine := "\n" - if hasNotNewLine(options) { - newLine = "" - } +func (w *Writer) writeMandatory(fwm FEDWireMessage) error { if fwm.SenderSupplied != nil { - if _, err := w.w.WriteString(fwm.SenderSupplied.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.SenderSupplied.String(w.variableLength) + w.newlineChar); err != nil { return err } } else { @@ -160,7 +171,7 @@ func (w *Writer) writeMandatory(fwm FEDWireMessage, options ...bool) error { } if fwm.TypeSubType != nil { - if _, err := w.w.WriteString(fwm.TypeSubType.String() + newLine); err != nil { + if _, err := w.w.WriteString(fwm.TypeSubType.String() + w.newlineChar); err != nil { return err } } else { @@ -168,7 +179,7 @@ func (w *Writer) writeMandatory(fwm FEDWireMessage, options ...bool) error { } if fwm.InputMessageAccountabilityData != nil { - if _, err := w.w.WriteString(fwm.InputMessageAccountabilityData.String() + newLine); err != nil { + if _, err := w.w.WriteString(fwm.InputMessageAccountabilityData.String() + w.newlineChar); err != nil { return err } } else { @@ -176,7 +187,7 @@ func (w *Writer) writeMandatory(fwm FEDWireMessage, options ...bool) error { } if fwm.Amount != nil { - if _, err := w.w.WriteString(fwm.Amount.String() + newLine); err != nil { + if _, err := w.w.WriteString(fwm.Amount.String() + w.newlineChar); err != nil { return err } } else { @@ -184,7 +195,7 @@ func (w *Writer) writeMandatory(fwm FEDWireMessage, options ...bool) error { } if fwm.SenderDepositoryInstitution != nil { - if _, err := w.w.WriteString(fwm.SenderDepositoryInstitution.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.SenderDepositoryInstitution.String(w.variableLength) + w.newlineChar); err != nil { return err } } else { @@ -192,7 +203,7 @@ func (w *Writer) writeMandatory(fwm FEDWireMessage, options ...bool) error { } if fwm.ReceiverDepositoryInstitution != nil { - if _, err := w.w.WriteString(fwm.ReceiverDepositoryInstitution.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.ReceiverDepositoryInstitution.String(w.variableLength) + w.newlineChar); err != nil { return err } } else { @@ -200,7 +211,7 @@ func (w *Writer) writeMandatory(fwm FEDWireMessage, options ...bool) error { } if fwm.BusinessFunctionCode != nil { - if _, err := w.w.WriteString(fwm.BusinessFunctionCode.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.BusinessFunctionCode.String(w.variableLength) + w.newlineChar); err != nil { return err } } else { @@ -210,51 +221,46 @@ func (w *Writer) writeMandatory(fwm FEDWireMessage, options ...bool) error { return nil } -func (w *Writer) writeOtherTransferInfo(fwm FEDWireMessage, options ...bool) error { - - newLine := "\n" - if hasNotNewLine(options) { - newLine = "" - } +func (w *Writer) writeOtherTransferInfo(fwm FEDWireMessage) error { if fwm.SenderReference != nil { - if _, err := w.w.WriteString(fwm.SenderReference.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.SenderReference.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.PreviousMessageIdentifier != nil { - if _, err := w.w.WriteString(fwm.PreviousMessageIdentifier.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.PreviousMessageIdentifier.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.LocalInstrument != nil { - if _, err := w.w.WriteString(fwm.LocalInstrument.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.LocalInstrument.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.PaymentNotification != nil { - if _, err := w.w.WriteString(fwm.PaymentNotification.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.PaymentNotification.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.Charges != nil { - if _, err := w.w.WriteString(fwm.Charges.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.Charges.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.InstructedAmount != nil { - if _, err := w.w.WriteString(fwm.InstructedAmount.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.InstructedAmount.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.ExchangeRate != nil { - if _, err := w.w.WriteString(fwm.ExchangeRate.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.ExchangeRate.String(w.variableLength) + w.newlineChar); err != nil { return err } } @@ -262,22 +268,17 @@ func (w *Writer) writeOtherTransferInfo(fwm FEDWireMessage, options ...bool) err return nil } -func (w *Writer) writeBeneficiary(fwm FEDWireMessage, options ...bool) error { - - newLine := "\n" - if hasNotNewLine(options) { - newLine = "" - } +func (w *Writer) writeBeneficiary(fwm FEDWireMessage) error { if fwm.BeneficiaryIntermediaryFI != nil { - if _, err := w.w.WriteString(fwm.BeneficiaryIntermediaryFI.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.BeneficiaryIntermediaryFI.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.BeneficiaryFI != nil { if fwm.BeneficiaryFI != nil { - if _, err := w.w.WriteString(fwm.BeneficiaryFI.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.BeneficiaryFI.String(w.variableLength) + w.newlineChar); err != nil { return err } } @@ -285,7 +286,7 @@ func (w *Writer) writeBeneficiary(fwm FEDWireMessage, options ...bool) error { if fwm.Beneficiary != nil { if fwm.Beneficiary != nil { - if _, err := w.w.WriteString(fwm.Beneficiary.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.Beneficiary.String(w.variableLength) + w.newlineChar); err != nil { return err } } @@ -293,7 +294,7 @@ func (w *Writer) writeBeneficiary(fwm FEDWireMessage, options ...bool) error { if fwm.BeneficiaryReference != nil { if fwm.BeneficiaryReference != nil { - if _, err := w.w.WriteString(fwm.BeneficiaryReference.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.BeneficiaryReference.String(w.variableLength) + w.newlineChar); err != nil { return err } } @@ -301,7 +302,7 @@ func (w *Writer) writeBeneficiary(fwm FEDWireMessage, options ...bool) error { if fwm.AccountDebitedDrawdown != nil { if fwm.AccountDebitedDrawdown != nil { - if _, err := w.w.WriteString(fwm.AccountDebitedDrawdown.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.AccountDebitedDrawdown.String(w.variableLength) + w.newlineChar); err != nil { return err } } @@ -310,45 +311,40 @@ func (w *Writer) writeBeneficiary(fwm FEDWireMessage, options ...bool) error { return nil } -func (w *Writer) writeOriginator(fwm FEDWireMessage, options ...bool) error { - - newLine := "\n" - if hasNotNewLine(options) { - newLine = "" - } +func (w *Writer) writeOriginator(fwm FEDWireMessage) error { if fwm.Originator != nil { - if _, err := w.w.WriteString(fwm.Originator.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.Originator.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.OriginatorOptionF != nil { - if _, err := w.w.WriteString(fwm.OriginatorOptionF.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.OriginatorOptionF.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.OriginatorFI != nil { - if _, err := w.w.WriteString(fwm.OriginatorFI.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.OriginatorFI.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.InstructingFI != nil { - if _, err := w.w.WriteString(fwm.InstructingFI.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.InstructingFI.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.AccountCreditedDrawdown != nil { - if _, err := w.w.WriteString(fwm.AccountCreditedDrawdown.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.AccountCreditedDrawdown.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.OriginatorToBeneficiary != nil { - if _, err := w.w.WriteString(fwm.OriginatorToBeneficiary.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.OriginatorToBeneficiary.String(w.variableLength) + w.newlineChar); err != nil { return err } } @@ -356,69 +352,64 @@ func (w *Writer) writeOriginator(fwm FEDWireMessage, options ...bool) error { return nil } -func (w *Writer) writeFinancialInstitution(fwm FEDWireMessage, options ...bool) error { - - newLine := "\n" - if hasNotNewLine(options) { - newLine = "" - } +func (w *Writer) writeFinancialInstitution(fwm FEDWireMessage) error { if fwm.FIReceiverFI != nil { - if _, err := w.w.WriteString(fwm.FIReceiverFI.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.FIReceiverFI.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.FIDrawdownDebitAccountAdvice != nil { - if _, err := w.w.WriteString(fwm.FIDrawdownDebitAccountAdvice.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.FIDrawdownDebitAccountAdvice.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.FIIntermediaryFI != nil { - if _, err := w.w.WriteString(fwm.FIIntermediaryFI.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.FIIntermediaryFI.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.FIIntermediaryFIAdvice != nil { - if _, err := w.w.WriteString(fwm.FIIntermediaryFIAdvice.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.FIIntermediaryFIAdvice.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.FIBeneficiaryFI != nil { - if _, err := w.w.WriteString(fwm.FIBeneficiaryFI.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.FIBeneficiaryFI.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.FIBeneficiaryFIAdvice != nil { - if _, err := w.w.WriteString(fwm.FIBeneficiaryFIAdvice.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.FIBeneficiaryFIAdvice.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.FIBeneficiary != nil { - if _, err := w.w.WriteString(fwm.FIBeneficiary.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.FIBeneficiary.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.FIBeneficiaryAdvice != nil { - if _, err := w.w.WriteString(fwm.FIBeneficiaryAdvice.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.FIBeneficiaryAdvice.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.FIPaymentMethodToBeneficiary != nil { - if _, err := w.w.WriteString(fwm.FIPaymentMethodToBeneficiary.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.FIPaymentMethodToBeneficiary.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.FIAdditionalFIToFI != nil { - if _, err := w.w.WriteString(fwm.FIAdditionalFIToFI.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.FIAdditionalFIToFI.String(w.variableLength) + w.newlineChar); err != nil { return err } } @@ -426,57 +417,52 @@ func (w *Writer) writeFinancialInstitution(fwm FEDWireMessage, options ...bool) return nil } -func (w *Writer) writeCoverPayment(fwm FEDWireMessage, options ...bool) error { - - newLine := "\n" - if hasNotNewLine(options) { - newLine = "" - } +func (w *Writer) writeCoverPayment(fwm FEDWireMessage) error { if fwm.CurrencyInstructedAmount != nil { - if _, err := w.w.WriteString(fwm.CurrencyInstructedAmount.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.CurrencyInstructedAmount.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.OrderingCustomer != nil { - if _, err := w.w.WriteString(fwm.OrderingCustomer.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.OrderingCustomer.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.OrderingInstitution != nil { - if _, err := w.w.WriteString(fwm.OrderingInstitution.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.OrderingInstitution.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.IntermediaryInstitution != nil { - if _, err := w.w.WriteString(fwm.IntermediaryInstitution.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.IntermediaryInstitution.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.InstitutionAccount != nil { - if _, err := w.w.WriteString(fwm.InstitutionAccount.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.InstitutionAccount.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.BeneficiaryCustomer != nil { - if _, err := w.w.WriteString(fwm.BeneficiaryCustomer.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.BeneficiaryCustomer.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.Remittance != nil { - if _, err := w.w.WriteString(fwm.Remittance.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.Remittance.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.SenderToReceiver != nil { - if _, err := w.w.WriteString(fwm.SenderToReceiver.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.SenderToReceiver.String(w.variableLength) + w.newlineChar); err != nil { return err } } @@ -484,92 +470,75 @@ func (w *Writer) writeCoverPayment(fwm FEDWireMessage, options ...bool) error { return nil } -func (w *Writer) writeRemittance(fwm FEDWireMessage, options ...bool) error { - - newLine := "\n" - if hasNotNewLine(options) { - newLine = "" - } +func (w *Writer) writeRemittance(fwm FEDWireMessage) error { // Related Remittance if fwm.RelatedRemittance != nil { - if _, err := w.w.WriteString(fwm.RelatedRemittance.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.RelatedRemittance.String(w.variableLength) + w.newlineChar); err != nil { return err } } // Structured Remittance if fwm.RemittanceOriginator != nil { - if _, err := w.w.WriteString(fwm.RemittanceOriginator.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.RemittanceOriginator.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.RemittanceBeneficiary != nil { - if _, err := w.w.WriteString(fwm.RemittanceBeneficiary.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.RemittanceBeneficiary.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.PrimaryRemittanceDocument != nil { - if _, err := w.w.WriteString(fwm.PrimaryRemittanceDocument.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.PrimaryRemittanceDocument.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.ActualAmountPaid != nil { - if _, err := w.w.WriteString(fwm.ActualAmountPaid.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.ActualAmountPaid.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.GrossAmountRemittanceDocument != nil { - if _, err := w.w.WriteString(fwm.GrossAmountRemittanceDocument.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.GrossAmountRemittanceDocument.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.AmountNegotiatedDiscount != nil { - if _, err := w.w.WriteString(fwm.AmountNegotiatedDiscount.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.AmountNegotiatedDiscount.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.Adjustment != nil { - if _, err := w.w.WriteString(fwm.Adjustment.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.Adjustment.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.DateRemittanceDocument != nil { - if _, err := w.w.WriteString(fwm.DateRemittanceDocument.String() + newLine); err != nil { + if _, err := w.w.WriteString(fwm.DateRemittanceDocument.String() + w.newlineChar); err != nil { return err } } if fwm.SecondaryRemittanceDocument != nil { - if _, err := w.w.WriteString(fwm.SecondaryRemittanceDocument.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.SecondaryRemittanceDocument.String(w.variableLength) + w.newlineChar); err != nil { return err } } if fwm.RemittanceFreeText != nil { - if _, err := w.w.WriteString(fwm.RemittanceFreeText.String(options...) + newLine); err != nil { + if _, err := w.w.WriteString(fwm.RemittanceFreeText.String(w.variableLength) + w.newlineChar); err != nil { return err } } return nil } - -// get second option from options, has not new line -func hasNotNewLine(options []bool) bool { - - firstOption := false - - if len(options) > 1 { - firstOption = options[1] - } - - return firstOption -}