/src/trafficserver/include/records/RecYAMLDefs.h
Line | Count | Source |
1 | | /** @file |
2 | | |
3 | | Public RecCore declarations |
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 <functional> |
27 | | #include <yaml-cpp/yaml.h> |
28 | | |
29 | | #include <swoc/BufferWriter.h> |
30 | | #include "tsutil/ts_errata.h" |
31 | | |
32 | | struct CfgNode; |
33 | | |
34 | | /// |
35 | | /// @brief Handler callback signature to deal with the found record. |
36 | | /// |
37 | | /// |
38 | | using RecYAMLNodeHandler = std::function<void(CfgNode const &field, swoc::Errata &)>; |
39 | | //------------------------------------------------------------------------------ |
40 | | /// |
41 | | /// @brief A wrapper class around a @c YAML::Node which also holds the record name. |
42 | | /// The record name is constructed base on every field name, so every child field will be appended to the |
43 | | /// parent name using a dot as separator. |
44 | | /// |
45 | | /// |
46 | | struct CfgNode { |
47 | | // /// @brief Construct a configuration node using just the @c YAML::Node |
48 | | // /// @param n The parsed @c YAML::node. |
49 | | // CfgNode(YAML::Node const &n, YAML::Node const &v) : node{n}, value_node{v} {} |
50 | | |
51 | | /// @brief Construct a configuration node using just the @c YAML::Node and the @c record_name |
52 | | /// @param n The parsed @c YAML::node. |
53 | | /// @param base_record_name The base record name from where the record name should be built up. |
54 | 0 | CfgNode(YAML::Node const &n, YAML::Node const &v, std::string_view base_record_name) : node{n}, value_node{v} |
55 | 0 | { |
56 | 0 | _legacy.record_name = base_record_name; |
57 | 0 | } |
58 | | |
59 | | /// @brief Returns the built record name. |
60 | | /// |
61 | | /// The record name was build up base on the yaml structure: i.e.: |
62 | | /// |
63 | | /// diags: |
64 | | /// debug: |
65 | | /// tag: rpc |
66 | | /// |
67 | | /// this function will return diags.debug.tag |
68 | | std::string_view |
69 | | get_record_name() const |
70 | 0 | { |
71 | 0 | return _legacy.record_name; |
72 | 0 | } |
73 | | |
74 | | /// @brief Append field name in order to build up the record name. |
75 | | void |
76 | | append_field_name() |
77 | 0 | { |
78 | 0 | if (!_legacy.record_name.empty()) { |
79 | 0 | _legacy.record_name.append("."); |
80 | 0 | _legacy.record_name.append(node.as<std::string>()); |
81 | 0 | } else { |
82 | 0 | _legacy.record_name.append(node.as<std::string>()); |
83 | 0 | } |
84 | 0 | } |
85 | | |
86 | | std::string |
87 | | mark_as_view(swoc::TextView fmt = "Line: {}, Column: {}") const |
88 | 0 | { |
89 | 0 | swoc::LocalBufferWriter<128> lbw; |
90 | 0 | lbw.print(fmt, node.Mark().line + 1, node.Mark().column + 1); |
91 | 0 | std::string mark; |
92 | 0 | mark.reserve(lbw.view().size()); |
93 | 0 | mark = std::string{lbw.view().data(), lbw.view().size()}; |
94 | 0 | return mark; |
95 | 0 | } |
96 | | // public |
97 | | YAML::Node node; |
98 | | YAML::Node value_node; |
99 | | |
100 | | private: |
101 | | /// @brief Keep track of legacy stuff like the name. |
102 | | struct Legacy { |
103 | | std::string record_name; |
104 | | }; |
105 | | Legacy _legacy; |
106 | | }; |