-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #199 from mransan/wip-include-yojson
wip: include yojson runtime library directly
- Loading branch information
Showing
8 changed files
with
177 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
[submodule "src/runtime-yojson"] | ||
path = src/runtime-yojson | ||
url = https://github.com/mransan/ocaml-protoc-yojson.git | ||
[submodule "src/runtime-bs"] | ||
path = src/runtime-bs | ||
url = https://github.com/mransan/bs-ocaml-protoc-json.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
opam-version: "2.0" | ||
version: "0.2.0" | ||
maintainer: "Maxime Ransan <[email protected]>" | ||
authors:[ | ||
"Maxime Ransan <[email protected]>" | ||
] | ||
synopsis: "Runtime library for ocaml-protoc to support JSON encoding/decoding" | ||
homepage: "https://github.com/mransan/ocaml-protoc-yojson" | ||
bug-reports:"https://github.com/mransan/ocaml-protoc-yojson/issues" | ||
dev-repo: "git+https://github.com/mransan/ocaml-protoc-yojson.git" | ||
license: "MIT" | ||
build: [ | ||
["dune" "subst"] {dev} | ||
[ | ||
"dune" | ||
"build" | ||
"-p" | ||
name | ||
"-j" | ||
jobs | ||
"@install" | ||
"@runtest" {with-test} | ||
"@doc" {with-doc} | ||
] | ||
] | ||
depends: [ | ||
"dune" { >= "2.0" } | ||
"yojson" {>= "1.6.0"} | ||
"base64" | ||
"ocaml" { >= "4.02.1" } | ||
] |
Submodule runtime-yojson
deleted from
03d212
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
(library | ||
(public_name pbrt_yojson) | ||
(wrapped false) | ||
(libraries yojson base64)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
module E = struct | ||
type error = | ||
| Unexpected_json_type of string * string | ||
| Malformed_variant of string | ||
|
||
exception Failure of error | ||
|
||
let unexpected_json_type record_name field_name = | ||
raise (Failure (Unexpected_json_type (record_name, field_name))) | ||
|
||
let malformed_variant variant_name = | ||
raise (Failure (Malformed_variant variant_name)) | ||
|
||
let string_of_error = function | ||
| Unexpected_json_type (record_name, field_name) -> | ||
Printf.sprintf "Unexpected json type (record name:%s, field_name:%s)" | ||
record_name field_name | ||
| Malformed_variant variant_name -> | ||
Printf.sprintf "Malformed variant (variant name: %s)" variant_name | ||
|
||
let () = | ||
Printexc.register_printer (fun exn -> | ||
match exn with | ||
| Failure e -> Some (string_of_error e) | ||
| _ -> None) | ||
end | ||
|
||
let int32 v record_name field_name = | ||
match v with | ||
| `String v -> Int32.of_string v | ||
| `Float f -> Int32.of_float f | ||
| `Int i -> Int32.of_int i | ||
| `Null -> 0l | ||
| _ -> E.unexpected_json_type record_name field_name | ||
|
||
let float v record_name field_name = | ||
match v with | ||
| `String v -> float_of_string v | ||
| `Float f -> f | ||
| `Int i -> float_of_int i | ||
| `Null -> 0.0 | ||
| _ -> E.unexpected_json_type record_name field_name | ||
|
||
let int64 v record_name field_name = | ||
match v with | ||
| `String v -> Int64.of_string v | ||
| `Float f -> Int64.of_float f | ||
| `Int i -> Int64.of_int i | ||
| `Null -> 0L | ||
| _ -> E.unexpected_json_type record_name field_name | ||
|
||
let int v record_name field_name = | ||
match v with | ||
| `String v -> int_of_string v | ||
| `Float f -> int_of_float f | ||
| `Int i -> i | ||
| `Null -> 0 | ||
| _ -> E.unexpected_json_type record_name field_name | ||
|
||
let string v record_name field_name = | ||
match v with | ||
| `String v -> v | ||
| `Null -> "" | ||
| _ -> E.unexpected_json_type record_name field_name | ||
|
||
let bool v record_name field_name = | ||
match v with | ||
| `Bool b -> b | ||
| `Null -> false | ||
| _ -> E.unexpected_json_type record_name field_name | ||
|
||
let bytes v record_name field_name = | ||
string v record_name field_name |> Base64.decode_exn |> Bytes.of_string | ||
|
||
let make_bool v = `Bool v | ||
let make_int v = `Int v | ||
let make_float v = `Float v | ||
let make_string v = `String v | ||
|
||
let make_bytes s = | ||
make_string (s |> Bytes.to_string |> Base64.encode_exn ~pad:true) | ||
|
||
let make_list v = `List v |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
(** Protobuf JSON encoding runtime *) | ||
|
||
(** All exception which could be raised by the generated JSON encoder | ||
and decode function *) | ||
module E : sig | ||
type error = | ||
| Unexpected_json_type of string * string | ||
| Malformed_variant of string | ||
|
||
exception Failure of error | ||
(** Decoding/Encoding failure *) | ||
|
||
val unexpected_json_type : string -> string -> 'a | ||
(** [unexpected_json_type record_name field_name] raises | ||
[Failure (Unexpected_json_type (record_name, field_name))] *) | ||
|
||
val malformed_variant : string -> 'a | ||
(** [malformed_variant variant_name] raise | ||
[Failure (Malformed_variant variant_name)] *) | ||
end | ||
|
||
(** Helper module for the generated code for common | ||
functionality *) | ||
|
||
val string : Yojson.Basic.t -> string -> string -> string | ||
val float : Yojson.Basic.t -> string -> string -> float | ||
val int32 : Yojson.Basic.t -> string -> string -> int32 | ||
val int64 : Yojson.Basic.t -> string -> string -> int64 | ||
val int : Yojson.Basic.t -> string -> string -> int | ||
val bool : Yojson.Basic.t -> string -> string -> bool | ||
val bytes : Yojson.Basic.t -> string -> string -> bytes | ||
val make_bool : bool -> Yojson.Basic.t | ||
val make_int : int -> Yojson.Basic.t | ||
val make_float : float -> Yojson.Basic.t | ||
val make_string : string -> Yojson.Basic.t | ||
val make_bytes : bytes -> Yojson.Basic.t | ||
val make_list : Yojson.Basic.t list -> Yojson.Basic.t |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
(test | ||
(name unit_tests) | ||
(libraries yojson pbrt_yojson)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
open! Pbrt_yojson | ||
|
||
let () = | ||
assert ("abc" = string (`String "abc") "r" "f"); | ||
assert (1.2 = float (`Float 1.2) "r" "f"); | ||
assert (1.2 = float (`String "1.2") "r" "f"); | ||
assert (1.0 = float (`Int 1) "r" "f"); | ||
assert (123l = int32 (`Int 123) "r" "f"); | ||
assert (123l = int32 (`String "123") "r" "f"); | ||
assert (123l = int32 (`Float 123.0) "r" "f"); | ||
assert (123L = int64 (`Int 123) "r" "f"); | ||
assert (123L = int64 (`String "123") "r" "f"); | ||
assert (123L = int64 (`Float 123.0) "r" "f"); | ||
assert (true = bool (`Bool true) "r" "f"); | ||
assert (false = bool (`Bool false) "r" "f"); | ||
() | ||
|