Background
Basically I got an api that returns something like this:
"order_detail": [
{
"id": 6938,
"order_id": 6404,
"item_price": "4",
..
"item": {
"id": 12644,
"ref": "Iced Caffe Americano",
"short_description": "",
..
and in my decodable obj i got this
public struct OrderDetail: Decodable {
public let id: Int
public let order_id: Int
public let item_price: String?
..
public let item: Item?
and
public struct Item: Decodable {
public var id: Int
public var ref: String?
public var short_description: String?
The problem is that somewhere else in the code, there is a method that's expecting the Item object to have item_price.
Question
What I want to do is swizzle or mutate this constant Item object and dynamically add item_price property to it.. How can I do that?
Workarounds, other solutions
1. Change json
I know there are many other solutions to this same problem (I'm working on it as we speak, which is simply modifying the api endpoint to suit my needs).. but again that option is not always possible (ie suppose the backend team is separate)
2. Change the function expectation
That is also possible, but also not inexpensive as this function is used in many other places in the app which I don't potentially have control over