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

Add extra deriving option for constant variant constructors #23

Open
patricoferris opened this issue Apr 29, 2021 · 12 comments
Open

Add extra deriving option for constant variant constructors #23

patricoferris opened this issue Apr 29, 2021 · 12 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@patricoferris
Copy link
Owner

A very common pattern is to want to encode string values as a polymorphic (or not) variant with constant constructors. For example:

module Member = struct 
  type t = [ `Student | `Professor ][@@deriving yaml]
end 

type t = { name : string; member : Member.t }[@@deriving yaml]

The standard [@@deriving yaml] deriver has to deal with the possibility of non-constant constructors, so these constant ones are encode as Student: [] which is annoying because then we have:

let person = { name = "Alice"; member = `Student } 

encoding to the following yaml:

name: Alice
member: 
  Student: []

What would be nice is to opt into some string only version that generates the following code:

let of_yaml = function
    | `String "Student" -> Ok `Student
    | `String "Professor" -> Ok `Professor
    | _ -> Error (`Msg "Unknown value ")

let to_yaml = function
  | `Student -> `String "Student"
  | `Duplicate -> `String "Professor"

And additionally an extra little [@value string] to override the default of using the constructor name verbatim.

@patricoferris patricoferris added enhancement New feature or request good first issue Good for newcomers labels Apr 29, 2021
@patricoferris patricoferris self-assigned this Apr 29, 2021
@patricoferris patricoferris removed their assignment Oct 26, 2021
@desirekaleba
Copy link

Hi @patricoferris, Can I be assigned to this issue please?

@pitag-ha
Copy link

Hi @desirekaleba, nice that you want to contribute to OCaml! Do you already have OCaml up and running on your computer?

@desirekaleba
Copy link

Hi @pitag-ha, Yes I have Ocaml up and running on my computer. I followed this to achieve that.

@pitag-ha
Copy link

Hi @pitag-ha, Yes I have Ocaml up and running on my computer. I followed this to achieve that.

Cool! So yes: perfect to start working on an issue now! Which one do you prefer? This one here or the one on ppx_yojson that you've expressed interest in?

@desirekaleba
Copy link

I prefer starting with this one here.

@pitag-ha
Copy link

Cool. Don't hesitate to ask, if you have any questions.

@desirekaleba
Copy link

Hi @pitag-ha,

I used the previous days to skill up my OCaml skills. I am looking at the issue description but I don't fully understand how to deal with it. Can this tool be installed via opam so that I can first have a look at the current behavior?

@pitag-ha
Copy link

Hi @desirekaleba,

Yes, the deriver can be installed via opam:

opam install ppx_deriving_yaml

Also, I was just about to explain you how to load the deriver into the OCaml toplevel (in OCaml, we say toplevel to what in other languages is called REPL), but I've just tried myself and there might be a bug. I'll have a closer look soon and let you know. In the meanwhile, you can just set up a project with dune init exe <some_name> and try the deriver in there. Have you already had a look at OCaml up and running?

@desirekaleba
Copy link

Hi @pitag-ha,

Yes, I have Ocaml up and running. I also took some time to go through A First Hour with OCaml, although this did not only take an hour.

I now have a clear understanding of Ocaml. I can start a new project, write some code, build it and run the executable.

I am now looking for a way to first test this tool to fully understand how it works.

@patricoferris
Copy link
Owner Author

Hi both :))

I just opened PR #30 -- this adds a little contributing guide and also "expect-style tests". The tests in test/expect takes files that uses the deriver and then outputs the generated OCaml code. This might make it easier to start understanding what exactly the deriver does and also help when changing the output to add new features.

@patricoferris
Copy link
Owner Author

Also @pitag-ha

Also, I was just about to explain you how to load the deriver into the OCaml toplevel (in OCaml, we say toplevel to what in other languages is called REPL), but I've just tried myself and there might be a bug.

I think there might be (see #22). If you have utop installed (opam install utop) then you should be able to load it in there via dune by executing dune utop at the root of the project.

utop # type t = { name : string } [@@deriving yaml];;
type t = { name : string; }
val to_yaml : t -> [> `O of (string * [> `String of string ]) list ] = <fun>
val of_yaml :
  [> `O of (string * Yaml.value) list ] -> (t, [> `Msg of string ]) result =
  <fun>

@patricoferris
Copy link
Owner Author

@pitag-ha just to follow-up, I don't think this is a ppx/ppxlib problem, just a yaml one avsm/ocaml-yaml#60

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants