Skip to content

Commit 9b984ea

Browse files
committed
Fix dynamicScope
1 parent 7bcd800 commit 9b984ea

File tree

2 files changed

+165
-152
lines changed

2 files changed

+165
-152
lines changed

packages/sury/src/Sury.res

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,17 +1391,21 @@ module Builder = {
13911391
}
13921392

13931393
let dynamicScope = (from: val, ~locationVar): val => {
1394-
let v =
1395-
from->next(
1396-
`${from.var()}[${locationVar}]`,
1397-
~schema=from.schema.additionalItems->(Obj.magic: option<additionalItems> => internal),
1398-
~expected=from.expected.additionalItems->(Obj.magic: option<additionalItems> => internal),
1399-
)
1400-
v.prev = None
1401-
v.parent = Some(from)
1402-
v.path = Path.empty
1403-
v.var = _notVarBeforeValidation
1404-
v
1394+
{
1395+
var: _notVarBeforeValidation,
1396+
inline: `${from.var()}[${locationVar}]`,
1397+
flag: from.flag,
1398+
schema: from.schema.additionalItems->(Obj.magic: option<additionalItems> => internal),
1399+
expected: from.expected.additionalItems->(Obj.magic: option<additionalItems> => internal),
1400+
codeFromPrev: "",
1401+
codeAfterValidation: "",
1402+
varsAllocation: "",
1403+
parent: from,
1404+
allocate: initialAllocate,
1405+
validation: None,
1406+
path: Path.empty,
1407+
global: from.global,
1408+
}
14051409
}
14061410

14071411
let allocateVal = (from: val, ~schema, ~expected=from.expected): val => {

packages/sury/src/Sury.res.mjs

Lines changed: 150 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -692,12 +692,21 @@ function refine(val, schemaOpt, validation, expectedOpt) {
692692
}
693693

694694
function dynamicScope(from, locationVar) {
695-
let v = next(from, from.v() + "[" + locationVar + "]", from.s.additionalItems, from.e.additionalItems);
696-
v.prev = undefined;
697-
v.p = from;
698-
v.path = "";
699-
v.v = _notVarBeforeValidation;
700-
return v;
695+
return {
696+
p: from,
697+
v: _notVarBeforeValidation,
698+
i: from.v() + "[" + locationVar + "]",
699+
f: from.f,
700+
s: from.s.additionalItems,
701+
e: from.e.additionalItems,
702+
c: "",
703+
cp: "",
704+
l: "",
705+
a: initialAllocate,
706+
validation: undefined,
707+
path: "",
708+
g: from.g
709+
};
701710
}
702711

703712
function allocateVal(from, schema, expectedOpt) {
@@ -3147,6 +3156,124 @@ function proxifyShapedSchema(schema, from, fromFlattened) {
31473156
});
31483157
}
31493158

