Skip to content

Commit

Permalink
Fix flatten validation duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
DZakh committed Dec 5, 2024
1 parent 024fdba commit 8580a4a
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 130 deletions.
18 changes: 9 additions & 9 deletions packages/tests/src/core/S_object_flatten_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test("Has correct tagged type", t => {
t->U.assertCompiledCode(
~schema,
~op=#Parse,
`i=>{if(!i||i.constructor!==Object){e[2](i)}let v1=i["foo"];let v0=i["bar"];if(typeof v0!=="string"){e[0](v0)}if(typeof v1!=="string"){e[1](v1)}return {"bar":v0,"foo":v1,}}`,
`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,}}`,
)
})

Expand All @@ -45,7 +45,7 @@ test("Can flatten S.schema", t => {
t->U.assertCompiledCode(
~schema,
~op=#Parse,
`i=>{if(!i||i.constructor!==Object){e[2](i)}let v1=i["foo"];let v0=i["bar"];if(typeof v0!=="string"){e[0](v0)}if(typeof v1!=="string"){e[1](v1)}return {"bar":v0,"foo":v1,}}`,
`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,}}`,
)
})

Expand Down Expand Up @@ -77,7 +77,7 @@ test("Can flatten strict object", t => {
t->U.assertCompiledCode(
~schema,
~op=#Parse,
`i=>{if(!i||i.constructor!==Object){e[2](i)}let v1=i["foo"];let v0=i["bar"];if(typeof v0!=="string"){e[0](v0)}if(typeof v1!=="string"){e[1](v1)}return {"bar":v0,"foo":v1,}}`,
`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,}}`,
)
})

Expand All @@ -101,7 +101,7 @@ test("Flatten inside of a strict object", t => {
t->U.assertCompiledCode(
~schema,
~op=#Parse,
`i=>{if(!i||i.constructor!==Object){e[3](i)}let v1=i["foo"],v2;let v0=i["bar"];if(typeof v0!=="string"){e[0](v0)}if(typeof v1!=="string"){e[1](v1)}for(v2 in i){if(v2!=="bar"&&v2!=="foo"){e[2](v2)}}return {"bar":v0,"foo":v1,}}`,
`i=>{if(!i||i.constructor!==Object){e[3](i)}let v0=i["bar"],v1=i["foo"],v2;if(typeof v0!=="string"){e[0](v0)}if(typeof v1!=="string"){e[1](v1)}for(v2 in i){if(v2!=="bar"&&v2!=="foo"){e[2](v2)}}return {"bar":v0,"foo":v1,}}`,
)
})

Expand Down Expand Up @@ -140,11 +140,11 @@ test("Flatten schema with duplicated field of the same type (flatten last)", t =
}
),
)
// FIXME: Can be improved

