SolidWallOfCode commented on code in PR #10732:
URL: https://github.com/apache/trafficserver/pull/10732#discussion_r1396529184


##########
include/iocore/dns/SplitDNSYAMLLoader.h:
##########
@@ -0,0 +1,80 @@
+/** @file
+
+  A brief file description
+
+  @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 "P_SplitDNSProcessor.h"
+
+#include <swoc/Errata.h>
+#include <swoc/swoc_file.h>
+#include <yaml-cpp/yaml.h>
+
+#include <cstdio>
+#include <ostream>
+#include <string_view>
+#include <system_error>
+#include <utility>
+
+namespace splitdns
+{
+namespace yaml
+{
+  class SplitDNSYAMLLoader
+  {
+  public:
+    static swoc::Errata
+    load(std::string const &content, SplitDNS &out)
+    {
+      self_type loader{content, out};
+      loader.setUpSplitDNSFromYAMLTree();
+      // A swoc::Errata may not be copied so we have to explicitly move it.
+      return std::move(loader.err);
+    }
+
+  private:
+    using self_type = SplitDNSYAMLLoader;
+
+    YAML::Node current_node;
+    swoc::Errata err;
+
+    SplitDNSYAMLLoader(std::string const &content, SplitDNS &out) { 
this->current_node = YAML::Load(content); }
+
+    void setUpSplitDNSFromYAMLTree();
+  };
+
+  inline void
+  load(std::string_view config_filename, SplitDNS &out, std::ostream 
&errorstream)
+  {
+    std::error_code ec;
+    auto content{swoc::file::load(config_filename, ec)};
+    if (ec.value() == 0) {
+      if (auto err{SplitDNSYAMLLoader::load(content, out)}; !err.is_ok()) {
+        errorstream << err << "While loading " << config_filename << ".\n";

Review Comment:
   On a higher level, for code like this, to avoid excessive nesting, it is 
generally best to check the error first, _if that will result in an early 
return_. E.g.
   
   ```
   if (ec) {
     return swoc::Errata(...);
   }
   
   // file load was successful, check the contents.
   if (auto err = load(...) ; !err.is_ok()) {
     err.note("While loading config file {}", config_filename);
     return err;
   }
   // Parse succeeded. We don't care about err since we know it was success.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to