qntfy / kazaam

Arbitrary transformations of JSON in Golang
MIT License
283 stars 54 forks source link

Merge nested objects in default transformation for same key #115

Open riyaagrahari opened 2 years ago

riyaagrahari commented 2 years ago

Spec Config

[
  {
    "operation": "default",
    "spec": {
      "data.amount": "200",
      "data.receipt": "transaction2",
      "data.currency": "INR",
      "data.notes": {
        "beta": "transaction2",
        "alpha": "test notes"
      },
      "data.notes.fund_account_id": "a_JMjPtaaaaaaaaaa"
    }
  }
]

Input {}

Expected Output

{
  "data": {
    "amount": "200",
    "receipt": "transaction2",
    "currency": "INR",
    "notes": {
      "alpha": "test notes",
      "beta": "transaction2",
      "fund_account_id": "fa_JMjPtaaaaaaaaaa"
    }
  }
}

Facing intermittent merge failures for data.notes child objects. Sometimes we get output as expected and sometimes it's overriding the child object during merge and resolution for same key.

Intermittent incorrect output

{
  "data": {
    "amount": "200",
    "receipt": "transaction2",
    "currency": "INR",
    "notes": {
      "alpha": "test notes",
      "beta": "transaction2",
    }
  }
}

data.notes.fund_account_id is getting overwritten by data.notes, if it gets iterated first. This is possibly happening due to random order of map (*spec.Spec) while iterating in default.go.