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

ATDD : ATDgen backend to generate dlang code #349

Merged
merged 92 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 91 commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
6fbe67e
wip first copy atdd
Jun 7, 2023
8f15686
wip2
Jun 7, 2023
2947933
wipwip
Jun 7, 2023
20e6295
codegen: replace python keywords by dlang keywords
elrandar Jun 8, 2023
d7521f5
wip: compile records
Jun 7, 2023
d6c1360
d: add 80% of readers
elrandar Jun 8, 2023
b3e1467
wip: remove unused var
Jun 8, 2023
e861e48
wip: almost complete records <-> JSONValue
Jun 8, 2023
b2e5668
add writer/reader for d
elrandar Jun 8, 2023
88740a2
remove unrequired file + some edit on tuple_writer
Jun 8, 2023
430975c
properly escape %
Jun 8, 2023
6f4dbd2
make 'private functions' header a comment
Jun 8, 2023
7ffd14d
wip records except for missing field check
Jun 8, 2023
4ca8832
reduce duplication in dlang record serialization
Jun 8, 2023
fba5d37
d-ify things
Jun 8, 2023
9bad076
atdd: remove unneeded comments
Jun 8, 2023
256dd93
read / write fns : add obj -> tuplelist -> obj
elrandar Jun 8, 2023
d6cd638
fix check in when fromJSON
elrandar Jun 8, 2023
13fb231
atdd : fix aliasing
Jun 8, 2023
fb15eb7
atdd : fix writing of record fields
Jun 8, 2023
2ad62a7
cast to int instead of using long
elrandar Jun 8, 2023
4f6ba47
atdd : use correct toJson for list map
Jun 8, 2023
f235f5f
atdd : depythonify comments
Jun 8, 2023
8a525ec
atdd : explicitly name class when converting list elt to json
Jun 8, 2023
f323562
atdd: 80% done with variant
Jun 8, 2023
be42403
write / read fns: change from function to delegate, make read list re…
elrandar Jun 8, 2023
3608cc6
tentative fix to nested function toDelegate issue
elrandar Jun 8, 2023
6e689dc
fix backslash -> forward flash for comments
elrandar Jun 9, 2023
01d26ca
minor syntax fix
Jun 9, 2023
f560aef
fix single value struct order
elrandar Jun 9, 2023
c3af51a
camlcase all the thing
Jun 9, 2023
e3c8c04
handle abstract
Jun 9, 2023
31db086
m
Jun 9, 2023
4075e8d
small changes
elrandar Jun 9, 2023
3926a45
fix lambda for untyped (abstract)
elrandar Jun 9, 2023
2556ae8
don't explictly specify null
Jun 9, 2023
72236c3
wrap variant key in jsonvalue
Jun 9, 2023
c7c9f89
fix opt
Jun 9, 2023
f8710c5
w
Jun 9, 2023
632f59e
implement reader/writer for alias
Jun 9, 2023
908314b
fix tuple lambda
Jun 9, 2023
fb59292
spf
Jun 9, 2023
598e6ee
fix object writers
Jun 9, 2023
cbe47fe
add writer for assoc dic into array
elrandar Jun 9, 2023
375762a
add option read and write
elrandar Jun 9, 2023
98e23fc
atdd : wip work on dlang test harness
Jun 9, 2023
c323103
fix input type of lambda
elrandar Jun 9, 2023
08b508d
remove to and from json string generated code, fix module name, fix d…
elrandar Jun 9, 2023
e8164d8
handle parametrized return type for function that throws in ternary o…
elrandar Jun 9, 2023
ecf5a9d
add simple test that pass
elrandar Jun 9, 2023
39e1068
atdd : fix diff from expected test
Jun 9, 2023
6c6136a
atdd : fix lcd compile check
Jun 9, 2023
146900e
atdd : mention ldc version constraint in README
Jun 9, 2023
0535737
some more cleanup
Jun 12, 2023
bc2190b
atdd: regenerate expected test result
Jun 12, 2023
02a6209
small fixes
elrandar Jun 10, 2023
422eac5
add missing parenthessis in _atd_write_assoc_dict
elrandar Jun 12, 2023
861eff2
make atd functions private to module
elrandar Jun 12, 2023
14b66a1
fix tuple lambda: check for expect size
elrandar Jun 12, 2023
8ac0ea7
set of fixes to make everything test pass
elrandar Jun 12, 2023
9380638
add tests
elrandar Jun 12, 2023
c51ccef
write options properly (so that they can be read into option type) an…
elrandar Jun 12, 2023
7de97b7
test: make make test run atd tests
elrandar Sep 1, 2023
c7db33f
atdd: support aliasing the same type multiple times, and provide help…
elrandar Sep 3, 2023
001f1d0
atdd: add some tests for aliases
elrandar Sep 3, 2023
d287ab4
atdd: fix indentation and add type checking
elrandar Sep 4, 2023
54f0607
wrapping
elrandar Sep 5, 2023
eda5cbb
wrapping: handle wrap and unwrap while preserving type
elrandar Sep 5, 2023
5792392
global imports
elrandar Sep 5, 2023
319c91e
wip deal with aliases of aliases
elrandar Sep 6, 2023
92ca06e
make toJson a template and use typedef to pick right one
elrandar Sep 6, 2023
f148dbb
add auto everywhere to infer types
elrandar Sep 7, 2023
f87bf41
remove fixed size premable file
elrandar Sep 7, 2023
e83ba26
add readme
elrandar Sep 7, 2023
e775b21
edit main readme
elrandar Sep 7, 2023
729ad7d
make toJson and fromJson callable from @safe code
elrandar Sep 7, 2023
412c8bc
atdd: show appropriate error message when no annotation is present fo…
elrandar Sep 7, 2023
16e090a
atdd: fix: inverse wrap and unwrap functions
elrandar Sep 7, 2023
e4a8bbf
atdd: remove mentions of python, add changelog, remove unused test
elrandar Sep 7, 2023
0a3a081
atdd: add ldc2 dependency in opam file
elrandar Sep 7, 2023
3ba7f0e
atdd: remove alcotest dep for atdd
elrandar Sep 8, 2023
463326e
atdd: add proper error message for wrapping and fix warning during co…
elrandar Sep 8, 2023
a7c62bd
atdd: fix error message order
elrandar Sep 8, 2023
6cea0a9
atdd: fix opam file
Khady Sep 12, 2023
a8026be
atdd: use templates instead of passing delegates
elrandar Sep 9, 2023
e020cd7
atdd: edit dune-project to add ldc2 dep, and remove alcotest
elrandar Sep 13, 2023
3234a8c
atdd: add test-d target. remove duplicate test-ts target. remove ldc2…
elrandar Sep 13, 2023
3d46a43
atdd: add install ldc to ci system setup
elrandar Sep 13, 2023
17b4887
atdd: install ldc manually instead of through apt, to get latest stab…
elrandar Sep 13, 2023
9e03944
atdd: try to fix ci
elrandar Sep 13, 2023
de2b520
changes: add PR number for atdd
Khady Sep 19, 2023
33e919c
Merge branch 'master' into alexandre/atdd
Khady Sep 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .circleci/setup-system
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ sudo apt-get install -y \
# For JSON Schema and atdpy testing
pip install jsonschema pytest mypy flake8

