Skip to content

Added streaming/IO support for MLI encoding and decoding - #21

Open
Anushre20 wants to merge 1 commit into
americanexpress:mainfrom
Anushre20:main
Open

Anushre20 wants to merge 1 commit into
americanexpress:mainfrom
Anushre20:main

Conversation

@Anushre20

Copy link
Copy Markdown

Description

This PR adds two new public functions to enable streaming/IO-based MLI encoding and decoding, making the library more suitable for real-world TCP socket implementations where data is read/written incrementally.

Changes

  • EncodeWriter(w io.Writer, key string, length int) error - Encodes the MLI for the given key and length, writing directly to an io.Writer
  • DecodeReader(r io.Reader, key string) (int, error) - Reads the MLI bytes from an io.Reader and returns the decoded message length
  • Private helper mliSize(key string) int for internal size lookup
  • Added io import

Motivation

The existing Encode/Decode API requires the full MLI byte slice upfront, which doesn't align well with streaming protocols like TCP where data arrives incrementally.

These new functions allow users to:

  • Write MLI + message directly to a network connection in one flow
  • Read MLI from a connection without pre-allocating byte slices
  • Build cleaner ISO 8583 message framing over raw sockets

Example Usage

// Writer side
func writeMessage(w io.Writer, message []byte) error {
    if err := simplemli.EncodeWriter(w, simplemli.MLI2I, len(message)); err != nil {
        return err
    }
    _, err := w.Write(message)
    return err
}

// Reader side
func readMessage(r io.Reader) ([]byte, error) {
    length, err := simplemli.DecodeReader(r, simplemli.MLI2I)
    if err != nil {
        return nil, err
    }

    message := make([]byte, length)
    if _, err := io.ReadFull(r, message); err != nil {
        return nil, err
    }

    return message, nil
}

Testing

  • All existing tests pass (32-bit and 64-bit)
  • Added 14 new test cases covering:
    • Round-trip encode/decode via bytes.Buffer for all 7 MLI types
    • Error cases: invalid key, negative length, insufficient bytes, EOF
  • Race detector: go test -race ./...
  • Benchmarks unchanged (no performance regression)
  • Code builds, vets, and formats cleanly

Backward Compatibility

✅ Fully backward compatible - no existing APIs modified, only new functions added

Checklist

  • Tests pass (go test -race ./...)
  • Code builds (go build ./...)
  • Vet passes (go vet ./...)
  • Formatted (gofmt -l .)
  • No breaking changes
  • Documentation in code comments

@CLAassistant

CLAassistant commented Sep 19, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@Anushre20

Copy link
Copy Markdown
Author

hi @amisol @kranthipoturaju @apsivam
please review my pull request
thankyou

@madflojo

Copy link
Copy Markdown
Member

Hello, thanks for the contribution.

We’ve intentionally kept I/O outside this library to keep its scope small and focused. Applications can combine the existing encoding and decoding functions with their own I/O and error handling.

Reader/writer helpers introduce additional decisions around partial reads and writes, error propagation, and recovery. We feel those decisions are better handled by the implementing applications.

@Anushre20

Copy link
Copy Markdown
Author

@madflojo so do i close this repository ?

also , is there any issue or task i can work upon this repository as this would be really helpful for my practical learning ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants