Skip to content

Commit ac80aea

Browse files
Merge pull request #10 from andersfugmann/arrays
Arrays
2 parents 49d218c + 9cf872d commit ac80aea

32 files changed

Lines changed: 234 additions & 100 deletions

Changelog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
Changes marked with '*' indicates a changes that breaks backward compatibility
44

55
[ ] Support extensible polymorphic variants
6+
3.1.2
7+
[x] Support arrays
68

79
3.1.1
810
[x] Do not reference ppx in libraries

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ update-version:
2323
@sed -i 's/^version: .*/version: "$(VERSION)"/' *.opam
2424
@sed -i 's/^\( *\)"ppx_protocol_conv" { >= ".*" }/\1"ppx_protocol_conv" { >= "$(VERSION)" }/' ppx_protocol_conv_*.opam
2525

26-
release: VERSION=$(shell cat Changelog | grep -E '^[0-9]' | head -n 1)
2726
release:
28-
@./release.sh $(VERSION)
27+
opam publish
2928

3029
doc:
3130
dune build @doc

drivers/generic/ppx_protocol_driver.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ module type Driver = sig
1111
val of_list: t list -> t
1212
val is_list: t -> bool
1313

14+
val to_array: t -> t array
15+
val of_array: t array -> t
16+
1417
val to_alist: t -> (string * t) list
1518
val of_alist: (string * t) list -> t
1619
val is_alist: t -> bool
@@ -171,6 +174,12 @@ module Make(Driver: Driver) = struct
171174
let of_list: ?flags:flag -> ('a -> t) -> 'a list -> t = fun ?flags:_ of_value_fun v ->
172175
List.map ~f:of_value_fun v |> Driver.of_list
173176

177+
let to_array: ?flags:flag -> (t -> 'a) -> t -> 'a array = fun ?flags:_ to_value_fun t ->
178+
to_list to_value_fun t |> Array.of_list
179+
180+
let of_array: ?flags:flag -> ('a -> t) -> 'a array -> t = fun ?flags:_ of_value_fun v ->
181+
Array.to_list v |> of_list of_value_fun
182+
174183
let to_lazy_t: ?flags:flag -> (t -> 'a) -> t -> 'a lazy_t = fun ?flags:_ to_value_fun t ->
175184
Lazy.from_fun (fun () -> to_value_fun t)
176185

drivers/generic/ppx_protocol_driver.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ module type Driver = sig
66
val of_list: t list -> t
77
val is_list: t -> bool
88

9+
val to_array: t -> t array
10+
val of_array: t array -> t
11+
912
val to_alist: t -> (string * t) list
1013
val of_alist: (string * t) list -> t
1114
val is_alist: t -> bool

drivers/json/json.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ module Driver : Ppx_protocol_driver.Driver with type t = Yojson.Safe.json = stru
99
let to_list = U.to_list
1010
let is_list = function `List _ -> true | _ -> false
1111

12+
let of_array l = `List (Array.to_list l)
13+
let to_array t = U.to_list t |> Array.of_list
14+
1215
let of_alist a = `Assoc a
1316
let to_alist = U.to_assoc
1417
let is_alist = function `Assoc _ -> true | _ -> false

drivers/json/test/unittest.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ module JsonTestDriver = struct
1313
let of_option x = Json.of_option x
1414
let to_list x = Json.to_list x
1515
let of_list x = Json.of_list x
16+
let to_array x = Json.to_array x
17+
let of_array x = Json.of_array x
1618
let to_lazy_t x = Json.to_lazy_t x
1719
let of_lazy_t x = Json.of_lazy_t x
1820
let to_int x = Json.to_int x

drivers/jsonm/jsonm.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ module Driver : Ppx_protocol_driver.Driver with type t = Ezjsonm.value = struct
99
let to_list = Ezjsonm.get_list identity
1010
let is_list = function `A _ -> true | _ -> false
1111

12+
let of_array v = Array.to_list v |> Ezjsonm.list identity
13+
let to_array v = Ezjsonm.get_list identity v |> Array.of_list
14+
1215
let of_alist = Ezjsonm.dict
1316
let to_alist = Ezjsonm.get_dict
1417
let is_alist = function `O _ -> true | _ -> false

drivers/jsonm/test/unittest.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ module TestDriver = struct
1313
let of_option x = Driver.of_option x
1414
let to_list x = Driver.to_list x
1515
let of_list x = Driver.of_list x
16+
let to_array x = Driver.to_array x
17+
let of_array x = Driver.of_array x
1618
let to_lazy_t x = Driver.to_lazy_t x
1719
let of_lazy_t x = Driver.of_lazy_t x
1820
let to_int x = Driver.to_int x

drivers/msgpack/msgpack.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ module Driver : Ppx_protocol_driver.Driver with type t = Msgpck.t = struct
1010
let to_list = Msgpck.to_list
1111
let is_list = function Msgpck.List _ -> true | _ -> false
1212

13+
let of_array t = Array.to_list t |> Msgpck.of_list
14+
let to_array v = Msgpck.to_list v |> Array.of_list
15+
1316
let of_alist alist = List.map (fun (k, v) -> Msgpck.of_string k, v) alist |> Msgpck.of_map
1417
let to_alist t = Msgpck.to_map t |> List.map (fun (k, v) -> (Msgpck.to_string k, v))
1518

drivers/msgpack/test/unittest.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ module TestDriver = struct
1313
let of_option x = of_option x
1414
let to_list x = to_list x
1515
let of_list x = of_list x
16+
let to_array x = to_array x
17+
let of_array x = of_array x
1618
let to_lazy_t x = to_lazy_t x
1719
let of_lazy_t x = of_lazy_t x
1820
let to_int x = to_int x

0 commit comments

Comments
 (0)