marksands / BetterCodable

Better Codable through Property Wrappers
http://marksands.github.io/2019/10/21/better-codable-through-property-wrappers.html
MIT License
1.74k stars 80 forks source link

Separate Decodable and Encodable? #31

Closed lordzsolt closed 3 years ago

lordzsolt commented 3 years ago

Question

What are the options and thoughts on adding separate Decodable / Encodable support?

Problem

With the current implementation, everything relies on T: Codable.

So for example, the following code will not compile, because @DefaultEmptyArray requires ResponseObject to confirm to Encodable as well.

struct NetworkResponse: Decodable {
     @DefaultEmptyArray var someArray: [ResponseObject]
}

struct ResponseObject: Decodable {
    var age: Int
}

But I only care that NetworkResponse be Decodable, I don't need Encodable as well.

In order to make it compile. I need to conform/implement Encodable on ResponseObject. It's trivial of course for the current ResponseObject, but when the object is more complex and uses a custom decoder initializer, I'm left with 2 bad options:

Solutions

marksands commented 3 years ago

💯 I will see what I can do.

entropious commented 3 years ago

Hi. Any progress on this? I have the same problem with LossyArray. Don't know why T is restricted to Codable in current implementation. If LossyArray is implemented like this, it will support both Codable and Decodable.

@propertyWrapper
public struct LossyArray<T: Decodable>: Decodable {
    private struct AnyDecodableValue: Decodable {}
    private struct LossyDecodableValue<Value: Decodable>: Decodable {
        let value: Value

        public init(from decoder: Decoder) throws {
            let container = try decoder.singleValueContainer()
            value = try container.decode(Value.self)
        }
    }

    public var wrappedValue: [T]

    public init(wrappedValue: [T]) {
        self.wrappedValue = wrappedValue
    }

    public init(from decoder: Decoder) throws {
        var container = try decoder.unkeyedContainer()

        var elements: [T] = []
        while !container.isAtEnd {
            do {
                let value = try container.decode(LossyDecodableValue<T>.self).value
                elements.append(value)
            } catch {
                _ = try? container.decode(AnyDecodableValue.self)
            }
        }

        self.wrappedValue = elements
    }

}
lordzsolt commented 3 years ago

Don't know why T is restricted to Codable in current implementation.

Answering just this part. @LossyArray supports encoding as well, which your code doesn't.

Some people might want that feature.

yonaskolb commented 3 years ago

Keen to see this too. I think this should be possible to implement with extensions instead similar to how Equatable and Hashable are handled

extension DefaultCodable: Decodable where Default.DefaultValue: Decodable {
...
 }
extension DefaultCodable: Encodable where Default.DefaultValue: Encodable {
...
 }
yonaskolb commented 3 years ago

PR here https://github.com/marksands/BetterCodable/pull/38