moov-io / bai2

BAI2 file format is a cash management balance reporting standard
Apache License 2.0
18 stars 9 forks source link

Discussion: Calculating trailer aggregates #109

Open brian-pickens opened 6 months ago

brian-pickens commented 6 months ago

I am investigating how one might update the library to generate the trailer record aggregates. At first this seemed to me like it should be a relatively simple task by following some of the ACH library Create() patterns and iterating through the hierarchy.

However as I've come to understand the file specification more, and from reading through the bai2 library code, I can see counting the continuation records is becoming a bit of a foil. In particular, reading through string() in pkg/lib/record_transaction_detail.go#L114 and reading through util.WriteBuffer() pkg/util/write.go#L16, I can see that the total number of records is also directly tied to the file's Physical Record Length which follows the rules of the file spec.

I have some ideas of how I could count the number or records, which I can present both here and/or in a PR but I am curious, what are your thoughts on this issue, and why weren't the aggregate fields calculated in the design of the library to this point?

brian-pickens commented 6 months ago

Here is one idea I had. Give each record type a recordCount() function like this. You would reuse the existing compile logic in string() to find the total records.

var accountIdentifierCountExpression = regexp.MustCompile(`(?m:^(?:(?:03)|(?:16)|(?:88)))`)
func (r *accountIdentifier) recordCount(opts ...int64) int {
    records := r.string(opts...)
    result := accountIdentifierCountExpression.FindAllStringSubmatch(records, 10000)
    return len(result)
}
adamdecaf commented 6 months ago

Adding recordCount makes sense to me. I'm also wondering if we should hide some of those computed fields/records and only offer Getter's to them.

brian-pickens commented 6 months ago

Actually I have a PR incoming. Feel free to critique it. My approach is to offer a non-breaking change.