Skip to content

Commit

Permalink
Gradually migrate to val
Browse files Browse the repository at this point in the history
  • Loading branch information
DZakh committed May 12, 2024
1 parent 2756267 commit f28811c
Show file tree
Hide file tree
Showing 9 changed files with 557 additions and 433 deletions.
4 changes: 2 additions & 2 deletions packages/tests/src/core/Example_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ test("Compiled parse code snapshot", t => {
t->U.assertCompiledCode(
~schema=filmSchema,
~op=#parse,
`i=>{if(!i||i.constructor!==Object){e[11](i)}let v0,v1,v2,v7,v8=i["Rating"],v9,v14,v15;v0=i["Id"];if(typeof v0!=="number"||Number.isNaN(v0)){e[0](v0)}v1=i["Title"];if(typeof v1!=="string"){e[1](v1)}v2=i["Tags"];if(v2!==void 0&&(!Array.isArray(v2))){e[2](v2)}if(v2!==void 0){let v6=[];for(let v3=0;v3<v2.length;++v3){let v5;try{v5=v2[v3];if(typeof v5!=="string"){e[3](v5)}}catch(v4){if(v4&&v4.s===s){v4.path="[\\"Tags\\"]"+\'["\'+v3+\'"]\'+v4.path}throw v4}v6.push(v5)}v7=v6}try{v8==="G"||e[5](v8);v9=v8}catch(v10){if(v10&&v10.s===s){try{v8==="PG"||e[6](v8);v9=v8}catch(v11){if(v11&&v11.s===s){try{v8==="PG13"||e[7](v8);v9=v8}catch(v12){if(v12&&v12.s===s){try{v8==="R"||e[8](v8);v9=v8}catch(v13){if(v13&&v13.s===s){e[9]([v10,v11,v12,v13])}else{throw v13}}}else{throw v12}}}else{throw v11}}}else{throw v10}}v14=i["Age"];if(v14!==void 0&&(typeof v14!=="number"||v14>2147483647||v14<-2147483648||v14%1!==0)){e[10](v14)}if(v14!==void 0){v15=v14}return {"id":v0,"title":v1,"tags":v7===void 0?e[4]:v7,"rating":v9,"deprecatedAgeRestriction":v15,}}`,
`i=>{if(!i||i.constructor!==Object){e[11](i)}let v0=i["Id"],v1=i["Title"],v2=i["Tags"],v7,v8=i["Rating"],v9,v14=i["Age"],v15;if(typeof v0!=="number"||Number.isNaN(v0)){e[0](v0)}if(typeof v1!=="string"){e[1](v1)}if(v2!==void 0&&(!Array.isArray(v2))){e[2](v2)}if(v2!==void 0){let v6=[];for(let v3=0;v3<v2.length;++v3){let v5=v2[v3];try{if(typeof v5!=="string"){e[3](v5)}}catch(v4){if(v4&&v4.s===s){v4.path="[\\"Tags\\"]"+\'["\'+v3+\'"]\'+v4.path}throw v4}v6.push(v5)}v7=v6}try{v8==="G"||e[5](v8);v9=v8}catch(v10){if(v10&&v10.s===s){try{v8==="PG"||e[6](v8);v9=v8}catch(v11){if(v11&&v11.s===s){try{v8==="PG13"||e[7](v8);v9=v8}catch(v12){if(v12&&v12.s===s){try{v8==="R"||e[8](v8);v9=v8}catch(v13){if(v13&&v13.s===s){e[9]([v10,v11,v12,v13])}else{throw v13}}}else{throw v12}}}else{throw v11}}}else{throw v10}}if(v14!==void 0&&(typeof v14!=="number"||v14>2147483647||v14<-2147483648||v14%1!==0)){e[10](v14)}if(v14!==void 0){v15=v14}return {"id":v0,"title":v1,"tags":v7===void 0?e[4]:v7,"rating":v9,"deprecatedAgeRestriction":v15,}}`,
)
})

