Skip to content

Enum Support #144

@wiredsister

Description

@wiredsister

Hi there,

Wondering if Enumerated Types would ever be supported at a library level? Would you be open to that type of feature as a contribution?

Example: let's say I have a union type

type User = Developer | API | Person

and in my Postgres database i have

CREATE TYPE user AS ENUM ('Developer', 'API', 'Person');

Would this make sense as an API?

open Npgsql.FSharp
open System

type UserData = {
  id: Guid
  kind: User
}

let getUser (uuid:Guid) : UserData = 
  Sql.connect Storage.connectionString 
  |> Sql.query $"SELECT * FROM {Storage.Users.table} WHERE id={uuid}"
  |> Sql.execute (fun read -> 
          { 
            id = read.uuid "id" 
            kind = read.enum<User> "kind" 
          }
        )

Or perhaps something even simpler, like:

open Npgsql.FSharp
open System

let toUser =
  function
  | "Developer" -> User.Developer
  | "API" -> User.API
  | "Person" -> User.Person 
  | _ -> failwith "FATAL: Unrecognized User Type"

type UserData = {
  id: Guid
  kind: User
}

let getUser (uuid:Guid) : UserData = 
  Sql.connect Storage.connectionString 
  |> Sql.query $"SELECT * FROM {Storage.Users.table} WHERE id={uuid}"
  |> Sql.execute (fun read -> 
          { 
            id = read.uuid "id" 
            kind = (read.enum "kind" |> toUser)
          }
        )

Or is this a solved problem that I'm just not finding anywhere?

Thank you for your great library.

Best,
Gina

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