Skip to content

update Core to v1.4 #878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@docsearch/react": "^3.5.2",
"@headlessui/react": "^1.2.0",
"@mdx-js/loader": "^2.3.0",
"@rescript/core": "^1.3.0",
"@rescript/core": "^1.4.0",
"@rescript/react": "^0.12.0-alpha.3",
"@rescript/tools": "^0.5.0",
"acorn": "^8.11.3",
Expand Down
2 changes: 1 addition & 1 deletion src/ApiDocs.res
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ let processStaticProps = (~slug: array<string>, ~version: string) => {

Variant({items: items})->Null.make
}
| None => Js.Null.empty
| None => Null.null
}
Type({
id,
Expand Down
3 changes: 1 addition & 2 deletions src/components/SearchBox.res
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ let make = (

let onAreaFocus = evt => {
let el = ReactEvent.Focus.target(evt)
// TODO(aspeddro): Replace with `Nullable.isNullable` when Core merge https://github.com/rescript-association/rescript-core/pull/227 and publish a new release
let isDiv = Js.Null_undefined.isNullable(el["type"])
let isDiv = Nullable.isNullable(el["type"])

if isDiv && state === Inactive {
focusInput()
Expand Down
10 changes: 4 additions & 6 deletions src/vendor/Json.res
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ module Encode = Json_encode
exception ParseError(string)

let parse = s =>
try Some(Js.Json.parseExn(s)) catch {
try Some(JSON.parseExn(s)) catch {
| _ => None
}

let parseOrRaise = s =>
try Js.Json.parseExn(s) catch {
| Js.Exn.Error(e) =>
let message = switch Js.Exn.message(e) {
try JSON.parseExn(s) catch {
| Exn.Error(e) =>
let message = switch Exn.message(e) {
| Some(m) => m
| None => "Unknown error"
}
\"@@"(raise, ParseError(message))
}

@val external stringify: Js.Json.t => string = "JSON.stringify"
19 changes: 10 additions & 9 deletions src/vendor/Json.resi
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,14 @@ module Encode = Json_encode

exception ParseError(string)

@ocaml.doc(" [parse s] returns [Some json] if s is a valid json string, [None] otherwise ")
let parse: string => option<Js.Json.t>
/**
`parse(s)` returns `option<JSON.t>` if s is a valid json string, `None`
otherwise
*/
let parse: string => option<JSON.t>

@ocaml.doc(
" [parse s] returns a [Js.Json.t] if s is a valid json string, raises [ParseError] otherwise "
)
let parseOrRaise: string => Js.Json.t

@ocaml.doc(" [stringify json] returns the [string] representation of the given [Js.Json.t] value ")
let stringify: Js.Json.t => string
/**
`parse(s)` returns a `JSON.t` if `s` is a valid json string, raises
`ParseError` otherwise
*/
let parseOrRaise: string => JSON.t
116 changes: 57 additions & 59 deletions src/vendor/Json_decode.res
Original file line number Diff line number Diff line change
@@ -1,115 +1,113 @@
@new external _unsafeCreateUninitializedArray: int => array<'a> = "Array"

@val external _stringify: Js.Json.t => string = "JSON.stringify"
let _isInteger = value => Float.isFinite(value) && Math.floor(value) === value

let _isInteger = value => Js.Float.isFinite(value) && Js.Math.floor_float(value) === value

type decoder<'a> = Js.Json.t => 'a
type decoder<'a> = JSON.t => 'a

exception DecodeError(string)

let id = json => json

let bool = json =>
if Js.typeof(json) == "boolean" {
(Obj.magic((json: Js.Json.t)): bool)
if typeof(json) == #boolean {
(Obj.magic((json: JSON.t)): bool)
} else {
\"@@"(raise, DecodeError("Expected boolean, got " ++ _stringify(json)))
\"@@"(raise, DecodeError("Expected boolean, got " ++ JSON.stringify(json)))
}

let float = json =>
if Js.typeof(json) == "number" {
(Obj.magic((json: Js.Json.t)): float)
if typeof(json) == #number {
(Obj.magic((json: JSON.t)): float)
} else {
\"@@"(raise, DecodeError("Expected number, got " ++ _stringify(json)))
\"@@"(raise, DecodeError("Expected number, got " ++ JSON.stringify(json)))
}

let int = json => {
let f = float(json)
if _isInteger(f) {
(Obj.magic((f: float)): int)
} else {
\"@@"(raise, DecodeError("Expected integer, got " ++ _stringify(json)))
\"@@"(raise, DecodeError("Expected integer, got " ++ JSON.stringify(json)))
}
}

let string = json =>
if Js.typeof(json) == "string" {
(Obj.magic((json: Js.Json.t)): string)
if typeof(json) == #string {
(Obj.magic((json: JSON.t)): string)
} else {
\"@@"(raise, DecodeError("Expected string, got " ++ _stringify(json)))
\"@@"(raise, DecodeError("Expected string, got " ++ JSON.stringify(json)))
}

let char = json => {
let s = string(json)
if String.length(s) == 1 {
OCamlCompat.String.get(s, 0)
} else {
\"@@"(raise, DecodeError("Expected single-character string, got " ++ _stringify(json)))
\"@@"(raise, DecodeError("Expected single-character string, got " ++ JSON.stringify(json)))
}
}

let date = json => Js.Date.fromString(string(json))
let date = json => Date.fromString(string(json))

let nullable = (decode, json) =>
if (Obj.magic(json): Js.null<'a>) === Js.null {
Js.null
if (Obj.magic(json): Null.t<'a>) === Null.null {
Null.null
} else {
Js.Null.return(decode(json))
Null.make(decode(json))
}

/* TODO: remove this? */
let nullAs = (value, json) =>
if (Obj.magic(json): Js.null<'a>) === Js.null {
if (Obj.magic(json): Null.t<'a>) === Null.null {
value
} else {
\"@@"(raise, DecodeError("Expected null, got " ++ _stringify(json)))
\"@@"(raise, DecodeError("Expected null, got " ++ JSON.stringify(json)))
}

let array = (decode, json) =>
if Js.Array.isArray(json) {
let source: array<Js.Json.t> = Obj.magic((json: Js.Json.t))
let length = Js.Array.length(source)
if Array.isArray(json) {
let source: array<JSON.t> = Obj.magic((json: JSON.t))
let length = Array.length(source)
let target = _unsafeCreateUninitializedArray(length)
for i in 0 to length - 1 {
let value = try decode(Array.getUnsafe(source, i)) catch {
| DecodeError(msg) =>
\"@@"(raise, DecodeError(msg ++ ("\n\tin array at index " ++ string_of_int(i))))
\"@@"(raise, DecodeError(msg ++ ("\n\tin array at index " ++ Int.toString(i))))
}

Array.setUnsafe(target, i, value)
}
target
} else {
\"@@"(raise, DecodeError("Expected array, got " ++ _stringify(json)))
\"@@"(raise, DecodeError("Expected array, got " ++ JSON.stringify(json)))
}

let list = (decode, json) => array(decode, json)->List.fromArray

let pair = (decodeA, decodeB, json) =>
if Js.Array.isArray(json) {
let source: array<Js.Json.t> = Obj.magic((json: Js.Json.t))
let length = Js.Array.length(source)
if Array.isArray(json) {
let source: array<JSON.t> = Obj.magic((json: JSON.t))
let length = Array.length(source)
if length == 2 {
try (decodeA(Array.getUnsafe(source, 0)), decodeB(Array.getUnsafe(source, 1))) catch {
| DecodeError(msg) => \"@@"(raise, DecodeError(msg ++ "\n\tin pair/tuple2"))
}
} else {
\"@@"(
raise,
DecodeError(`Expected array of length 2, got array of length ${length->string_of_int}`),
DecodeError(`Expected array of length 2, got array of length ${length->Int.toString}`),
)
}
} else {
\"@@"(raise, DecodeError("Expected array, got " ++ _stringify(json)))
\"@@"(raise, DecodeError("Expected array, got " ++ JSON.stringify(json)))
}

let tuple2 = pair

let tuple3 = (decodeA, decodeB, decodeC, json) =>
if Js.Array.isArray(json) {
let source: array<Js.Json.t> = Obj.magic((json: Js.Json.t))
let length = Js.Array.length(source)
if Array.isArray(json) {
let source: array<JSON.t> = Obj.magic((json: JSON.t))
let length = Array.length(source)
if length == 3 {
try (
decodeA(Array.getUnsafe(source, 0)),
Expand All @@ -121,17 +119,17 @@ let tuple3 = (decodeA, decodeB, decodeC, json) =>
} else {
\"@@"(
raise,
DecodeError(`Expected array of length 3, got array of length ${length->string_of_int}`),
DecodeError(`Expected array of length 3, got array of length ${length->Int.toString}`),
)
}
} else {
\"@@"(raise, DecodeError("Expected array, got " ++ _stringify(json)))
\"@@"(raise, DecodeError("Expected array, got " ++ JSON.stringify(json)))
}

let tuple4 = (decodeA, decodeB, decodeC, decodeD, json) =>
if Js.Array.isArray(json) {
let source: array<Js.Json.t> = Obj.magic((json: Js.Json.t))
let length = Js.Array.length(source)
if Array.isArray(json) {
let source: array<JSON.t> = Obj.magic((json: JSON.t))
let length = Array.length(source)
if length == 4 {
try (
decodeA(Array.getUnsafe(source, 0)),
Expand All @@ -144,52 +142,52 @@ let tuple4 = (decodeA, decodeB, decodeC, decodeD, json) =>
} else {
\"@@"(
raise,
DecodeError(`Expected array of length 4, got array of length ${length->string_of_int}`),
DecodeError(`Expected array of length 4, got array of length ${length->Int.toString}`),
)
}
} else {
\"@@"(raise, DecodeError("Expected array, got " ++ _stringify(json)))
\"@@"(raise, DecodeError("Expected array, got " ++ JSON.stringify(json)))
}

let dict = (decode, json) =>
if (
Js.typeof(json) == "object" &&
(!Js.Array.isArray(json) &&
!((Obj.magic(json): Js.null<'a>) === Js.null))
typeof(json) == #object &&
(!Array.isArray(json) &&
!((Obj.magic(json): Null.t<'a>) === Null.null))
) {
let source: Js.Dict.t<Js.Json.t> = Obj.magic((json: Js.Json.t))
let keys = Js.Dict.keys(source)
let l = Js.Array.length(keys)
let target = Js.Dict.empty()
let source: Dict.t<JSON.t> = Obj.magic((json: JSON.t))
let keys = Dict.keysToArray(source)
let l = Array.length(keys)
let target = Dict.make()
for i in 0 to l - 1 {
let key = Array.getUnsafe(keys, i)
let value = try decode(Js.Dict.unsafeGet(source, key)) catch {
let value = try decode(Dict.getUnsafe(source, key)) catch {
| DecodeError(msg) => \"@@"(raise, DecodeError(msg ++ "\n\tin dict"))
}

Js.Dict.set(target, key, value)
Dict.set(target, key, value)
}
target
} else {
\"@@"(raise, DecodeError("Expected object, got " ++ _stringify(json)))
\"@@"(raise, DecodeError("Expected object, got " ++ JSON.stringify(json)))
}

let field = (key, decode, json) =>
if (
Js.typeof(json) == "object" &&
(!Js.Array.isArray(json) &&
!((Obj.magic(json): Js.null<'a>) === Js.null))
typeof(json) == #object &&
(!Array.isArray(json) &&
!((Obj.magic(json): Null.t<'a>) === Null.null))
) {
let dict: Js.Dict.t<Js.Json.t> = Obj.magic((json: Js.Json.t))
switch Js.Dict.get(dict, key) {
let dict: Dict.t<JSON.t> = Obj.magic((json: JSON.t))
switch Dict.get(dict, key) {
| Some(value) =>
try decode(value) catch {
| DecodeError(msg) => \"@@"(raise, DecodeError(msg ++ ("\n\tat field '" ++ (key ++ "'"))))
}
| None => \"@@"(raise, DecodeError(`Expected field '${key}'`))
}
} else {
\"@@"(raise, DecodeError("Expected object, got " ++ _stringify(json)))
\"@@"(raise, DecodeError("Expected object, got " ++ JSON.stringify(json)))
}

let rec at = (key_path, decoder, json) =>
Expand All @@ -208,12 +206,12 @@ let oneOf = (decoders, json) => {
let rec inner = (decoders, errors) =>
switch decoders {
| list{} =>
let formattedErrors = "\n- " ++ Js.Array.joinWith("\n- ", List.toArray(List.reverse(errors)))
let formattedErrors = "\n- " ++ Array.join(List.toArray(List.reverse(errors)), "\n- ")
\"@@"(
raise,
DecodeError(
`All decoders given to oneOf failed. Here are all the errors: ${formattedErrors}\\nAnd the JSON being decoded: ` ++
_stringify(json),
JSON.stringify(json),
),
)
| list{decode, ...rest} =>
Expand Down
Loading