Skip to content

Commit cf20ee8

Browse files
committed
Fix transform
1 parent 704dea3 commit cf20ee8

File tree

3 files changed

+129
-127
lines changed

3 files changed

+129
-127
lines changed

packages/sury/src/Sury.res

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,9 +1344,9 @@ module Builder = {
13441344
`${b->var(input)}=${inlined}`
13451345
}
13461346

1347-
let map = (inlinedFn, input: val) => {
1347+
let map = (b, inlinedFn, input: val) => {
13481348
{
1349-
b: input.b,
1349+
b,
13501350
var: _notVar,
13511351
inline: `${inlinedFn}(${input.inline})`,
13521352
flag: ValFlag.none,
@@ -1391,7 +1391,7 @@ module Builder = {
13911391
if input.flag->Flag.unsafeHas(ValFlag.async) {
13921392
input.b->asyncVal(`${input.inline}.then(${b->embed(fn)})`)
13931393
} else {
1394-
Val.map(b->embed(fn), input)
1394+
b->Val.map(b->embed(fn), input)
13951395
}
13961396
}
13971397

@@ -2539,7 +2539,7 @@ let recursiveDecoder = Builder.make((b, ~input, ~selfSchema, ~path) => {
25392539
}
25402540
}
25412541
let output = b->B.withPathPrepend(~input, ~path, (_, ~input, ~path as _) => {
2542-
let output = B.Val.map(recOperation, input)
2542+
let output = b->B.Val.map(recOperation, input)
25432543
if def.isAsync === None {
25442544
let defsMut = defs->X.Dict.copy
25452545
defsMut->Js.Dict.set(identifier, unknown)

packages/sury/src/Sury.res.mjs

Lines changed: 111 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -731,9 +731,9 @@ function setInlined(b, input, inlined) {
731731
return input.v(b) + "=" + inlined;
732732
}
733733

734-
function map(inlinedFn, input) {
734+
function map(b, inlinedFn, input) {
735735
return {
736-
b: input.b,
736+
b: b,
737737
v: _notVar,
738738
i: inlinedFn + "(" + input.i + ")",
739739
f: 0,
@@ -749,7 +749,7 @@ function embedSyncOperation(b, input, fn) {
749749
if (input.f & 2) {
750750
return asyncVal(input.b, input.i + ".then(" + embed(b, fn) + ")");
751751
} else {
752-
return map(embed(b, fn), input);
752+
return map(b, embed(b, fn), input);
753753
}
754754
}
755755

@@ -1321,16 +1321,37 @@ function parse$1(prevB, schema, inputArg, path, reuseScopeOpt) {
13211321
return input;
13221322
}
13231323

1324-
function getOutputSchema(_schema) {
1325-
while (true) {
1326-
let schema = _schema;
1327-
let to = schema.to;
1328-
if (to === undefined) {
1329-
return schema;
1324+
function jsonableValidation(output, parent, path, flag) {
1325+
let tagFlag = flags[output.type];
1326+
if (tagFlag & 48129 || tagFlag & 16 && parent.type !== objectTag) {
1327+
throw new SuryError({
1328+
TAG: "InvalidJsonSchema",
1329+
_0: parent
1330+
}, flag, path);
1331+
}
1332+
if (tagFlag & 256) {
1333+
output.anyOf.forEach(s => jsonableValidation(s, parent, path, flag));
1334+
return;
1335+
}
1336+
if (!(tagFlag & 192)) {
1337+
return;
1338+
}
1339+
let additionalItems = output.additionalItems;
1340+
if (additionalItems === "strip" || additionalItems === "strict") {
1341+
additionalItems === "strip";
1342+
} else {
1343+
jsonableValidation(additionalItems, parent, path, flag);
1344+
}
1345+
let p = output.properties;
1346+
if (p !== undefined) {
1347+
let keys = Object.keys(p);
1348+
for (let idx = 0, idx_finish = keys.length; idx < idx_finish; ++idx) {
1349+
let key = keys[idx];
1350+
jsonableValidation(p[key], parent, path, flag);
13301351
}
1331-
_schema = to;
1332-
continue;
1333-
};
1352+
return;
1353+
}
1354+
output.items.forEach(item => jsonableValidation(item.schema, output, path + ("[" + fromString(item.location) + "]"), flag));
13341355
}
13351356

13361357
function internalCompile(schema, flag, defs) {
@@ -1372,39 +1393,6 @@ function internalCompile(schema, flag, defs) {
13721393
return new Function("e", "s", "return " + inlinedFunction)(ctxVarValue1, s);
13731394
}
13741395

1375-
function jsonableValidation(output, parent, path, flag) {
1376-
let tagFlag = flags[output.type];
1377-
if (tagFlag & 48129 || tagFlag & 16 && parent.type !== objectTag) {
1378-
throw new SuryError({
1379-
TAG: "InvalidJsonSchema",
1380-
_0: parent
1381-
}, flag, path);
1382-
}
1383-
if (tagFlag & 256) {
1384-
output.anyOf.forEach(s => jsonableValidation(s, parent, path, flag));
1385-
return;
1386-
}
1387-
if (!(tagFlag & 192)) {
1388-
return;
1389-
}
1390-
let additionalItems = output.additionalItems;
1391-
if (additionalItems === "strip" || additionalItems === "strict") {
1392-
additionalItems === "strip";
1393-
} else {
1394-
jsonableValidation(additionalItems, parent, path, flag);
1395-
}
1396-
let p = output.properties;
1397-
if (p !== undefined) {
1398-
let keys = Object.keys(p);
1399-
for (let idx = 0, idx_finish = keys.length; idx < idx_finish; ++idx) {
1400-
let key = keys[idx];
1401-
jsonableValidation(p[key], parent, path, flag);
1402-
}
1403-
return;
1404-
}
1405-
output.items.forEach(item => jsonableValidation(item.schema, output, path + ("[" + fromString(item.location) + "]"), flag));
1406-
}
1407-
14081396
function reverse(schema) {
14091397
if (reverseKey in schema) {
14101398
return schema[reverseKey];
@@ -1505,10 +1493,22 @@ function reverse(schema) {
15051493
return r;
15061494
}
15071495

1508-
let valueOptions = {};
1496+
function getOutputSchema(_schema) {
1497+
while (true) {
1498+
let schema = _schema;
1499+
let to = schema.to;
1500+
if (to === undefined) {
1501+
return schema;
1502+
}
1503+
_schema = to;
1504+
continue;
1505+
};
1506+
}
15091507

15101508
let valKey = "value";
15111509

1510+
let valueOptions = {};
1511+
15121512
let reverseKey = "r";
15131513

15141514
function initOperation(s, flag) {
@@ -1680,7 +1680,7 @@ function recursiveDecoder(b, input, selfSchema, path) {
16801680
recOperation = embed(b, fn$2);
16811681
}
16821682
let output = withPathPrepend(b, input, path, undefined, undefined, (param, input, param$1) => {
1683-
let output = map(recOperation, input);
1683+
let output = map(b, recOperation, input);
16841684
if (def.isAsync === undefined) {
16851685
let defsMut = copy(defs);
16861686
defsMut[identifier] = unknown;
@@ -3200,70 +3200,6 @@ function proxify(item) {
32003200
});
32013201
}
32023202

3203-
function definitionToRitem(definition, path, ritemsByItemPath) {
3204-
if (typeof definition !== "object" || definition === null) {
3205-
return {
3206-
k: 1,
3207-
p: path,
3208-
s: copySchema(parse(definition))
3209-
};
3210-
}
3211-
let item = definition[itemSymbol];
3212-
if (item !== undefined) {
3213-
let ritemSchema = copySchema(getOutputSchema(item.schema));
3214-
((delete ritemSchema.serializer));
3215-
let ritem = {
3216-
k: 0,
3217-
p: path,
3218-
s: ritemSchema
3219-
};
3220-
item.r = ritem;
3221-
ritemsByItemPath[getFullDitemPath(item)] = ritem;
3222-
return ritem;
3223-
}
3224-
if (Array.isArray(definition)) {
3225-
let items = [];
3226-
for (let idx = 0, idx_finish = definition.length; idx < idx_finish; ++idx) {
3227-
let location = idx.toString();
3228-
let inlinedLocation = "\"" + location + "\"";
3229-
let ritem$1 = definitionToRitem(definition[idx], path + ("[" + inlinedLocation + "]"), ritemsByItemPath);
3230-
let item_schema = ritem$1.s;
3231-
let item$1 = {
3232-
schema: item_schema,
3233-
location: location
3234-
};
3235-
items[idx] = item$1;
3236-
}
3237-
let mut = new Schema(arrayTag);
3238-
return {
3239-
k: 2,
3240-
p: path,
3241-
s: (mut.items = items, mut.additionalItems = "strict", mut.decoder = arrayDecoder, mut.serializer = neverBuilder, mut)
3242-
};
3243-
}
3244-
let fieldNames = Object.keys(definition);
3245-
let properties = {};
3246-
let items$1 = [];
3247-
for (let idx$1 = 0, idx_finish$1 = fieldNames.length; idx$1 < idx_finish$1; ++idx$1) {
3248-
let location$1 = fieldNames[idx$1];
3249-
let inlinedLocation$1 = fromString(location$1);
3250-
let ritem$2 = definitionToRitem(definition[location$1], path + ("[" + inlinedLocation$1 + "]"), ritemsByItemPath);
3251-
let item_schema$1 = ritem$2.s;
3252-
let item$2 = {
3253-
schema: item_schema$1,
3254-
location: location$1
3255-
};
3256-
items$1[idx$1] = item$2;
3257-
properties[location$1] = item_schema$1;
3258-
}
3259-
let mut$1 = new Schema(objectTag);
3260-
return {
3261-
k: 2,
3262-
p: path,
3263-
s: (mut$1.items = items$1, mut$1.properties = properties, mut$1.additionalItems = globalConfig.a, mut$1.serializer = neverBuilder, mut$1.decoder = objectDecoder, mut$1)
3264-
};
3265-
}
3266-
32673203
function definitionToSchema(definition) {
32683204
if (typeof definition !== "object" || definition === null) {
32693205
return parse(definition);
@@ -3390,6 +3326,70 @@ function nested(fieldName) {
33903326
return ctx$1;
33913327
}
33923328

3329+
function definitionToRitem(definition, path, ritemsByItemPath) {
3330+
if (typeof definition !== "object" || definition === null) {
3331+
return {
3332+
k: 1,
3333+
p: path,
3334+
s: copySchema(parse(definition))
3335+
};
3336+
}
3337+
let item = definition[itemSymbol];
3338+
if (item !== undefined) {
3339+
let ritemSchema = copySchema(getOutputSchema(item.schema));
3340+
((delete ritemSchema.serializer));
3341+
let ritem = {
3342+
k: 0,
3343+
p: path,
3344+
s: ritemSchema
3345+
};
3346+
item.r = ritem;
3347+
ritemsByItemPath[getFullDitemPath(item)] = ritem;
3348+
return ritem;
3349+
}
3350+
if (Array.isArray(definition)) {
3351+
let items = [];
3352+
for (let idx = 0, idx_finish = definition.length; idx < idx_finish; ++idx) {
3353+
let location = idx.toString();
3354+
let inlinedLocation = "\"" + location + "\"";
3355+
let ritem$1 = definitionToRitem(definition[idx], path + ("[" + inlinedLocation + "]"), ritemsByItemPath);
3356+
let item_schema = ritem$1.s;
3357+
let item$1 = {
3358+
schema: item_schema,
3359+
location: location
3360+
};
3361+
items[idx] = item$1;
3362+
}
3363+
let mut = new Schema(arrayTag);
3364+
return {
3365+
k: 2,
3366+
p: path,
3367+
s: (mut.items = items, mut.additionalItems = "strict", mut.decoder = arrayDecoder, mut.serializer = neverBuilder, mut)
3368+
};
3369+
}
3370+
let fieldNames = Object.keys(definition);
3371+
let properties = {};
3372+
let items$1 = [];
3373+
for (let idx$1 = 0, idx_finish$1 = fieldNames.length; idx$1 < idx_finish$1; ++idx$1) {
3374+
let location$1 = fieldNames[idx$1];
3375+
let inlinedLocation$1 = fromString(location$1);
3376+
let ritem$2 = definitionToRitem(definition[location$1], path + ("[" + inlinedLocation$1 + "]"), ritemsByItemPath);
3377+
let item_schema$1 = ritem$2.s;
3378+
let item$2 = {
3379+
schema: item_schema$1,
3380+
location: location$1
3381+
};
3382+
items$1[idx$1] = item$2;
3383+
properties[location$1] = item_schema$1;
3384+
}
3385+
let mut$1 = new Schema(objectTag);
3386+
return {
3387+
k: 2,
3388+
p: path,
3389+
s: (mut$1.items = items$1, mut$1.properties = properties, mut$1.additionalItems = globalConfig.a, mut$1.serializer = neverBuilder, mut$1.decoder = objectDecoder, mut$1)
3390+
};
3391+
}
3392+
33933393
function advancedBuilder(definition, flattened) {
33943394
return (b, input, selfSchema, path) => {
33953395
let isFlatten = b.g.o & 64;

packages/sury/tests/S_to_test.res

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -425,23 +425,25 @@ test("Coerce from string to bigint", t => {
425425
test("Coerce string after a transform", t => {
426426
let schema = S.string->S.transform(_ => {parser: v => v, serializer: v => v})->S.to(S.bool)
427427

428-
// t->U.assertThrowsMessage(
429-
// () => "true"->S.parseOrThrow(schema),
430-
// `Failed parsing: Expected boolean, received "true"`,
431-
// )
428+
t->U.assertThrowsMessage(
429+
() => "true"->S.parseOrThrow(schema),
430+
`Failed parsing: Expected boolean, received "true"`,
431+
)
432432
t->U.assertCompiledCode(
433433
~schema,
434434
~op=#Parse,
435-
`i=>{if(typeof i!=="string"){e[0](i)}let v0=e[1](i);if(typeof v0!=="boolean"){e[2](v0)}return v0}`,
435+
`i=>{if(typeof i!=="string"){e[2](i)}let v0=e[0](i);if(typeof v0!=="boolean"){e[1](v0)}return v0}`,
436436
)
437437

438-
// FIXME: This is not correct. Should be fixed after S.transform is removed by S.to
439-
// t->Assert.deepEqual(true->S.parseOrThrow(S.reverse(schema)), %raw(`true`))
440-
// t->U.assertCompiledCode(
441-
// ~schema,
442-
// ~op=#ReverseParse,
443-
// `i=>{if(typeof i!=="boolean"){e[1](i)}return e[0](i)}`,
444-
// )
438+
t->U.assertThrowsMessage(
439+
() => true->S.parseOrThrow(S.reverse(schema)),
440+
`Failed parsing: Expected string, received true`,
441+
)
442+
t->U.assertCompiledCode(
443+
~schema,
444+
~op=#ReverseParse,
445+
`i=>{if(typeof i!=="boolean"){e[2](i)}let v0=e[0](i);if(typeof v0!=="string"){e[1](v0)}return v0}`,
446+
)
445447
})
446448

447449
@unboxed

0 commit comments

Comments
 (0)