forked from alibaba/loongcollector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ParseYamlConfig
- Loading branch information
Showing
5 changed files
with
602 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.