Skip to content

Commit

Permalink
feat: port to Melange v2 (#3)
Browse files Browse the repository at this point in the history
* feat: port to Melange v2

* update opam and makefile

* better constraint for opam-check-npm-deps

---------

Co-authored-by: Javier Chavarri <[email protected]>
  • Loading branch information
anmonteiro and jchavarri authored Sep 15, 2023
1 parent bfeb902 commit 276aef9
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 210 deletions.
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ help: ## Print this help message

.PHONY: create-switch
create-switch: ## Create opam switch
opam switch create . 4.14.1 -y --deps-only
opam switch create . 5.1.0~rc3 -y --deps-only

.PHONY: init
init: create-switch install ## Configure everything to develop this repository in local
Expand All @@ -23,8 +23,7 @@ install: ## Install development dependencies
yarn
opam update
opam install -y . --deps-only --with-test
# used to build the tests
opam pin add melange-jest.dev -y git+https://github.com/melange-community/melange-jest.git#main
opam-check-npm-deps

.PHONY: build
build: ## Build the project
Expand Down
25 changes: 12 additions & 13 deletions examples/decode.ml
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
(* Decoding a fixed JSON data structure using Json.Decode *)
let mapJsonObjectString f decoder (encoder: int -> Js.Json.t) str =
let mapJsonObjectString f decoder (encoder : int -> Js.Json.t) str =
let json = Json.parseOrRaise str in
Json.Decode.(dict decoder json)
|> Js.Dict.map ((fun v -> f v) [@bs])
|> Json.Encode.dict encoder
|> Json.stringify
|> Js.Dict.map (fun [@u] v -> f v)
|> Json.Encode.dict encoder |> Json.stringify

let sum =
Array.fold_left (+) 0
let sum = Array.fold_left ( + ) 0

(* prints `{ "foo": 6, "bar": 24 }` *)
let _ =
Js.log @@
(mapJsonObjectString sum Json.Decode.(array int) Json.Encode.int {|
Js.log
@@ mapJsonObjectString sum
Json.Decode.(array int)
Json.Encode.int
{|
{
"foo": [1, 2, 3],
"bar": [9, 8, 7]
}
|})
|}

(* Error handling *)
let _ =
let json = {|{ "y": 42 } |} |> Json.parseOrRaise in
match Json.Decode.(field "x" int json) with
| x ->
Js.log x
| exception Json.Decode.DecodeError msg ->
Js.log ("Error:" ^ msg)
| x -> Js.log x
| exception Json.Decode.DecodeError msg -> Js.log ("Error:" ^ msg)
14 changes: 11 additions & 3 deletions melange-json.opam
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ maintainer: [
authors: [
"glennsl"
]
license: "(LGPL-3.0 OR MPL-2.0)"
license: ["LGPL-3.0-only" "MPL-2.0"]
homepage: "https://github.com/melange-community/melange-json/"
doc: "https://github.com/melange-community/melange-json/"
bug-reports: "https://github.com/melange-community/melange-json/issues"
depends: [
"dune" {>= "3.8"}
"ocaml" {>= "4.14.0"}
"melange" {>= "1.0.0"}
"ocaml"
"melange" {>= "2.0.0"}
"melange-jest" {with-test}
"reason" {with-test}
"opam-check-npm-deps" {with-test} # todo: use with-dev-setup once opam 2.2 is out
"ocaml-lsp-server" {with-test}
"odoc" {with-doc}
]
Expand All @@ -35,3 +36,10 @@ build: [
]
]
dev-repo: "git+https://github.com/melange-community/melange-json.git"
pin-depends: [
[ "melange.2.0.0" "git+https://github.com/melange-re/melange.git#e114ad55d185badeb32b3c766c9ab547495eac1b" ]
[ "reason.3.10.0" "git+https://github.com/reasonml/reason.git#972261dab3b651ff8ab9b8b9fcc32940595073dc" ]
[ "melange-jest.dev" "git+https://github.com/melange-community/melange-jest.git#acb6ef50beef3c486805d616b90aa7b56b51172d" ]
[ "melange-webapi.dev" "git+https://github.com/melange-community/melange-webapi.git#96dc1e2a867624d18050fad25cf1c71af7a098e1" ]
[ "melange-fetch.dev" "git+https://github.com/melange-community/melange-fetch.git#796f941b6b85eb7e6182ac6e4f40708bfde7a9a9" ]
]
17 changes: 7 additions & 10 deletions src/Json.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ module Encode = Json_encode

exception ParseError of string

let parse s =
try Some (Js.Json.parseExn s) with
| _ -> None
let parse s = try Some (Js.Json.parseExn s) with _ -> None

let parseOrRaise s =
try Js.Json.parseExn s with
| Js.Exn.Error e ->
try Js.Json.parseExn s
with Js.Exn.Error e ->
let message =
match Js.Exn.message e with
| Some m -> m
| None -> "Unknown error"
in raise @@ ParseError message
match Js.Exn.message e with Some m -> m | None -> "Unknown error"
in
raise @@ ParseError message

external stringify : Js.Json.t -> string = "JSON.stringify" [@@bs.val]
external stringify : Js.Json.t -> string = "JSON.stringify"
Loading

0 comments on commit 276aef9

Please sign in to comment.