Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support arrays of RawRepresentable types #450

Open
tonyarnold opened this issue Aug 17, 2021 · 0 comments
Open

Support arrays of RawRepresentable types #450

tonyarnold opened this issue Aug 17, 2021 · 0 comments
Labels
enhancement New feature or request

Comments

@tonyarnold
Copy link

I'm currently using a simple Set of enumerations to represent the role of a user in a certain context within my model, ie:

enum Role: Int16 {
     case licenseManager = 0
     case billingManager = 1
     case owner = 2
}

I would love to be able to declare the following:

// Model
class User: Model {
    @Field("roles")
    var roles: Set<Role>
}

// Migration 
.field("roles", .array(of: .int16), .required)

And have it be queryable, and saved to the database with a column type of smallint[].

Right now, I have to resort to workarounds like:

class User: Model {
    var roles: Set<Role> {
        get {
            Set(rolesRawValues.compactMap { Role(rawValue: $0) })
        }
        set {
            rolesRawValues = Set(newValue.map(\.rawValue))
        }
    }

    @Field("roles")
    var rolesRawValues: Set<Int16>
}

Which makes my Fluent queries end up looking like:

User.query(on: database)
  .filter(\.$rolesRawValues, .custom("@>"), [Role.owner.rawValue])

It's not ideal.

It would be great if this were supported out of the box. I can see that in a few places arrays of Strings have been special cased - include @Enum, but there's no support for other implicitly Codable types like Int and friends.

@tonyarnold tonyarnold added the enhancement New feature or request label Aug 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant