Skip to content

Commit

Permalink
Use built-in dict type
Browse files Browse the repository at this point in the history
  • Loading branch information
DZakh committed Apr 27, 2024
1 parent b8de8ee commit c0b7eba
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 24 deletions.
6 changes: 3 additions & 3 deletions packages/prepack/src/Prepack.res
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let jsInputPath = NodeJs.Path.join2(artifactsPath, "src/S.js")
module Stdlib = {
module Dict = {
@val
external copy: (@as(json`{}`) _, Dict.t<'a>) => Dict.t<'a> = "Object.assign"
external copy: (@as(json`{}`) _, dict<'a>) => dict<'a> = "Object.assign"
}

module Json = {
Expand Down Expand Up @@ -45,7 +45,7 @@ module Stdlib = {

module Execa = {
type returnValue = {stdout: string}
type options = {env?: Dict.t<string>, cwd?: string}
type options = {env?: dict<string>, cwd?: string}

@module("execa")
external sync: (string, array<string>, ~options: options=?, unit) => returnValue = "execaSync"
Expand All @@ -68,7 +68,7 @@ module Rollup = {
}

module ReplacePlugin = {
type options = {values: Dict.t<string>}
type options = {values: dict<string>}
@module("@rollup/plugin-replace") external make: options => Plugin.t = "default"
}

Expand Down
7 changes: 7 additions & 0 deletions packages/tests/src/ppx/Ppx_Primitive_test.bs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ Ava("Dict of string schema", (function (t) {
U.assertEqualSchemas(t, myDictOfStringSchema, S$RescriptSchema.dict(S$RescriptSchema.string), undefined);
}));

var myDictOfStringFromJsSchema = S$RescriptSchema.dict(S$RescriptSchema.string);

Ava("Dict of string schema from Js", (function (t) {
U.assertEqualSchemas(t, myDictOfStringSchema, S$RescriptSchema.dict(S$RescriptSchema.string), undefined);
}));

var myDictOfStringFromCoreSchema = S$RescriptSchema.dict(S$RescriptSchema.string);

Ava("Dict of string schema from Core", (function (t) {
Expand Down Expand Up @@ -171,6 +177,7 @@ export {
myArrayOfStringSchema ,
myListOfStringSchema ,
myDictOfStringSchema ,
myDictOfStringFromJsSchema ,
myDictOfStringFromCoreSchema ,
myJsonSchema ,
myJsonFromCoreSchema ,
Expand Down
8 changes: 7 additions & 1 deletion packages/tests/src/ppx/Ppx_Primitive_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,17 @@ test("List of string schema", t => {
})

@schema
type myDictOfString = Js.Dict.t<string>
type myDictOfString = dict<string>
test("Dict of string schema", t => {
t->assertEqualSchemas(myDictOfStringSchema, S.dict(S.string))
})

@schema
type myDictOfStringFromJs = Js.Dict.t<string>
test("Dict of string schema from Js", t => {
t->assertEqualSchemas(myDictOfStringSchema, S.dict(S.string))
})

@schema
type myDictOfStringFromCore = Dict.t<string>
test("Dict of string schema from Core", t => {
Expand Down
6 changes: 3 additions & 3 deletions src/S.resi
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Literal: {
| BigInt(Js.Types.bigint_val)
| Symbol(Js.Types.symbol)
| Array(array<t>)
| Dict(Js.Dict.t<t>)
| Dict(dict<t>)
| Function(Js.Types.function_val)
| Object(Js.Types.obj_val)
| Null
Expand Down Expand Up @@ -55,7 +55,7 @@ and tagged =
| Option(t<unknown>)
| Null(t<unknown>)
| Array(t<unknown>)
| Object({fields: Js.Dict.t<t<unknown>>, fieldNames: array<string>, unknownKeys: unknownKeys})
| Object({fields: dict<t<unknown>>, fieldNames: array<string>, unknownKeys: unknownKeys})
| Tuple(array<t<unknown>>)
| Union(array<t<unknown>>)
| Dict(t<unknown>)
Expand Down Expand Up @@ -110,7 +110,7 @@ let json: t<Js.Json.t>
let literal: 'value => t<'value>
let array: t<'value> => t<array<'value>>
let list: t<'value> => t<list<'value>>
let dict: t<'value> => t<Js.Dict.t<'value>>
let dict: t<'value> => t<dict<'value>>
let option: t<'value> => t<option<'value>>
let null: t<'value> => t<option<'value>>
let nullable: t<'value> => t<option<'value>>
Expand Down
26 changes: 13 additions & 13 deletions src/S_Core.res
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ module Stdlib = {

module Dict = {
@val
external copy: (@as(json`{}`) _, Js.Dict.t<'a>) => Js.Dict.t<'a> = "Object.assign"
external copy: (@as(json`{}`) _, dict<'a>) => dict<'a> = "Object.assign"

@inline
let has = (dict, key) => {
Expand All @@ -106,18 +106,18 @@ module Stdlib = {

@inline
let deleteInPlace = (dict, key) => {
Js.Dict.unsafeDeleteKey(dict->(Obj.magic: Js.Dict.t<'a> => Js.Dict.t<string>), key)
Js.Dict.unsafeDeleteKey(dict->(Obj.magic: dict<'a> => dict<string>), key)
}

let mapValues: (Js.Dict.t<'a>, 'a => 'b) => Js.Dict.t<'b> = %raw(`(dict, fn)=>{
let mapValues: (dict<'a>, 'a => 'b) => dict<'b> = %raw(`(dict, fn)=>{
var key,newDict = {};
for (key in dict) {
newDict[key] = fn(dict[key])
}
return newDict
}`)

let every: (Js.Dict.t<'a>, 'a => bool) => bool = %raw(`(dict, fn)=>{
let every: (dict<'a>, 'a => bool) => bool = %raw(`(dict, fn)=>{
for (var key in dict) {
if (!fn(dict[key])) {
return false
Expand Down Expand Up @@ -193,7 +193,7 @@ module Literal = {
| BigInt(Js.Types.bigint_val)
| Symbol(Js.Types.symbol)
| Array(array<t>)
| Dict(Js.Dict.t<t>)
| Dict(dict<t>)
| Function(Js.Types.function_val)
| Object(Js.Types.obj_val)
| Null
Expand All @@ -209,7 +209,7 @@ module Literal = {
Array(value->(Obj.magic: 'a => array<'b>)->Js.Array2.map(i => i->classify))
| #object
if (value->(Obj.magic: 'a => {"constructor": unknown}))["constructor"] === %raw("Object") =>
Dict(value->(Obj.magic: 'a => Js.Dict.t<'b>)->Dict.mapValues(classify))
Dict(value->(Obj.magic: 'a => dict<'b>)->Dict.mapValues(classify))
| #object => Object(value->(Obj.magic: 'a => Js.Types.obj_val))
| #function => Function(value->(Obj.magic: 'a => Js.Types.function_val))
| #string => String(value->(Obj.magic: 'a => string))
Expand Down Expand Up @@ -340,7 +340,7 @@ type rec t<'value> = {
@as("i")
mutable isAsyncParse: isAsyncParse,
@as("m")
metadataMap: Js.Dict.t<unknown>,
metadataMap: dict<unknown>,
}
and tagged =
| Never
Expand All @@ -353,7 +353,7 @@ and tagged =
| Option(t<unknown>)
| Null(t<unknown>)
| Array(t<unknown>)
| Object({fields: Js.Dict.t<t<unknown>>, fieldNames: array<string>, unknownKeys: unknownKeys})
| Object({fields: dict<t<unknown>>, fieldNames: array<string>, unknownKeys: unknownKeys})
| Tuple(array<t<unknown>>)
| Union(array<t<unknown>>)
| Dict(t<unknown>)
Expand Down Expand Up @@ -1443,7 +1443,7 @@ let rec literalCheckBuilder = (b, ~value, ~inputVar) => {
->Js.Array2.joinWith("&&")
: "") ++ ")"
} else if %raw(`value&&value.constructor===Object`) {
let value = value->(Obj.magic: unknown => Js.Dict.t<unknown>)
let value = value->(Obj.magic: unknown => dict<unknown>)
let keys = value->Js.Dict.keys
let numberOfKeys = keys->Js.Array2.length
`(${check}||${inputVar}&&${inputVar}.constructor===Object&&Object.keys(${inputVar}).length===${numberOfKeys->Stdlib.Int.unsafeToString}` ++
Expand Down Expand Up @@ -1494,7 +1494,7 @@ let unit = literal(%raw("void 0"))

module Definition = {
type t<'embeded>
type node<'embeded> = Js.Dict.t<t<'embeded>>
type node<'embeded> = dict<t<'embeded>>
type kind = | @as(0) Node | @as(1) Constant | @as(2) Embeded

let toKindWithSet = (definition: t<'embeded>, ~embededSet: Stdlib.Set.t<'embeded>) => {
Expand Down Expand Up @@ -1879,7 +1879,7 @@ module Object = {
@as("n")
fieldNames: array<string>,
@as("h")
fields: Js.Dict.t<schema<unknown>>,
fields: dict<schema<unknown>>,
@as("d")
itemDefinitionsSet: Stdlib.Set.t<itemDefinition>,
// Public API for JS/TS users.
Expand Down Expand Up @@ -3163,7 +3163,7 @@ let json = makeWithNoopSerializer(
}
output->Js.Json.array
} else {
let input = input->(Obj.magic: unknown => Js.Dict.t<unknown>)
let input = input->(Obj.magic: unknown => dict<unknown>)
let keys = input->Js.Dict.keys
let output = Js.Dict.empty()
for idx in 0 to keys->Js.Array2.length - 1 {
Expand Down Expand Up @@ -3764,7 +3764,7 @@ let js_object = definer => {
let definer = definer->(Obj.magic: unknown => Object.ctx => 'a)
object(definer)
} else {
let definer = definer->(Obj.magic: unknown => Js.Dict.t<t<unknown>>)
let definer = definer->(Obj.magic: unknown => dict<t<unknown>>)
object(s => {
let definition = Js.Dict.empty()
let fieldNames = definer->Js.Dict.keys
Expand Down
8 changes: 4 additions & 4 deletions src/S_Core.resi
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Literal: {
| BigInt(Js.Types.bigint_val)
| Symbol(Js.Types.symbol)
| Array(array<t>)
| Dict(Js.Dict.t<t>)
| Dict(dict<t>)
| Function(Js.Types.function_val)
| Object(Js.Types.obj_val)
| Null
Expand Down Expand Up @@ -55,7 +55,7 @@ and tagged =
| Option(t<unknown>)
| Null(t<unknown>)
| Array(t<unknown>)
| Object({fields: Js.Dict.t<t<unknown>>, fieldNames: array<string>, unknownKeys: unknownKeys})
| Object({fields: dict<t<unknown>>, fieldNames: array<string>, unknownKeys: unknownKeys})
| Tuple(array<t<unknown>>)
| Union(array<t<unknown>>)
| Dict(t<unknown>)
Expand Down Expand Up @@ -110,7 +110,7 @@ let json: t<Js.Json.t>
let literal: 'value => t<'value>
let array: t<'value> => t<array<'value>>
let list: t<'value> => t<list<'value>>
let dict: t<'value> => t<Js.Dict.t<'value>>
let dict: t<'value> => t<dict<'value>>
let option: t<'value> => t<option<'value>>
let null: t<'value> => t<option<'value>>
let nullable: t<'value> => t<option<'value>>
Expand Down Expand Up @@ -382,7 +382,7 @@ let js_transform: (
~serializer: ('transformed, effectCtx<'transformed>) => 'output=?,
) => t<'transformed>

let js_object: unknown => t<Js.Dict.t<unknown>>
let js_object: unknown => t<dict<unknown>>

let js_merge: (t<unknown>, t<unknown>) => t<unknown>

Expand Down

1 comment on commit c0b7eba

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: c0b7eba Previous: 9fb2e69 Ratio
Parse string 819558333 ops/sec (±0.09%) 819064824 ops/sec (±0.11%) 1.00
Serialize string 820233287 ops/sec (±0.05%) 819779932 ops/sec (±0.08%) 1.00
Advanced object schema factory 417788 ops/sec (±0.48%) 431262 ops/sec (±0.44%) 1.03
Parse advanced object 46551453 ops/sec (±0.23%) 46671133 ops/sec (±0.42%) 1.00
Create and parse advanced object 33720 ops/sec (±0.66%) 34129 ops/sec (±0.58%) 1.01
Parse advanced strict object 22081901 ops/sec (±0.73%) 22262984 ops/sec (±0.24%) 1.01
Serialize advanced object 807767155 ops/sec (±0.23%) 807737293 ops/sec (±0.20%) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.