Skip to content

Commit

Permalink
Merge pull request #199 from mransan/wip-include-yojson
Browse files Browse the repository at this point in the history
wip: include yojson runtime library directly
  • Loading branch information
c-cube authored Aug 12, 2023
2 parents 69ec51a + e83a9db commit 4d9e70a
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 4 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
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
31 changes: 31 additions & 0 deletions pbrt_yojson.opam
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" }
]
1 change: 0 additions & 1 deletion src/runtime-yojson
Submodule runtime-yojson deleted from 03d212
5 changes: 5 additions & 0 deletions src/runtime-yojson/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

(library
(public_name pbrt_yojson)
(wrapped false)
(libraries yojson base64))
83 changes: 83 additions & 0 deletions src/runtime-yojson/pbrt_yojson.ml
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
37 changes: 37 additions & 0 deletions src/runtime-yojson/pbrt_yojson.mli
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
4 changes: 4 additions & 0 deletions src/tests/yojson/yojson/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

(test
(name unit_tests)
(libraries yojson pbrt_yojson))
17 changes: 17 additions & 0 deletions src/tests/yojson/yojson/unit_tests.ml
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");
()

0 comments on commit 4d9e70a

Please sign in to comment.