From b5d2b6683e129706feb27db4cf866d0f3893e154 Mon Sep 17 00:00:00 2001 From: Brandon Heath Date: Wed, 25 Mar 2020 13:59:36 -0500 Subject: [PATCH] Make sure we handle cases where an editable scalar has a value that is an unexpected tag. This scenario can happen if someone edits the value from, say `__MISSING__` to `__OPTIONAL__` or if we change a field within Hyde from optional to required, for example. Before, we would have have accepted such a value. Now, it will be a validation error, and will corrected to the expected tag during the update operation. --- emitters/yaml_base_emitter.cpp | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/emitters/yaml_base_emitter.cpp b/emitters/yaml_base_emitter.cpp index d35d468..12c8c74 100644 --- a/emitters/yaml_base_emitter.cpp +++ b/emitters/yaml_base_emitter.cpp @@ -378,13 +378,7 @@ bool yaml_base_emitter::check_editable_scalar(const std::string& filepath, } const json& have = have_node[key]; - - if (expected_scalar == tag_value_optional_k) { - // Value is optional, and the docs have *something*, so we're good. - result = have; - return false; - } - + if (!have.is_string()) { notify("value not scalar; expected `" + expected_scalar + "`", "value not scalar; updated to `" + expected_scalar + "`"); @@ -394,12 +388,8 @@ bool yaml_base_emitter::check_editable_scalar(const std::string& filepath, const std::string& have_scalar(have); - if (expected_scalar == tag_value_missing_k) { + if (expected_scalar == tag_value_missing_k && have_scalar == tag_value_missing_k) { result = have; - - if (have_scalar != tag_value_missing_k) - return false; - if (_mode == yaml_mode::validate) { notify("value not documented", ""); } @@ -412,14 +402,19 @@ bool yaml_base_emitter::check_editable_scalar(const std::string& filepath, return true; } - if (expected_scalar != have_scalar) { - notify("value mismatch; have `" + have_scalar + "`, expected `" + expected_scalar + "`", + if (expected_scalar == tag_value_optional_k && have_scalar == tag_value_optional_k) { + result = have; + return false; + } + + if (hyde::is_tag(have_scalar)) { + notify("value is unexpected tag `" + have_scalar + "`", "value updated from `" + have_scalar + "` to `" + expected_scalar + "`"); - result = expected; + result = expected; // Replace unexpected tag return true; } - // both have and expected are both scalar and are the same value + // The docs have *something*, so we're good. result = have; return false; } @@ -490,7 +485,7 @@ bool yaml_base_emitter::check_editable_scalar_array(const std::string& filepath, } if (hyde::is_tag(have_scalar)) { - notify("value is unexpected tag", + notify("value is unexpected tag `" + have_scalar + "`", "value updated from `" + have_scalar + "` to `" + expected_scalar + "`"); result = expected; // Replace unexpected tag return true;