Last active
July 28, 2021 20:25
-
-
Save mikeacjones/8a1450c533ee2df3b09bc93a470964b9 to your computer and use it in GitHub Desktop.
About all I can say about this is that it works for this specific use case.. will need to evaluate again at some point to try and find improvements. Maybe. If I can work up the energy to care about this cursed file format.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%dw 2.0 | |
fun parseBAI(payload: String) : Object = do { | |
payload splitBy /\/?\r?\n/ reduce ((line, accum={ Accounts: []}) -> ( | |
baiFuncs[line[0 to 1]](line splitBy ',', accum) | |
)) | |
} | |
fun baiHeader(payload: Array<String>, accum: Object) : Object = | |
accum ++ Header: { | |
Sender: payload[1], | |
Receiver: payload[2], | |
DateCreated: payload[3], | |
TimeCreated: payload[4], | |
FileID: payload[5], | |
PhysRecordLen: payload[6], | |
BlockSize: payload[7], | |
Version: payload[8] | |
} | |
fun baiGroupHeader(payload: Array<String>, accum: Object) : Object = | |
accum ++ GroupHeader: { | |
Receiver: payload[1], | |
Sender: payload[2], | |
GroupStatus: payload[3], | |
AsOfDate: payload[4], | |
AsOfTime: payload[5], | |
CurrencyCode: payload[6], | |
AsOfDateModifier: payload[7] | |
} | |
fun baiSummary(payload: Array<String>, accum: Object) : Object = | |
{ | |
(accum - 'Accounts'), | |
Accounts: accum.Accounts << | |
if (sizeOf(payload) == 13) { | |
TypeOfRecord: 'TRAN', | |
Size: sizeOf(payload), | |
AccountNumber: payload[1], | |
CurrencyCode: payload[2], | |
TypeCode: payload[3], | |
LedgerBalance: payload[4], | |
ItemCount: payload[5], | |
FundsType: payload[6], | |
TypeCode: payload[7], | |
ClosingBalance: payload[8], | |
ItemCount: payload[9], | |
FundsType: payload[10], | |
Transactions: [] | |
} else { | |
TypeOfRecord: 'Loan', | |
AccountNumber: payload[1], | |
LoadBalance: payload[4] | |
} | |
} | |
fun baiSummaryExtension(payload: Array<String>, accum: Object) : Object = | |
{ | |
(accum - 'Accounts'), | |
Accounts: (accum.Accounts[0 to -2] default []) << { | |
(accum.Accounts[-1] - "ExtendedData"), | |
ExtendedData: (accum.Accounts[-1].ExtendedData default {}) ++ ( | |
payload[1 to -1] reduce ((item,accum={ kvps: {}, pk: null }) -> | |
if (isEmpty(item)) { | |
kvps: accum.kvps, | |
pk: null | |
} | |
else if (accum.pk == null) { | |
kvps: accum.kvps ++ (item): [], | |
pk: item | |
} | |
else { | |
kvps: accum.kvps mapObject ((value, key, index) -> | |
if (key ~= accum.pk) (key): value << item | |
else (key): value | |
), | |
pk: accum.pk | |
} | |
) | |
).kvps | |
} | |
} | |
fun accountTrailer(payload: Array<String>, accum: Object) : Object = | |
{ | |
(accum - 'Accounts'), | |
Accounts: (accum.Accounts[0 to -2] default []) << { | |
(accum.Accounts[-1]), | |
AccountTotal: payload[1], | |
NumberOfRecords: payload[2] | |
} | |
} | |
fun accountTransaction(payload: Array<String>, accum: Object) : Object = | |
{ | |
(accum - 'Accounts'), | |
Accounts: (accum.Accounts[0 to -2] default []) << { | |
(accum.Accounts[-1] - 'Transactions'), | |
Transactions: (accum.Accounts[-1].Transactions default []) << { | |
TypeCode: payload[1], | |
DollarAmount: payload[2], | |
FundsType: payload[3], | |
ValueDate: payload[4], | |
ValueTime: payload[5], | |
Description: payload[6], | |
} | |
} | |
} | |
fun groupTrailer(payload: Array<String>, accum: Object) : Object = | |
accum ++ GroupTrailer: { | |
GroupControlTotal: payload[1], | |
NumberOfAccounts: payload[2], | |
NumberOfRecords: payload[3] | |
} | |
fun baiTrailer(payload: Array<String>, accum: Object) : Object = | |
accum ++ Trailer: { | |
FileControlTotal: payload[1], | |
NumberOfGroups: payload[2], | |
NumberOfRecords: payload[3] | |
} | |
var baiFuncs = { | |
"01": baiHeader, | |
"02": baiGroupHeader, | |
"03": baiSummary, | |
"16": accountTransaction, | |
"49": accountTrailer, | |
"88": baiSummaryExtension, | |
"98": groupTrailer, | |
"99": baiTrailer | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment