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

Document decoding recursive structures #44

Open
mlms13 opened this issue Jun 21, 2019 · 2 comments
Open

Document decoding recursive structures #44

mlms13 opened this issue Jun 21, 2019 · 2 comments
Labels
documentation Documentation is missing or confusing
Milestone

Comments

@mlms13
Copy link
Owner

mlms13 commented Jun 21, 2019

This may very well already work perfectly. If that's the case, we should just document it and maybe add a test. If it doesn't work, we should make it possible.

The Elm example uses nested comments as their recursive structure. They have a special helper function (lazy), but that might be because their decoders have a newtype wrapper, while ours are functions (and might be more naturally lazy as a result?).

@mlms13
Copy link
Owner Author

mlms13 commented Nov 21, 2019

I haven't added it to the docs yet, but it's definitely possible without modifications:

module Comment = {
  type t = {
    text: string,
    author: string,
    timestamp: Js.Date.t,
    likes: int,
    replies: list(t),
  };

  let make = (text, author, timestamp, likes, replies) => {
    text,
    author,
    timestamp,
    likes,
    replies,
  };

  let rec decode = json =>
    Decode.Pipeline.(
      pure(make)
      |> field("text", string)
      |> field("author", string)
      |> field("timestamp", date)
      |> field("likes", intFromNumber)
      |> field("replies", list(decode))
      |> run(json)
    );
};

@mlms13 mlms13 changed the title Think about decoding recursive structures Document decoding recursive structures Nov 21, 2019
@mlms13 mlms13 added the documentation Documentation is missing or confusing label Apr 8, 2023
@mlms13
Copy link
Owner Author

mlms13 commented Apr 8, 2023

I'm going to wait for 2.x to document this, because it's easiest to understand the need for this in the context of record types, and record decoding will change quite a bit with the deprecation of Pipeline and the addition of letops.

@mlms13 mlms13 added this to the v2.0-rc.1 milestone Apr 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Documentation is missing or confusing
Projects
None yet
Development

No branches or pull requests

1 participant