Skip to content

Commit 612505d

Browse files
authored
Merge pull request #311 from ahrefs/mj-yojson2
Adapt to use Yojson 2.0 API (retry #299)
2 parents b995e68 + 7473a59 commit 612505d

30 files changed

+1294
-1291
lines changed

CHANGES.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
Master
22
------------------
33

4-
* atdpy: Support recursive definitions
4+
* atdgen: use Yojson 2.0 API (#299)
5+
* atdpy: Support recursive definitions (#315)
56
* atdts: fix nullable object field writer (#312)
67

7-
88
2.9.1 (2022-06-10)
99
------------------
1010

atdcat/test/schema-draft-2019-09.expected.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
{
22
"$schema": "https://json-schema.org/draft/2019-09/schema",
33
"title": "root",
4-
"description":
5-
"Translated by atdcat from 'schema.atd'.\n\nThis is the title. Here's a code block:\n\n{{{\nthis is line 1\nthis is line 2\n}}}\n\nThis is the root object. For example, the empty object {{ {} }} is invalid.",
4+
"description": "Translated by atdcat from 'schema.atd'.\n\nThis is the title. Here's a code block:\n\n{{{\nthis is line 1\nthis is line 2\n}}}\n\nThis is the root object. For example, the empty object {{ {} }} is invalid.",
65
"type": "object",
76
"required": [
87
"ID", "items", "aliased", "point", "kinds", "assoc1", "assoc2"
98
],
109
"properties": {
1110
"ID": { "description": "This is the 'id' field.", "type": "string" },
1211
"items": {
13-
"description":
14-
"An example of JSON value is {{[[1, 2], [3], [4, 5, 6]]}}",
12+
"description": "An example of JSON value is {{[[1, 2], [3], [4, 5, 6]]}}",
1513
"type": "array",
1614
"items": { "type": "array", "items": { "type": "integer" } }
1715
},

atdcat/test/schema-no-xprop.expected.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"$schema": "https://json-schema.org/draft/2020-12/schema",
33
"title": "root",
4-
"description":
5-
"Translated by atdcat from 'schema.atd'.\n\nThis is the title. Here's a code block:\n\n{{{\nthis is line 1\nthis is line 2\n}}}\n\nThis is the root object. For example, the empty object {{ {} }} is invalid.",
4+
"description": "Translated by atdcat from 'schema.atd'.\n\nThis is the title. Here's a code block:\n\n{{{\nthis is line 1\nthis is line 2\n}}}\n\nThis is the root object. For example, the empty object {{ {} }} is invalid.",
65
"type": "object",
76
"required": [
87
"ID", "items", "aliased", "point", "kinds", "assoc1", "assoc2"
@@ -11,8 +10,7 @@
1110
"properties": {
1211
"ID": { "description": "This is the 'id' field.", "type": "string" },
1312
"items": {
14-
"description":
15-
"An example of JSON value is {{[[1, 2], [3], [4, 5, 6]]}}",
13+
"description": "An example of JSON value is {{[[1, 2], [3], [4, 5, 6]]}}",
1614
"type": "array",
1715
"items": { "type": "array", "items": { "type": "integer" } }
1816
},

atdcat/test/schema.expected.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
{
22
"$schema": "https://json-schema.org/draft/2020-12/schema",
33
"title": "root",
4-
"description":
5-
"Translated by atdcat from 'schema.atd'.\n\nThis is the title. Here's a code block:\n\n{{{\nthis is line 1\nthis is line 2\n}}}\n\nThis is the root object. For example, the empty object {{ {} }} is invalid.",
4+
"description": "Translated by atdcat from 'schema.atd'.\n\nThis is the title. Here's a code block:\n\n{{{\nthis is line 1\nthis is line 2\n}}}\n\nThis is the root object. For example, the empty object {{ {} }} is invalid.",
65
"type": "object",
76
"required": [
87
"ID", "items", "aliased", "point", "kinds", "assoc1", "assoc2"
98
],
109
"properties": {
1110
"ID": { "description": "This is the 'id' field.", "type": "string" },
1211
"items": {
13-
"description":
14-
"An example of JSON value is {{[[1, 2], [3], [4, 5, 6]]}}",
12+
"description": "An example of JSON value is {{[[1, 2], [3], [4, 5, 6]]}}",
1513
"type": "array",
1614
"items": { "type": "array", "items": { "type": "integer" } }
1715
},

atdgen-runtime.opam

+1-2
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@ bug-reports: "https://github.com/ahrefs/atd/issues"
6565
depends: [
6666
"dune" {>= "2.8"}
6767
"ocaml" {>= "4.08"}
68-
"yojson" {>= "1.7.0" & < "2.0.0"}
68+
"yojson" {>= "2.0.2"}
6969
"biniou" {>= "1.0.6"}
70-
"camlp-streams"
7170
"odoc" {with-doc}
7271
]
7372
dev-repo: "git+https://github.com/ahrefs/atd.git"

atdgen-runtime/src/dune

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
(library
22
(name atdgen_runtime)
33
(public_name atdgen-runtime)
4-
(libraries biniou yojson camlp-streams))
4+
(libraries biniou yojson))

atdgen-runtime/src/oj_run.ml

+27-27
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
open Printf
66

7-
type 'a write = Bi_outbuf.t -> 'a -> unit
7+
type 'a write = Buffer.t -> 'a -> unit
88

99
exception Error of string
1010

@@ -47,67 +47,67 @@ let array_iter f sep x a =
4747
)
4848

4949
let write_comma ob =
50-
Bi_outbuf.add_char ob ','
50+
Buffer.add_char ob ','
5151

5252
let write_list write_item ob l =
53-
Bi_outbuf.add_char ob '[';
53+
Buffer.add_char ob '[';
5454
list_iter write_item write_comma ob l;
55-
Bi_outbuf.add_char ob ']'
55+
Buffer.add_char ob ']'
5656

5757
let write_array write_item ob a =
58-
Bi_outbuf.add_char ob '[';
58+
Buffer.add_char ob '[';
5959
array_iter write_item write_comma ob a;
60-
Bi_outbuf.add_char ob ']'
60+
Buffer.add_char ob ']'
6161

6262
let write_assoc_list write_key write_item ob l =
63-
Bi_outbuf.add_char ob '{';
63+
Buffer.add_char ob '{';
6464
list_iter (
6565
fun ob (k, v) ->
6666
write_key ob k;
67-
Bi_outbuf.add_char ob ':';
67+
Buffer.add_char ob ':';
6868
write_item ob v
6969
) write_comma ob l;
70-
Bi_outbuf.add_char ob '}'
70+
Buffer.add_char ob '}'
7171

7272
let write_assoc_array write_key write_item ob l =
73-
Bi_outbuf.add_char ob '{';
73+
Buffer.add_char ob '{';
7474
array_iter (
7575
fun ob (k, v) ->
7676
write_key ob k;
77-
Bi_outbuf.add_char ob ':';
77+
Buffer.add_char ob ':';
7878
write_item ob v
7979
) write_comma ob l;
80-
Bi_outbuf.add_char ob '}'
80+
Buffer.add_char ob '}'
8181

8282

8383
let write_option write_item ob = function
84-
None -> Bi_outbuf.add_string ob "<\"None\">"
84+
None -> Buffer.add_string ob "<\"None\">"
8585
| Some x ->
86-
Bi_outbuf.add_string ob "<\"Some\":";
86+
Buffer.add_string ob "<\"Some\":";
8787
write_item ob x;
88-
Bi_outbuf.add_string ob ">"
88+
Buffer.add_string ob ">"
8989

9090
let write_std_option write_item ob = function
91-
None -> Bi_outbuf.add_string ob "\"None\""
91+
None -> Buffer.add_string ob "\"None\""
9292
| Some x ->
93-
Bi_outbuf.add_string ob "[\"Some\",";
93+
Buffer.add_string ob "[\"Some\",";
9494
write_item ob x;
95-
Bi_outbuf.add_string ob "]"
95+
Buffer.add_string ob "]"
9696

9797
let write_nullable write_item ob = function
98-
None -> Bi_outbuf.add_string ob "null"
98+
None -> Buffer.add_string ob "null"
9999
| Some x -> write_item ob x
100100

101101
let write_int8 ob x =
102102
Yojson.Safe.write_int ob (int_of_char x)
103103

104104
let write_int32 ob x =
105-
Bi_outbuf.add_string ob (Int32.to_string x)
105+
Buffer.add_string ob (Int32.to_string x)
106106

107107
let write_int64 ob x =
108-
Bi_outbuf.add_char ob '"';
109-
Bi_outbuf.add_string ob (Int64.to_string x);
110-
Bi_outbuf.add_char ob '"'
108+
Buffer.add_char ob '"';
109+
Buffer.add_string ob (Int64.to_string x);
110+
Buffer.add_char ob '"'
111111

112112
let min_float = float min_int
113113
let max_float = float max_int
@@ -120,7 +120,7 @@ let write_float_as_int ob x =
120120
match classify_float x with
121121
FP_normal
122122
| FP_subnormal
123-
| FP_zero -> Bi_outbuf.add_string ob (Printf.sprintf "%.0f" x)
123+
| FP_zero -> Buffer.add_string ob (Printf.sprintf "%.0f" x)
124124
| FP_infinite -> error "Cannot convert inf or -inf into a JSON int"
125125
| FP_nan -> error "Cannot convert NaN into a JSON int"
126126

@@ -235,12 +235,12 @@ let read_with_adapter normalize reader p lb =
235235
reader p lb'
236236

237237
let write_with_adapter restore writer ob x =
238-
let ob_tmp = Bi_outbuf.create 1024 in
238+
let ob_tmp = Buffer.create 1024 in
239239
writer ob_tmp x;
240-
let s_tmp = Bi_outbuf.contents ob_tmp in
240+
let s_tmp = Buffer.contents ob_tmp in
241241
let ast = Yojson.Safe.from_string s_tmp in
242242
let ast' = restore ast in
243-
Yojson.Safe.to_outbuf ob ast'
243+
Yojson.Safe.to_buffer ob ast'
244244

245245
(*
246246
Checking at runtime that our assumptions on unspecified compiler behavior

atdgen-runtime/src/oj_run.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exception Error of string
44

5-
type 'a write = Bi_outbuf.t -> 'a -> unit
5+
type 'a write = Buffer.t -> 'a -> unit
66

77
val error : string -> _
88

atdgen-runtime/src/util.ml

+46-37
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ end
4444
module Json =
4545
struct
4646
type 'a reader = Yojson.Safe.lexer_state -> Lexing.lexbuf -> 'a
47-
type 'a writer = Bi_outbuf.t -> 'a -> unit
47+
type 'a writer = Buffer.t -> 'a -> unit
4848

4949
let finish ls lexbuf =
5050
Yojson.Safe.read_space ls lexbuf;
@@ -84,10 +84,17 @@ struct
8484
in
8585
input_file fname (fun ic -> from_channel ?buf ~fname:fname0 ?lnum read ic)
8686

87-
let stream_from_lexbuf ?(fin = fun () -> ()) read ls lexbuf =
88-
let stream = Some true in
89-
let f _ =
90-
try Some (from_lexbuf ?stream read ls lexbuf)
87+
(* seq_unfold is Seq.unfold, needed for ocaml < 4.11 *)
88+
let rec seq_unfold f u () =
89+
match f u with
90+
| None -> Seq.Nil
91+
| Some (x, u') -> Seq.Cons (x, seq_unfold f u')
92+
93+
let seq_from_lexbuf ?(fin = fun () -> ()) read ls lexbuf =
94+
let f () =
95+
try
96+
let v = from_lexbuf ~stream:true read ls lexbuf in
97+
Some (v, ())
9198
with
9299
Yojson.End_of_input ->
93100
fin ();
@@ -96,39 +103,36 @@ struct
96103
(try fin () with _ -> ());
97104
raise e
98105
in
99-
Stream.from f
106+
(* Seq.unfold is only available from ocaml 4.11 *)
107+
seq_unfold f ()
100108
101-
let stream_from_string ?buf ?fin ?fname ?lnum read ic =
109+
let seq_from_string ?buf ?fin ?fname ?lnum read ic =
102110
let lexbuf = Lexing.from_string ic in
103111
let ls = Yojson.Safe.init_lexer ?buf ?fname ?lnum () in
104-
stream_from_lexbuf ?fin read ls lexbuf
112+
seq_from_lexbuf ?fin read ls lexbuf
105113
106-
let stream_from_channel ?buf ?fin ?fname ?lnum read ic =
114+
let seq_from_channel ?buf ?fin ?fname ?lnum read ic =
107115
let lexbuf = Lexing.from_channel ic in
108116
let ls = Yojson.Safe.init_lexer ?buf ?fname ?lnum () in
109-
stream_from_lexbuf ?fin read ls lexbuf
117+
seq_from_lexbuf ?fin read ls lexbuf
110118
111-
let stream_from_file ?buf ?(fin = fun () -> ()) ?fname:src ?lnum read fname =
119+
let seq_from_file ?buf ?(fin = fun () -> ()) ?fname:src ?lnum read fname =
112120
let fname0 =
113121
match src with
114122
None -> fname
115123
| Some s -> s
116124
in
117125
let ic = open_in_bin fname in
118126
let fin () = close_in_noerr ic; fin () in
119-
stream_from_channel ?buf ~fin ~fname:fname0 ?lnum read ic
127+
seq_from_channel ?buf ~fin ~fname:fname0 ?lnum read ic
120128
121129
let list_from_string ?buf ?fin ?fname ?lnum read ic =
122-
let stream = stream_from_string ?buf ?fin ?fname ?lnum read ic in
123-
let acc = ref [] in
124-
Stream.iter (fun x -> acc := x :: !acc) stream;
125-
List.rev !acc
130+
let seq = seq_from_string ?buf ?fin ?fname ?lnum read ic in
131+
List.of_seq seq
126132
127133
let list_from_channel ?buf ?fin ?fname ?lnum read ic =
128-
let stream = stream_from_channel ?buf ?fin ?fname ?lnum read ic in
129-
let acc = ref [] in
130-
Stream.iter (fun x -> acc := x :: !acc) stream;
131-
List.rev !acc
134+
let seq = seq_from_channel ?buf ?fin ?fname ?lnum read ic in
135+
List.of_seq seq
132136
133137
let list_from_file ?buf ?fname:src ?lnum read fname =
134138
let fname0 =
@@ -141,34 +145,39 @@ struct
141145
list_from_channel ?buf ~fin ~fname:fname0 ?lnum read ic
142146
143147
let to_string ?(len = 1024) write x =
144-
let ob = Bi_outbuf.create len in
148+
let ob = Buffer.create len in
149+
write ob x;
150+
Buffer.contents ob
151+
152+
let to_channel ?(len = 1024) write oc x =
153+
let ob = Buffer.create len in
145154
write ob x;
146-
Bi_outbuf.contents ob
155+
Buffer.output_buffer oc ob
147156
148-
let to_channel ?len write oc x = Biniou.to_channel ?len ~shrlen:0 write oc x
149-
let to_file ?len write fname x = Biniou.to_file ?len ~shrlen:0 write fname x
157+
let to_file ?len write fname x =
158+
output_file fname (fun oc -> to_channel ?len write oc x)
150159
151-
let stream_to_string ?(len = 1024) ?(lf = "\n") write stream =
152-
let ob = Bi_outbuf.create len in
153-
Stream.iter (fun x -> write ob x; Bi_outbuf.add_string ob lf) stream;
154-
Bi_outbuf.contents ob
160+
let seq_to_string ?(len = 1024) ?(lf = "\n") write seq =
161+
let ob = Buffer.create len in
162+
Seq.iter (fun x -> write ob x; Buffer.add_string ob lf) seq;
163+
Buffer.contents ob
155164
156-
let stream_to_channel ?len ?(lf = "\n") write oc stream =
157-
let ob = Bi_outbuf.create_channel_writer ?len ~shrlen:0 oc in
158-
Stream.iter (fun x -> write ob x; Bi_outbuf.add_string ob lf) stream;
159-
Bi_outbuf.flush_channel_writer ob
165+
let seq_to_channel ?(len = 1024) ?(lf = "\n") write oc seq =
166+
let ob = Buffer.create len in
167+
Seq.iter (fun x -> write ob x; Buffer.add_string ob lf) seq;
168+
Buffer.output_buffer oc ob
160169
161-
let stream_to_file ?len ?lf write fname stream =
162-
output_file fname (fun oc -> stream_to_channel ?len ?lf write oc stream)
170+
let seq_to_file ?len ?lf write fname seq =
171+
output_file fname (fun oc -> seq_to_channel ?len ?lf write oc seq)
163172
164173
let list_to_string ?len ?lf write l =
165-
stream_to_string ?len ?lf write (Stream.of_list l)
174+
seq_to_string ?len ?lf write (List.to_seq l)
166175
167176
let list_to_channel ?len ?lf write oc l =
168-
stream_to_channel ?len ?lf write oc (Stream.of_list l)
177+
seq_to_channel ?len ?lf write oc (List.to_seq l)
169178
170179
let list_to_file ?len ?lf write fname l =
171-
stream_to_file ?len ?lf write fname (Stream.of_list l)
180+
seq_to_file ?len ?lf write fname (List.to_seq l)
172181
173182
let preset_unknown_field_handler loc name =
174183
let msg =

0 commit comments

Comments
 (0)