3159+
function getShapedSerializerOutput(cleanRootInput, acc, targetSchema, path) {
3160+
if (acc !== undefined) {
3161+
let val = acc.val;
3162+
if (val !== undefined) {
3163+
let v = cleanValFrom(val);
3164+
v.e = targetSchema;
3165+
return parse$1(v, undefined);
3166+
}
3167+
3168+
}
3169+
if (constField in targetSchema) {
3170+
let v$1 = nextConst(cleanRootInput, targetSchema, undefined);
3171+
v$1.prev = undefined;
3172+
v$1.e = targetSchema;
3173+
return parse$1(v$1, undefined);
3174+
}
3175+
let output = makeObjectVal(cleanRootInput, targetSchema);
3176+
output.prev = undefined;
3177+
let flattened = targetSchema.flattened;
3178+
let items = targetSchema.items;
3179+
if (items !== undefined) {
3180+
for (let idx = 0, idx_finish = items.length; idx < idx_finish; ++idx) {
3181+
let location = idx.toString();
3182+
let tmp;
3183+
if (acc !== undefined) {
3184+
let properties = acc.properties;
3185+
tmp = properties !== undefined ? properties[location] : undefined;
3186+
} else {
3187+
tmp = undefined;
3188+
}
3189+
let inlinedLocation = inlineLocation(cleanRootInput.g, location);
3190+
add(output, location, getShapedSerializerOutput(cleanRootInput, tmp, items[idx], path + ("[" + inlinedLocation + "]")));
3191+
}
3192+
} else {
3193+
let properties$1 = targetSchema.properties;
3194+
if (properties$1 !== undefined) {
3195+
if (flattened !== undefined && acc !== undefined) {
3196+
let flattenedAcc = acc.flattened;
3197+
if (flattenedAcc !== undefined) {
3198+
flattenedAcc.forEach((acc, idx) => {
3199+
let flattenedOutput = getShapedSerializerOutput(cleanRootInput, acc, reverse(flattened[idx]), path);
3200+
let vals = flattenedOutput.d;
3201+
let locations = Object.keys(vals);
3202+
for (let idx$1 = 0, idx_finish = locations.length; idx$1 < idx_finish; ++idx$1) {
3203+
let location = locations[idx$1];
3204+
add(output, location, vals[location]);
3205+
}
3206+
});
3207+
}
3208+
3209+
}
3210+
let keys = Object.keys(properties$1);
3211+
for (let idx$1 = 0, idx_finish$1 = keys.length; idx$1 < idx_finish$1; ++idx$1) {
3212+
let location$1 = keys[idx$1];
3213+
if (!(location$1 in output.d)) {
3214+
let tmp$1;
3215+
if (acc !== undefined) {
3216+
let properties$2 = acc.properties;
3217+
tmp$1 = properties$2 !== undefined ? properties$2[location$1] : undefined;
3218+
} else {
3219+
tmp$1 = undefined;
3220+
}
3221+
let inlinedLocation$1 = inlineLocation(cleanRootInput.g, location$1);
3222+
add(output, location$1, getShapedSerializerOutput(cleanRootInput, tmp$1, properties$1[location$1], path + ("[" + inlinedLocation$1 + "]")));
3223+
}
3224+
3225+
}
3226+
} else {
3227+
let from = targetSchema.from;
3228+
let path$1 = from !== undefined ? path + from.map(item => "[\"" + item + "\"]").join("") : path;
3229+
let tmp$2 = path$1 === "" ? "" : " at " + path$1;
3230+
invalidOperation(cleanRootInput, "Missing input for " + toExpression(targetSchema) + tmp$2);
3231+
}
3232+
}
3233+
return complete(output);
3234+
}
3235+
3236+
function traverseDefinition(definition, onNode) {
3237+
if (typeof definition !== "object" || definition === null) {
3238+
return parse(definition);
3239+
}
3240+
let s = onNode(definition);
3241+
if (s !== undefined) {
3242+
return s;
3243+
}
3244+
if (Array.isArray(definition)) {
3245+
for (let idx = 0, idx_finish = definition.length; idx < idx_finish; ++idx) {
3246+
let schema = traverseDefinition(definition[idx], onNode);
3247+
definition[idx] = schema;
3248+
}
3249+
let mut = base(arrayTag, false);
3250+
mut.items = definition;
3251+
mut.additionalItems = "strict";
3252+
mut.decoder = arrayDecoder;
3253+
return mut;
3254+
}
3255+
let cnstr = definition.constructor;
3256+
if (cnstr && cnstr !== Object) {
3257+
let mut$1 = base(instanceTag, true);
3258+
mut$1.class = cnstr;
3259+
mut$1.const = definition;
3260+
mut$1.decoder = literalDecoder;
3261+
return mut$1;
3262+
}
3263+
let fieldNames = Object.keys(definition);
3264+
let length = fieldNames.length;
3265+
for (let idx$1 = 0; idx$1 < length; ++idx$1) {
3266+
let location = fieldNames[idx$1];
3267+
let schema$1 = traverseDefinition(definition[location], onNode);
3268+
definition[location] = schema$1;
3269+
}
3270+
let mut$2 = base(objectTag, false);
3271+
mut$2.properties = definition;
3272+
mut$2.additionalItems = globalConfig.a;
3273+
mut$2.decoder = objectDecoder;
3274+
return mut$2;
3275+
}
3276+
31503277
function prepareShapedSerializerAcc(acc, input) {
31513278
let match = input.e;
31523279
let from = match.from;
@@ -3203,57 +3330,6 @@ function prepareShapedSerializerAcc(acc, input) {
32033330
}
32043331
}
32053332

