<commit_before>/*
 * Copyright (C) 2016-2017, <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "yaml.h"

#include "checks.h"
#include "project.h"

#include <boost/algorithm/string.hpp>

// no links allowed
// to do this we call YAML::Clone()
void merge(yaml &dst, const yaml &src, const YamlMergeFlags &flags)
{
    if (!src.IsDefined())
        return;

    // if 'dst' node is not a map, make it so
    if (!dst.IsMap())
        dst = yaml();

    for (const auto &f : src)
    {
        auto sf = f.first.as<String>();
        auto ff = f.second.Type();

        bool found = false;
        for (auto &t : dst)
        {
            const auto st = t.first.as<String>();
            if (sf != st)
                continue;

            const auto ft = t.second.Type();
            if (ff == YAML::NodeType::Scalar && ft == YAML::NodeType::Scalar)
            {
                switch (flags.scalar_scalar)
                {
                case YamlMergeFlags::ScalarsToSet:
                {
                    yaml nn;
                    nn.push_back(t.second);
                    nn.push_back(f.second);
                    dst[st] = nn;
                    break;
                }
                case YamlMergeFlags::OverwriteScalars:
                    dst[st] = YAML::Clone(src[sf]);
                    break;
                case YamlMergeFlags::DontTouchScalars:
                    break;
                }
            }
            else if (ff == YAML::NodeType::Scalar && ft == YAML::NodeType::Sequence)
            {
                t.second.push_back(f.second);
            }
            else if (ff == YAML::NodeType::Sequence && ft == YAML::NodeType::Scalar)
            {
                yaml nn = YAML::Clone(f);
                nn.push_back(t.second);
                dst[st] = nn;
            }
            else if (ff == YAML::NodeType::Sequence && ft == YAML::NodeType::Sequence)
            {
                for (auto &fv : f.second)
                    t.second.push_back(YAML::Clone(fv));
            }
            else if (ff == YAML::NodeType::Map && ft == YAML::NodeType::Map)
                merge(t.second, f.second, flags);
            else // elaborate more on this?
                throw std::runtime_error("yaml merge: nodes ('" + sf + "') has incompatible types");
            found = true;
        }
        if (!found)
        {<commit_msg>Fix references.<commit_after>/*
 * Copyright (C) 2016-2017, <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "yaml.h"

#include "checks.h"
#include "project.h"

#include <boost/algorithm/string.hpp>

// no links allowed
// to do this we call YAML::Clone()
void merge(yaml &dst, const yaml &src, const YamlMergeFlags &flags)
{
    if (!src.IsDefined())
        return;

    // if 'dst' node is not a map, make it so
    if (!dst.IsMap())
        dst = yaml();

    for (const auto &f : src)
    {
        auto sf = f.first.as<String>();
        auto ff = f.second.Type();

        bool found = false;
        for (auto t : dst)
        {
            const auto st = t.first.as<String>();
            if (sf != st)
                continue;

            const auto ft = t.second.Type();
            if (ff == YAML::NodeType::Scalar && ft == YAML::NodeType::Scalar)
            {
                switch (flags.scalar_scalar)
                {
                case YamlMergeFlags::ScalarsToSet:
                {
                    yaml nn;
                    nn.push_back(t.second);
                    nn.push_back(f.second);
                    dst[st] = nn;
                    break;
                }
                case YamlMergeFlags::OverwriteScalars:
                    dst[st] = YAML::Clone(src[sf]);
                    break;
                case YamlMergeFlags::DontTouchScalars:
                    break;
                }
            }
            else if (ff == YAML::NodeType::Scalar && ft == YAML::NodeType::Sequence)
            {
                t.second.push_back(f.second);
            }
            else if (ff == YAML::NodeType::Sequence && ft == YAML::NodeType::Scalar)
            {
                yaml nn = YAML::Clone(f);
                nn.push_back(t.second);
                dst[st] = nn;
            }
            else if (ff == YAML::NodeType::Sequence && ft == YAML::NodeType::Sequence)
            {
                for (const auto &fv : f.second)
                    t.second.push_back(YAML::Clone(fv));
            }
            else if (ff == YAML::NodeType::Map && ft == YAML::NodeType::Map)
                merge(t.second, f.second, flags);
            else // elaborate more on this?
                throw std::runtime_error("yaml merge: nodes ('" + sf + "') has incompatible types");
            found = true;
        }
        if (!found)
        {
{'fraction_non_alphanumeric': 0.12034973427052975, 'fraction_numerical': 0.0041145208297617, 'mean_word_length': 1.572310405643739, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6676386', 'n_tokens_mistral': 1706, 'n_tokens_neox': 1593, 'n_words': 474}