Skip to content

Commit

Permalink
Add adapted spec types and roundtrip tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rauanmayemir committed Sep 28, 2018
1 parent 2bbe8d0 commit 364e9b6
Show file tree
Hide file tree
Showing 5 changed files with 523 additions and 1 deletion.
18 changes: 17 additions & 1 deletion atdgen/test/bucklescript/bucklespec.atd
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,20 @@ type record_json_name =
{ foo <json name="bar"> : int
}

type single_tuple = [ Single_tuple of (int) ]
type single_tuple = [ Single_tuple of (int) ]

(*** Test json adapters ***)

type adapted = [
| A of a
| B of b
] <json adapter.ocaml="Atdgen_codec_runtime.Json_adapter.Type_field">

type a = {
thing: string;
other_thing: bool;
}

type b = {
thing: int;
}
115 changes: 115 additions & 0 deletions atdgen/test/bucklescript/bucklespec_bs.expected.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ type labeled = Bucklespec_t.labeled = { flag: valid; lb: label; count: int }

type from_module_a = A_t.from_module_a

type b = Bucklespec_t.b = { thing: int }

type a = Bucklespec_t.a = { thing: string; other_thing: bool }

type adapted = Bucklespec_t.adapted

let write_valid = (
Atdgen_codec_runtime.Encode.bool
)
Expand Down Expand Up @@ -590,3 +596,112 @@ let write_from_module_a = (
let read_from_module_a = (
A_bs.read_from_module_a
)
let write_b = (
Atdgen_codec_runtime.Encode.make (fun (t : b) ->
(
Atdgen_codec_runtime.Encode.obj
[
Atdgen_codec_runtime.Encode.field
(
Atdgen_codec_runtime.Encode.int
)
~name:"thing"
t.thing
]
)
)
)
let read_b = (
Atdgen_codec_runtime.Decode.make (fun json ->
(
({
thing =
Atdgen_codec_runtime.Decode.decode
(
Atdgen_codec_runtime.Decode.int
|> Atdgen_codec_runtime.Decode.field "thing"
) json;
} : b)
)
)
)
let write_a = (
Atdgen_codec_runtime.Encode.make (fun (t : a) ->
(
Atdgen_codec_runtime.Encode.obj
[
Atdgen_codec_runtime.Encode.field
(
Atdgen_codec_runtime.Encode.string
)
~name:"thing"
t.thing
;
Atdgen_codec_runtime.Encode.field
(
Atdgen_codec_runtime.Encode.bool
)
~name:"other_thing"
t.other_thing
]
)
)
)
let read_a = (
Atdgen_codec_runtime.Decode.make (fun json ->
(
({
thing =
Atdgen_codec_runtime.Decode.decode
(
Atdgen_codec_runtime.Decode.string
|> Atdgen_codec_runtime.Decode.field "thing"
) json;
other_thing =
Atdgen_codec_runtime.Decode.decode
(
Atdgen_codec_runtime.Decode.bool
|> Atdgen_codec_runtime.Decode.field "other_thing"
) json;
} : a)
)
)
)
let write_adapted = (
Atdgen_codec_runtime.Encode.adapter Atdgen_codec_runtime.Json_adapter.Type_field.restore (
Atdgen_codec_runtime.Encode.make (fun (x : _) -> match x with
| `A x ->
Atdgen_codec_runtime.Encode.constr1 "A" (
write_a
) x
| `B x ->
Atdgen_codec_runtime.Encode.constr1 "B" (
write_b
) x
)
)
)
let read_adapted = (
Atdgen_codec_runtime.Decode.adapter Atdgen_codec_runtime.Json_adapter.Type_field.normalize (
Atdgen_codec_runtime.Decode.enum
[
(
"A"
,
`Decode (
read_a
|> Atdgen_codec_runtime.Decode.map (fun x -> ((`A x) : _))
)
)
;
(
"B"
,
`Decode (
read_b
|> Atdgen_codec_runtime.Decode.map (fun x -> ((`B x) : _))
)
)
]
)
)
18 changes: 18 additions & 0 deletions atdgen/test/bucklescript/bucklespec_bs.expected.mli
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ type labeled = Bucklespec_t.labeled = { flag: valid; lb: label; count: int }

type from_module_a = A_t.from_module_a

type b = Bucklespec_t.b = { thing: int }

type a = Bucklespec_t.a = { thing: string; other_thing: bool }

type adapted = Bucklespec_t.adapted

val read_valid : valid Atdgen_codec_runtime.Decode.t

val write_valid : valid Atdgen_codec_runtime.Encode.t
Expand Down Expand Up @@ -106,3 +112,15 @@ val read_from_module_a : from_module_a Atdgen_codec_runtime.Decode.t

val write_from_module_a : from_module_a Atdgen_codec_runtime.Encode.t

val read_b : b Atdgen_codec_runtime.Decode.t

val write_b : b Atdgen_codec_runtime.Encode.t

val read_a : a Atdgen_codec_runtime.Decode.t

val write_a : a Atdgen_codec_runtime.Encode.t

val read_adapted : adapted Atdgen_codec_runtime.Decode.t

val write_adapted : adapted Atdgen_codec_runtime.Encode.t

Loading

0 comments on commit 364e9b6

Please sign in to comment.