3206-
function traverseDefinition(definition, onNode) {
3207-
if (typeof definition !== "object" || definition === null) {
3208-
return parse(definition);
3209-
}
3210-
let s = onNode(definition);
3211-
if (s !== undefined) {
3212-
return s;
3213-
}
3214-
if (Array.isArray(definition)) {
3215-
for (let idx = 0, idx_finish = definition.length; idx < idx_finish; ++idx) {
3216-
let schema = traverseDefinition(definition[idx], onNode);
3217-
definition[idx] = schema;
3218-
}
3219-
let mut = base(arrayTag, false);
3220-
mut.items = definition;
3221-
mut.additionalItems = "strict";
3222-
mut.decoder = arrayDecoder;
3223-
return mut;
3224-
}
3225-
let cnstr = definition.constructor;
3226-
if (cnstr && cnstr !== Object) {
3227-
let mut$1 = base(instanceTag, true);
3228-
mut$1.class = cnstr;
3229-
mut$1.const = definition;
3230-
mut$1.decoder = literalDecoder;
3231-
return mut$1;
3232-
}
3233-
let fieldNames = Object.keys(definition);
3234-
let length = fieldNames.length;
3235-
for (let idx$1 = 0; idx$1 < length; ++idx$1) {
3236-
let location = fieldNames[idx$1];
3237-
let schema$1 = traverseDefinition(definition[location], onNode);
3238-
definition[location] = schema$1;
3239-
}
3240-
let mut$2 = base(objectTag, false);
3241-
mut$2.properties = definition;
3242-
mut$2.additionalItems = globalConfig.a;
3243-
mut$2.decoder = objectDecoder;
3244-
return mut$2;
3245-
}
3246-
3247-
function shapedSerializer(input, selfSchema) {
3248-
let acc = {};
3249-
prepareShapedSerializerAcc(acc, input);
3250-
let targetSchema = selfSchema.to;
3251-
let output = getShapedSerializerOutput(cleanValFrom(input), acc, targetSchema, "");
3252-
output.prev = input;
3253-
output.k = getOutputSchema(targetSchema).to === undefined;
3254-
return output;
3255-
}
3256-
32573333
function getShapedParserOutput(input, targetSchema) {
32583334
let from = targetSchema.from;
32593335
let fromFlattened = targetSchema.fromFlattened;
@@ -3305,6 +3381,15 @@ function getValByFrom(_input, from, _idx) {
33053381
};
33063382
}
33073383

