1
votes

I am working with an API that can have different types for it's attributes

The attributes can either be Ids or Objects

I want to build a generalized type that handles this for me with swift Codables

Example:

"platforms": [
    6
]

"platforms": [
    {
        "id": 6,
        "name": "PC (Microsoft Windows)",
        "slug": "win",
        "url": "https://www.igdb.com/platforms/win",
        "created_at": 1297639288000,
        "updated_at": 1470063140518,
        "website": "http://windows.microsoft.com/",
        "alternative_name": "mswin"
    }
]

I have created an indirect enum called ObjectType to handle this

extension ObjectType{
    enum CodingError: Error {
        case decoding(String)
    }
    enum CodableKeys: String, CodingKey {
        case Struct, Id
    }
}

/**
    ObjectType keeps track of struct expansion with two different 'states'
    Struct OR Int64
    If the request is expanded then the ObjectType will be a struct of that type. Otherwise it will be the id

    @param T the struct type it should expand to, if expanded.
*/
public indirect enum ObjectType<T: Codable>: Codable {
    case Struct(T)
    case Id(Int64)

    // decodes the response to the correct 'state', Struct or Int64.
    public init(from decoder: Decoder) throws {
        let values = try decoder.singleValueContainer()

        if let standardID = try? values.decode(Int64.self) {
            self = .Id(standardID)
        } else if let extendedID = try? values.decode(T.self) {
            self = .Struct(extendedID)
        } else {
            throw CodingError.decoding("Decoding Failed \(dump(values))")
        }
    }

    // encodes the response to the correct 'state', Struct or Int64.
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodableKeys.self)

        switch self {
        case let .Struct(extendedID):
            /* this line is weird */
            try container.encode(extendedID, forKey: .Struct)

        case let .Id(standardID):
            try container.encode(standardID, forKey: .Id)
        }
    }

This works fine for decoding but does not work for encoding structs.

Debugging the line "try container.encode(extendedID, forKey: .Struct)" in Xcode for the .Struct case returns "(())", empty struct.

I don't understand why the encoder return empty here, what am i doing wrong?

1

1 Answers

0
votes

So after trying a bunch of different things i managed to solve it and get my desired result.

I was not using the encoder correctly. This is my old code:

public func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodableKeys.self)

    switch self {
    case let .Struct(extendedID):
        /* this line is weird */
        try container.encode(extendedID, forKey: .Struct)

    case let .Id(standardID):
        try container.encode(standardID, forKey: .Id)
    }
}

This is the working code:

public func encode(to encoder: Encoder) throws {
/* var container = encoder.container(keyedBy: CodableKeys.self) */

    switch self {
    case let .Struct(extendedID):
        try extendedID.encode(to: encoder)
     /* try container.encode(extendedID, forKey: .Struct) */

    case let .Id(standardID):
        try standardID.encode(to: encoder)
     /* try container.encode(standardID, forKey: .Id) */
    }
}

The solution looks quite similar, but i still don't understand why this works and not my previous solution.