Skip to content

Commit ffc8464

Browse files
switch to ocamlformat 0.27.0 and format everything.
1 parent c903fec commit ffc8464

28 files changed

+612
-514
lines changed

.ocamlformat

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
break-infix=fit-or-vertical
22
margin=74
3-
parens-tuple=multi-line-only
3+
parens-tuple=multi-line-only
4+
version=0.27.0

examples/complex.ml

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,23 @@
1-
type line = {
2-
start: point;
3-
end_: point;
4-
thickness: int option
5-
}
6-
and point = {
7-
x: int;
8-
y: int
9-
}
1+
type line = { start : point; end_ : point; thickness : int option }
2+
and point = { x : int; y : int }
103

114
module Decode = struct
125
let point json =
13-
Json.Decode.{
14-
x = json |> field "x" int;
15-
y = json |> field "y" int
16-
}
6+
Json.Decode.{ x = json |> field "x" int; y = json |> field "y" int }
177

188
let line json =
19-
Json.Decode.{
20-
start = json |> field "start" point;
21-
end_ = json |> field "end" point;
22-
thickness = json |> optional (field "thickness" int)
23-
}
9+
Json.Decode.
10+
{
11+
start = json |> field "start" point;
12+
end_ = json |> field "end" point;
13+
thickness = json |> optional (field "thickness" int);
14+
}
2415
end
2516

26-
let data = {| {
17+
let data =
18+
{| {
2719
"start": { "x": 1, "y": -4 },
2820
"end": { "x": 5, "y": 8 }
2921
} |}
3022

31-
let _ =
32-
data |> Json.parseOrRaise
33-
|> Decode.line
34-
|> Js.log
23+
let _ = data |> Json.parseOrRaise |> Decode.line |> Js.log

examples/decode.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ let mapJsonObjectString f decoder (encoder : int -> Js.Json.t) str =
33
let json = Json.parseOrRaise str in
44
Json.Decode.(dict decoder json)
55
|> Js.Dict.map ~f:(fun [@u] v -> f v)
6-
|> Json.Encode.dict encoder |> Json.stringify
6+
|> Json.Encode.dict encoder
7+
|> Json.stringify
78

89
let sum = Array.fold_left ( + ) 0
910

@@ -25,4 +26,5 @@ let _ =
2526
let json = {|{ "y": 42 } |} |> Json.parseOrRaise in
2627
match Json.Decode.(field "x" int json) with
2728
| x -> Js.log x
28-
| exception Json.Decode.DecodeError err -> Js.log ("Error:" ^ Json.Decode.error_to_string err)
29+
| exception Json.Decode.DecodeError err ->
30+
Js.log ("Error:" ^ Json.Decode.error_to_string err)

examples/dune

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
(target examples)
33
(alias examples)
44
(libraries melange-json)
5-
(preprocess (pps melange.ppx)))
5+
(preprocess
6+
(pps melange.ppx)))

examples/dynamicDict_Ocaml.ml

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,40 @@
1313
Could be dynamic JS keys generated by user or generally at run time.
1414
*)
1515

16-
type obj = {
17-
static: string;
18-
dynamics: int Js.Dict.t;
19-
}
16+
type obj = { static : string; dynamics : int Js.Dict.t }
2017

2118
module Decode = struct
2219
let obj json =
23-
Json.Decode.{
24-
static = json |> field "static" string;
25-
dynamics = json |> field "dynamics" (dict int)
26-
}
20+
Json.Decode.
21+
{
22+
static = json |> field "static" string;
23+
dynamics = json |> field "dynamics" (dict int);
24+
}
2725
end
2826

2927
module Encode = struct
3028
let obj c =
3129
Json.Encode.(
32-
object_ [
33-
"static", c.static |> string;
34-
"dynamics", c.dynamics |> dict int
35-
]
36-
)
30+
object_
31+
[
32+
"static", c.static |> string; "dynamics", c.dynamics |> dict int;
33+
])
3734
end
3835