3384+
function definitionToSchema(definition) {
3385+
return traverseDefinition(definition, node => {
3386+
if (node["~standard"]) {
3387+
return node;
3388+
}
3389+
3390+
});
3391+
}
3392+
33083393
function nested(fieldName) {
33093394
let parentCtx = this;
33103395
let cacheId = "~" + fieldName;
@@ -3368,90 +3453,14 @@ function nested(fieldName) {
33683453
return ctx$1;
33693454
}
33703455

3371-
function definitionToSchema(definition) {
3372-
return traverseDefinition(definition, node => {
3373-
if (node["~standard"]) {
3374-
return node;
3375-
}
3376-
3377-
});
3378-
}
3379-
3380-
function getShapedSerializerOutput(cleanRootInput, acc, targetSchema, path) {
3381-
if (acc !== undefined) {
3382-
let val = acc.val;
3383-
if (val !== undefined) {
3384-
let v = cleanValFrom(val);
3385-
v.e = targetSchema;
3386-
return parse$1(v, undefined);
3387-
}
3388-
3389-
}
3390-
if (constField in targetSchema) {
3391-
let v$1 = nextConst(cleanRootInput, targetSchema, undefined);
3392-
v$1.prev = undefined;
3393-
v$1.e = targetSchema;
3394-
return parse$1(v$1, undefined);
3395-
}
3396-
let output = makeObjectVal(cleanRootInput, targetSchema);
3397-
output.prev = undefined;
3398-
let flattened = targetSchema.flattened;
3399-
let items = targetSchema.items;
3400-
if (items !== undefined) {
3401-
for (let idx = 0, idx_finish = items.length; idx < idx_finish; ++idx) {
3402-
let location = idx.toString();
3403-
let tmp;
3404-
if (acc !== undefined) {
3405-
let properties = acc.properties;
3406-
tmp = properties !== undefined ? properties[location] : undefined;
3407-
} else {
3408-
tmp = undefined;
3409-
}
3410-
let inlinedLocation = inlineLocation(cleanRootInput.g, location);
3411-
add(output, location, getShapedSerializerOutput(cleanRootInput, tmp, items[idx], path + ("[" + inlinedLocation + "]")));
3412-
}
3413-
} else {
3414-
let properties$1 = targetSchema.properties;
3415-
if (properties$1 !== undefined) {
3416-
if (flattened !== undefined && acc !== undefined) {
3417-
let flattenedAcc = acc.flattened;
3418-
if (flattenedAcc !== undefined) {
3419-
flattenedAcc.forEach((acc, idx) => {
3420-
let flattenedOutput = getShapedSerializerOutput(cleanRootInput, acc, reverse(flattened[idx]), path);
3421-
let vals = flattenedOutput.d;
3422-
let locations = Object.keys(vals);
3423-
for (let idx$1 = 0, idx_finish = locations.length; idx$1 < idx_finish; ++idx$1) {
3424-
let location = locations[idx$1];
3425-
add(output, location, vals[location]);
3426-
}
3427-
});
3428-
}
3429-
3430-
}
3431-
let keys = Object.keys(properties$1);
3432-
for (let idx$1 = 0, idx_finish$1 = keys.length; idx$1 < idx_finish$1; ++idx$1) {
3433-
let location$1 = keys[idx$1];
3434-
if (!(location$1 in output.d)) {
3435-
let tmp$1;
3436-
if (acc !== undefined) {
3437-
let properties$2 = acc.properties;
3438-
tmp$1 = properties$2 !== undefined ? properties$2[location$1] : undefined;
3439-
} else {
3440-
tmp$1 = undefined;
3441-
}
3442-
let inlinedLocation$1 = inlineLocation(cleanRootInput.g, location$1);
3443-
add(output, location$1, getShapedSerializerOutput(cleanRootInput, tmp$1, properties$1[location$1], path + ("[" + inlinedLocation$1 + "]")));
3444-
}
3445-
3446-
}
3447-
} else {
3448-
let from = targetSchema.from;
3449-
let path$1 = from !== undefined ? path + from.map(item => "[\"" + item + "\"]").join("") : path;
3450-
let tmp$2 = path$1 === "" ? "" : " at " + path$1;
3451-
invalidOperation(cleanRootInput, "Missing input for " + toExpression(targetSchema) + tmp$2);
3452-
}
3453-
}
3454-
return complete(output);
3456+
function shapedSerializer(input, selfSchema) {
3457+
let acc = {};
3458+
prepareShapedSerializerAcc(acc, input);
3459+
let targetSchema = selfSchema.to;
3460+
let output = getShapedSerializerOutput(cleanValFrom(input), acc, targetSchema, "");
3461+
output.prev = input;
3462+
output.k = getOutputSchema(targetSchema).to === undefined;
3463+
return output;
34553464
}
34563465

34573466
function shapedParser(input, selfSchema) {

0 commit comments

Comments
 (0)