Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.
This repository was 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

@usatie

Description

@usatie

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions