File: examples.mlt

package info (click to toggle)
ppx-bin-prot 0.17.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 396 kB
  • sloc: ml: 5,351; makefile: 15
file content (313 lines) | stat: -rw-r--r-- 6,389 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
open Core
module Shape = Bin_prot.Shape;;

#verbose true

module type Interface_for_types_named_t = sig
  type t [@@deriving bin_shape]
end

[%%expect
  {|
module type Interface_for_types_named_t =
  sig type t val bin_shape_t : Shape.t end
|}]

module type Interface_for_types_named_other_than_t = sig
  type my_type [@@deriving bin_shape]
end

[%%expect
  {|
module type Interface_for_types_named_other_than_t =
  sig type my_type val bin_shape_my_type : Shape.t end
|}]

module Record = struct
  type r = { x : int } [@@deriving bin_shape]
end

[%%expect
  {|
module Record : sig type r = { x : int; } val bin_shape_r : Shape.t end
|}]

module Annotation_syntax = struct
  type t = int [@@deriving bin_shape ~annotate:"my-annotation"]
end

[%%expect
  {|
module Annotation_syntax : sig type t = int val bin_shape_t : Shape.t end
|}]

module Annotation_on_multi_type_dec = struct
  type t = int
  and u = float [@@deriving bin_shape ~annotate:"this-is-not-allowed"]
end

[%%expect
  {|
Line _, characters _-_:
Error: ppx_bin_shape: unexpected [~annotate] on multi type-declaration
|}]

module Annotation_syntax_uuid = struct
  let uuid = Shape.Uuid.of_string "test-it"

  type t = int [@@deriving bin_shape ~annotate:uuid]
end

[%%expect
  {|
module Annotation_syntax_uuid :
  sig val uuid : Shape.Uuid.t type t = int val bin_shape_t : Shape.t end
|}]

module Annotation_syntax_uuid_wrong_type = struct
  let uuid = true

  type t = int [@@deriving bin_shape ~annotate:uuid]
end

[%%expect
  {|
Line _, characters _-_:
Error: This expression has type bool but an expression was expected of type
         Shape.Uuid.t
|}]

module Inheriting_from_polymorphic_variant = struct
  type t1 =
    [ `A
    | `B of int
    ]
  [@@deriving bin_shape]

  type t2 =
    [ `C
    | t1
    ]
  [@@deriving bin_shape]
end

[%%expect
  {|
module Inheriting_from_polymorphic_variant :
  sig
    type t1 = [ `A | `B of int ]
    val bin_shape_t1 : Shape.t
    type t2 = [ `A | `B of int | `C ]
    val bin_shape_t2 : Shape.t
  end
|}]

module Inheriting_from_recursive_polymorphic_variant = struct
  type t1 =
    [ `A
    | `B of t1
    ]
  [@@deriving bin_shape]

  type t2 =
    [ `C
    | t1
    ]
  [@@deriving bin_shape ~hide_locations]

  let (_ : Shape.Digest.t) = Shape.eval_to_digest bin_shape_t2
end

[%%expect
  {|
Exception:
(Failure
 "<hidden>: The shape for an inherited type is not described as a polymorphic-variant: (Application ...())").
|}]

module Inheriting_from_annotated_polymorphic_variant = struct
  type t1 =
    [ `A
    | `B of int
    ]
  [@@deriving bin_shape ~annotate:"my-t1"]

  type t2 =
    [ `C
    | t1
    ]
  [@@deriving bin_shape ~hide_locations]

  let (_ : Shape.Digest.t) = Shape.eval_to_digest bin_shape_t2
end

[%%expect
  {|
Exception:
(Failure
 "<hidden>: The shape for an inherited type is not described as a polymorphic-variant: (Annotate my-t1 ...)").
|}]

module Duplicated_variant_constructor_allowed = struct
  type t1 = T1 [@@deriving bin_shape]
  type t2 = t1 [@@deriving bin_shape]
  type v1 = [ `a of t1 ] [@@deriving bin_shape]
  type v2 = [ `a of t2 ] [@@deriving bin_shape]

  type t =
    [ v1
    | v2
    ]
  [@@deriving bin_shape]
end

[%%expect
  {|
module Duplicated_variant_constructor_allowed :
  sig
    type t1 = T1
    val bin_shape_t1 : Shape.t
    type t2 = t1
    val bin_shape_t2 : Shape.t
    type v1 = [ `a of t2 ]
    val bin_shape_v1 : Shape.t
    type v2 = [ `a of t2 ]
    val bin_shape_v2 : Shape.t
    type t = [ `a of t2 ]
    val bin_shape_t : Shape.t
  end
|}]

module Duplicated_variant_constructor_Same_type_diff_serialization = struct
  type t1 = T1 [@@deriving bin_shape]
  type t2 = t1

  let bin_shape_t2 =
    Shape.(annotate (Uuid.of_string "t2 has a special meaning") bin_shape_t1)
  ;;

  type v1 = [ `a of t1 ] [@@deriving bin_shape]
  type v2 = [ `a of t2 ] [@@deriving bin_shape]

  type t =
    [ v1
    | v2
    ]
  [@@deriving bin_shape ~hide_locations]

  let (_ : Shape.Digest.t) = Shape.eval_to_digest bin_shape_t
end

[%%expect
  {|
Exception:
(Failure
 "<hidden>: Different shapes for duplicated polymorphic constructor: `a").
|}]

module No_such_bin_shape_syntax = struct
  type t = int [@@deriving bin_shape ~floob:"X"]
end

[%%expect
  {|
Line _, characters _-_:
Error: Ppxlib.Deriving: generator 'bin_shape' doesn't accept argument 'floob'
|}]

module Shapeless = struct
  type t = int
end

[%%expect
  {|
module Shapeless : sig type t = int end
|}]

module Annotation_requires_shape_on_subtypes = struct
  type t = Shapeless.t [@@deriving bin_shape ~annotate:"my-annotation"]
end

[%%expect
  {|
Line _, characters _-_:
Error: Unbound value Shapeless.bin_shape_t
|}]

module Not_allowed_both_annotation_and_basetype_syntax = struct
  type t = int [@@deriving bin_shape ~annotate:"X" ~basetype:"X"]
end

[%%expect
  {|
Line _, characters _-_:
Error: ppx_bin_shape: cannot write both [bin_shape ~annotate] and [bin_shape ~basetype]
|}]

module Basetype_syntax = struct
  type t = Shapeless.t [@@deriving bin_shape ~basetype:"my-base"]
end

[%%expect
  {|
module Basetype_syntax : sig type t = int val bin_shape_t : Shape.t end
|}]

module Basetype_syntax_kind2 = struct
  type ('a, 'b) t = 'a * 'b [@@deriving bin_shape ~basetype:"my-base-kind2"]
end

[%%expect
  {|
module Basetype_syntax_kind2 :
  sig
    type ('a, 'b) t = 'a * 'b
    val bin_shape_t : Shape.t -> Shape.t -> Shape.t
  end
|}]

module Basetype_on_multi_type_dec = struct
  type t = int
  and u = float [@@deriving bin_shape ~basetype:"this-is-not-allowed"]
end

[%%expect
  {|
Line _, characters _-_:
Error: ppx_bin_shape: unexpected [~basetype] on multi type-declaration
|}]

module Basetype_syntax_uuid = struct
  let uuid = Shape.Uuid.of_string "test-it"

  type t = Shapeless.t [@@deriving bin_shape ~basetype:uuid]
end

[%%expect
  {|
module Basetype_syntax_uuid :
  sig val uuid : Shape.Uuid.t type t = int val bin_shape_t : Shape.t end
|}]

module Basetype_syntax_uuid_wrong_type = struct
  let uuid = true

  type t = Shapeless.t [@@deriving bin_shape ~basetype:uuid]
end

[%%expect
  {|
Line _, characters _-_:
Error: This expression has type bool but an expression was expected of type
         Shape.Uuid.t
|}]

module Cannot_evaluate_shape_containing_type_vars = struct
  let (_ : _) = [%bin_shape: 'a list]
end

[%%expect
  {|
Line _, characters _-_:
Error: ppx_bin_shape: unexpected free type variable: 'a
|}]