Skip to content

Commit

Permalink
[WIP] Report evaluation of ControlEvaluate
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti committed Oct 17, 2024
1 parent e150395 commit da10b5e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/compiler/compile_describe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ struct DescribeVisitor {
}

auto operator()(const ControlEvaluate &) const -> std::string {
return unknown();
return "The instance location was marked as evaluated";
}

auto operator()(const ControlJump &) const -> std::string {
Expand Down
12 changes: 5 additions & 7 deletions src/compiler/default_compiler_2019_09.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ auto compiler_2019_09_applicator_contains_with_options(
}

if (track_evaluation) {
children.push_back(make<ControlEvaluate>(false, context, schema_context,
children.push_back(make<ControlEvaluate>(true, context, schema_context,
relative_dynamic_context,
ValuePointer{}));
}
Expand Down Expand Up @@ -222,9 +222,8 @@ auto compiler_2019_09_applicator_unevaluateditems(
}

// TODO: Do this out the box on LoopItemsUnevaluated?
children.push_back(make<ControlEvaluate>(false, context, schema_context,
relative_dynamic_context,
ValuePointer{}));
children.push_back(make<ControlEvaluate>(
true, context, schema_context, relative_dynamic_context, ValuePointer{}));

return {make<LoopItemsUnevaluated>(true, context, schema_context,
dynamic_context, ValueNone{},
Expand All @@ -250,9 +249,8 @@ auto compiler_2019_09_applicator_unevaluatedproperties(
}

// TODO: Do this out the box on LoopPropertiesUnevaluated?
children.push_back(make<ControlEvaluate>(false, context, schema_context,
relative_dynamic_context,
ValuePointer{}));
children.push_back(make<ControlEvaluate>(
true, context, schema_context, relative_dynamic_context, ValuePointer{}));

return {make<LoopPropertiesUnevaluated>(true, context, schema_context,
dynamic_context, ValueNone{},
Expand Down
14 changes: 7 additions & 7 deletions src/compiler/default_compiler_draft4.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ auto compiler_draft4_applicator_properties_with_options(

for (auto &&[name, substeps] : properties) {
if (track_evaluation) {
substeps.push_back(make<ControlEvaluate>(false, context, schema_context,
substeps.push_back(make<ControlEvaluate>(true, context, schema_context,
relative_dynamic_context,
ValuePointer{name}));
}
Expand Down Expand Up @@ -628,7 +628,7 @@ auto compiler_draft4_applicator_patternproperties_with_options(
}

if (track_evaluation) {
substeps.push_back(make<ControlEvaluate>(false, context, schema_context,
substeps.push_back(make<ControlEvaluate>(true, context, schema_context,
relative_dynamic_context,
ValuePointer{}));
}
Expand Down Expand Up @@ -684,7 +684,7 @@ auto compiler_draft4_applicator_additionalproperties_with_options(
}

if (track_evaluation) {
children.push_back(make<ControlEvaluate>(false, context, schema_context,
children.push_back(make<ControlEvaluate>(true, context, schema_context,
relative_dynamic_context,
ValuePointer{}));
}
Expand Down Expand Up @@ -865,7 +865,7 @@ auto compiler_draft4_applicator_items_array(

if (track_evaluation) {
children.push_back(make<ControlEvaluate>(
false, context, schema_context, relative_dynamic_context,
true, context, schema_context, relative_dynamic_context,
ValuePointer{subschemas.size()}));
}

Expand Down Expand Up @@ -943,7 +943,7 @@ auto compiler_draft4_applicator_items_with_options(
}

if (track_evaluation) {
children.push_back(make<ControlEvaluate>(false, context, schema_context,
children.push_back(make<ControlEvaluate>(true, context, schema_context,
relative_dynamic_context,
ValuePointer{}));
}
Expand All @@ -957,7 +957,7 @@ auto compiler_draft4_applicator_items_with_options(
sourcemeta::jsontoolkit::empty_pointer,
sourcemeta::jsontoolkit::empty_pointer)};
if (track_evaluation) {
children.push_back(make<ControlEvaluate>(false, context, schema_context,
children.push_back(make<ControlEvaluate>(true, context, schema_context,
relative_dynamic_context,
ValuePointer{}));
}
Expand Down Expand Up @@ -1004,7 +1004,7 @@ auto compiler_draft4_applicator_additionalitems_from_cursor(
}

if (track_evaluation) {
children.push_back(make<ControlEvaluate>(false, context, schema_context,
children.push_back(make<ControlEvaluate>(true, context, schema_context,
relative_dynamic_context,
ValuePointer{}));
}
Expand Down
22 changes: 22 additions & 0 deletions src/evaluator/evaluator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ auto evaluate_step(const sourcemeta::blaze::Template::value_type &step,
}

case IS_STEP(ControlMark): {
SOURCEMETA_TRACE_END(trace_dispatch_id, "Dispatch");
SOURCEMETA_TRACE_START(trace_id, "ControlMark");
const auto &control{std::get<ControlMark>(step)};
context.mark(control.value, control.children);
Expand All @@ -629,9 +630,30 @@ auto evaluate_step(const sourcemeta::blaze::Template::value_type &step,
}

case IS_STEP(ControlEvaluate): {
SOURCEMETA_TRACE_END(trace_dispatch_id, "Dispatch");
SOURCEMETA_TRACE_START(trace_id, "ControlEvaluate");
const auto &control{std::get<ControlEvaluate>(step)};
context.evaluate(control.value);

if (control.report && callback.has_value()) {
// We only want to pay for the pointer overhead if
// reporting this step to the callback
// TODO: Improve Pointer API to make this easier
auto destination = context.instance_location();
for (const auto &token : control.value) {
if (token.is_property()) {
destination.push_back(token.to_property());
} else {
destination.push_back(token.to_index());
}
}

callback.value()(EvaluationType::Pre, true, step,
context.evaluate_path(), destination, context.null);
callback.value()(EvaluationType::Post, true, step,
context.evaluate_path(), destination, context.null);
}

SOURCEMETA_TRACE_END(trace_id, "ControlEvaluate");
return true;
}
Expand Down
27 changes: 19 additions & 8 deletions test/evaluator/evaluator_2019_09_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2614,35 +2614,46 @@ TEST(Evaluator_2019_09, unevaluatedProperties_1) {
const sourcemeta::jsontoolkit::JSON instance{
sourcemeta::jsontoolkit::parse("{ \"foo\": \"baz\", \"bar\": true }")};

EVALUATE_WITH_TRACE_FAST_SUCCESS(schema, instance, 4);
EVALUATE_WITH_TRACE_FAST_SUCCESS(schema, instance, 6);

EVALUATE_TRACE_PRE(0, LogicalAnd, "/properties", "#/properties", "");
EVALUATE_TRACE_PRE(1, AssertionTypeStrict, "/properties/foo/type",
"#/properties/foo/type", "/foo");
EVALUATE_TRACE_PRE(2, LoopPropertiesUnevaluated, "/unevaluatedProperties",
EVALUATE_TRACE_PRE(2, ControlEvaluate, "/properties", "#/properties", "/foo");
EVALUATE_TRACE_PRE(3, LoopPropertiesUnevaluated, "/unevaluatedProperties",
"#/unevaluatedProperties", "");
EVALUATE_TRACE_PRE(3, AssertionTypeStrict, "/unevaluatedProperties/type",
EVALUATE_TRACE_PRE(4, AssertionTypeStrict, "/unevaluatedProperties/type",
"#/unevaluatedProperties/type", "/bar");
EVALUATE_TRACE_PRE(5, ControlEvaluate, "/unevaluatedProperties",
"#/unevaluatedProperties", "/bar");

EVALUATE_TRACE_POST_SUCCESS(0, AssertionTypeStrict, "/properties/foo/type",
"#/properties/foo/type", "/foo");
EVALUATE_TRACE_POST_SUCCESS(1, LogicalAnd, "/properties", "#/properties", "");
EVALUATE_TRACE_POST_SUCCESS(2, AssertionTypeStrict,
EVALUATE_TRACE_POST_SUCCESS(1, ControlEvaluate, "/properties", "#/properties",
"/foo");
EVALUATE_TRACE_POST_SUCCESS(2, LogicalAnd, "/properties", "#/properties", "");
EVALUATE_TRACE_POST_SUCCESS(3, AssertionTypeStrict,
"/unevaluatedProperties/type",
"#/unevaluatedProperties/type", "/bar");
EVALUATE_TRACE_POST_SUCCESS(3, LoopPropertiesUnevaluated,
EVALUATE_TRACE_POST_SUCCESS(4, ControlEvaluate, "/unevaluatedProperties",
"#/unevaluatedProperties", "/bar");
EVALUATE_TRACE_POST_SUCCESS(5, LoopPropertiesUnevaluated,
"/unevaluatedProperties",
"#/unevaluatedProperties", "");

EVALUATE_TRACE_POST_DESCRIBE(instance, 0,
"The value was expected to be of type string");
EVALUATE_TRACE_POST_DESCRIBE(instance, 1,
"The instance location was marked as evaluated");
EVALUATE_TRACE_POST_DESCRIBE(instance, 2,
"The object value was expected to validate "
"against the single defined property subschema");
EVALUATE_TRACE_POST_DESCRIBE(instance, 2,
EVALUATE_TRACE_POST_DESCRIBE(instance, 3,
"The value was expected to be of type boolean");
EVALUATE_TRACE_POST_DESCRIBE(instance, 4,
"The instance location was marked as evaluated");
EVALUATE_TRACE_POST_DESCRIBE(
instance, 3,
instance, 5,
"The object properties not covered by other object keywords were "
"expected to validate against this subschema");
}
Expand Down

0 comments on commit da10b5e

Please sign in to comment.