Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

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

Open
usatie opened this issue Apr 2, 2019 · 0 comments

Comments

@usatie
Copy link

usatie commented Apr 2, 2019

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) }
}
usatie added a commit to usatie/core that referenced this issue Apr 2, 2019
…yedDecoder.decode ` to return the truthy value when the context is active
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant