GithubHelp home page GithubHelp logo

Comments (5)

marksands avatar marksands commented on July 30, 2024 1

💯 I will see what I can do.

from bettercodable.

entropious avatar entropious commented on July 30, 2024

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
    }
    
}

from bettercodable.

lordzsolt avatar lordzsolt commented on July 30, 2024

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.

from bettercodable.

yonaskolb avatar yonaskolb commented on July 30, 2024

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 {
...
 }

from bettercodable.

yonaskolb avatar yonaskolb commented on July 30, 2024

PR here #38

from bettercodable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.