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

Operator <|> logic looks wrong, why would I need to use both codes for encoding #141

Open
xperiandri opened this issue Jul 20, 2023 · 3 comments

Comments

@xperiandri
Copy link

I expect that operator <|> allows to execute the first decoder but if it fails try the alternative one. But for encoding that it always uses a source encoder.
While currently it uses both encoders that has no sense to me.

@wallymathieu
Copy link
Member

Do you have an example so that we can add a reproduction for this issue @xperiandri ?

@xperiandri
Copy link
Author

xperiandri commented Mar 21, 2024

Imagine I have an old wrong codec stroring

{
  "Wrong": "",
  "right": 5
}

then I write code that stores both properties correctly

{
  "wrong": "",
  "right": 5
}

then I expect my output to be

{
  "wrong": "",
  "right": 5
}

but not

{
  "Wrong": "",
  "wrong": "",
  "right": 5
}

Reading is sequential (try next if failed)
But writing is single (the first one codec wins)

@wallymathieu
Copy link
Member

wallymathieu commented Mar 23, 2024

So for an example:

#r "nuget: System.Json, 4.7.1"
#r "nuget: FSharpPlus, 1.2.2"
#r "nuget: Fleece.SystemTextJson, 0.10.0"

open Fleece
open Fleece.SystemTextJson

type Rectangle = Rectangle of width  : float* length : float
    with
        static member create w l = Rectangle (w, l)
        static member oldCodec = 
            Rectangle.create
            <!> jreq "Wrong" (function Rectangle (x, _) -> Some x)
            <*> jreq "right" (function Rectangle (_, x) -> Some x)
        static member newCodec =
            Rectangle.create
            <!> jreq "wrong" (function Rectangle (x, _) -> Some x)
            <*> jreq "right" (function Rectangle (_, x) -> Some x)
        static member JsonObjCodec : Codec<PropertyList<Encoding>, Rectangle> =
            Rectangle.newCodec <|> Rectangle.oldCodec

let x = Rectangle (1.0,1.0);;  
toJsonText x;;

this seems a bit like:

let y = 0b011 ||| 0b101
y.ToString("B")

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

2 participants