test("Compiled serialize code snapshot", t => {
t->U.assertCompiledCode(
~schema=filmSchema,
~op=#serialize,
`i=>{let v0=i["tags"],v1,v2=i["rating"],v3,v8=i["deprecatedAgeRestriction"],v9;if(v0!==void 0){v1=e[0](v0)}try{v2==="G"||e[1](v2);v3=v2}catch(v4){if(v4&&v4.s===s){try{v2==="PG"||e[2](v2);v3=v2}catch(v5){if(v5&&v5.s===s){try{v2==="PG13"||e[3](v2);v3=v2}catch(v6){if(v6&&v6.s===s){try{v2==="R"||e[4](v2);v3=v2}catch(v7){if(v7&&v7.s===s){e[5]([v4,v5,v6,v7,])}else{throw v7}}}else{throw v6}}}else{throw v5}}}else{throw v4}}if(v8!==void 0){v9=e[6](v8)}return {"Id":i["id"],"Title":i["title"],"Tags":v1,"Rating":v3,"Age":v9,}}`,
`i=>{let v0=i["tags"],v1,v2,v4=i["rating"],v8=i["deprecatedAgeRestriction"],v9;if(v0!==void 0){v1=e[0](v0)}try{v4==="G"||e[1](v4);v2=v4}catch(v3){if(v3&&v3.s===s){try{v4==="PG"||e[2](v4);v2=v4}catch(v5){if(v5&&v5.s===s){try{v4==="PG13"||e[3](v4);v2=v4}catch(v6){if(v6&&v6.s===s){try{v4==="R"||e[4](v4);v2=v4}catch(v7){if(v7&&v7.s===s){e[5]([v3,v5,v6,v7,])}else{throw v7}}}else{throw v6}}}else{throw v5}}}else{throw v3}}if(v8!==void 0){v9=e[6](v8)}return {"Id":i["id"],"Title":i["title"],"Tags":v1,"Rating":v2,"Age":v9,}}`,
)
})
2 changes: 1 addition & 1 deletion packages/tests/src/core/S_array_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module CommonWithNested = {
t->U.assertCompiledCode(
~schema,
~op=#parse,
`i=>{if(!Array.isArray(i)){e[1](i)}let v3=[];for(let v0=0;v0<i.length;++v0){let v2;try{v2=i[v0];if(typeof v2!=="string"){e[0](v2)}}catch(v1){if(v1&&v1.s===s){v1.path=""+\'["\'+v0+\'"]\'+v1.path}throw v1}v3.push(v2)}return v3}`,
`i=>{if(!Array.isArray(i)){e[1](i)}let v3=[];for(let v0=0;v0<i.length;++v0){let v2=i[v0];try{if(typeof v2!=="string"){e[0](v2)}}catch(v1){if(v1&&v1.s===s){v1.path=""+\'["\'+v0+\'"]\'+v1.path}throw v1}v3.push(v2)}return v3}`,
)
})

Expand Down
2 changes: 1 addition & 1 deletion packages/tests/src/core/S_dict_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module CommonWithNested = {
t->U.assertCompiledCode(
~schema,
~op=#parse,
`i=>{if(!i||i.constructor!==Object){e[1](i)}let v1;v1={};for(let v0 in i){let v3;try{v3=i[v0];if(typeof v3!=="string"){e[0](v3)}}catch(v2){if(v2&&v2.s===s){v2.path=""+\'["\'+v0+\'"]\'+v2.path}throw v2}v1[v0]=v3}return v1}`,
`i=>{if(!i||i.constructor!==Object){e[1](i)}let v1;v1={};for(let v0 in i){let v3=i[v0];try{if(typeof v3!=="string"){e[0](v3)}}catch(v2){if(v2&&v2.s===s){v2.path=""+\'["\'+v0+\'"]\'+v2.path}throw v2}v1[v0]=v3}return v1}`,
)
})

Expand Down
8 changes: 4 additions & 4 deletions packages/tests/src/core/S_object_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ module Compiled = {
t->U.assertCompiledCode(
~schema,
~op=#parse,
`i=>{if(!i||i.constructor!==Object){e[2](i)}let v0,v1;v0=i["foo"];if(typeof v0!=="string"){e[0](v0)}v1=i["bar"];if(typeof v1!=="boolean"){e[1](v1)}return {"foo":v0,"bar":v1,}}`,
`i=>{if(!i||i.constructor!==Object){e[2](i)}let v0=i["foo"],v1=i["bar"];if(typeof v0!=="string"){e[0](v0)}if(typeof v1!=="boolean"){e[1](v1)}return {"foo":v0,"bar":v1,}}`,
)
})