39-
let data = {| {
36+
let data =
37+
{| {
4038
"static": "hi",
4139
"dynamics": { "hello": 5, "random": 8 }
4240
} |}
4341

44-
let decodedData =
45-
data |> Json.parseOrRaise
46-
|> Decode.obj
42+
let decodedData = data |> Json.parseOrRaise |> Decode.obj
4743

4844
(*
4945
Will log [ 'hi', { hello: 5, random: 8 } ]
5046
*)
51-
let _ =
52-
decodedData |> Js.log
47+
let _ = decodedData |> Js.log
5348

5449
(*
5550
Will log { static: 'hi', dynamics: { hello: 5, random: 8 } }
5651
*)
57-
let encodedDataBack =
58-
decodedData |> Encode.obj
59-
|> Js.log
52+
let encodedDataBack = decodedData |> Encode.obj |> Js.log

examples/dynamicDict_Reason.re

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
/*
2-
Handling an object with dynamic keys for sub-objects.
3-
example:
4-
{
5-
static: "hello",
6-
dynamics: {
7-
"undetermined1": 2
8-
"undetermined2": 6
9-
}
10-
}
11-
12-
Where the "undetermined" keys, are unknown at compile time.
13-
Could be dynamic JS keys generated by user or generally at run time.
14-
*/
2+
Handling an object with dynamic keys for sub-objects.
3+
example:
4+
{
5+
static: "hello",
6+
dynamics: {
7+
"undetermined1": 2
8+
"undetermined2": 6
9+
}
10+
}
11+
12+
Where the "undetermined" keys, are unknown at compile time.
13+
Could be dynamic JS keys generated by user or generally at run time.
14+
*/
1515

1616
type obj = {
1717
static: string,
18-
dynamics: Js.Dict.t(int)
18+
dynamics: Js.Dict.t(int),
1919
};
2020

2121
module Decode = {
@@ -27,7 +27,7 @@ module Decode = {
2727
};
2828

2929
module Encode = {
30-
let obj = (c) => {
30+
let obj = c => {
3131
Json.Encode.(
3232
object_([
3333
("static", string(c.static)),
@@ -42,19 +42,14 @@ let data = {| {
4242
"dynamics": { "hello": 5, "random": 8 }
4343
} |};
4444

45-
let decodedData =
46-
data |> Json.parseOrRaise
47-
|> Decode.obj;
45+
let decodedData = data |> Json.parseOrRaise |> Decode.obj;
4846

4947
/*
50-
Will log [ 'hi', { hello: 5, random: 8 } ]
51-
*/
52-
let _ =
53-
decodedData |> Js.log;
48+
Will log [ 'hi', { hello: 5, random: 8 } ]
49+
*/
50+
let _ = decodedData |> Js.log;
5451

5552
/*
56-
Will log { static: 'hi', dynamics: { hello: 5, random: 8 } }
57-
*/
58-
let encodedDataBack =
59-
decodedData |> Encode.obj
60-
|> Js.log;
53+
Will log { static: 'hi', dynamics: { hello: 5, random: 8 } }
54+
*/
55+
let encodedDataBack = decodedData |> Encode.obj |> Js.log;

examples/encode.ml

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
(* prints ["foo", "bar"] *)
44
let _ =
5-
[| "foo"; "bar" |] |> Json.Encode.stringArray |> Json.stringify |> Js.log
5+
[| "foo"; "bar" |]
6+
|> Json.Encode.stringArray
7+
|> Json.stringify
8+
|> Js.log
69

710
(* prints ["foo", "bar"] *)
811
let _ =
@@ -13,7 +16,8 @@ let _ =
1316
|> Js.log
1417

1518
(* prints { x: 42, foo: 'bar' } *)
16-
let _ = Json.Encode.(object_ [ ("x", int 42); ("foo", string "bar") ] |> Js.log)
19+
let _ =
20+
Json.Encode.(object_ [ "x", int 42; "foo", string "bar" ] |> Js.log)
1721

1822
(* Advanced example: encode a record *)
1923
type line = { start : point; end_ : point; thickness : int option }
@@ -22,15 +26,16 @@ and point = { x : float; y : float }
2226
module Encode = struct
2327
let point r =
2428
let open! Json.Encode in
25-
object_ [ ("x", float r.x); ("y", float r.y) ]
29+
object_ [ "x", float r.x; "y", float r.y ]
2630

2731
let line r =
2832
Json.Encode.(
2933
object_
3034
[
31-
("start", point r.start);
32-
("end", point r.end_);
33-
("thickness", match r.thickness with Some x -> int x | None -> null);
35+
"start", point r.start;
36+
"end", point r.end_;
37+
( "thickness",
38+
match r.thickness with Some x -> int x | None -> null );
3439
])
3540
end
3641

@@ -41,7 +46,4 @@ let data =
4146
thickness = Some 2;
4247
}
4348

44-
let _ =
45-
data
46-
|> Encode.line
47-
|> Js.log
49+
let _ = data |> Encode.line |> Js.log

examples/parse.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ let arrayOfInts str =
55
Json.Decode.(array int json)
66

77
(* prints `[3, 2, 1]` *)
8-
let _ = Js.log (arrayOfInts "[1, 2, 3]" |> Js.Array.reverseInPlace)
8+
let _ = Js.log (arrayOfInts "[1, 2, 3]" |> Js.Array.reverseInPlace)

examples/tree.ml

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
(* Decode a JSON tree structure *)
2-
type 'a tree =
3-
| Node of 'a * 'a tree list
4-
| Leaf of 'a
2+
type 'a tree = Node of 'a * 'a tree list | Leaf of 'a
53

64
module Decode = struct
75
open Json.Decode
86

97
let rec tree decoder =
10-
field "type" string |> andThen (
11-
function | "node" -> node decoder
12-
| "leaf" -> leaf decoder
13-
| _ -> failwith "unknown node type"
14-
)
8+
field "type" string
9+
|> andThen (function
10+
| "node" -> node decoder
11+
| "leaf" -> leaf decoder
12+
| _ -> failwith "unknown node type")
1513

1614
and node decoder json =
17-
Node (
18-
(json |> field "value" decoder),
19-
(json |> field "children" (array (tree decoder) |> map Array.to_list))
20-
)
15+
Node
16+
( json |> field "value" decoder,
17+
json
18+
|> field "children" (array (tree decoder) |> map Array.to_list) )
2119

22-
and leaf decoder json =
23-
Leaf (json |> field "value" decoder)
20+
and leaf decoder json = Leaf (json |> field "value" decoder)
2421
end
2522

26-
let rec indent =
27-
function | n when n <= 0 -> ()
28-
| n -> print_string " "; indent (n - 1)
23+
let rec indent = function
24+
| n when n <= 0 -> ()
25+
| n ->
26+
print_string " ";
27+
indent (n - 1)
2928

3029
let print =
31-
let rec aux level =
32-
function | Node (value, children) ->
33-
indent level;
34-
Js.log value;
35-
children |> List.iter (fun child -> aux (level + 1) child)
36-
| Leaf value ->
37-
indent level;
38-
Js.log value
39-
in
40-
aux 0
41-
42-
let json = {| {
30+
let rec aux level = function
31+
| Node (value, children) ->
32+
indent level;
33+
Js.log value;
34+
children |> List.iter (fun child -> aux (level + 1) child)
35+
| Leaf value ->
36+
indent level;
37+
Js.log value
38+
in
39+
aux 0
40+
41+
let json =
42+
{| {
4343
"type": "node",
4444
"value": 9,
4545
"children": [{
@@ -59,6 +59,4 @@ let json = {| {
5959
} |}
6060

6161
let myTree =
62-
json |> Json.parseOrRaise
63-
|> Decode.tree Json.Decode.int
64-
|> print
62+
json |> Json.parseOrRaise |> Decode.tree Json.Decode.int |> print

0 commit comments

Comments
 (0)