# To get latest ldc version
source $(curl https://dlang.org/install.sh | bash -s ldc -a)
echo "source $(~/dlang/install.sh ldc -a)" >> "$BASH_ENV"

###### Sanity checks ######

echo 'check opam'
Expand Down Expand Up @@ -56,3 +60,6 @@ node --version

echo 'check npm'
npm --version

echo 'check ldc2 (dlang)'
ldc2 --version
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
master
-------------------
* atdd: Add `dlang` backend to generate D code from ATD definitions (#349)

2.12.0 (2023-05-12)
-------------------

Expand Down
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ DUNE ?= dune
.PHONY: all
all:
$(MAKE) -C atdpy clean-for-dune
$(MAKE) -C atdd clean-for-dune
$(MAKE) -C atdts clean-for-dune
$(DUNE) build

Expand Down Expand Up @@ -43,12 +44,13 @@ docker-build:
# to support all the target languages.
.PHONY: test
test:
$(MAKE) test-ts
$(MAKE) test-ocaml
$(MAKE) test-scala
$(MAKE) test-java
$(MAKE) test-python
$(MAKE) test-ts
$(MAKE) test-d


# Test the OCaml code used by all the backends
test-common:
Expand Down Expand Up @@ -86,6 +88,11 @@ test-ts:
$(MAKE) test-common
$(MAKE) -C atdts test

.PHONY: test-d
test-d:
$(MAKE) test-common
$(MAKE) -C atdd test

############################################################################

.PHONY: js
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Target languages currently supported:
* Python: [atdpy](atdpy)
* Scala: [atds](atds)
* TypeScript: [atdts](atdts)
* DLang: [atdd](atdd)

All can installed with opam e.g.
```
Expand Down
84 changes: 84 additions & 0 deletions atdd.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
synopsis: "DLang code generation for ATD APIs"
description: "DLang code generation for ATD APIs"
maintainer: [
"Louis Roché <louis@louisroche.net>"
"Martin Jambon <martin@mjambon.com>"
"Rudi Grinberg <me@rgrinberg.com>"
]
authors: [
"Martin Jambon <martin@mjambon.com>"
"Rudi Grinberg <rudi.grinberg@gmail.com>"
"Martin Jambon <martin@r2c.dev>"
"Martin Jambon <github@mjambon.com>"
"Ivan Jager <aij+git@mrph.org>"
"oleksiy <oleksiy.golovko@ahrefs.com>"
"David Sheets <sheets@alum.mit.edu>"
"Rudi Grinberg <me@rgrinberg.com>"
"Martin Jambon <martin@esper.com>"
"Jeff Meister <nanaki@gmail.com>"
"Caio Wakamatsu <caio.wakamatsu@ahrefs.com>"
"Carmelo Piccione <carmelo.piccione@gmail.com>"
"Daniel Weil <danweil68@gmail.com>"
"Egor Chemokhonenko <egor.chemohonenko@ahrefs.com>"
"Gabriel Scherer <gabriel.scherer@gmail.com>"
"Raman Varabets <roman.vorobets@gmail.com>"
"tzm <frank@boldsolutions.de>"
"Mathieu Baudet <mathieubaudet@fb.com>"
"Oleksiy Golovko <alexei.golovko@gmail.com>"
"Rauan Mayemir <rauan@mayemir.io>"
"Carmelo Piccione <cep1@solvuu.com>"
"John Billings <john@monkeynut.org>"
"Louis Roché <louis@louisroche.net>"
"Brendan Long <self@brendanlong.com>"
"Chris Yocum <cyocum@gmail.com>"
"Louis Roché (Ahrefs) <louis.roche@ahrefs.com>"
"Louis Roché <louis.roche@ahrefs.com>"
"Pavel Antoshkin <pavel.antoshkin@ahrefs.com>"
"Pierre Boutillier <pierre.boutillier@laposte.net>"
"Shon Feder <shon.feder@key.me>"
"Anurag Soni <anuragsoni.13@gmail.com>"
"Arjun Ravi Narayan <arjunravinarayan@gmail.com>"
"Asya-kawai <kawai-toshiki@aintek.xyz>"
"Christophe Troestler <christophe.Troestler@umons.ac.be>"
"Damien Doligez <ddoligez@janestreet.com>"
"Daniel M <dan.mntg@gmail.com>"
"Ding Xiang Fei <dingxiangfei2009@protonmail.ch>"
"François Pottier <francois.pottier@inria.fr>"
"Javier Chavarri <javier.chavarri@gmail.com>"
"Kate <kit.ty.kate@disroot.org>"
"Louis <louis.roche@ahrefs.com>"
"Louis Roché <louis@cryptosense.com>"
"Raman Varabets <raman+git@ahrefs.com>"
"Stephane Legrand <slegrand45@gmail.com>"
"Vincent Bernardoff <vb@luminar.eu.org>"
"haoyang <haoyang@esper.co>"
"pmundkur <prashanth.mundkur@gmail.com>"
"ygrek <ygrek@autistici.org>"
]
license: "MIT"
homepage: "https://github.com/ahrefs/atd"
bug-reports: "https://github.com/ahrefs/atd/issues"
depends: [
"dune" {>= "2.8"}
"ocaml" {>= "4.08"}
"atd" {>= "2.11.0"}
"cmdliner" {>= "1.1.0"}
"re"
"odoc" {with-doc}
]
dev-repo: "git+https://github.com/ahrefs/atd.git"
build: [
["dune" "subst"] {dev}
[
"dune"
"build"
"-p"
name
"-j"
jobs
"@install"
"@doc" {with-doc}
]
]
1 change: 1 addition & 0 deletions atdd/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin
33 changes: 33 additions & 0 deletions atdd/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#
# Dlang/JSON backend
#

DUNE ?= dune

.PHONY: build
build:
rm -f bin/atdd
$(MAKE) clean-for-dune
$(DUNE) build @all
mkdir -p bin
ln -s ../../_build/install/default/bin/atdd bin/atdd

# The symlink facilitates the development of test code that depends on the
# generated code.
.PHONY: test
test:
$(MAKE) clean-for-dune
$(DUNE) runtest -f; status=$$?; \
ln -s ../../../_build/default/atdd/test/dlang-tests/everything.d \
test/dlang-tests/everything.d && \
exit "$$status"

.PHONY: clean-for-dune
clean-for-dune:
rm -f test/dlang-tests/everything.d

.PHONY: clean
clean:
$(MAKE) clean-for-dune
$(DUNE) clean
rm -rf bin
47 changes: 47 additions & 0 deletions atdd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
Atdd
==

Atdd takes type definitions in the ATD format and derives `dlang`
classes that can read and write JSON data. This saves the developer the
labor writing boilerplate that converts between dicts and classes.

This allows safe interoperability with other languages supported by
ATD such as OCaml, Java, Python or Scala.

See the sample input type definitions
[everything.atd](test/atd-input/everything.atd) and
the D output [everything.d](test/dlang-expected/everything.d).

Requirements
--

Requirements for building and testing `atdd`:
* Opam and dependencies installed from the [`atd` project root](..)
with `make setup`.
* ldc >= 1.27.0

Requirements for generating D code:
* the `atdd` executable

Requirements for compiling the generated D code:
* ldc >= 1.27.0

Documentation
--

* TODO

Development notes
--

Build or rebuild with `make`. Test with `make test`. This requires
ldc2.

Running the tests is done from the `atdd/` main folder with `make
test`.

We have two kinds of tests for atdd:
* code generation and D tests:
* they generate D code from ATD files and compare the D output
against the [expectations](dlang-expected).
* the generated code is executed by some tests.
134 changes: 134 additions & 0 deletions atdd/src/bin/Atdd_main.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
(*
Entry point to the atdd command.
*)

open Printf
open Cmdliner

type conf = {
input_files: string list;
version: bool;
}

let run conf =
if conf.version then (
print_endline Atdd.Version.version;
exit 0
)
else
conf.input_files
|> List.iter (fun atd_file ->
Atdd.Codegen.run_file atd_file
)

(***************************************************************************)
(* Command-line processing *)
(***************************************************************************)

let error msg =
eprintf "Error: %s\n%!" msg;
exit 1

let input_files_term =
let info =
Arg.info [] (* list must be empty for anonymous arguments *)
~docv:"PATH"
~doc:"Input file in the ATD format with the '.atd' extension"
in
let default = [] in
Arg.value (Arg.pos_all Arg.file default info)

let version_term =
let info =
Arg.info ["version"]
~doc:"Prints the version of atdd and exits"
in
Arg.value (Arg.flag info)

let doc =
"Type-safe JSON serializers for D"

(*
The structure of the help page.
*)
let man = [
(* 'NAME' and 'SYNOPSIS' sections are inserted here by cmdliner. *)

`S Manpage.s_description; (* standard 'DESCRIPTION' section *)
`P "atdd turns a file containing type definitions into D classes \
that read, write, and validate JSON data. The generated code \
can be type-checked statically upon compilation to ensure user code agrees \
with the ATD interface.";

(* 'ARGUMENTS' and 'OPTIONS' sections are inserted here by cmdliner. *)

`S Manpage.s_examples; (* standard 'EXAMPLES' section *)
`P "The following is a sample ATD file. 'sample.atd' becomes 'sample.d' \
with the command 'atdd sample.atd'.";
`Pre "\
(* Sample ATD file sample.atd *)

type foo = {
name: string; (* required field *)
?description: string option; (* optional field *)
~tags: string list; (* optional with implicit default *)
~price <dlang default=\"0.99\">: float; (* explicit default *)
items: bar list;
}

(* sum type *)
type bar = [
| Thing <json name=\"thing\"> of int
| Nothing <json name=\"nothing\">
]
";

`S Manpage.s_authors;
`P "Martin Jambon <martin@r2c.dev>";

`S Manpage.s_bugs;
`P "Report issues at https://github.com/ahrefs/atd";

`S Manpage.s_see_also;
`P "atdgen, atdj, atds, atdts"
]

let cmdline_term run =
let combine input_files version =
run {
input_files;
version;
}
in
Term.(const combine
$ input_files_term
$ version_term
)

let parse_command_line_and_run run =
let info =
Cmd.info
~doc
~man
"atdd"
in
Cmd.v info (cmdline_term run) |> Cmd.eval |> exit

let safe_run conf =
try run conf
with
(* for other exceptions, we show a backtrace *)
| Failure msg -> error msg
| Atd.Ast.Atd_error msg -> error msg
| e ->
let trace = Printexc.get_backtrace () in
eprintf "Error: exception %s\n%s%!"
(Printexc.to_string e)
trace

let main () =
Printexc.record_backtrace true;
let conf = parse_command_line_and_run safe_run in
safe_run conf

let () = main ()
10 changes: 10 additions & 0 deletions atdd/src/bin/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(executable
(name Atdd_main)
(public_name atdd)
(package atdd)
(libraries
cmdliner
atdd
atd
)
)
Loading