lundi 23 novembre 2020

Unmarshal a struct out of data and use what's left over

I am looking to use the fields that are left over from what doesn't fit from unmarshaling the struct.

type Data struct {
     ID    string   `json:"id"`
     Name  string.  `json:"name"`
}

The data coming in sometimes matches the struct,

{
  "id": "ABC-123"
  "name": "Foo"
}

and sometimes it has extra key value pairs, with unknown keys or number of extra keys.

{
  "id": "ABC-1234"
  "name": "Bar"
  "color": yellow
  "shape": "oval"
  ...
}

I am interested in obtaining the diff from the two:

{
  "color": yellow
  "shape": "oval"
  ...
}

Using a custom UnMarshaller seems ideal, but I am running into issues with 'removing' a struct from the incoming response data.

dataStruct := make([]*Data, 0)

err := CustomUnmarshal(response.Body, &dataStruct)




Aucun commentaire:

Enregistrer un commentaire