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

Is it the time now to include Codec support? #80

Open
MangelMaxime opened this issue Aug 27, 2020 · 3 comments
Open

Is it the time now to include Codec support? #80

MangelMaxime opened this issue Aug 27, 2020 · 3 comments
Milestone

Comments

@MangelMaxime
Copy link
Contributor

MangelMaxime commented Aug 27, 2020

Inspiration: https://package.elm-lang.org/packages/miniBill/elm-codec/latest/Codec#Codec

Ideas comes from twitter: https://twitter.com/huwmanity/status/1298891485258166272

And this is something I wanted to do for a long time as there is no good way to declare encoder/decoder right now and they can get out of sync easily etc.

It should also be designed with recommendation on how to structure the code to make it easier to use/transparent independently from the type attached to it from the final user POV.

For example, attach as a class member for DUs, classes etc.

Using a module of the name of the type for the type who can't have members.

Pseudocode to explains my idea:

open Thoth.Json
open Fable.Core

type Codec<'T> =
    {
        Encoder : obj
        Decoder : obj
    }

type MyEnum =
    | Value1 = 0
    | Value2 = 2

module MyEnum =

    let codec : Codec<MyEnum> = 
        {
            Encoder = 
                //...
                ""
            Decoder =
                //...
                ""
        }

type Animal =
    | Dog
    | Cat 

    static member codec : Codec<Animal> = 
        {
            Encoder = 
                //...
                ""
            Decoder =
                //...
                ""
        }

// Final user usage

MyEnum.codec |> ignore

Animal.codec |> ignore
@huwmanatee
Copy link

huwmanatee commented Aug 27, 2020

Here are some basic examples of codecs for unions I created using my port of the codec library:

module Example =
    open MiniBill.ElmCodec.Net

    type Animal =
    | Dog
    | Cat

    module Animal =
        let codec =
            Codec.custom
                (fun dog cat->
                    function
                    | Dog -> dog
                    | Cat -> cat
                )
            |> Codec.variant0 "dog" Dog
            |> Codec.variant0 "cat" Cat
            |> Codec.buildCustom

    type Input =
        | Digit of int
        | Letter of char 

    module Input =  
        let codec = 
            Codec.custom
                (fun digit letter -> 
                    function 
                    | Digit n -> digit n
                    | Letter c -> letter c
                )
            |> Codec.variant1 "digit" Digit Codec.int
            |> Codec.variant1 "letter" Letter Codec.char
            |> Codec.buildCustom

    type User =
        { EmailAddress: string
          PasswordHash: byte array
          Role: string
          Forenames: string
          Surname: string
        }

   module User =
        let codec : Codec<User> =
            Codec.object
                (fun emailAddress passwordHash role forenames surname ->
                    { EmailAddress = emailAddress
                      PasswordHash = passwordHash
                      Role = role
                      Forenames = forenames
                      Surname = surname
                    })
            |> Codec.field "emailAddress" (fun o -> o.EmailAddress) Codec.string
            |> Codec.field "passwordHash" (fun o -> o.PasswordHash) (Codec.array Codec.byte)
            |> Codec.field "role" (fun o -> o.Role) Codec.string
            |> Codec.field "forenames" (fun o -> o.Forenames) Codec.string
            |> Codec.field "surname" (fun o -> o.Surname) Codec.string
            |> Codec.buildObject

@MangelMaxime MangelMaxime added this to the Beyond milestone Jul 29, 2021
@njlr
Copy link
Contributor

njlr commented May 22, 2022

Not sure if this feature is still planned, but I was interested in what could be done building on top of the library without modifying it.

I put together a proof-of-concept library here: https://github.com/njlr/thoth-json-codec

I think the new Applicative CEs make this quite nice in F#!

@huwmanatee
Copy link

@njlr I like what you have done with your Codec library, especially the Computation Expressions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants