Skip to content

Commit

Permalink
Yaml (#3)
Browse files Browse the repository at this point in the history
* ParseYamlConfig
  • Loading branch information
quzard authored Nov 20, 2023
1 parent 2b7123f commit d4e558b
Show file tree
Hide file tree
Showing 5 changed files with 602 additions and 1 deletion.
96 changes: 96 additions & 0 deletions core/common/YamlUtil.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2023 iLogtail Authors
//
// 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 "common/YamlUtil.h"

#include <string>

#include "common/ExceptionBase.h"

namespace logtail {

bool ParseYamlConfig(const std::string& config, YAML::Node& yamlRoot, std::string& errorMsg) {
try {
yamlRoot = YAML::Load(config);
} catch (const YAML::ParserException& e) {
errorMsg = "parse yaml failed: " + std::string(e.what());
return false;
} catch (const std::exception& e) {
errorMsg = "unknown std::exception: " + std::string(e.what());
return false;
} catch (...) {
errorMsg = "unknown error";
return false;
}
return true;
}

Json::Value ParseScalar(const YAML::Node& node) {
// yaml-cpp automatically discards quotes in quoted values,
// so to differentiate strings and integer for purely-digits value,
// we can tell from node.Tag(), which will return "!" for quoted values and "?" for others
if (node.Tag() == "!") {
return node.as<std::string>();
}

int i;
if (YAML::convert<int>::decode(node, i))
return Json::Value(i);

double d;
if (YAML::convert<double>::decode(node, d))
return Json::Value(d);

bool b;
if (YAML::convert<bool>::decode(node, b))
return Json::Value(b);

std::string s;
if (YAML::convert<std::string>::decode(node, s))
return Json::Value(s);

return Json::Value();
}

Json::Value CovertYamlToJson(const YAML::Node& rootNode) {
Json::Value resultJson;

switch (rootNode.Type()) {
case YAML::NodeType::Scalar:
return ParseScalar(rootNode);

case YAML::NodeType::Sequence: {
resultJson = Json::Value(Json::arrayValue); // create an empty array
for (const auto& node : rootNode) {
resultJson.append(CovertYamlToJson(node));
}
break;
}

case YAML::NodeType::Map: {
resultJson = Json::Value(Json::objectValue); // create an empty object
for (const auto& node : rootNode) {
resultJson[node.first.as<std::string>()] = CovertYamlToJson(node.second);
}
break;
}

case YAML::NodeType::Null:
default:
return resultJson;
}

return resultJson;
}
} // namespace logtail
28 changes: 28 additions & 0 deletions core/common/YamlUtil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2023 iLogtail Authors
//
// 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.

#pragma once

#include <string>

#include "json/json.h"
#include "yaml-cpp/yaml.h"

namespace logtail {

bool ParseYamlConfig(const std::string& config, YAML::Node& yamlRoot, std::string& errorMsg);
Json::Value CovertYamlToJson(const YAML::Node& rootNode);
Json::Value ParseScalar(const YAML::Node& node);

} // namespace logtail
9 changes: 9 additions & 0 deletions core/config/NewConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@

#include "config/NewConfig.h"

#include <string>

#include "common/Flags.h"
#include "common/JsonUtil.h"
#include "common/ParamExtractor.h"
#include "common/YamlUtil.h"
#include "plugin/PluginRegistry.h"

DEFINE_FLAG_BOOL(enable_env_ref_in_config, "enable environment variable reference replacement in configuration", false);
Expand Down Expand Up @@ -367,6 +370,12 @@ bool ParseConfigDetail(const string& content, const string& extension, Json::Val
if (extension == ".json") {
return ParseConfig(content, detail, errorMsg);
} else if (extension == ".yaml" || extension == ".yml") {
YAML::Node yamlRoot;
if (!ParseYamlConfig(content, yamlRoot, errorMsg)){
return false;
}
detail = CovertYamlToJson(yamlRoot);
return true;
}
return false;
}
Expand Down
5 changes: 4 additions & 1 deletion core/unittest/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ add_executable(common_machine_info_util_unittest MachineInfoUtilUnittest.cpp)
target_link_libraries(common_machine_info_util_unittest unittest_base)

add_executable(encoding_converter_unittest EncodingConverterUnittest.cpp)
target_link_libraries(encoding_converter_unittest unittest_base)
target_link_libraries(encoding_converter_unittest unittest_base)

add_executable(yaml_util_unittest YamlUtilUnittest.cpp)
target_link_libraries(yaml_util_unittest unittest_base)
Loading

0 comments on commit d4e558b

Please sign in to comment.