Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Add an optionalFrom to create optional array of RawRepresentables #66

Closed
marcelofabri opened this issue Jul 29, 2016 · 2 comments
Closed

Comments

@marcelofabri
Copy link

#61 introduced a function to create an array of RawRepresentables.

However, we have a model that contains an optional array of enums, so optionalFrom would be needed. An implementation would be:

@warn_unused_result
public func optionalFrom<T: RawRepresentable where T.RawValue: Convertible,
    T.RawValue == T.RawValue.ConvertedType>(field: String, defaultValue: T? = nil) -> [T]?
{
    let value = try? self.JSONFromField(field)
    guard let array = value as? [AnyObject] else {
        return nil
    }

    let rawValues = try? array.map { try T.RawValue.fromMap($0) }
    return rawValues?.flatMap { T(rawValue: $0) ?? defaultValue }
}

If it's a welcome addition, I can make a PR adding it and some tests 🚀

@marcelofabri
Copy link
Author

Actually, just this would be enough:

public func optionalFrom<T: RawRepresentable where T.RawValue: Convertible,
        T.RawValue == T.RawValue.ConvertedType>(field: String, defaultValue: T? = nil) -> [T]?
    {
        return try? self.from(field, defaultValue: defaultValue)
    }

@keith
Copy link
Contributor

keith commented Nov 1, 2017

This is super super super late, but I just merged #125 with support for this. Sorry about that.

@keith keith closed this as completed Nov 1, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants