moov-io / iso8583

A golang implementation to marshal and unmarshal iso8583 message.
https://moov.io
Apache License 2.0
353 stars 105 forks source link

Add `message.Marshal()` and `message.Unmarshal()` with index tag support #163

Closed alovak closed 2 years ago

alovak commented 2 years ago

Changes in this PR are not backward compatible! Few things have changed. Hopefully, migration will not be painful for you. If you have any questions, please, let us know here or in our community Slack channel.

You can install and test it using v0.10.0-beta: go get github.com/moov-io/iso8583@v0.10.0-beta

Changes:

data.F1.Value // in previous versions, values were populated during unpacking, but now they are not


Now `message.SetData(data)` only sets field values and you can't get values back to the struct using it. In order to get values into the struct you have to use `message.Unmarshal(data)` like this:

```go
message.Unpack(rawMessage)
data := &ISOMessage{}
mesage.Unmarshall(data)

data.F1.Value //  now you can get the value

Migration Guide

Two main changes that you may need to do in your code:

  1. Replace message.SetData(data) with message.Marshal(data). Latter simply sets field values with values provided in the data struct. Without any side effects.
  2. Replace message.Data()(*YouMessageTypehere) with message.Unmarshal(data). It sets data struct values with message field values. No side-effects.
  3. If you used SetData() with Unpack to get access to message field values, then you have to replace it with just message.Unmarshal(data).

You have to change your code from this

type ISO87Data struct {
    F0 *field.String
    F2 *field.String
    F4 *field.String
}

message := NewMessage(spec)
data := &ISO87Data{}
err := message.SetData(data) // you "link" data internally
err = message.Unpack(rawMsg) // here values are populated into linked data struct. it will not work anymore.

// access field values
data.F0.Value
data.F2.Value
data.F4.Value

to this:

type ISO87Data struct {
    F0 *field.String
    F2 *field.String
    F4 *field.String
}

message := NewMessage(spec)
err := message.Unpack(rawMsg)

data := &ISO87Data{}
err = message.Unmarshal(data)

// access field values
data.F0.Value
data.F2.Value
data.F4.Value
alovak commented 2 years ago

@cheukwing @louisheath please, take a look!