Skip to content
This repository was archived by the owner on Aug 24, 2019. It is now read-only.
This repository was archived by the owner on Aug 24, 2019. It is now read-only.

Provide a default decode for RawRepresentable type #11

@nanoxd

Description

@nanoxd

👋 I was wondering if there was interest in me adding a default decode function for RawRepresentable types?

An example of what I'm using in my own project:

func decode<T: RawRepresentable>(_ dictionary: JSONDictionary, key: String) throws -> T {
    let object: Any = try decode(dictionary, key: key)
    
    guard let rawValue = object as? T.RawValue else {
        throw JSONDeserializationError.invalidAttributeType(key: key, expectedType: T.self, receivedValue: object)
    }
    
    guard let value = T(rawValue: rawValue) else {
       // Ideally represented by a new error case called `.invalidRawValue(key: key, rawValue: rawValue)`
        throw JSONDeserializationError.invalidAttribute(key: key)
    }
    
    return value
}

If there's interest, I can add the code, add a specific error case, update the readme, and add tests 😄 .

Regardless, thanks for the project. It's been a pleasure to work with.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions