Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
8d27f4f
atdcpp: copy files from atdd
elrandar Feb 2, 2024
c275fb7
atdcpp: iterate on serializer generation
elrandar Feb 2, 2024
05a4975
rename
elrandar Feb 2, 2024
9e436c2
atdcpp: handle basic struct
elrandar Feb 2, 2024
fb16edf
atdcpp: fix compilation
elrandar Feb 2, 2024
1ad5013
atdcpp: add test_main
elrandar Feb 2, 2024
d91a4b8
atdcpp: get multiple level of list to work with function pointer inst…
elrandar Feb 26, 2024
30e7ea4
atdcpp: support recursive lists
elrandar Feb 26, 2024
a788edb
atdcpp: support type definitions
elrandar Feb 26, 2024
75bd2ba
atdcpp: tuple
elrandar Feb 27, 2024
4331819
atdcpp: handle variants
elrandar Feb 27, 2024
31c2531
atdcpp: fix name clashing with variants
elrandar Feb 27, 2024
485491d
atdcpp: wrapping
elrandar Feb 27, 2024
2fd3e5c
atdcpp: solve alias and namespace issue
elrandar Feb 27, 2024
4c45db7
atdcpp: make everything in root compile
elrandar Feb 27, 2024
26cb449
atdcpp: test root
elrandar Feb 27, 2024
c7fce01
atdcpp: change how we handle optional
elrandar Feb 27, 2024
7577070
atdcpp: fix the test situation
elrandar Feb 27, 2024
c3c660e
atdcpp: some cleanup
elrandar Feb 27, 2024
62fb7ec
atdcpp: opam file
elrandar Feb 27, 2024
2d5e5e6
atdcpp: handle includes
elrandar Feb 27, 2024
663c7be
atdcpp: forward declare variant components
elrandar Feb 28, 2024
faaf32a
atdcpp: separate into header and source file
elrandar Feb 28, 2024
afb3f5b
atdcpp: fix
elrandar Feb 28, 2024
2ef87a7
atdcpp: add cpp expected file
elrandar Mar 4, 2024
dcee981
atdcpp: small edit readme
elrandar Mar 4, 2024
c4ae9f5
atdcpp: properly handle recursion via wrapping with pointer
elrandar Mar 5, 2024
f7417a8
atdcpp: forward declaration and typedefs at top of file
elrandar Mar 6, 2024
cf107c9
atdcpp: refactor code to remove copied code
elrandar Mar 6, 2024
ea993a7
atdcpp: remove static from namespaces
elrandar Mar 6, 2024
19fb4b7
atdcpp: fix optional and nullable serialization
elrandar Mar 7, 2024
3fd0e15
atdcpp: add maybe unused to helper function and put them in anonymous…
elrandar Apr 5, 2024
2526270
atdcpp: add better exception logging
elrandar Apr 5, 2024
f0a492e
atdcpp: dont forget to include optional in header
elrandar Apr 5, 2024
398ff80
atdcpp: support cpp namespace definition
elrandar Apr 5, 2024
e6e5758
atdcpp: update test expected
elrandar Apr 6, 2024
5ad2f51
atdcpp: add enum storage
elrandar Apr 8, 2024
877daba
atdcpp: remove dlang mentions
elrandar Apr 9, 2024
7476df3
atdcpp: call .empty() for lists when no user default is set
elrandar Apr 9, 2024
cefe438
atdcpp: better parsing error reporting
elrandar Apr 11, 2024
580e6fc
atdcpp: get rapidjson from apt
elrandar May 3, 2024
6a987b5
atdcpp: add tests to makefile
elrandar May 3, 2024
2815395
atdcpp: tests on std=c++17
elrandar May 3, 2024
ea577a6
atdcpp: add memory include
elrandar May 3, 2024
0c2e9e1
atdcpp: add required imports in test
elrandar May 3, 2024
b62feb9
atdcpp: handle untyped things
elrandar May 3, 2024
426eff7
atdcpp: update expected files
elrandar May 3, 2024
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
4 changes: 4 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

# For atdcpp testing
sudo apt install -y \
rapidjson-dev

# 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"
Expand Down
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Unreleased
* atdd: Workaround d compiler bug regarding declaration order when using aliases (#393)
Algebraic data types (SumType) now uses `alias this` syntax.
* atdgen: Add support for `<json open_enum>` in Melange (#401)
* atdcpp: Initial Release

2.15.0 (2023-10-26)
-------------------
Expand Down
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ DUNE ?= dune
all:
$(MAKE) -C atdpy clean-for-dune
$(MAKE) -C atdd clean-for-dune
$(MAKE) -C atdcpp clean-for-dune
$(MAKE) -C atdts clean-for-dune
$(DUNE) build

Expand Down Expand Up @@ -50,6 +51,7 @@ test:
$(MAKE) test-python
$(MAKE) test-ts
$(MAKE) test-d
$(MAKE) test-cpp


# Test the OCaml code used by all the backends
Expand Down Expand Up @@ -94,6 +96,11 @@ test-d:
$(MAKE) test-common
$(MAKE) -C atdd test

.PHONY: test-cpp
test-cpp:
$(MAKE) test-common
$(MAKE) -C atdcpp 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 @@ -8,6 +8,7 @@ cross-language data types. It is used as input to generate efficient
and type-safe serializers, deserializers and validators.

Target programming languages currently supported:
* C++: [atdcpp](atdcpp)
* DLang: [atdd](atdd)
* Java: [atdj](atdj)
* OCaml, Melange: [atdgen](atdgen)
Expand Down
84 changes: 84 additions & 0 deletions atdcpp.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: "C++ code generation for ATD APIs"
description: "C++ 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 atdcpp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin
36 changes: 36 additions & 0 deletions atdcpp/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#
# Dlang/JSON backend
#

DUNE ?= dune

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

# 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/atdcpp/test/cpp-tests/everything_atd.hpp \
test/cpp-tests/everything_atd.hpp && \
ln -s ../../../_build/default/atdcpp/test/cpp-tests/everything_atd.cpp \
test/cpp-tests/everything_atd.cpp && \
exit "$$status"

.PHONY: clean-for-dune
clean-for-dune:
rm -f test/cpp-tests/everything_atd.hpp
rm -f test/cpp-tests/everything_atd.cpp

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

atdcpp takes type definitions in the ATD format and derives `C++`
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 C++ output [everything.hpp](test/cpp-expected/everything.hpp).

This implementation makes use of the RapidJson C++ library.

Requirements
--

Requirements for building and testing `atdcpp`:
* Opam and dependencies installed from the [`atd` project root](..)
with `make setup`.
* gcc / clang
* librapidjson

Requirements for generating C++ code:
* the `atdcpp` executable

Requirements for compiling the generated C++ code:
* A working C++ compiler (gcc / clang)
* The rapidjson library

Documentation
--

* TODO

Development notes
--

Build or rebuild with `make`. Test with `make test`.

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

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

open Printf
open Cmdliner

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

let run conf =
if conf.version then (
print_endline Atdcpp.Version.version;
exit 0
)
else
conf.input_files
|> List.iter (fun atd_file ->
Atdcpp.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 Atdcpp 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 "Atdcpp 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 'Atdcpp 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 <cpp 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
"Atdcpp"
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 atdcpp/src/bin/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(executable
(name atdcpp_main)
(public_name atdcpp)
(package atdcpp)
(libraries
cmdliner
atdcpp
atd
)
)
Loading