/src/trafficserver/include/tsutil/YamlCfg.h
Line | Count | Source |
1 | | /** @file |
2 | | |
3 | | Utilities to help with parsing YAML files with good error reporting. |
4 | | |
5 | | @section license License |
6 | | |
7 | | Licensed to the Apache Software Foundation (ASF) under one |
8 | | or more contributor license agreements. See the NOTICE file |
9 | | distributed with this work for additional information |
10 | | regarding copyright ownership. The ASF licenses this file |
11 | | to you under the Apache License, Version 2.0 (the |
12 | | "License"); you may not use this file except in compliance |
13 | | with the License. You may obtain a copy of the License at |
14 | | |
15 | | http://www.apache.org/licenses/LICENSE-2.0 |
16 | | |
17 | | Unless required by applicable law or agreed to in writing, software |
18 | | distributed under the License is distributed on an "AS IS" BASIS, |
19 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
20 | | See the License for the specific language governing permissions and |
21 | | limitations under the License. |
22 | | */ |
23 | | |
24 | | #pragma once |
25 | | |
26 | | #include <vector> |
27 | | #include <string_view> |
28 | | |
29 | | #include <yaml-cpp/yaml.h> |
30 | | #include <swoc/bwf_fwd.h> |
31 | | |
32 | | namespace ts |
33 | | { |
34 | | namespace Yaml |
35 | | { |
36 | | constexpr std::string_view YAML_FLOAT_TAG_URI{"tag:yaml.org,2002:float"}; |
37 | | constexpr std::string_view YAML_INT_TAG_URI{"tag:yaml.org,2002:int"}; |
38 | | constexpr std::string_view YAML_STR_TAG_URI{"tag:yaml.org,2002:str"}; |
39 | | constexpr std::string_view YAML_BOOL_TAG_URI{"tag:yaml.org,2002:bool"}; |
40 | | constexpr std::string_view YAML_NULL_TAG_URI{"tag:yaml.org,2002:null"}; |
41 | | |
42 | | // A class that is a wrapper for a YAML::Node that corresponds to a map in a YAML input file. |
43 | | // It's purpose is to make sure all keys in the map are processed. |
44 | | // |
45 | | class Map |
46 | | { |
47 | | public: |
48 | | // A YAML::ParserException will be thrown if 'map' isn't actually a map. |
49 | | // |
50 | | explicit Map(const YAML::Node &map); |
51 | | |
52 | | // Get the node for a key. Throw a YAML::Exception if 'key' is not in the map. The node for each key in the |
53 | | // map must be gotten at least once. The lifetime of the char array referenced by passed key must be as long |
54 | | // as this instance. |
55 | | // |
56 | | YAML::Node operator[](std::string_view key); |
57 | | |
58 | | // Call this after the last call to the [] operator. Will throw a YAML::ParserException if instance not |
59 | | // already marked bad, and all keys in the map were not accessed at least once with the [] operator. The |
60 | | // 'what' of the exception will list the keys that were not accessed as invalid for the map. |
61 | | // |
62 | | void done(); |
63 | | |
64 | | // Mark instance as bad. |
65 | | // |
66 | | void |
67 | | bad() |
68 | 0 | { |
69 | 0 | _bad = true; |
70 | 0 | } |
71 | | |
72 | | // No copy/move. |
73 | | // |
74 | | Map(Map const &) = delete; |
75 | | Map &operator=(Map const &) = delete; |
76 | | |
77 | | private: |
78 | | YAML::Node _map; |
79 | | std::vector<std::string_view> _used_key; |
80 | | bool _bad{false}; |
81 | | }; |
82 | | |
83 | | } // end namespace Yaml |
84 | | } // end namespace ts |
85 | | |
86 | | namespace swoc |
87 | | { |
88 | | // This needs to be in namespace "swoc" or "YAML" or ADL doesn't find the overload. |
89 | | extern BufferWriter &bwformat(BufferWriter &w, bwf::Spec const &spec, YAML::Mark const &mark); |
90 | | } // namespace swoc |