brbzull0 commented on a change in pull request #7478: URL: https://github.com/apache/trafficserver/pull/7478#discussion_r592494120
########## File path: src/traffic_ctl_jsonrpc/jsonrpc/yaml_codecs.h ########## @@ -0,0 +1,362 @@ +/** + @section license License + + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you 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_view> +#include <yaml-cpp/yaml.h> + +#include "RPCRequests.h" + +// the whole point if to not fail if the data is not available, and just return something +// back so we can still display some error. +template <typename T> +static auto +try_extract(YAML::Node const &node, std::string_view name, bool throwOnFail = false) +{ + try { + if (auto n = node[name.data()]) { + return n.as<T>(); + } + } catch (YAML::Exception const &ex) { + if (throwOnFail) { + throw ex; + } + } + return T{}; +} + +/** + * YAML namespace. All json rpc request codecs can be placed here. It will read all the definitions from "requests.h" + * It's noted that there may be some duplicated with the rpc server implementation structures but as this is very simple idiom where + * we define the data as plain as possible and it's just used for printing purposes, No major harm on having them duplicated + */ + +namespace YAML +{ +//------------------------------------------------------------------------------------------------------------------------------------ +template <> struct convert<specs::JSONRPCError> { + static bool + decode(Node const &node, specs::JSONRPCError &error) + { + error.code = try_extract<int32_t>(node, "code"); + error.message = try_extract<std::string>(node, "message"); + if (auto data = node["data"]) { + for (auto &&err : data) { + error.data.emplace_back(try_extract<int32_t>(err, "code"), try_extract<std::string>(err, "message")); + } + } + return true; + } +}; +//------------------------------------------------------------------------------------------------------------------------------------ +template <> struct convert<RecordLookUpResponse::RecordParamInfo::ConfigMeta> { + static bool + decode(Node const &node, RecordLookUpResponse::RecordParamInfo::ConfigMeta &meta) + { + try { + meta.accessType = try_extract<int32_t>(node, "access_type"); + meta.updateStatus = try_extract<int32_t>(node, "update_status"); + meta.updateType = try_extract<int32_t>(node, "update_type"); + meta.checkType = try_extract<int32_t>(node, "checktype"); + meta.source = try_extract<int32_t>(node, "source"); + meta.checkExpr = try_extract<std::string>(node, "check_expr"); + } catch (Exception const &ex) { Review comment: it's inside the `YAML` namespace already. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