Expand All @@ -1004,7 +1004,7 @@ module Compiled = {
t->U.assertCompiledCode(
~schema,
~op=#parse,
`i=>{if(!i||i.constructor!==Object){e[2](i)}let v0,v1,v2=()=>Promise.all([v0()]).then(([v0])=>({"foo":v0,"bar":v1,}));v0=e[0](i["foo"]);v1=i["bar"];if(typeof v1!=="boolean"){e[1](v1)}return v2}`,
`i=>{if(!i||i.constructor!==Object){e[2](i)}let v0,v1=i["bar"],v2=()=>Promise.all([v0()]).then(([v0])=>({"foo":v0,"bar":v1,}));v0=e[0](i["foo"]);if(typeof v1!=="boolean"){e[1](v1)}return v2}`,
)
})

Expand All @@ -1030,7 +1030,7 @@ module Compiled = {
t->U.assertCompiledCode(
~schema,
~op=#parse,
`i=>{if(!i||i.constructor!==Object){e[3](i)}let v0,v1,v2;v1=i["foo"];if(typeof v1!=="string"){e[1](v1)}v2=i["bar"];if(typeof v2!=="boolean"){e[2](v2)}for(v0 in i){if(v0!=="foo"&&v0!=="bar"){e[0](v0)}}return {"foo":v1,"bar":v2,}}`,
`i=>{if(!i||i.constructor!==Object){e[3](i)}let v0,v1=i["foo"],v2=i["bar"];if(typeof v1!=="string"){e[1](v1)}if(typeof v2!=="boolean"){e[2](v2)}for(v0 in i){if(v0!=="foo"&&v0!=="bar"){e[0](v0)}}return {"foo":v1,"bar":v2,}}`,
)
})

Expand Down Expand Up @@ -1060,7 +1060,7 @@ module Compiled = {
t->U.assertCompiledCode(
~schema,
~op=#parse,
`i=>{if(!i||i.constructor!==Object){e[5](i)}let v0,v1,v2,v3=i["tag"];v3===0||e[4](v3);v1=i["FOO"];if(typeof v1!=="string"){e[1](v1)}v2=i["BAR"];if(typeof v2!=="boolean"){e[2](v2)}for(v0 in i){if(v0!=="tag"&&v0!=="FOO"&&v0!=="BAR"){e[0](v0)}}return {"foo":v1,"bar":v2,"zoo":e[3],}}`,
`i=>{if(!i||i.constructor!==Object){e[5](i)}let v0,v1=i["FOO"],v2=i["BAR"],v3=i["tag"];v3===0||e[4](v3);if(typeof v1!=="string"){e[1](v1)}if(typeof v2!=="boolean"){e[2](v2)}for(v0 in i){if(v0!=="tag"&&v0!=="FOO"&&v0!=="BAR"){e[0](v0)}}return {"foo":v1,"bar":v2,"zoo":e[3],}}`,
)
},
)
Expand Down
4 changes: 2 additions & 2 deletions packages/tests/src/core/S_refine_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ test("Compiled parse code snapshot for simple object with refine", t => {
t->U.assertCompiledCode(
~schema,
~op=#parse,
// FIXME: let v0,v1;v0= looks wrong
`i=>{if(!i||i.constructor!==Object){e[3](i)}let v2;let v0,v1;v0=i["foo"];if(typeof v0!=="string"){e[0](v0)}v1=i["bar"];if(typeof v1!=="boolean"){e[1](v1)}v2={"foo":v0,"bar":v1,};e[2](v2);return v2}`,
// FIXME: Double "let" looks wrong
`i=>{if(!i||i.constructor!==Object){e[3](i)}let v2={"foo":v0,"bar":v1,};let v0=i["foo"],v1=i["bar"];if(typeof v0!=="string"){e[0](v0)}if(typeof v1!=="boolean"){e[1](v1)}e[2](v2);return v2}`,
)
})
6 changes: 3 additions & 3 deletions packages/tests/src/core/S_tuple_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ module Compiled = {
t->U.assertCompiledCode(
~schema,
~op=#parse,
`i=>{if(!Array.isArray(i)){e[3](i)}if(i.length!==2){e[0](i.length)}let v0,v1;v0=i["0"];if(typeof v0!=="string"){e[1](v0)}v1=i["1"];if(typeof v1!=="boolean"){e[2](v1)}return [v0,v1,]}`,
`i=>{if(!Array.isArray(i)){e[3](i)}if(i.length!==2){e[0](i.length)}let v0=i["0"],v1=i["1"];if(typeof v0!=="string"){e[1](v0)}if(typeof v1!=="boolean"){e[2](v1)}return [v0,v1,]}`,
)
})

