Home > Blockchain >  How to manually decode an array of items conforming to a custom protocol?
How to manually decode an array of items conforming to a custom protocol?

Time:09-22

I am trying to decode an array of items logicBlocks that conform to a custom protocol LogicBlock.

struct MyModel: Codable {
    var var1: String
    var var2: Int
    var logicBlocks: [LogicBlock]
}

I've looked at this question which asks how to decode an array of items, however I have a protocol which contains an enum like this:

enum LogicBlockTypeID: Int, Codable {
    case a = 500
    case b = 501
    case c = 502
}

protocol LogicBlock: Codable {
    var typeID: LogicBlockTypeID { get }
}

and structs implement LogicBlock like this:

struct Struct1: LogicBlock {
    var someVar = 32
    var typeID = .a
}

struct Struct2: LogicBlock {
    var someString = "hello"
    var typeID = .b
}

struct Struct2: LogicBlock {
    var typeID = .c
}

I have attempted to decode MyModel by using init(from decoder: Decoder)

var codeContainer = try values.nestedUnkeyedContainer(forKey: .code)
var parsedCode = [LogicBlock]()

enum codingKeys: String, CodingKey {
    ...
    case .logicBlocks
}

init(from decoder: Decoder) {
...
    var codeContainer = try values.nestedUnkeyedContainer(forKey: .code)
    var parsedCode = [LogicBlock]()
    while !codeContainer.isAtEnd {
        let nestedDecoder = try codeContainer.superDecoder()
        let block = try Program.decodeLogicBlock(from: nestedDecoder)
        parsedCode.append(block)
    }
}

private static func decodeLogicBlock(from decoder: Decoder) throws -> LogicBlock {
    let values = try decoder.container(keyedBy: LogicBlockTypeID.self)

    // SOMETHING TO DISTINGUISH WHAT TYPE WE HAVE

    return ...
}

How are we able to know the type of object that we are decoding here?

CodePudding user response:

You can implement a concrete type, i.e AnyLogicBlock that would expose two variables let typeID: LogicBlockTypeID and let wrapped: LogicBlock

Here is a simplified example:

enum TypeID: String, Codable {
    case a, b
}

protocol MyType {
    var type: TypeID { get }
}

struct MyConreteTypeA: MyType {
    var type: TypeID
    var someVar: Int
}

struct MyConreteTypeB: MyType {
    var type: TypeID
    var someString: String
}

struct AnyType: MyType, Decodable {
    private enum CodingKeys: String, CodingKey {
        case type, someVar, someString
    }

    let type: TypeID
    let wrapped: MyType

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)

        type = try container.decode(TypeID.self, forKey: .type)
        switch type {
        case .a:
            wrapped = MyConreteTypeA(type: type, someVar: try container.decode(Int.self, forKey: .someVar))
        case .b:
            wrapped = MyConreteTypeB(type: type, someString: try container.decode(String.self, forKey: .someString))
        }
    }
}

var json = #"""
[
  { "type": "a", "someVar": 1 },
  { "type": "b", "someString": "test" }
]
"""#

let result = try! JSONDecoder().decode([AnyType].self, from: json.data(using: .utf8)!)

for item in result {
    switch item.type {
    case .a:
        let casted = item.wrapped as! MyConreteTypeA
        print("MyConreteTypeA: \(casted)")
    case .b:
        let casted = item.wrapped as! MyConreteTypeB
        print("MyConreteTypeB: \(casted)")
    }
}

Alternatively you can implement Decodable for concrete types and delegate decoding to them after determining the expected type based on type ID property.

  • Related