Closed zhumanrakhat closed 1 year ago
@zhumanrakhat thanks for reporting the issue! I'll check it.
I can explain the issue you observe.
First, when the following code is executed:
message := iso8583.NewMessage(spec)
if err := json.Unmarshal([]byte(data), message); err != nil {
log.Printf("Unmarshal error [error occurred] - %+v", err)
}
the following code inside the iso8583
package is executed:
func (m *Message) UnmarshalJSON(b []byte) error {
var data map[string]json.RawMessage
if err := json.Unmarshal(b, &data); err != nil {
return err
}
for id, rawMsg := range data { // < --- this is the root cause of random results
// ...
as you may see, we iterate over a map, which does not guarantee the order of keys to be the same every time. It means, that sometimes it can unmarshal some fields, but when it gets the field 1
it fails to unmarshal it and returns the error.
But because you ignore the error in your example and use the message that contains only some (each time it's a random fields, until field 1 is attempted to be unmarshal) unmarshalled fields to pack, you get different results each time.
It's certainly not an issue per se, but I'm considering whether we should sort the keys of the map to ensure UnmarshalJSON operates deterministically. @adamdecaf what do you think? We sort keys when we MarshalJSON
(just to make it... readable?) but here I'm leaning towards keeping it as is as it will negatively impact the performance of the method for no real benefit.
@zhumanrakhat in addition, I have found that when we marshal message to JSON, we don't include the bitmap, but when we unmarshal - we don't ignore it.
We can start a new thread for the discussion about including bitmap into the message JSON or not.
Bitmap was removed from JSON because of p.3 here
Readme is fixed by https://github.com/moov-io/iso8583/pull/244
@alovak thank you for detailed answer
Problem
Example snippet which unmarshals json to iso8583 format from the readme is unstable and does not work locally. There is an error occurred when parsing and produces randomly packed message. To reproduce issue, please copy snippet from demo section and try to launch several time. It should outputs different message each time...
Demo