Expand All @@ -247,7 +247,7 @@ module Compiled = {
t->U.assertCompiledCode(
~schema,
~op=#parse,
`i=>{if(!Array.isArray(i)){e[3](i)}if(i.length!==2){e[0](i.length)}let v0,v1,v2=()=>Promise.all([v0()]).then(([v0])=>([v0,v1,]));v0=e[1](i["0"]);v1=i["1"];if(typeof v1!=="boolean"){e[2](v1)}return v2}`,
`i=>{if(!Array.isArray(i)){e[3](i)}if(i.length!==2){e[0](i.length)}let v0,v1=i["1"],v2=()=>Promise.all([v0()]).then(([v0])=>([v0,v1,]));v0=e[1](i["0"]);if(typeof v1!=="boolean"){e[2](v1)}return v2}`,
)
})

Expand Down Expand Up @@ -277,7 +277,7 @@ module Compiled = {
t->U.assertCompiledCode(
~schema,
~op=#parse,
`i=>{if(!Array.isArray(i)){e[5](i)}if(i.length!==3){e[0](i.length)}let v0,v1,v2=i["0"];v2===0||e[4](v2);v0=i["1"];if(typeof v0!=="string"){e[1](v0)}v1=i["2"];if(typeof v1!=="boolean"){e[2](v1)}return {"foo":v0,"bar":v1,"zoo":e[3],}}`,
`i=>{if(!Array.isArray(i)){e[5](i)}if(i.length!==3){e[0](i.length)}let v0=i["1"],v1=i["2"],v2=i["0"];v2===0||e[4](v2);if(typeof v0!=="string"){e[1](v0)}if(typeof v1!=="boolean"){e[2](v1)}return {"foo":v0,"bar":v1,"zoo":e[3],}}`,
)
},
)
Expand Down
10 changes: 9 additions & 1 deletion packages/tests/src/core/S_union_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,14 @@ test("Compiled parse code snapshot", t => {
)
})

test("Compiled parse code snapshot for discriminated union", t => {
t->U.assertCompiledCode(
~schema=Advanced.shapeSchema,
~op=#parse,
`i=>{let v7;try{if(!i||i.constructor!==Object){e[0](i)}let v0=i["radius"],v1=i["kind"];v1==="circle"||e[3](v1);if(typeof v0!=="number"||Number.isNaN(v0)){e[2](v0)}v7={"TAG":e[1],"radius":v0,}}catch(v8){if(v8&&v8.s===s){try{if(!i||i.constructor!==Object){e[4](i)}let v2=i["x"],v3=i["kind"];v3==="square"||e[7](v3);if(typeof v2!=="number"||Number.isNaN(v2)){e[6](v2)}v7={"TAG":e[5],"x":v2,}}catch(v9){if(v9&&v9.s===s){try{if(!i||i.constructor!==Object){e[8](i)}let v4=i["x"],v5=i["y"],v6=i["kind"];v6==="triangle"||e[12](v6);if(typeof v4!=="number"||Number.isNaN(v4)){e[10](v4)}if(typeof v5!=="number"||Number.isNaN(v5)){e[11](v5)}v7={"TAG":e[9],"x":v4,"y":v5,}}catch(v10){if(v10&&v10.s===s){e[13]([v8,v9,v10])}else{throw v10}}}else{throw v9}}}else{throw v8}}return v7}`,
)
})

test("Compiled async parse code snapshot", t => {
let schema = S.union([
S.literal(0)->S.transform(_ => {asyncParser: i => () => Promise.resolve(i)}),
Expand Down Expand Up @@ -381,6 +389,6 @@ test("Compiled serialize code snapshot for unboxed variant", t => {
t->U.assertCompiledCode(
~schema,
~op=#serialize,
`i=>{let v0;try{if(typeof i!=="string"){e[0](i)}v0=i}catch(v1){if(v1&&v1.s===s){try{let v3;v3=e[1](i);if(typeof v3!=="string"){e[2](v3)}v0=v3}catch(v2){if(v2&&v2.s===s){e[3]([v1,v2,])}else{throw v2}}}else{throw v1}}return v0}`,
`i=>{let v0;try{if(typeof i!=="string"){e[0](i)}v0=i}catch(v1){if(v1&&v1.s===s){try{let v3=e[1](i);if(typeof v3!=="string"){e[2](v3)}v0=v3}catch(v2){if(v2&&v2.s===s){e[3]([v1,v2,])}else{throw v2}}}else{throw v1}}return v0}`,
)
})
Loading

0 comments on commit f28811c

Please sign in to comment.