Skip to content

Commit 21a3f32

Browse files
committed
Fix S.schema used in s.flatten
1 parent 13ceb78 commit 21a3f32

File tree

7 files changed

+80
-23
lines changed

7 files changed

+80
-23
lines changed

packages/tests/src/core/S_object_flatten_test.res

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,42 @@ test("Has correct tagged type", t => {
2525
})
2626

2727
test("Can flatten S.schema", t => {
28+
let schema = S.object(s => {
29+
{
30+
"baz": s.flatten(
31+
S.schema(
32+
s =>
33+
{
34+
"bar": s.matches(S.string),
35+
},
36+
),
37+
),
38+
"foo": s.field("foo", S.string),
39+
}
40+
})
41+
42+
t->U.unsafeAssertEqualSchemas(
43+
schema,
44+
S.object(s =>
45+
{
46+
"bar": s.field("bar", S.string),
47+
"foo": s.field("foo", S.string),
48+
}
49+
),
50+
)
51+
t->U.assertCompiledCode(
52+
~schema,
53+
~op=#Parse,
54+
`i=>{if(!i||i.constructor!==Object){e[2](i)}let v0=i["bar"],v1=i["foo"];if(typeof v0!=="string"){e[0](v0)}if(typeof v1!=="string"){e[1](v1)}return {"baz":{"bar":v0,},"foo":v1,}}`,
55+
)
56+
t->U.assertCompiledCode(
57+
~schema,
58+
~op=#ReverseConvert,
59+
`i=>{let v0=i["baz"];return {"bar":v0["bar"],"foo":i["foo"],}}`,
60+
)
61+
})
62+
63+
test("Can flatten & destructure S.schema", t => {
2864
let schema = S.object(s => {
2965
let flattened = s.flatten(S.schema(s => {"bar": s.matches(S.string)}))
3066
{
@@ -47,6 +83,11 @@ test("Can flatten S.schema", t => {
4783
~op=#Parse,
4884
`i=>{if(!i||i.constructor!==Object){e[2](i)}let v0=i["bar"],v1=i["foo"];if(typeof v0!=="string"){e[0](v0)}if(typeof v1!=="string"){e[1](v1)}return {"bar":v0,"foo":v1,}}`,
4985
)
86+
t->U.assertCompiledCode(
87+
~schema,
88+
~op=#ReverseConvert,
89+
`i=>{return {"bar":i["bar"],"foo":i["foo"],}}`,
90+
)
5091
})
5192

5293
test("Can flatten strict object", t => {

packages/tests/src/core/S_object_nested_test.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ test("Object with a deep strict applied to the nested field parent", t => {
372372
t->U.assertCompiledCode(
373373
~schema,
374374
~op=#Parse,
375-
`i=>{if(!i||i.constructor!==Object){e[4](i)}let v0=i["nested"],v3;if(!v0||v0.constructor!==Object){e[0](v0)}let v1=v0["foo"],v2;if(typeof v1!=="string"){e[1](v1)}for(v2 in v0){if(v2!=="foo"){e[2](v2)}}for(v3 in i){if(v3!=="nested"){e[3](v3)}}return v0["foo"]}`,
375+
`i=>{if(!i||i.constructor!==Object){e[4](i)}let v0=i["nested"],v3;if(!v0||v0.constructor!==Object){e[0](v0)}let v1=v0["foo"],v2;if(typeof v1!=="string"){e[1](v1)}for(v2 in v0){if(v2!=="foo"){e[2](v2)}}for(v3 in i){if(v3!=="nested"){e[3](v3)}}return v1}`,
376376
)
377377
t->U.assertCompiledCode(~schema, ~op=#ReverseConvert, `i=>{return {"nested":{"foo":i,},}}`)
378378
})

packages/tests/src/core/S_union_test.res

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ test("Union with nested variant", t => {
458458
t->U.assertCompiledCode(
459459
~schema,
460460
~op=#ReverseConvert,
461-
`i=>{let v5=i;if(!i||i.constructor!==Object){e[3](i)}else{try{let v0=i["foo"];let v1=v0["tag"];let v2=v1["NAME"],v3=v1["VAL"],v4;if(v2!=="Null"){e[0](v2)}if(v3!==void 0){v4=v3}else{v4=null}v5={"foo":{"tag":{"NAME":v2,"VAL":v4,},},}}catch(e0){try{let v6=i["foo"];let v7=v6["tag"];let v8=v7["NAME"];if(v8!=="Option"){e[1](v8)}}catch(e1){e[2]([e0,e1,])}}}return v5}`,
461+
`i=>{let v5=i;if(!i||i.constructor!==Object){e[3](i)}else{try{let v0=i["foo"];let v1=v0["tag"];let v2=v1["NAME"],v3=v1["VAL"],v4;if(v2!=="Null"){e[0](v2)}if(v3!==void 0){v4=v3}else{v4=null}v5={"foo":{"tag":{"NAME":v2,"VAL":v4,},},}}catch(e0){try{let v6=i["foo"];let v7=v6["tag"];let v8=v7["NAME"];if(v8!=="Option"){e[1](v8)}v5=i}catch(e1){e[2]([e0,e1,])}}}return v5}`,
462462
)
463463
})
464464

@@ -765,7 +765,7 @@ test("Issue https://github.com/DZakh/rescript-schema/issues/101", t => {
765765
t->U.assertCompiledCode(
766766
~schema,
767767
~op=#ReverseConvert,
768-
`i=>{let v5=i;if(!i||i.constructor!==Object||i["NAME"]!=="request"){if(!i||i.constructor!==Object||i["NAME"]!=="response"){e[3](i)}else{let v2=i["NAME"],v3=i["VAL"];if(v2!=="response"){e[1](v2)}let v4=v3["response"];if(v4!=="accepted"){if(v4!=="rejected"){e[2](v4)}}v5={"NAME":v2,"VAL":{"collectionName":v3["collectionName"],"response":v4,},}}}else{let v0=i["NAME"],v1=i["VAL"];if(v0!=="request"){e[0](v0)}}return v5}`,
768+
`i=>{let v2=i;if(!i||i.constructor!==Object||i["NAME"]!=="request"){if(!i||i.constructor!==Object||i["NAME"]!=="response"){e[3](i)}else{let v3=i["NAME"],v4=i["VAL"];if(v3!=="response"){e[1](v3)}let v5=v4["response"];if(v5!=="accepted"){if(v5!=="rejected"){e[2](v5)}}v2={"NAME":v3,"VAL":{"collectionName":v4["collectionName"],"response":v5,},}}}else{let v0=i["NAME"],v1=i["VAL"];if(v0!=="request"){e[0](v0)}v2=i}return v2}`,
769769
)
770770
t->U.assertCompiledCode(
771771
~schema,

packages/tests/src/core/S_unnest_test.res

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ test("Successfully parses and reverse converts a simple object with unnest", t =
1818
t->U.assertCompiledCode(
1919
~schema,
2020
~op=#Convert,
21-
`i=>{let v1=new Array(Math.max(i[0].length,i[1].length,));for(let v0=0;v0<v1.length;++v0){try{v1[v0]={"foo":i[0][v0],"bar":i[1][v0],};}catch(v2){if(v2&&v2.s===s){v2.path=""+\'["\'+v0+\'"]\'+v2.path}throw v2}}return v1}`,
21+
`i=>{let v1=new Array(Math.max(i[0].length,i[1].length,));for(let v0=0;v0<v1.length;++v0){v1[v0]={"foo":i[0][v0],"bar":i[1][v0],};}return v1}`,
2222
)
2323
t->U.assertCompiledCode(
2424
~schema,
@@ -28,7 +28,7 @@ test("Successfully parses and reverse converts a simple object with unnest", t =
2828
t->U.assertCompiledCode(
2929
~schema,
3030
~op=#ReverseParse,
31-
`i=>{if(!Array.isArray(i)){e[3](i)}let v1=[new Array(i.length),new Array(i.length),];for(let v0=0;v0<i.length;++v0){let v3=i[v0];try{if(!v3||v3.constructor!==Object){e[0](v3)}let v4=v3["foo"],v5=v3["bar"];if(typeof v4!=="string"){e[1](v4)}if(typeof v5!=="number"||v5>2147483647||v5<-2147483648||v5%1!==0){e[2](v5)}v1[0][v0]=v3["foo"];v1[1][v0]=v3["bar"];}catch(v2){if(v2&&v2.s===s){v2.path=""+\'["\'+v0+\'"]\'+v2.path}throw v2}}return v1}`,
31+
`i=>{if(!Array.isArray(i)){e[3](i)}let v1=[new Array(i.length),new Array(i.length),];for(let v0=0;v0<i.length;++v0){let v3=i[v0];try{if(!v3||v3.constructor!==Object){e[0](v3)}let v4=v3["foo"],v5=v3["bar"];if(typeof v4!=="string"){e[1](v4)}if(typeof v5!=="number"||v5>2147483647||v5<-2147483648||v5%1!==0){e[2](v5)}v1[0][v0]=v4;v1[1][v0]=v5;}catch(v2){if(v2&&v2.s===s){v2.path=""+\'["\'+v0+\'"]\'+v2.path}throw v2}}return v1}`,
3232
)
3333

3434
t->Assert.deepEqual(

src/S_Core.bs.mjs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ function withCatch(b, input, $$catch, appendSafe, fn) {
388388
};
389389
var fnOutput = fn(bb);
390390
b.c = b.c + allocateScope(bb);
391-
var isNoop = fnOutput === input && b.c === "";
391+
var isNoop = fnOutput.i === input.i && b.c === "";
392392
if (appendSafe !== undefined) {
393393
appendSafe(b, fnOutput);
394394
}
@@ -2212,7 +2212,10 @@ function builder$1(parentB, input, selfSchema, path) {
22122212
objectStrictModeCheck(b, input, items, unknownKeys, path);
22132213
parentB.c = parentB.c + allocateScope(b);
22142214
if ((unknownKeys !== "Strip" || b.g.o & 32) && selfSchema === selfSchema["~r"]()) {
2215-
return input;
2215+
objectVal$1.v = input.v;
2216+
objectVal$1.i = input.i;
2217+
objectVal$1.a = input.a;
2218+
return objectVal$1;
22162219
} else {
22172220
return complete(objectVal$1, isArray);
22182221
}

src/S_Core.res

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ module Builder = {
761761
let fnOutput = fn(bb)
762762
b.code = b.code ++ bb->allocateScope
763763

764-
let isNoop = fnOutput === input && b.code === ""
764+
let isNoop = fnOutput.inline === input.inline && b.code === ""
765765

766766
switch appendSafe {
767767
| Some(append) => append(b, ~output=fnOutput)
@@ -3175,7 +3175,10 @@ module Schema = {
31753175
(unknownKeys !== Strip || b.global.flag->Flag.unsafeHas(Flag.reverse)) &&
31763176
selfSchema === selfSchema.reverse()
31773177
) {
3178-
input
3178+
objectVal.var = input.var
3179+
objectVal.inline = input.inline
3180+
objectVal.isAsync = input.isAsync
3181+
(objectVal :> val)
31793182
} else {
31803183
objectVal->B.Val.Object.complete(~isArray)
31813184
}
@@ -3440,6 +3443,7 @@ module Schema = {
34403443
)
34413444
}
34423445
}
3446+
34433447
for idx in 0 to items->Js.Array2.length - 1 {
34443448
let item: item = items->Js.Array2.unsafe_get(idx)
34453449

test.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
(i) => {
2-
let v0 = i["tags"],
3-
v3 = i["rating"];
4-
if (v3 !== "G") {
5-
if (v3 !== "PG") {
6-
if (v3 !== "PG13") {
7-
if (v3 !== "R") {
8-
e[0](v3);
2+
let v3 = i;
3+
if (!i || i.constructor !== Object || i["NAME"] !== "request") {
4+
if (!i || i.constructor !== Object || i["NAME"] !== "response") {
5+
e[3](i);
6+
} else {
7+
if (v0 !== "response") {
8+
e[1](v0);
9+
}
10+
let v2 = v1["response"];
11+
if (v2 !== "accepted") {
12+
if (v2 !== "rejected") {
13+
e[2](v2);
914
}
1015
}
16+
v3 = {
17+
NAME: v0,
18+
VAL: { collectionName: v1["collectionName"], response: v2 },
19+
};
20+
}
21+
} else {
22+
let v0 = i["NAME"],
23+
v1 = i["VAL"];
24+
if (v0 !== "request") {
25+
e[0](v0);
1126
}
1227
}
13-
return {
14-
Id: i["id"],
15-
Title: i["title"],
16-
Tags: v0,
17-
Rating: v3,
18-
Age: i["deprecatedAgeRestriction"],
19-
};
28+
return v3;
2029
};

0 commit comments

Comments
 (0)