vapor / core

🌎 Utility package containing tools for byte manipulation, Codable, OS APIs, and debugging.
MIT License
82 stars 50 forks source link

ReflectionKeyedDecoder.decode() is not returning the "truthy" value when the context is active #200

Open usatie opened 5 years ago

usatie commented 5 years ago

I think when the context is active, it should return the reflected.1. However currently it returns reflected.0.

    func decode<T>(_ type: T.Type, forKey key: K) throws -> T where T : Decodable {
        if nextIsOptional {
            context.addProperty(type: T?.self, at: codingPath + [key])
            nextIsOptional = false
        } else {
            context.addProperty(type: T.self, at: codingPath + [key])
        }
        if let type = T.self as? AnyReflectionDecodable.Type, let reflected = try? type.anyReflectDecoded() {
            if context.isActive {
                context.activeCodingPath = codingPath + [key]
                return reflected.0 as! T
            }
            return reflected.1 as! T
        } else {
            let decoder = ReflectionDecoder(codingPath: codingPath + [key], context: context)
            return try T(from: decoder)
        }
    }

https://github.com/vapor/core/blob/master/Sources/Core/CodableReflection/ReflectionDecoders.swift#L144

The default implementation of ReflectionDecodable sets the .0 to be falsy value and .1 to be truthy value. eg.

extension Bool: ReflectionDecodable {
    /// See `ReflectionDecodable.reflectDecoded()` for more information.
    public static func reflectDecoded() -> (Bool, Bool) { return (false, true) }
}

extension FixedWidthInteger {
    /// See `ReflectionDecodable.reflectDecoded()` for more information.
    public static func reflectDecoded() -> (Self, Self) { return (0, 1) }
}