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

Allow arguments to adapters #184

Closed
yonilevy opened this issue Feb 6, 2019 · 3 comments
Closed

Allow arguments to adapters #184

yonilevy opened this issue Feb 6, 2019 · 3 comments
Labels
feature request Big and small feature requests target:ocaml Issues related to the OCaml backend (atdgen)

Comments

@yonilevy
Copy link

yonilevy commented Feb 6, 2019

[Feature Request]
[Bear with me, as I'm new to yojson/atd and OCaml in general]

I find adapters to be critical when working with external json structures, which I presume is a very common use case. The ability to pass arguments to adapters would make them much more flexible, readable within the .atd context, and allow for reuse and sharing of adapters.

Two use-case examples:

  1. The provided Atdgen_runtime.Json_adapter.Type_field to take "type" field key as an argument
  2. I've written an adapter to "lift" some fields, so {"a": 1, "b": 2, "c": 3} when lifting with "body", ["a"] would become {"a": 1, "body": {"b": 2, "c": 3}} -- it'd be great if i could pass those arguments in the .atd file.

Does that sound reasonable at all?

@mjambon mjambon added the feature request Big and small feature requests label Feb 6, 2019
@mjambon
Copy link
Collaborator

mjambon commented Feb 6, 2019

It seems reasonable to me. Keep in mind that json adapters are a relatively recent feature.

There are a few things to take into account:

  • We could support annotations like <json adapter.ocaml="Lift" adapter.ocaml_param="(\"body\", [\"a\"])">. In this case, the presence of ocaml_param would indicate that the functions normalize and restore take its value as an extra argument. Another option I can think of would be to support <json adapter.ocaml_normalize="Lift.normalize \"body\" [\"a\"]" adapter.ocaml_restore="Lift.restore \"body\" [\"a\"]">.
  • Having to escape double quotes isn't very nice but I don't know if there's a better solution without making things too complicated.
  • We're short in personnel. If you or someone else could post first a detailed proposal and later the (simple) implementation with documentation, it would be ideal.

@yonilevy
Copy link
Author

yonilevy commented Feb 6, 2019

Great!

Your first suggested syntax is what I had in mind, I agree the escaping problem is annoying but that it'd be best to start with that and perhaps improve in the future. I'll try to dive in and see if I can come up with something.

@mjambon
Copy link
Collaborator

mjambon commented May 10, 2022

Implemented by #232

@mjambon mjambon closed this as completed May 10, 2022
mjambon added a commit to mjambon/opam-repository that referenced this issue May 18, 2022
…n-codec-runtime and atd (2.7.0)

CHANGES:

* Add ability to specify JSON/OCaml adapter with the arbitrary code
  using `<json adapter.to_ocaml="..." adapter.from_ocaml="...">` (ahrefs/atd#184).
* atdcat: add option `-jsonschema-no-additional-properties` for JSON Schema
  output to specify that JSON objects may not have extra properties
  (ahrefs/atd#293, ahrefs/atd#294).
* atdcat: add `title` field to JSON Schema output containing the name
  of root type (ahrefs/atd#294).
* atdcat: add command-line option to choose the version of JSON Schema
  to target. Options are the latest version "Draft 2020-12" and the
  previous version "Draft 2019-09" (ahrefs/atd#294).
* ATD language: the `abstract` built-in can now be used like any
  other type to hold untyped data, if the implementation supports it.
  The supported targets so far are OCaml/JSON (atdgen), Python
  (atdpy), TypeScript (atdts), JSON Schema (atdcat) (ahrefs/atd#295).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Big and small feature requests target:ocaml Issues related to the OCaml backend (atdgen)
Projects
None yet
Development

No branches or pull requests

2 participants