FSharp.Data icon indicating copy to clipboard operation
FSharp.Data copied to clipboard

Idea: Add CE Builder for Json erased types (as they are not records and with is missing)

Open jkone27 opened this issue 3 years ago • 5 comments

Would be pretty useful to have CE builders for erased type, e.g. JsonProvider provided types, as they are immutable and using the full constructor is annoying especially for big objects...

type MyProvidedType = 
   JsonProvider<"""{ "name" : "hello", "test" : { "nested" : true }} """>

instead of

let myJsonValue = new MyJsonProvider
   .Root(name="test", new MyJsonProvider.Nester(false)) 
   // quickly becomes cumbersome for big object hierarchies! try with a big json file

do..


let my_provided_ce = MyProvidedType.Builder()

let myJsonValue = // MyProvidedType.Root 
     my_provided_ce {   // uses Sample as base for builder
         name "test" 
    }
    
let myOtherValue =  // MyProvidedType.Root 
     my_provided_ce {   // uses Sample as base for builder
         test {
            nested false
         }
    }

jkone27 avatar Jan 24 '22 13:01 jkone27

@jkone27 would you be interested in submitting a PR with a proposed implementation?

cartermp avatar Jan 24 '22 16:01 cartermp

i could give it a try but i have not much idea where to start 😆, i did this (see below) once to add "copy mutation" to JsonProvider erased type, but ofc it's quite hacky and messy...

https://github.com/jkone27/FSharp.Data.Mutator

so i thought a similar approach could make it easier to work with and maybe useful in general for other erased types too? will try to checkout the repo as a start 👍

jkone27 avatar Jan 24 '22 16:01 jkone27

In the topic of possibly extending Fsharp.Data, which one would be the easiest option between these as per implementation ? Pros/cons/possible

  1. Allow JsonValue setters in generated types making them mutable (is it possible with erased types?) trike passing allowMutableProperties in the constructor of the provider
  2. Somehow generate record types instead of current types, as they have already the with copy mutator construct built in… (not sure if it’s feasible)
  3. Adding the CEs as proposed in the current approach (would it be for all types in Helper.fs ???

jkone27 avatar Jan 24 '22 16:01 jkone27

as per option 3 (add a CEs for generated types), should it be done in Helper.fs around here? image

or here but then i guess it becomes something more specific, like each provider needs to add the extra CE builder to constructor..

image

jkone27 avatar Jan 24 '22 17:01 jkone27

one way i could add an optional setter code, this wouldn't require the overcomplication of adding the CEs methods (with CustomOperationAttribute) maybe... could be done in JsonGenerator.fs

(https://gist.github.com/odytrice/313b2107f6ad52de12ae89f35941d4b2)

jkone27 avatar Jan 24 '22 19:01 jkone27