t->U.assertCompiledCode(
~schema,
~op=#Parse,
`i=>{if(!i||i.constructor!==Object){e[2](i)}let v0=i["foo"];if(typeof v0!=="string"){e[0](v0)}let v1=i["foo"];if(typeof v1!=="string"){e[1](v1)}return {"foo":v0,"bar":v1,}}`,
`i=>{if(!i||i.constructor!==Object){e[1](i)}let v0=i["foo"];if(typeof v0!=="string"){e[0](v0)}return {"foo":v0,"bar":v0,}}`,
)
// FIXME: Should validate that the fields are equal
t->U.assertCompiledCode(
Expand Down Expand Up @@ -192,7 +192,7 @@ test("Can flatten renamed object schema", t => {
t->U.assertCompiledCode(
~schema,
~op=#Parse,
`i=>{if(!i||i.constructor!==Object){e[2](i)}let v1=i["foo"];let v0=i["bar"];if(typeof v0!=="string"){e[0](v0)}if(typeof v1!=="string"){e[1](v1)}return {"bar":v0,"foo":v1,}}`,
`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,}}`,
)
t->Assert.is(schema->S.name, `{ bar: string; foo: string; }`, ())
})
Expand All @@ -216,7 +216,7 @@ test("Can flatten transformed object schema", t => {
t->U.assertCompiledCode(
~schema,
~op=#Parse,
`i=>{if(!i||i.constructor!==Object){e[3](i)}let v1=i["foo"];let v0=i["bar"];if(typeof v0!=="string"){e[0](v0)}if(typeof v1!=="string"){e[2](v1)}return {"bar":e[1](v0),"foo":v1,}}`,
`i=>{if(!i||i.constructor!==Object){e[3](i)}let v0=i["bar"],v1=i["foo"];if(typeof v0!=="string"){e[0](v0)}if(typeof v1!=="string"){e[1](v1)}return {"bar":e[2](v0),"foo":v1,}}`,
)
})

Expand Down Expand Up @@ -284,7 +284,7 @@ test("Can destructure flattened schema", t => {
t->U.assertCompiledCode(
~op=#Parse,
~schema=entitySchema,
`i=>{if(!i||i.constructor!==Object){e[3](i)}let v2=i["id"];let v0=i["name"],v1=i["age"];if(typeof v0!=="string"){e[0](v0)}if(typeof v1!=="number"||v1>2147483647||v1<-2147483648||v1%1!==0){e[1](v1)}if(typeof v2!=="string"){e[2](v2)}return {"id":v2,"name":v0,"age":v1,}}`,
`i=>{if(!i||i.constructor!==Object){e[3](i)}let v0=i["name"],v1=i["age"],v2=i["id"];if(typeof v0!=="string"){e[0](v0)}if(typeof v1!=="number"||v1>2147483647||v1<-2147483648||v1%1!==0){e[1](v1)}if(typeof v2!=="string"){e[2](v2)}return {"id":v2,"name":v0,"age":v1,}}`,
)

t->Assert.deepEqual(
Expand Down
146 changes: 80 additions & 66 deletions src/S_Core.bs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2124,32 +2124,41 @@ function builder$1(parentB, input, selfSchema, path) {
var unknownKeys = tagged.unknownKeys;
var items = tagged.items;
var isArray = tagged.TAG === "tuple";
if (parentB.g.o & 64) {
var objectVal = make(parentB, isArray);
for(var idx = 0 ,idx_finish = items.length; idx < idx_finish; ++idx){
var match = items[idx];
var inlinedLocation = match.inlinedLocation;
add(objectVal, inlinedLocation, input[inlinedLocation]);
}
return complete(objectVal, isArray);
}
var b = {
c: "",
l: "",
g: parentB.g
};
var objectVal = make(b, isArray);
for(var idx = 0 ,idx_finish = items.length; idx < idx_finish; ++idx){
var match = items[idx];
var inlinedLocation = match.inlinedLocation;
var schema = match.schema;
var itemPath = "[" + inlinedLocation + "]";
var itemInput = get(b, input, inlinedLocation);
var objectVal$1 = make(b, isArray);
for(var idx$1 = 0 ,idx_finish$1 = items.length; idx$1 < idx_finish$1; ++idx$1){
var match$1 = items[idx$1];
var inlinedLocation$1 = match$1.inlinedLocation;
var schema = match$1.schema;
var itemPath = "[" + inlinedLocation$1 + "]";
var itemInput = get(b, input, inlinedLocation$1);
var path$1 = path + itemPath;
if (schema.f !== undefined && (
b.g.o & 1 ? schema.t.TAG !== "literal" : schema.t.TAG === "literal" && !(itemInput.v && itemInput.i[0] === "e")
)) {
b.c = b.c + typeFilterCode(b, schema, itemInput, path$1);
}
add(objectVal, inlinedLocation, schema.b(b, itemInput, schema, path$1));
add(objectVal$1, inlinedLocation$1, schema.b(b, itemInput, schema, path$1));
}
objectStrictModeCheck(b, input, items, unknownKeys, path);
parentB.c = parentB.c + allocateScope(b);
if ((unknownKeys !== "Strip" || b.g.o & 32) && selfSchema === selfSchema["~r"]()) {
return input;
} else {
return complete(objectVal, isArray);
return complete(objectVal$1, isArray);
}
}

Expand Down Expand Up @@ -2277,61 +2286,6 @@ function nested(fieldName) {
return ctx$1;
}

function advancedBuilder(definition, flattened) {
return function (parentB, input, selfSchema, path) {
var outputs = {};
var tagged = selfSchema.t;
var unknownKeys = tagged.unknownKeys;
var items = tagged.items;
var b = {
c: "",
l: "",
g: parentB.g
};
var inputVar = $$var(b, input);
for(var idx = 0 ,idx_finish = items.length; idx < idx_finish; ++idx){
var match = items[idx];
var inlinedLocation = match.inlinedLocation;
var schema = match.schema;
var itemPath = "[" + inlinedLocation + "]";
var itemInput = {
v: false,
i: inputVar + itemPath,
a: false
};
var path$1 = path + itemPath;
if (schema.f !== undefined && (
b.g.o & 1 ? schema.t.TAG !== "literal" : schema.t.TAG === "literal"
)) {
b.c = b.c + typeFilterCode(b, schema, itemInput, path$1);
}
outputs[inlinedLocation] = schema.b(b, itemInput, schema, path$1);
}
if (flattened !== undefined) {
for(var idx$1 = 0 ,idx_finish$1 = flattened.length; idx$1 < idx_finish$1; ++idx$1){
var item = flattened[idx$1];
var schema$1 = item.schema;
outputs[item.i] = schema$1.b(b, input, schema$1, path);
}
}
objectStrictModeCheck(b, input, tagged.items, unknownKeys, path);
var getItemOutput = function (item) {
switch (item.k) {
case 0 :
return outputs[item.inlinedLocation];
case 1 :
return get(b, getItemOutput(item.of), item.inlinedLocation);
case 2 :
return outputs[item.i];

}
};
var output = definitionToOutput(b, definition, getItemOutput);
parentB.c = parentB.c + allocateScope(b);
return output;
};
}

function advancedReverse(definition, kind, ditems) {
return function () {
var ritemsByItemPath = {};
Expand Down Expand Up @@ -2478,6 +2432,67 @@ function advancedReverse(definition, kind, ditems) {
};
}

function advancedBuilder(definition, flattened) {
return function (parentB, input, selfSchema, path) {
var isFlatten = parentB.g.o & 64;
var outputs = isFlatten ? input : ({});
var b = {
c: "",
l: "",
g: parentB.g
};
if (!isFlatten) {
var tagged = selfSchema.t;
var unknownKeys = tagged.unknownKeys;
var items = tagged.items;
var inputVar = $$var(b, input);
for(var idx = 0 ,idx_finish = items.length; idx < idx_finish; ++idx){
var match = items[idx];
var inlinedLocation = match.inlinedLocation;
var schema = match.schema;
var itemPath = "[" + inlinedLocation + "]";
var itemInput = {
v: false,
i: inputVar + itemPath,
a: false
};
var path$1 = path + itemPath;
if (schema.f !== undefined && (
b.g.o & 1 ? schema.t.TAG !== "literal" : schema.t.TAG === "literal"
)) {
b.c = b.c + typeFilterCode(b, schema, itemInput, path$1);
}
outputs[inlinedLocation] = schema.b(b, itemInput, schema, path$1);
}
objectStrictModeCheck(b, input, items, unknownKeys, path);
}
if (flattened !== undefined) {
var prevFlag = b.g.o;
b.g.o = prevFlag | 64;
for(var idx$1 = 0 ,idx_finish$1 = flattened.length; idx$1 < idx_finish$1; ++idx$1){
var item = flattened[idx$1];
var schema$1 = item.schema;
outputs[item.i] = schema$1.b(b, outputs, schema$1, path);
}
b.g.o = prevFlag;
}
var getItemOutput = function (item) {
switch (item.k) {
case 0 :
return outputs[item.inlinedLocation];
case 1 :
return get(b, getItemOutput(item.of), item.inlinedLocation);
case 2 :
return outputs[item.i];

}
};
var output = definitionToOutput(b, definition, getItemOutput);
parentB.c = parentB.c + allocateScope(b);
return output;
};
}

function to(schema, definer) {
var item = {
k: 2,
Expand Down Expand Up @@ -2540,11 +2555,10 @@ function object(definer) {
}
}
var f = (flattened || (flattened = []));
var item_0 = setUnknownKeys(schema, "Strip", false);
var item_2 = f.length;
var item$2 = {
k: 2,
schema: item_0,
schema: schema,
p: "",
i: item_2
};
Expand Down
Loading

0 comments on commit 8580a4a

Please sign in to comment.